Refactoring labels and radio buttons, css classes

This commit is contained in:
Carlos Antonio da Silva 2009-12-09 11:12:23 -02:00
parent aa7e9302db
commit 6bbb04f61c
5 changed files with 39 additions and 12 deletions

View File

@ -31,16 +31,20 @@ module SimpleForm
end
def attribute_required?
true unless @options[:required] == false
@options[:required] != false
end
def default_css_classes(merge_class=nil)
"#{@input_type} #{required_class} #{merge_class}".strip
end
def default_input_type
input_type = @object.try(:column_for_attribute, @attribute)
column = @object.column_for_attribute(@attribute)
input_type = column.type
case input_type
when nil then :string
when :decimal then :numeric
when :timestamp then :datetime
when :string then
when nil, :string then
@attribute.to_s =~ /password/ ? :password : :string
else input_type
end

View File

@ -5,15 +5,16 @@ module SimpleForm
def generate_input
html_options = @options[:html] || {}
html_options[:class] = "#{html_options[:class]} #{@input_type} #{required_class}".strip
html_options[:class] = default_css_classes(html_options[:class])
@options[:options] ||= {}
input_field = case @input_type
when :boolean then
check_box(@attribute, html_options)
when :radio then
['yes', 'no'].inject('') do |result, value|
result << radio_button(@attribute, value, html_options)
when :radio then
boolean_collection.inject('') do |result, (text, value)|
result << radio_button(@attribute, value, html_options) <<
label("#{@attribute}_#{value}", text, :class => default_css_classes)
end
when :text then
text_area(@attribute, html_options)
@ -32,5 +33,9 @@ module SimpleForm
end
end
def boolean_collection
[['Yes', true], ['No', false]]
end
end
end

View File

@ -5,7 +5,7 @@ module SimpleForm
def generate_label
return '' if skip_label?
html_options = { :class => "#{@input_type} #{required_class}".strip }
html_options = { :class => default_css_classes }
html_options[:for] = @options[:html][:id] if @options.key?(:html)
label(@attribute, label_text, html_options)
end

View File

@ -139,8 +139,16 @@ class InputTest < ActionView::TestCase
simple_form_for @user do |f|
concat f.input :active, :as => :radio
end
assert_select 'form input[type=radio][value=yes].radio#user_active_yes'
assert_select 'form input[type=radio][value=no].radio#user_active_no'
assert_select 'form input[type=radio][value=true].radio#user_active_true'
assert_select 'form input[type=radio][value=false].radio#user_active_false'
end
test 'input as radio should generate labels by default' do
simple_form_for @user do |f|
concat f.input :active, :as => :radio
end
assert_select 'form label.radio[for=user_active_true]', 'Yes'
assert_select 'form label.radio[for=user_active_false]', 'No'
end
test 'input should generate a password field for password attributes' do

View File

@ -1,5 +1,14 @@
require 'ostruct'
class Column
attr_accessor :name, :type#, :limit, :precision, :scale
def initialize(attrs={})
self.name = attrs[:name]
self.type = attrs[:type]
end
end
class User < OpenStruct
def id
@ -11,7 +20,7 @@ class User < OpenStruct
end
def column_for_attribute(attribute)
case attribute.to_sym
column_type = case attribute.to_sym
when :name, :status, :password then :string
when :description then :text
when :age then :integer
@ -22,6 +31,7 @@ class User < OpenStruct
when :created_at then :datetime
when :updated_at then :timestamp
end
Column.new(:name => attribute, :type => column_type)
end
def human_attribute_name(attribute)