Permalink
Browse files

Added :time_zone and :country select support.

  • Loading branch information...
1 parent b307691 commit 74014e2743b69a3a8d50a648ee542d6f624c0e38 @josevalim josevalim committed Dec 11, 2009
View
@@ -0,0 +1,3 @@
+[submodule "test/support/country_select"]
+ path = test/support/country_select
+ url = git://github.com/rails/country_select
View
@@ -1,7 +1,7 @@
== General
* Sample CSS
-* :country, :time_zone, :group and :file types support
+* :group types support
== Validations
View
@@ -39,4 +39,12 @@ module SimpleForm
# Collection of methods to detect if a file type was given.
mattr_accessor :file_methods
@@file_methods = [ :file?, :public_filename ]
+
+ # Default priority for time_zone inputs.
+ mattr_accessor :time_zone_priority
+ @@time_zone_priority = nil
+
+ # Default priority for country inputs.
+ mattr_accessor :country_priority
+ @@country_priority = nil
end
@@ -8,20 +8,28 @@ class Input < Base
extend MapType
map_type :boolean, :to => :check_box
- map_type :text, :to => :text_area
- map_type :datetime, :to => :datetime_select, :options => true
- map_type :date, :to => :date_select, :options => true
- map_type :time, :to => :time_select, :options => true
- map_type :password, :to => :password_field
- map_type :hidden, :to => :hidden_field
- map_type :select, :to => :collection_select, :options => true, :collection => true
- map_type :radio, :to => :collection_radio, :collection => true
map_type :string, :to => :text_field
+ map_type :password, :to => :password_field
+ map_type :text, :to => :text_area
map_type :file, :to => :file_field
+ map_type :hidden, :to => :hidden_field
# Numeric types
map_type :integer, :float, :decimal, :to => :text_field
+ # Date/time types
+ map_type :datetime, :to => :datetime_select, :options => true
+ map_type :date, :to => :date_select, :options => true
+ map_type :time, :to => :time_select, :options => true
+
+ # Collection types
+ map_type :select, :to => :collection_select, :options => true, :collection => true
+ map_type :radio, :to => :collection_radio, :collection => true
+
+ # With priority zones
+ map_type :country, :to => :country_select, :options => true, :with_priority => true
+ map_type :time_zone, :to => :time_zone_select, :options => true, :with_priority => true
+
# Default boolean collection for use with selects/radios when no
# collection is given. Always fallback to this boolean collection.
# Texts can be translated using i18n in "simple_form.true" and
@@ -40,29 +48,35 @@ def content
raise "Invalid input type #{input_type.inspect}" unless mapping
args = [ attribute ]
- apply_collection_behavior(args) if mapping.collection
- apply_options_behavior(args) if mapping.options
+ apply_with_priority_behavior(args) if mapping.with_priority
+ apply_collection_behavior(args) if mapping.collection
+ apply_options_behavior(args) if mapping.options
apply_html_options(args)
@builder.send(mapping.method, *args)
end
protected
+ # Applies priority behavior to configured types.
+ def apply_with_priority_behavior(args)
+ priorities = options[:priority] || SimpleForm.send(:"#{input_type}_priority")
+ args.push(priorities)
+ end
+
# Applies default collection behavior, mapping the default collection to
# boolean collection if it was not set, and defining default include_blank
# option
def apply_collection_behavior(args)
collection = (options[:collection] || self.class.boolean_collection).to_a
detect_collection_methods(collection, options)
-
- options[:include_blank] = true unless skip_include_blank?
args.push(collection, options[:value_method], options[:label_method])
end
# Apply default behavior for inputs that need extra options, such as date
# and time selects.
def apply_options_behavior(args)
+ options[:include_blank] = true unless skip_include_blank?
args << options
end
@@ -77,9 +91,9 @@ def apply_html_options(args)
args << html_options
end
+ # Check if :include_blank must be included by default.
def skip_include_blank?
- input_type != :select || options.key?(:prompt) || options.key?(:include_blank) ||
- options[:input_html].try(:[], :multiple)
+ options.key?(:prompt) || options.key?(:include_blank) || options[:input_html].try(:[], :multiple)
end
# Detect the right method to find the label and value for a collection.
@@ -65,6 +65,11 @@ class FormBuilder < ActionView::Helpers::FormBuilder
#
# :value_method => the method to apply on the array collection to get the value
#
+ # == Priority
+ #
+ # Some inputs, as :time_zone and :country accepts a :priority option. If none is
+ # given SimpleForm.time_zone_priority and SimpleForm.country_priority are used respectivelly.
+ #
def input(attribute, options={})
define_simple_form_attributes(attribute, options)
@@ -1,5 +1,5 @@
module SimpleForm
- Mapping = Struct.new(:method, :collection, :options)
+ Mapping = Struct.new(:method, :collection, :options, :with_priority)
module MapType
def mappings
@@ -9,7 +9,8 @@ def mappings
def map_type(*types)
options = types.extract_options!
raise ArgumentError, "You need to give :to as option to map_type" unless options[:to]
- mapping = Mapping.new(options[:to], options[:collection] || false, options[:options] || false)
+ mapping = Mapping.new(options[:to], options[:collection] || false,
+ options[:options] || false, options[:with_priority] || false)
types.each { |t| mappings[t] = mapping }
end
end
@@ -83,6 +83,37 @@ def with_input_for(object, attribute, type, options={})
assert_select 'input#user_name[type=file]'
end
+ test 'input should generate a country select field' do
+ with_input_for @user, :country, :country
+ assert_select 'select#user_country'
+ assert_select 'select option[value=Brazil]', 'Brazil'
+ assert_no_select 'select option[value=][disabled=disabled]'
+ end
+
+ test 'input should generate a country select with simple form default' do
+ swap SimpleForm, :country_priority => [ 'Brazil' ] do
+ with_input_for @user, :country, :country
+ assert_select 'select option[value=][disabled=disabled]'
+ end
+ end
+
+ test 'input should generate a time zone select field' do
+ with_input_for @user, :time_zone, :time_zone
+ assert_select 'select#user_time_zone'
+ assert_select 'select option[value=Brasilia]', '(GMT-03:00) Brasilia'
+ assert_no_select 'select option[value=][disabled=disabled]'
+ end
+
+ test 'input should generate a time zone select field with default' do
+ with_input_for @user, :time_zone, :time_zone, :default => 'Brasilia'
+ assert_select 'select option[value=Brasilia][selected=selected]'
+ end
+
+ test 'input should generate a time zone select using options priority' do
+ with_input_for @user, :time_zone, :time_zone, :priority => /Brasilia/
+ assert_select 'select option[value=][disabled=disabled]'
+ end
+
test 'input should generate a datetime select by default for datetime attributes' do
with_input_for @user, :created_at, :datetime
1.upto(5) do |i|
View
@@ -63,11 +63,21 @@ def with_association_for(object, *args)
assert_select 'form input#user_credit_limit.decimal'
end
- test 'builder should generate password fields for columns that match password' do
+ test 'builder should generate password fields for columns that matches password' do
with_form_for @user, :password
assert_select 'form input#user_password.password'
end
+ test 'builder should generate country fields for columns that matches country' do
+ with_form_for @user, :residence_country
+ assert_select 'form select#user_residence_country.country'
+ end
+
+ test 'builder should generate time_zone fields for columns that matches time_zone' do
+ with_form_for @user, :time_zone
+ assert_select 'form select#user_time_zone.time_zone'
+ end
+
test 'builder should generate date select for date columns' do
with_form_for @user, :born_at
assert_select 'form select#user_born_at_1i.date'
Submodule country_select added at d3b5d8
View
@@ -12,9 +12,12 @@
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib', 'simple_form')
require 'simple_form'
-Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
+Dir["#{File.dirname(__FILE__)}/support/*.rb"].each { |f| require f }
I18n.default_locale = :en
+$:.unshift "#{File.dirname(__FILE__)}/support/country_select/lib"
+require 'country_select'
+
class SimpleForm::FormBuilder
attr_accessor :attribute, :column, :reflection, :input_type, :options
end

0 comments on commit 74014e2

Please sign in to comment.