Ruby On Rails, Design, Simplicity, Web 2.0, Ajax, Mac and Tons of Pizza.

Jul 04

A new CheckBox component in Streamlined

Posted by Sandro Paganotti in Ruby on Rails - no comments digg this add to delicious

I’ve noticed that Streamlined when display has_and_belongs_to_many or has_many relationships in an edit view calls ‘Streamlined::Components::Select’ (lib/streamlined/column/association.rb line 123). That class simply render a ‘select’ tag with multiple choices.

I created a new component that can be called instead the standard one. It’s called CheckBox and it displays, as the name may suggest, a checkbox for each choice available.

To add this component simply create a new file under ‘lib/streamlined/components’ called ‘checkbox.rb’ and paste the same content of ‘select.rb’ except these two changes:


    # line 15
    class CheckBox
    REQUIRED_ATTRS = [:view, :object, :method, :choices, :item]

    # Substitute the render method with this 
    def render
      ids = item.send(Inflector.pluralize(method.to_s)).collect{|b| b.id}
      name = "#{object}[#{method}][]" 
      choices.collect do |c| 
        view.check_box_tag(name,c[1],ids.include?(c[1])) + "#{c[0]}" 
      end.join("<br/>") + view.hidden_field_tag(name, STREAMLINED_SELECT_NONE)
   end

Remeber also to include this new file (from lib/streamlined/components.rb) and change the line 123 in association.rb to call this new component:


      result = Streamlined::Components::CheckBox.render do |s|
        s.view = view
        s.object = model_underscore
        s.method = name
        s.choices = choices
        s.options = {:selected => selected_choices }
        s.html_options = {:size => 5, :multiple => true}
        s.item = item
      end

Sandro

Post a comment

Categories:

Tags:

Powered by Mephisto, Valid XHTML 1.1, Valid CSS - Supported by Wave Factory