1
0
Fork 0
mirror of https://github.com/heartcombo/simple_form.git synced 2022-11-09 12:19:26 -05:00

Merge conflicts solved.

This commit is contained in:
José Valim 2009-12-10 12:15:55 -02:00
commit 7fe0379113
6 changed files with 51 additions and 16 deletions

View file

@ -8,7 +8,7 @@ module SimpleForm
# The Base component is a raw component with some helpers and a default behavior
# of prepending the content available in the method content.
class Base
delegate :template, :object, :object_name, :column, :attribute,
delegate :template, :object, :object_name, :attribute, :column,
:input_type, :options, :to => :@builder
def self.basename
@ -52,4 +52,4 @@ module SimpleForm
end
end
end
end
end

View file

@ -34,7 +34,7 @@ module SimpleForm
args = [ attribute ]
apply_collection_behavior(args) if mapping.collection
apply_options_behavior(args) if mapping.options
args << component_html_options
apply_html_options(args)
@builder.send(mapping.method, *args)
end
@ -53,6 +53,16 @@ module SimpleForm
args << options[:options]
end
def apply_html_options(args)
html_options = component_html_options
if column && [:string, :password, :decimal, :float].include?(input_type)
html_options[:maxlength] ||= column.limit
end
args << html_options
end
def detect_collection_methods(collection, options)
sample = collection.first || collection.last

View file

@ -1,9 +1,11 @@
module SimpleForm
class FormBuilder < ActionView::Helpers::FormBuilder
attr_reader :template, :object_name, :object, :attribute, :input_type, :options
attr_reader :template, :object_name, :object, :attribute, :column,
:input_type, :options
def input(attribute, options={})
@attribute, @options = attribute, options
@column = find_attribute_column
@input_type = default_input_type
component = SimpleForm.terminator
@ -17,10 +19,6 @@ module SimpleForm
private
def column
@object.column_for_attribute(@attribute) if @object.respond_to?(:column_for_attribute)
end
def default_input_type
return @options[:as].to_sym if @options[:as]
return :select if @options[:collection]
@ -37,5 +35,9 @@ module SimpleForm
end
end
def find_attribute_column
@object.column_for_attribute(@attribute) if @object.respond_to?(:column_for_attribute)
end
end
end

View file

@ -9,6 +9,7 @@ class InputTest < ActionView::TestCase
def with_input_for(object, attribute, type, options={})
simple_form_for object do |f|
f.attribute = attribute
f.column = object.column_for_attribute(attribute) if object.respond_to?(:column_for_attribute)
f.input_type = type
f.options = options
@ -245,6 +246,27 @@ class InputTest < ActionView::TestCase
assert_select 'input.optional#user_name'
end
test 'input should get options from column definition for string attributes' do
with_input_for @user, :name, :string
assert_select 'input.string[maxlength=100]'
end
test 'input should get options from column definition for decimal attributes' do
with_input_for @user, :credit_limit, :decimal
assert_select 'input.decimal[maxlength=15]'
end
test 'input should get options from column definition for password attributes' do
with_input_for @user, :password, :password
assert_select 'input.password[maxlength=100]'
end
test 'input should not generate options for different attributes' do
with_input_for @user, :description, :text
assert_select 'textarea'
assert_no_select 'textarea[maxlength]'
end
test 'input should be generated properly when object is not present' do
with_input_for :project, :name, :string
assert_select 'input.string.required#project_name'

View file

@ -1,11 +1,12 @@
require 'ostruct'
class Column
attr_accessor :name, :type#, :limit, :precision, :scale
attr_accessor :name, :type, :limit#, :precision, :scale
def initialize(attrs={})
self.name = attrs[:name]
self.type = attrs[:type]
self.name = attrs[:name]
self.type = attrs[:type]
self.limit = attrs[:limit]
end
end
@ -22,18 +23,18 @@ class User < OpenStruct
end
def column_for_attribute(attribute)
column_type = case attribute.to_sym
when :name, :status, :password then :string
column_type, limit = case attribute.to_sym
when :name, :status, :password then [:string, 100]
when :description then :text
when :age then :integer
when :credit_limit then :decimal
when :credit_limit then [:decimal, 15]
when :active then :boolean
when :born_at then :date
when :delivery_time then :time
when :created_at then :datetime
when :updated_at then :timestamp
end
Column.new(:name => attribute, :type => column_type)
Column.new(:name => attribute, :type => column_type, :limit => limit)
end
def human_attribute_name(attribute)

View file

@ -16,7 +16,7 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
I18n.default_locale = :en
class SimpleForm::FormBuilder
attr_accessor :attribute, :input_type, :options
attr_accessor :attribute, :column, :input_type, :options
end
class ActionView::TestCase