+ All Categories
Home > Technology > Flex And Rails

Flex And Rails

Date post: 06-May-2015
Category:
Upload: tony-hillerson
View: 2,128 times
Download: 3 times
Share this document with a friend
42
Flex and Rails Tony Hillerson Software Architect EffectiveUI 360|Flex
Transcript
Page 1: Flex And Rails

Flex and RailsTony HillersonSoftware ArchitectEffectiveUI360|Flex

Page 2: Flex And Rails

Code and Slides:http://github.com/thillerson/preso_code/

Page 3: Flex And Rails

A Flex on Rails app in three acts:

Page 4: Flex And Rails

A Flex on Rails app in three acts:(for realz this time)

Page 5: Flex And Rails

Sample du Jour: Stuff

Page 6: Flex And Rails
Page 7: Flex And Rails

Why Flex and Rails?

They Make The Great Tag Team!

Page 8: Flex And Rails

You Already Know‘Why Flex’

So Why Rails?

(I hope)

Page 9: Flex And Rails

Rails is Fun

Get Rid of UnsightlyRed Tape! ===>

Page 10: Flex And Rails

Rails is for Creating Services

def index @contexts = Context.find(:all)

respond_to do |format| format.html # index.html.erb format.xml { render :xml => @contexts } end end

Page 11: Flex And Rails

Rails is for Agile Development

1 class SomeTable < ActiveRecord::Base2 3 belongs_to :parent_table4 has_many :some_other_table, :dependent => :destroy5 6 validates_presence_of :name7 8 end

Page 12: Flex And Rails

Rails is Ruby

http://poignantguide.net/ruby/

Page 13: Flex And Rails

Ruby is DynamicAnyone remember ActionScript 1.0?

• dynamically extend classes!• __resolve__ == method_missing• method.apply == method.send

Page 14: Flex And Rails

Ruby is Expressive

1.upto(10) do print "Hello Flex Nerds!"end do_something_awesome! unless lame? Person.create :name => "Tony", :lame => true

Page 15: Flex And Rails

What are the Options?

Page 16: Flex And Rails

XML

Page 17: Flex And Rails

XML is the Default Option

Page 18: Flex And Rails

# POST /contexts# POST /contexts.xmldef create @context = Context.new(params[:context]) respond_to do |format| if @context.save flash[:notice] = 'Context was successfully created.' format.html { redirect_to(@context) } format.xml { render :xml => @context, :status => :created, :location => @context } else format.html { render :action => "new" } format.xml { render :xml => @context.errors, :status => :unprocessable_entity } end endend

Page 19: Flex And Rails

JSON

Javascript Object Notation

Page 20: Flex And Rails

JSON is in Rails Tooformat.json { render :json => @context.to_json }

http://as3corlib.googlecode.com

var obj:Object = JSON.decode(jsonString)

Page 21: Flex And Rails

AMF

Action Message Format

Page 22: Flex And Rails

AMF is the Good Stuff

Buck Thinks it’s Great!

Page 23: Flex And Rails

RubyAMF

http://rubyamf.orghttp://rubyamf.googlecode.com

Page 24: Flex And Rails

Installing RubyAMF$ script/plugin install http://rubyamf.googlecode.com/svn/current/rubyamf

• New File: app/controllers/rubyamf_controller.rb• New File: config/rubyamf_config.rb• config/initializers/mime_types.rb modified: Mime::Type.register "application/x-amf", :amf• config/routes.rb modified: ActionController::Routing::Routes.draw do |map| map.rubyamf_gateway 'rubyamf_gateway', :controller => 'rubyamf', :action => 'gateway' end

Page 25: Flex And Rails

