Skip to content

Commit

Permalink
Implementing #27 , some basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ujifgc committed Jan 10, 2013
1 parent 560c949 commit 89fabaa
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
Expand Up @@ -95,6 +95,31 @@ def select(field, options={})
@template.select_tag field_name(field), options
end

# f.check_box_group :color, :options => ['red', 'green', 'blue'], :selected => ['red', 'blue']
# f.check_box_group :color, :collection => @colors, :fields => [:name, :id]
def check_box_group(field, options={})
selected_values = Array(options[:selected] || field_value(field))
if options[:collection]
fields = options[:fields] || [:name, :id]
# don't use map!, it will break some orms
selected_values = selected_values.map{ |v| (v.respond_to?(fields[1]) ? v.send(fields[1]) : v).to_s }
end
labeled_group( field, options ) do |variant|
@template.check_box_tag( field_name(field)+'[]', :value => variant[1], :id => variant[2], :checked => selected_values.include?(variant[1]) )
end
end

# f.radio_button_group :color, :options => ['red', 'green']
# f.radio_button_group :color, :collection => @colors, :fields => [:name, :id], :selected => @colors.first
def radio_button_group(field, options={})
fields = options[:fields] || [:name, :id]
selected_value = options[:selected] || field_value(field)
selected_value = selected_value.send(fields[1]) if selected_value.respond_to?(fields[1])
labeled_group( field, options ) do |variant|
@template.radio_button_tag( field_name(field), :value => variant[1], :id => variant[2], :checked => variant[1] == selected_value.to_s )
end
end

# f.check_box :remember_me, :value => 'true', :uncheck_value => '0'
def check_box(field, options={})
html =""
Expand Down Expand Up @@ -246,6 +271,26 @@ def object_class(explicit_object)
def root_form?
!nested_form?
end

# Builds a group of labels for radios or checkboxes
def labeled_group(field, options={})
options.reverse_merge!(:id => field_id(field), :selected => field_value(field))
options.merge!(:class => field_error(field, options))
variants = case
when options[:options]
options[:options].map{ |caption, value| [caption.to_s, (value||caption).to_s] }
when options[:collection]
fields = options[:fields] || [:name, :id]
options[:collection].map{ |variant| [variant.send(fields.first).to_s, variant.send(fields.last).to_s] }
else
[]
end
variants.inject('') do |html, variant|
variant[2] = "#{field_id(field)}_#{variant[1]}"
html << @template.label_tag("#{field_name(field)}[]", :for => variant[2], :caption => "#{yield(variant)} #{variant[0]}")
end
end

end # AbstractFormBuilder
end # FormBuilder
end # Helpers
Expand Down
33 changes: 32 additions & 1 deletion padrino-helpers/test/test_form_builder.rb
Expand Up @@ -20,7 +20,7 @@ def setup
mock_model('Role', :name => 'Moderate', :id => 2), mock_model('Role', :name => 'Limited', :id => 3)]
@user = mock_model("User", :first_name => "Joe", :email => '', :session_id => 54)
@user.stubs(:errors => {:a => "must be present", :b => "must be valid", :email => "Must be valid", :first_name => []})
@user.stubs(:role_types => role_types, :role => "1")
@user.stubs(:role_types => role_types, :role => "1", :roles => [1,3])
@user_none = mock_model("User")
end

Expand Down Expand Up @@ -492,6 +492,37 @@ def standard_builder(object=@user)
end
end

context 'for #check_box_group and #radio_button_group methods' do
should 'display checkbox group html' do
checkboxes = standard_builder.check_box_group(:role, :collection => @user.role_types, :fields => [:name, :id], :selected => [2,3])
assert_has_tag('input[type=checkbox]', :value => '1') { checkboxes }
assert_has_no_tag('input[type=checkbox][checked]', :value => '1') { checkboxes }
assert_has_tag('input[type=checkbox]', :checked => 'checked', :value => '2') { checkboxes }
assert_has_tag('label[for=user_role_3] input[name="user[role][]"][value="3"][checked]') { checkboxes }
end

should 'display checkbox group html and extract selected values from the object' do
checkboxes = standard_builder.check_box_group(:roles, :collection => @user.role_types, :fields => [:name, :id])
assert_has_tag('input[type=checkbox][name="user[roles][]"][value="1"][checked]') { checkboxes }
assert_has_tag('input[type=checkbox][name="user[roles][]"][value="3"][checked]') { checkboxes }
assert_has_no_tag('input[type=checkbox][name="user[roles][]"][value="2"][checked]') { checkboxes }
end

should 'display radio group html' do
radios = standard_builder.radio_button_group(:role, :options => %W(red yellow blue), :selected => 'yellow')
assert_has_tag('input[type=radio]', :value => 'red') { radios }
assert_has_no_tag('input[type=radio][checked]', :value => 'red') { radios }
assert_has_tag('input[type=radio]', :checked => 'checked', :value => 'yellow') { radios }
assert_has_tag('label[for=user_role_blue] input[name="user[role]"][value=blue]') { radios }
end

should 'display radio group html and extract selected value from the object' do
radios = standard_builder.radio_button_group(:role, :collection => @user.role_types)
assert_has_tag('input[type=radio][value="1"][checked]') { radios }
assert_has_no_tag('input[type=radio][value="2"][checked]') { radios }
end
end

context 'for #radio_button method' do
should "display correct radio button html" do
actual_html = standard_builder.radio_button(:gender, :value => 'male', :class => 'large')
Expand Down

0 comments on commit 89fabaa

Please sign in to comment.