Skip to content

Commit

Permalink
Use a more common name :checkboxes instead of :checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
whitequark committed Mar 27, 2012
1 parent 46b71b4 commit 74a6997
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.markdown
Expand Up @@ -72,7 +72,7 @@ Use ranges as options for your select tags


= f.input :year, :options => (1950..Time.now.year) = f.input :year, :options => (1950..Time.now.year)


Options may also be rendered as **:radios** and **:checks** Options may also be rendered as **:radios** and **:checkboxes**


= f.input :user, :options => User.all.map(&:name), :as => :radios = f.input :user, :options => User.all.map(&:name), :as => :radios


Expand Down Expand Up @@ -109,7 +109,7 @@ Mapping | Input | Column Type
**:date** |date field |date, datetime, timestamps **:date** |date field |date, datetime, timestamps
**:select** |select |- **:select** |select |-
**:radios** |radio buttons |- **:radios** |radio buttons |-
**:checks** |check boxes |- **:checkboxes** |check boxes |-




### Validations ### Validations
Expand Down
8 changes: 4 additions & 4 deletions lib/padrino-fields/form_builder.rb
Expand Up @@ -46,7 +46,7 @@ def input(attribute, options={})
def default_input(attribute,type,options={}) def default_input(attribute,type,options={})
input_options = options.keep_if {|key, value| key != :as} input_options = options.keep_if {|key, value| key != :as}
if options[:options] || options[:grouped_options] if options[:options] || options[:grouped_options]
if type==:radios || type == :checks if type==:radios || type == :checkboxes
collect_inputs_as(attribute,type,input_options) collect_inputs_as(attribute,type,input_options)
else else
select(attribute,input_options) select(attribute,input_options)
Expand Down Expand Up @@ -117,16 +117,16 @@ def collection_input(attribute,type,item, options={})
unchecked_value = options.delete(:uncheck_value) || '0' unchecked_value = options.delete(:uncheck_value) || '0'
options.reverse_merge!(:id => field_id(attribute), :value => '1') options.reverse_merge!(:id => field_id(attribute), :value => '1')
options.reverse_merge!(:checked => true) if values_matches_field?(attribute, options[:value]) options.reverse_merge!(:checked => true) if values_matches_field?(attribute, options[:value])
klass = css_class(attribute,type,options[:disabled]) klass = css_class(attribute, type.to_s.singularize, options[:disabled])
name = type == :checks ? field_name(attribute) + '[]' : field_name(attribute) name = type == :checkboxes ? field_name(attribute) + '[]' : field_name(attribute)
if item.is_a?(Array) if item.is_a?(Array)
text, value = item[0], item[1] text, value = item[0], item[1]
else else
text = item; value = item; text = item; value = item;
end end
id = field_id(attribute) + "_" + domize(text) id = field_id(attribute) + "_" + domize(text)
opts = html_options(attribute,type,options.merge(:id => id, :class => klass, :value => value)) opts = html_options(attribute,type,options.merge(:id => id, :class => klass, :value => value))
if type == :checks if type == :checkboxes
html = @template.hidden_field_tag(options[:name] || name, :value => unchecked_value, :id => nil) html = @template.hidden_field_tag(options[:name] || name, :value => unchecked_value, :id => nil)
input_item = @template.check_box_tag(name, opts) input_item = @template.check_box_tag(name, opts)
else else
Expand Down
12 changes: 6 additions & 6 deletions test/test_form_builder.rb
Expand Up @@ -116,20 +116,20 @@ def setup
end end


should "return a collection of checkboxes" do should "return a collection of checkboxes" do
actual = field.input(:string, :options => ["Ann","Bob"], :as => :checks) actual = field.input(:string, :options => ["Ann","Bob"], :as => :checkboxes)
assert_has_tag("label", :class => "checks", :for => "person_string_ann", content:"Ann") { actual } assert_has_tag("label", :class => "checkboxes", :for => "person_string_ann", content:"Ann") { actual }
assert_has_tag("input", type:"checkbox", value:"Ann", id:"person_string_ann", name:"person[string][]") { actual } assert_has_tag("input", type:"checkbox", value:"Ann", id:"person_string_ann", name:"person[string][]") { actual }
assert_has_tag("label", :class => "checks", :for => "person_string_bob", content:"Bob") { actual } assert_has_tag("label", :class => "checkboxes", :for => "person_string_bob", content:"Bob") { actual }
assert_has_tag("input", type:"checkbox", value:"Bob", id:"person_string_bob", name:"person[string][]") { actual } assert_has_tag("input", type:"checkbox", value:"Bob", id:"person_string_bob", name:"person[string][]") { actual }
assert_has_tag("input", type:"hidden", name:"person[string][]", value:"0") { actual } assert_has_tag("input", type:"hidden", name:"person[string][]", value:"0") { actual }
end end


should "return a collection of checkboxes using a multi-dimensional array" do should "return a collection of checkboxes using a multi-dimensional array" do
actual = field.input(:string, :options => [["Ann",1],["Bob",2]], :as => :checks) actual = field.input(:string, :options => [["Ann",1],["Bob",2]], :as => :checkboxes)
assert_has_tag("label", :class => "checks", :for => "person_string_ann", content:"Ann") { actual } assert_has_tag("label", :class => "checkboxes", :for => "person_string_ann", content:"Ann") { actual }
assert_has_tag("input", type:"checkbox", value:"1", id:"person_string_ann", name:"person[string][]") { actual } assert_has_tag("input", type:"checkbox", value:"1", id:"person_string_ann", name:"person[string][]") { actual }
assert_has_tag("input", type:"hidden", name:"person[string][]", value:"0") { actual } assert_has_tag("input", type:"hidden", name:"person[string][]", value:"0") { actual }
assert_has_tag("label", :class => "checks", :for => "person_string_bob", content:"Bob") { actual } assert_has_tag("label", :class => "checkboxes", :for => "person_string_bob", content:"Bob") { actual }
assert_has_tag("input", type:"checkbox", value:"2", id:"person_string_bob", name:"person[string][]") { actual } assert_has_tag("input", type:"checkbox", value:"2", id:"person_string_bob", name:"person[string][]") { actual }
assert_has_tag("input", type:"hidden", name:"person[string][]", value:"0") { actual } assert_has_tag("input", type:"hidden", name:"person[string][]", value:"0") { actual }
end end
Expand Down

0 comments on commit 74a6997

Please sign in to comment.