Configuring RubyAMFmodule RubyAMF module Configuration ClassMappings.translate_case = true ClassMappings.assume_types = false ParameterMappings.scaffolding = true ClassMappings.register( :actionscript => 'Task', :ruby => 'Task', :type => 'active_record', #:associations => ["context"], :attributes => ["id", "label", "context_id", "completed_at", "created_at", "updated_at"]) endend

Page 26: Flex And Rails

Configuring RubyAMFClassMappings.translate_case = false

public var created_at:Date;

ClassMappings.translate_case = true

public var createdAt:Date; // created_at in rails

Page 27: Flex And Rails

Configuring RubyAMFClassMappings.assume_types = true

class Context < ActiveRecord::Base

[RemoteClass(alias="Context")]public class Context {

matches

for free

Page 28: Flex And Rails

Configuring RubyAMFClassMappings.assume_types = false

class Context < ActiveRecord::Base

[RemoteClass(alias="Context")]public class Context {

matches

ClassMappings.register( :actionscript => 'Context', :ruby => 'Context', :type => 'active_record', :attributes => ["id", ...])

with registration

Page 29: Flex And Rails

Configuring RubyAMFParameterMappings.scaffolding = false

save(context);def save @context = params[0]becomes

ParameterMappings.scaffolding = true

save( {context:context});

def save @context = params[:context]becomes

Page 30: Flex And Rails

Connecting to Rails via RubyAMF

<mx:RemoteObject id="contextsService" destination="rubyamf" endpoint="http://localhost:3000/rubyamf_gateway/" source="ContextsController" showBusyCursor="true"/>public function save(context:Context):void { var call:AsyncToken =

contextsService.save({context:context}); call.addResponder(responder);}

Page 31: Flex And Rails

Development Workflow

Page 32: Flex And Rails

1. Generate and Migrate$ script/generate rubyamf_scaffold context label:string

$ rake db:migrate

class CreateContexts < ActiveRecord::Migration def self.up create_table :contexts do |t| t.string :label t.timestamps end end

def self.down drop_table :contexts endend

Page 33: Flex And Rails

def load_all @contexts = Context.find :all respond_to do |format| format.amf { render :amf => @contexts } end end def save respond_to do |format| format.amf do if params[:context].save render :amf => params[:context] else render :amf =>

FaultObject.new(params[:context].errors.join("\n")) end end end end

Page 34: Flex And Rails

2. Sample Datawork: id: 1 label: Work

home: id: 2 label: Home

anarco_syndicalist_commune_biweekly_meetings: id: 3 label: Anarcho-syndicalist Commune Bi-weekly Meetings

Page 35: Flex And Rails

3. Test

class ContextTest < ActiveSupport::TestCase def test_context_without_label_fails non_label_context = Context.new assert !(non_label_context.save) error_messages = non_label_context.errors.on(:label) assert !(error_messages.empty?) end

class Context < ActiveRecord::Base validates_presence_of :label

Page 36: Flex And Rails

4. Configure

ClassMappings.register( :actionscript => 'Context', :ruby => 'Context', :type => 'active_record', #:associations => ["tasks"], :attributes => ["id", "label", "created_at", "updated_at"])

Page 37: Flex And Rails

5. Wire<mx:RemoteObject id="contextsService" destination="rubyamf" endpoint="http://localhost:3000/rubyamf_gateway/" source="ContextsController" showBusyCursor="true"/>

<mx:RemoteObjectid="tasksService"

destination="rubyamf" endpoint="http://localhost:3000/rubyamf_gateway/" source="TasksController" showBusyCursor="true"/>

Page 38: Flex And Rails

5. Wirepublic function loadAll():void { var call:AsyncToken = service.load_all(); call.addResponder(responder);}

public function save(context:Context):void { var call:AsyncToken =

service.save({context:context}); call.addResponder(responder);}

public function destroy(context:Context):void { var call:AsyncToken =

service.destroy({id:context.id}); call.addResponder(responder);}

Page 39: Flex And Rails

5. Wirepublic function execute(event:CairngormEvent):void { var evt:SaveContextEvent = event as SaveContextEvent; var delegate:ContextsDelegate = new ContextsDelegate(this); delegate.save(evt.context);}

public function result(data:Object):void { var result:ResultEvent = data as ResultEvent; var context:Context = result.result as Context; ...}

Page 40: Flex And Rails

X. Rinse and Repeat


Recommended