http://www.linux-mag.com/cache/7478/1.html
http://www.linux-mag.com/cache/7484/1.html
http://www.linux-mag.com/cache/7489/1.html
http://www.linux-mag.com/id/7492/

Just some nice things in Rails 3.0 that might be useful in a hobo
context ;)
Anyone using Hobo with Rails 3.0 out there? How do they currently fit?

Validations
--------------

class Wheels < ActiveRecord::Validator
  def validate
    number_of_wheels = options[ :number_of_wheels ] || 4

    if record.number_of_wheels != number_of_wheels
      record.errors[ :number_of_wheels ] << "Your #{record.class}
won't run"
    end
  end
end

class Engine < ActiveRecord::Validator
  def validate
    # Valid?
  end
end

class Weight < ActiveRecord::Validator
  def validate
    # Valid?
  end
end

class Vehicle < ActiveRecord::Base
  attr_accessor :number_of_wheels
end

class Car < Vehicle
  validates_with Wheels, :number_of_wheels => 4
  validates_with Engine, Weight
end

class Motorcycle < Vehicle
  validates_with Wheels, :number_of_wheels => 2
end


..."validates_with encapsulates rules and makes those rules reusable
akin to any other class. I can imagine modules and gems full of
validator classes for email, telephone numbers, and common formats
that are best written once and shared among many applications.
Thankfully, I was able to reuse some code — Wheels — even in this
limited example."

State machine
-------------------
Several plugins provide state machines for Rails, but the feature is
so fundamental, the best features of available solutions were
integrated into the core to create ActiveRecord::StateMachine.

state_machine do
    state :placed # In limbo
    state :paid
     ...

    event :advance_order do
      transitions :to => :paid,     :from => [ :placed ],
        :on_transition => :pick_list
      transitions :to => :assembled,:from => [ :paid ],
      ...
    end
    ...
end

Hmm, I think there is some overlap with hobo here... but perhaps the
hobo solution is even more powerful and flexible?

---
Selection drop-downs

grouped_collection_select(:city, :country_id,
@continents, :countries,
  :name, :id, :name)

… produces:

<select name="city[country_id]">
  <optgroup label="Africa">
    <option value="1">South Africa</option>
    <option value="3">Somalia</option>
  </optgroup>
  <optgroup label="Europe">
    <option value="7" selected="selected">Denmark</option>
    <option value="2">Ireland</option>
  </optgroup>
</select>

Try!!!
------
Student.find_by_name(  'joe'  ).try( :homeroom ).try(: teacher )

Scopes
----------
class Student < ActiveRecord::Base
  default_scope :order => 'last_name ASC'
  named_scope :seniors, :conditions => { :year => 'senior' }
end

The default scope imposes a sort order on all results from
Student.find*(). The named scope invents the method Student.seniors()
to be the analog of Student.find(:all, :conditions => { :year =>
'senior' }. Ultimately, the combination of scopes produce a query such
as SELECT * FROM `students` WHERE `year`='senior' ORDER BY `last_name`
ASC for Student.seniors(). Again, which code would you prefer to write
and read?

You can now scope dynamically, too. This code…

Student.scoped_by_year( 'senior' ).find( :all )

… is the equivalent of the named scope


DRY RESTful controllers
---------------------------------
Edge Rails does away with all that cruft and reduces the same
controller method to this:

respond_to :html, :xml

def create
  @student = Student.new(params[:student])
  flash[:notice] = 'Student was successfully created.' if
@student.save
  respond_with(@student)
end

respond_with() deduces what format is required and attempts to find a
view associated with the action and the format.



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Hobo 
Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/hobousers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to