New Book: "Flex on Rails: Building Rich Internet Applications with Adobe Flex 3 and Rails 2" 3

Finally our book on using Flex with Rails is released and will appear over the next few days in stores around the US and is available on Amazon. I received a couple of copies from the publisher and it felt like an accomplishment to hold a physical version in my hands. I am sure my co-author, Tony, felt the same. Good job man! With this release we are also launching http://flexonrails.com where you can find all the source code of the book as well as other resources related to Flex and Rails, and our blog http://blog.flexonrails.com the blog for everything on Flex with Ruby On Rails…
So here are the top 10 reasons why should you absolutely buy this book even if you are not a programmer….1) Your vision will improve 2) You’ll run faster 3) Aging gets reverted 4)... Just kidding, the main reasons for us to write this book was that we really wanted to share many of the experiences and findings we had on using Flex and Rails on many projects, and we are proud of the outcome. It’s a book by developers for developers. I’ll be blogging in a next entry about the process of writing this book, and will create a screencast presenting the different applications we are creating in the book, so stay tuned. When writing the book we assumed that you where a developer, either a Flex developer or a Rails developer that needed to interact with the other side and wanted to add Rails or Flex to it’s battery of languages. You will certainly find your way around even if you don’t know Flex or Rails. In either case we didn’t create a reference book so you won’t find all the answers about each api that is available in both frameworks, but you will find everything you need to get started integrating Flex with Rails, and delve into wonderful world of Flex on Rails applications. For the example code we didn’t want to build a large application and refine it over time through the chapters, so most chapter contain one or several working applications used to highlight the major integration aspects explained in that chapter. And Rails is just fantastic to create small applications on the fly, and Flex and Actionscript is pretty good for that too. So to give you a better overview of the content of this book I have attached the official description and the table of content. For more info come back to http://blog.flexonrails.com and http://flexonrails.com. I hope you enjoy the book and the sample applications. So go check it out and let us know what you think.
Daniel.
Description
“There’s no question you’re going to be a better Flex and Rails developer when you’re done reading this book.” –From the Foreword by Matt Chotin, Senior Product Manager, Adobe Systems, Inc.
Adobe Flex enables the rapid development of rich and engaging user experiences. Ruby on Rails dramatically simplifies the development of database-driven web applications. Now there’s a book that shows how to use the newest versions of both frameworks together to create state-of-the-art Rich Internet Applications (RIAs).
Flex on Rails begins with the absolute essentials: setting up your environment for Flex and Rails, passing data with XML, and integrating Flex front-ends with Rails back-ends. Then, using practical, easy-to-understand code examples, the authors take you from the basics to advanced topics only discussed in this book. Techniques covered here include
- Constructing sophisticated interfaces that can’t be created with AJAX alone
- Using RESTful services to expose applications for access via APIs
- Testing Flex and Rails together
- Using Flex Frameworks
- Getting Flex into your build/deploy process
- And more…
The authors also offer practical introductions to powerful complementary technologies, such as RubyAMF and Juggernaut.
Written by developers with extensive experience using both frameworks, this book covers the new Adobe Flex 3 and Ruby on Rails 2 from the ground up. Even if you have minimal experience with Flex or Rails, you’ll learn all you need to know to use them to build exceptional production applications.
Table of Contents
Foreword xv Preface xvii Acknowledgments from Tony Hillerson xxi Acknowledgments from Daniel Wanja xxii About the Authors xxiv <strong>PART I: Flex and Rails Essentials</strong> Chapter 1: Developing with Flex and Rails 3 Installation: What You Need to Get Running 3 The Structure of a Flex and Rails Application 6 The Example Code 9 Compiling MXML 9 Running the Rails Server 9 Summary 9 Chapter 2: Passing Data with XML 11 XML in Rails 11 XML in Flex 14 Getting XML to Flex 17 Sending XML to Rails 19 Mapping Data Types 21 Error Handling 25 Summary 28 Chapter 3: Flex with RESTful Services 29 Creating the Stock Portfolio Rails Application 29 Accessing Our RESTful Application with Flex 39 Summary 48 Chapter 4: Using Fluint to Test a Flex with Rails Application 49 Using Fluint to Write Your Flex Unit Tests 50 The Basics of Testing a Flex Application 51 Testing a Cairngorm-Based Application 59 Using Fixtures 79 Summary 83 Chapter 5: Passing Data with AMF 85 What Is AMF? 85 Benefits of AMF 86 RubyAMF 87 A Simple RubyAMF Example 95 A RESTful RubyAMF Integration 101 Summary 103 Chapter 6: Debugging 105 Logging 106 Debuggers 110 Command Line Debuggers 117 Debugging Communication 127 Summary 129 Chapter 7: Data Visualization 131 Online Analytical Processing (OLAP) 133 Advanced DataGrid 144 Charting 147 Summary 152 Chapter 8: Flex MVC Frameworks 153 What Do We Mean by a Framework? 153 Roll Your Own 154 Cairngorm at a High Level 154 PureMVC at a High Level 159 Stuff 163 Summary 182 Chapter 9: Performance and Optimization 185 Flex Performance 185 Rails Performance 206 Summary 211 <strong>PART II: Cookbook Recipes</strong> Chapter 10: Source Control Flex and Rails Projects 215 Goal 215 Solution 215 Ignoring Files in Subversion 215 Git 217 Discussion 218 Summary 219 Chapter 11: Building Flex with Rake 221 Goal 221 Solution 221 Rake Is Your Friend 221 The Rakefile 222 Summary 225 Chapter 12: Deploying Flex and Rails Applications 227 Goal 227 Solution 227 Capistrano 227 Deploying with Capistrano 228 Summary 232 Chapter 13: Read the Source! 233 Goal 233 Solution 233 The Beauty of Open Source 233 The Rails Source 235 Flex Source 238 Generated Flex Source 240 Summary 243 Chapter 14: Using Observers to Clean Up Code 245 Goal 245 Solution 245 BindingUtils and ChangeWatchers in Flex 245 Taking Action on ActiveRecord Lifecycle Events 248 Summary 250 Chapter 15: Authenticating 251 Goal 251 Solution 251 Authenticating Users 251 Installing restful_authentication 251 Summary 257 Chapter 16: Reusing Commands with Prana Sequences 259 Goal 259 Solution 259 Sequences 259 Prana’s EventSequence 261 Summary 265 Chapter 17: Hierarchical Data with RubyAMF 267 Goal 267 Solution 267 Nested Sets 267 Summary 273 Chapter 18: Advanced Data Grid and Awesome Nested Set 275 Goal 275 Solution 275 Overview 275 Create the Rails Application and Database 275 Creating a Script to Load the Data 276 Flex Application 279 Adding CRUD 282 Summary 287 Chapter 19: Runtime Flex Configuration with Prana 289 Goal 289 Solution 289 IoC, Eh? 289 Summary 293 Chapter 20: Server Push with Juggernaut 295 Goal 295 Solution 295 Push Technology 295 Juggernaut 295 Creating the Rails Messaging Application 297 Creating the Flex Messaging Client Application 299 Summary 301 Chapter 21: Communicating between Flex and JavaScript 303 Goal 303 Solution 303 Communication between Flex and JavaScript 303 Security 303 Building the Samples 304 ExternalInterface 304 SWFObject and Prototype 305 ExternalInterface in Action 305 Flex-Ajax Bridge in Action 309 Summary 311 Chapter 22: File Upload 313 Goal 313 Solution 313 File Upload 313 Creating the Rails Application and Installing attachment_fu 315 Using Flex’s FileReference Class to Upload One or Several Files 316 Using Flex URLLoader Class to Upload a PNG File 318 Summary 320 Index 321
The future of Rails: Rails 3.0
The Merb team joins force with the Rails core team to work on Rails 3.0 and brings many of the modularity and performance concepts from Merb to Rails. At first I thought “Oh, no”, that’s gonna be a mess. But after reading several of the announcements I can see the benefits of the effort and this will make Rails more simple, more modular, more robust, better defined, and certainly faster. Bringing two different teams together is very difficult but the fact that both team are complementary and look for excellence in their work and managed to come up with a great vision of the future of Rails, got me excited about the future of Rails. They gonna make it happen. You can read more about here:
- http://weblog.rubyonrails.org/2008/12/23/merb-gets-merged-into-rails-3
- http://rubyonrails.org/merb
- http://yehudakatz.com/2008/12/23/rails-and-merb-merge/
- http://brainspl.at/articles/2008/12/23/merb-is-rails
- http://merbist.com/2008/12/23/rails-and-merb-merge/
Merry Christmas/Hanukkah/Hollidays!
Daniel.
Flex Job: Full time Flex developer in Denver at Videopros.com
Solomon, who also writes on this blog, is currently doing Ruby on Rails work for videopros.com and they are looking to hire a Flex developer to complement their small development team. Here are the details…

We seek a Flex on Rails Kingpin to lead our development team. This is for a full-time or contract-to-hire position. Private consultants should not apply. Depending on skill level and commitment an equity stake is available.
Compensation
- 70-100K
- Potential Equity
- Potential Profit Sharing
Skills and Experience
- Ruby on Rails | 2.5 Years minimum
- Flex | 1 Year minimum
- Web Services
- Experience as the lead developer for a commercial project.
- Agile development
- Subversion (GIT)
- Linux
- MySQL
- XML
Benefits
- Competitive Salary
- Health Insurance
- Long Term Disability Insurance
About Us – The VideoPros Dojo
Dojo means “place of the Way.” Much like a martial arts practice hall, the WAY we operate at VideoPros is a pursuit for mastery in the face of adversity and challenge. The true challenge is not between people and external things – but within ourselves. Working at VideoPros means going within yourself and seeing the work you do as a reflection of who you are on the inside. It’s more than “work” or “my job.” We are a place you go to practice being the best you can be – every day.
Apply
VideoPros is motivated to fill this position quickly. To apply, email your resume and one reason why you want to join us at the Dojo to: Careers{at}VideoPros{dot}com
Screencast: Using Webby to create a static website.
Screencast: Using Webby to create a static website from daniel wanja. Enjoy! Daniel.
Moving "private" and non-Rails related entries to http:blog.wanja.com 2
Flex test coverage?
It’s hard to imaging writing a Rails application without using rcov and get that fuzzy feeling you tested most if not all you application. Or at least get a good idea of what’s tested. I heard way back that Alex Uhlmann was working on some test coverage tools, but I didn’t see anything coming out from Adobe. I haven’t used test coverage for my Flex apps, but want to start. Any body has any good hints, stories, frameworks they use for test unit coverage in Flex? A quick google search pointed me to this article describing Flexcover, just what I was looking for. Please share your experience.
Thanks! Daniel.
UPDATE: I am playing with FlexCover and just saw this “Flexcover is a joint effort with Alex Uhlmann of Adobe Consulting, who has been working on a related set of ideas.”. So I guess this is really Alex’s work that Steven was referring to which is now open source under the MIT license. Cool!
Compassionate Communications. A different kind of Rails application. 1
Advanced Rails Studio: Custom Form Builder 9
Custom Form Builder
Use a custom form builder to clean up your html.erb files.
class LabelFormBuilder < ActionView::Helpers::FormBuilder
helpers = field_helpers +
%w{date_select datetime_select time_select} +
%w{collection_select select country_select time_zone_select} -
%w{hidden_field label fields_for} # Don't decorate these
helpers.each do |name|
define_method(name) do |field, *args|
options = args.last.is_a?(Hash) ? args.pop : {}
label = label(field, options[:label], :class => options[:label_clas])
@template.content_tag(:p, label +'<br/>' + super) #wrap with a paragraph
end
end
endThen you can remove all the <p> and label tags from you form.
<h1>Editing user</h1>
<% form_for(@user, :builder => LabelFormBuilder) do |f| %>
<%= f.error_messages %>
<%= f.text_field :name %>
<%= f.text_field :address %>
<%= f.text_area :comment %>
<%= f.check_box :check %>
<%= f.submit "Update" %>
<% end %>
<%= link_to 'Show', @user %> |
<%= link_to 'Back', users_path %>Add this to your application initializer to have all form use this form builder
ActionView::Base.default_form_builder = LabelFormBuilder<% form_for(@user, :builder => LabelFormBuilder) do |f| %><% form_for(@user) do |f| %>Now the same form with no custom builder was looking like this before.
<h1>Editing user</h1>
<% form_for(@user) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :address %><br />
<%= f.text_field :address %>
</p>
<p>
<%= f.label :comment %><br />
<%= f.text_area :comment %>
</p>
<p>
<%= f.label :check %><br />
<%= f.check_box :check %>
</p>
<p>
<%= f.submit "Update" %>
</p>
<% end %>
<%= link_to 'Show', @user %> |
<%= link_to 'Back', users_path %>Advanced Rails Studio: Meta Programming 2
Chad is giving a very nice presentation walking us through meta programming step by step. You can see the code examples we are creating during his talk, but just looking at the code will note give the whole picture.
# ruby it self uses meta programming
class Person
attr_accessor :name
end
chad = Person.new
chad.name = 'chad'
# classes are open
# create new class
class Blah
def greeting
puts "hello"
end
end
# reopen class and return id
class Blah
def do_something!
greeting
end
end
b = Blah.new
b.greeting
b.do_something!
# reopen existing class
class String
def encrypt
tr "a-z", "b-za"
end
end
puts "cat".encrypt
# Conceptually ruby (the virtual machine) creates a structure to represent the class
# And this structure can dynamically be defined and changed at runtime.
{
:String => {:name => "String",
:methods => {
:ecryypt => '<method body>',
:tr => '<method body>',
:update => '<method body>',
},
:instance_variables => {
"@name" => "Chad"
}
}
}
# replace existing method
class String
def encrypt
upcase.reverse
end
end
puts "cat".encrypt
# Rails extends base classes in activesupport. I.e Fixnum 20.minutes.ago
class Fixnum
def minutes
self*60
end
end
puts 20.minutes
class Fixnum
def from_now
Time.now + self
end
def ago
Time.now - self
end
end
puts 20.minutes.from_now
puts 20.minutes.ago
# Class definition are executed line by line
class Chad
#exit #uncomment this and the program will halt here!
puts "Hello, defining #{self}"
end
puts Chad.new.inspect
# Can conditionaly create class
class Chad
#exit #uncomment this and the program will halt here!
puts "Hello, defining #{self}"
puts "Type OK when prompted"
response = gets.chomp
if response == "OK"
def greeting
puts "OK"
end
else
def greeting
puts "O NO!!!"
end
end
end
puts Chad.new.greeting
# Could use this to have different code for RAILS_ENV is "Prodution" or "Development"# Sending messages to Object. Object receive message, all method calls have received
"Chad".upcase #,I.e. String "Chad" gets message upcase
puts "Hello" # event 'puts' is a message
puts self.class.name # Even when running script, running in context of an Object
class Person
def initialize(name)
@name = name
greeting
puts "self.inspect: #{self.inspect}"
end
def greeting
puts "0, hello #{@name}."
end
end
puts Person.new('daniel').greeting
# Calling class methods
class Person
puts "Puts is send to self. Self is person:#{self} when defining class."
end
# Can point to a class
person_class = Person
puts person_class.class
class Man
end
class AstroMan
end
# Factory method, classes are just object that can be passed around
def man_or_astroman
klass = (rand(2) > 0 ? Man : AstroMan)
klass.new
end
puts man_or_astroman
puts man_or_astroman
puts man_or_astroman
puts man_or_astroman
# Constance in Ruby are not Constants
if false #Don't run this
String = "HAHAHA" #You can even change the class constants implementation
# You'll get a warning (Warning: already initialized constant String), but you can change it
Integer = "bla"
Array = 123
end
# Methods can be defined on Objects and not just Classes
animal = "Cat"
def animal.speak
"woof"
end
puts "animal.speak: #{animal.speak}"
# "dog".speak doesn't exists, only the specific animal instance has speak
# It's not often done in Ruby, but in another context you'll do it all the time...
# .. Adding class methods (singleton methods (not related to pattern)).
class Human
def self.announce_self
puts "I AM #{self}, and I AM BEING DEFINED"
end
announce_self # Can invoked defined class method while defining class
end
# Same as doing def Human.annouce_sef end
Human.announce_self
# We are getting closer to has_many
class Superman < Human
announce_self
end
# Let's try doing something similar to ActiveRecord
module ActiveRecord
class Base
def self.has_many(*things)
puts "#{self} has_many #{things}"
end
end
end
class BuzzLightYear < ActiveRecord::Base
has_many :space_ships # does nothing for now but it's valid syntax
end# This is more the way Rails works
# Can do included hook and extend
module ActiveRecord
module Associations
module HashManyAssocation
def self.included(klass)
klass.extend(ClassMethods)
end
module ClassMethods
def has_many(things, options = {})
# TODO: define methods
puts "#{self} has many #{things}"
end
end
end
end
end
module ActiveRecord
class Base
# INCLUDE
include ActiveRecord::Associations::HashManyAssocation
end
end
class AstorMan < ActiveRecord::Base
has_many :space_ships
end# But could simply extend
module ActiveRecord
module Associations
module HashManyAssocation
def has_many(things, options = {})
# TODO: define methods
puts "#{self} has many #{things}"
end
end
end
end
module ActiveRecord
class Base
# EXTENDS
extend ActiveRecord::Associations::HashManyAssocation
end
end
class AstroMan < ActiveRecord::Base
has_many :space_ships
end
# acts_as_ ... to add functionality without extend ActiveRecord::Base
# We could use include with the included hook
module SuperHero
def self.included(klass)
klass.extend(ClassMethods)
end
module ClassMethods
def acts_as_superhero
puts "I'm a bird, I'm a plane, no I'm #{self}"
end
end
def fight_crime
puts "OK, fighting crime"
end
end
# use include
class AstroMan < ActiveRecord::Base
include SuperHero
end
AstroMan.new.fight_crime
# And include in your base class
class ActiveRecord::Base
include SuperHero
end
# then acts_as is available
class SuperMan < ActiveRecord::Base
acts_as_superhero
end
SuperMan.new.fight_crime# Calling non-existant methods
class Chad
def method_missing(method_name, *args)
puts "You called #{method_name} with #{args.inspect}"
end
end
Chad.new.just_do_it('again', 'and again')
# Calling non-existant classes
def Object.const_missing(name)
puts "Trying to get to non existing clas #{name}"
# trick: could require the file if class is missing
end
AnythingClass# Now that we went through the concepts let's do some meta programming
# eval
def evaluator(str, a_binding)
a_value = 123
eval(str, a_binding)
end
str = "puts a_value"
a_value = 321
evaluator(str, binding) # -> 321. binding is the current scope of the program
evaluator(str, nil) # -> 123. don't pass binding
# instance_eval
class Thing
def a_value
123
end
end
Thing.new.instance_eval("puts self.a_value") # -> 123. run in context of an instance
# Two more 'eval': class_eval and module_val
#class_eval
class Person
end
Person.class_eval do # Be in context of class
p self # -> Person
def greeting
puts "Hello"
end
end
Person.new.greeting # defining instance method
def add_greeting_to(klass)
klass.class_eval do
def greeting
puts "Greeting"
end
end
end
add_greeting_to(String)
"asdf".greeting # -> Greeting
# module_eval is basically same thing as class_eval
# define_method
class Chad
define_method(:foo) do |arg1|
puts "hello, #{arg1}"
end
end
Chad.new.foo(:bar)RailsConf registration opens today. Be ready! 2
May 29-June 1, 2008 in Portland, Oregon,
UPDATE: registration is now open.
UPDATE2: I’ll be presenting with Tony a 3 hour tutorial on Powering AIR Applications with Rails. See you all there!