simple_form 2.1.0 → 3.0.0.rc

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +15 -32
  3. data/README.md +103 -71
  4. data/lib/generators/simple_form/install_generator.rb +3 -3
  5. data/lib/generators/simple_form/templates/README +1 -1
  6. data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +13 -13
  7. data/lib/generators/simple_form/templates/config/initializers/simple_form_bootstrap.rb +14 -14
  8. data/lib/generators/simple_form/templates/config/initializers/simple_form_foundation.rb +3 -3
  9. data/lib/simple_form/action_view_extensions/builder.rb +1 -319
  10. data/lib/simple_form/action_view_extensions/form_helper.rb +2 -9
  11. data/lib/simple_form/components/html5.rb +5 -2
  12. data/lib/simple_form/components/labels.rb +3 -3
  13. data/lib/simple_form/components/maxlength.rb +1 -8
  14. data/lib/simple_form/components/pattern.rb +2 -2
  15. data/lib/simple_form/components.rb +1 -1
  16. data/lib/simple_form/error_notification.rb +2 -2
  17. data/lib/simple_form/form_builder.rb +147 -49
  18. data/lib/simple_form/helpers.rb +1 -1
  19. data/lib/simple_form/inputs/base.rb +2 -6
  20. data/lib/simple_form/inputs/block_input.rb +1 -1
  21. data/lib/simple_form/inputs/boolean_input.rb +5 -4
  22. data/lib/simple_form/inputs/collection_input.rb +6 -6
  23. data/lib/simple_form/inputs/numeric_input.rb +0 -6
  24. data/lib/simple_form/inputs/password_input.rb +0 -1
  25. data/lib/simple_form/inputs/string_input.rb +0 -1
  26. data/lib/simple_form/railtie.rb +7 -0
  27. data/lib/simple_form/tags.rb +61 -0
  28. data/lib/simple_form/version.rb +1 -1
  29. data/lib/simple_form/wrappers/builder.rb +5 -29
  30. data/lib/simple_form/wrappers/many.rb +1 -1
  31. data/lib/simple_form/wrappers/root.rb +1 -1
  32. data/lib/simple_form/wrappers.rb +1 -1
  33. data/lib/simple_form.rb +39 -47
  34. data/test/action_view_extensions/builder_test.rb +75 -88
  35. data/test/action_view_extensions/form_helper_test.rb +25 -16
  36. data/test/components/label_test.rb +46 -46
  37. data/test/form_builder/association_test.rb +39 -29
  38. data/test/form_builder/button_test.rb +4 -4
  39. data/test/form_builder/error_notification_test.rb +8 -8
  40. data/test/form_builder/error_test.rb +12 -12
  41. data/test/form_builder/general_test.rb +58 -52
  42. data/test/form_builder/hint_test.rb +22 -22
  43. data/test/form_builder/input_field_test.rb +12 -12
  44. data/test/form_builder/label_test.rb +6 -6
  45. data/test/form_builder/wrapper_test.rb +21 -21
  46. data/test/inputs/boolean_input_test.rb +23 -23
  47. data/test/inputs/collection_check_boxes_input_test.rb +61 -55
  48. data/test/inputs/collection_radio_buttons_input_test.rb +76 -79
  49. data/test/inputs/collection_select_input_test.rb +70 -45
  50. data/test/inputs/datetime_input_test.rb +17 -11
  51. data/test/inputs/disabled_test.rb +10 -10
  52. data/test/inputs/discovery_test.rb +4 -4
  53. data/test/inputs/file_input_test.rb +1 -1
  54. data/test/inputs/general_test.rb +12 -12
  55. data/test/inputs/grouped_collection_select_input_test.rb +20 -20
  56. data/test/inputs/hidden_input_test.rb +3 -2
  57. data/test/inputs/numeric_input_test.rb +3 -3
  58. data/test/inputs/priority_input_test.rb +9 -3
  59. data/test/inputs/readonly_test.rb +12 -12
  60. data/test/inputs/required_test.rb +5 -5
  61. data/test/inputs/string_input_test.rb +15 -25
  62. data/test/inputs/text_input_test.rb +1 -1
  63. data/test/support/misc_helpers.rb +46 -24
  64. data/test/support/mock_controller.rb +6 -6
  65. data/test/support/models.rb +62 -61
  66. data/test/test_helper.rb +20 -24
  67. metadata +32 -33
  68. data/lib/simple_form/action_view_extensions/builder.rb.orig +0 -247
  69. data/lib/simple_form/core_ext/hash.rb +0 -16
  70. data/lib/simple_form/form_builder.rb.orig +0 -486
  71. data/lib/simple_form/version.rb.orig +0 -7
@@ -1,27 +1,38 @@
1
1
  Association = Struct.new(:klass, :name, :macro, :options)
2
2
 
3
- class Column < Struct.new(:name, :type, :limit)
3
+ Column = Struct.new(:name, :type, :limit) do
4
4
  # Returns +true+ if the column is either of type integer, float or decimal.
5
5
  def number?
6
6
  type == :integer || type == :float || type == :decimal
7
7
  end
8
8
  end
9
9
 
10
- class Company < Struct.new(:id, :name)
10
+ Relation = Struct.new(:all) do
11
+ def where(conditions = nil)
12
+ self.class.new conditions ? all.first : all
13
+ end
14
+
15
+ def order(conditions = nil)
16
+ self.class.new conditions ? all.last : all
17
+ end
18
+
19
+ alias_method :to_a, :all
20
+ end
21
+
22
+ Company = Struct.new(:id, :name) do
11
23
  extend ActiveModel::Naming
12
24
  include ActiveModel::Conversion
13
25
 
14
- def self.all(options={})
15
- all = (1..3).map{|i| Company.new(i, "Company #{i}")}
16
- return [all.first] if options[:conditions].present?
17
- return [all.last] if options[:order].present?
18
- return all[0..1] if options[:include].present?
19
- return all[1..2] if options[:joins].present?
20
- all
26
+ class << self
27
+ delegate :order, :where, to: :_relation
21
28
  end
22
29
 
23
- def self.merge_conditions(a, b)
24
- (a || {}).merge(b || {})
30
+ def self._relation
31
+ Relation.new(all)
32
+ end
33
+
34
+ def self.all
35
+ (1..3).map { |i| new(i, "#{name} #{i}") }
25
36
  end
26
37
 
27
38
  def persisted?
@@ -29,14 +40,9 @@ class Company < Struct.new(:id, :name)
29
40
  end
30
41
  end
31
42
 
32
- class Tag < Company
33
- def self.all(options={})
34
- (1..3).map{|i| Tag.new(i, "Tag #{i}")}
35
- end
36
- end
43
+ class Tag < Company; end
37
44
 
38
- class TagGroup < Struct.new(:id, :name, :tags)
39
- end
45
+ TagGroup = Struct.new(:id, :name, :tags)
40
46
 
41
47
  class User
42
48
  extend ActiveModel::Naming
@@ -112,7 +118,7 @@ class User
112
118
  when :first_company
113
119
  Association.new(Company, association, :has_one, {})
114
120
  when :special_company
115
- Association.new(Company, association, :belongs_to, { :conditions => { :id => 1 } })
121
+ Association.new(Company, association, :belongs_to, { conditions: { id: 1 } })
116
122
  end
117
123
  end
118
124
 
@@ -120,11 +126,11 @@ class User
120
126
  @errors ||= begin
121
127
  hash = Hash.new { |h,k| h[k] = [] }
122
128
  hash.merge!(
123
- :name => ["can't be blank"],
124
- :description => ["must be longer than 15 characters"],
125
- :age => ["is not a number", "must be greater than 18"],
126
- :company => ["company must be present"],
127
- :company_id => ["must be valid"]
129
+ name: ["can't be blank"],
130
+ description: ["must be longer than 15 characters"],
131
+ age: ["is not a number", "must be greater than 18"],
132
+ company: ["company must be present"],
133
+ company_id: ["must be valid"]
128
134
  )
129
135
  end
130
136
  end
@@ -136,31 +142,31 @@ end
136
142
 
137
143
  class ValidatingUser < User
138
144
  include ActiveModel::Validations
139
- validates :name, :presence => true
140
- validates :company, :presence => true
141
- validates :age, :presence => true, :if => Proc.new { |user| user.name }
142
- validates :amount, :presence => true, :unless => Proc.new { |user| user.age }
145
+ validates :name, presence: true
146
+ validates :company, presence: true
147
+ validates :age, presence: true, if: Proc.new { |user| user.name }
148
+ validates :amount, presence: true, unless: Proc.new { |user| user.age }
143
149
 
144
- validates :action, :presence => true, :on => :create
145
- validates :credit_limit, :presence => true, :on => :save
146
- validates :phone_number, :presence => true, :on => :update
150
+ validates :action, presence: true, on: :create
151
+ validates :credit_limit, presence: true, on: :save
152
+ validates :phone_number, presence: true, on: :update
147
153
 
148
154
  validates_numericality_of :age,
149
- :greater_than_or_equal_to => 18,
150
- :less_than_or_equal_to => 99,
151
- :only_integer => true
155
+ greater_than_or_equal_to: 18,
156
+ less_than_or_equal_to: 99,
157
+ only_integer: true
152
158
  validates_numericality_of :amount,
153
- :greater_than => :min_amount,
154
- :less_than => :max_amount,
155
- :only_integer => true
159
+ greater_than: :min_amount,
160
+ less_than: :max_amount,
161
+ only_integer: true
156
162
  validates_numericality_of :attempts,
157
- :greater_than_or_equal_to => :min_attempts,
158
- :less_than_or_equal_to => :max_attempts,
159
- :only_integer => true
160
- validates_length_of :name, :maximum => 25
161
- validates_length_of :description, :maximum => 50
162
- validates_length_of :action, :maximum => 10, :tokenizer => lambda { |str| str.scan(/\w+/) }
163
- validates_length_of :home_picture, :is => 12
163
+ greater_than_or_equal_to: :min_attempts,
164
+ less_than_or_equal_to: :max_attempts,
165
+ only_integer: true
166
+ validates_length_of :name, maximum: 25
167
+ validates_length_of :description, maximum: 50
168
+ validates_length_of :action, maximum: 10, tokenizer: lambda { |str| str.scan(/\w+/) }
169
+ validates_length_of :home_picture, is: 12
164
170
 
165
171
  def min_amount
166
172
  10
@@ -182,26 +188,21 @@ end
182
188
  class OtherValidatingUser < User
183
189
  include ActiveModel::Validations
184
190
  validates_numericality_of :age,
185
- :greater_than => 17,
186
- :less_than => 100,
187
- :only_integer => true
191
+ greater_than: 17,
192
+ less_than: 100,
193
+ only_integer: true
188
194
  validates_numericality_of :amount,
189
- :greater_than => Proc.new { |user| user.age },
190
- :less_than => Proc.new { |user| user.age + 100},
191
- :only_integer => true
195
+ greater_than: Proc.new { |user| user.age },
196
+ less_than: Proc.new { |user| user.age + 100 },
197
+ only_integer: true
192
198
  validates_numericality_of :attempts,
193
- :greater_than_or_equal_to => Proc.new { |user| user.age },
194
- :less_than_or_equal_to => Proc.new { |user| user.age + 100},
195
- :only_integer => true
196
-
197
- validates_format_of :country, :with => /\w+/
199
+ greater_than_or_equal_to: Proc.new { |user| user.age },
200
+ less_than_or_equal_to: Proc.new { |user| user.age + 100 },
201
+ only_integer: true
198
202
 
199
- # TODO: Remove this check when we drop Rails 3.0 support
200
- if ActiveModel::VERSION::MAJOR == 3 && ActiveModel::VERSION::MINOR >= 1
201
- validates_format_of :name, :with => Proc.new { /\w+/ }
202
- else
203
- validates_format_of :name, :with => /\w+/
204
- end
203
+ validates_format_of :country, with: /\w+/
204
+ validates_format_of :name, with: Proc.new { /\w+/ }
205
+ validates_format_of :description, without: /\d+/
205
206
  end
206
207
 
207
208
  class HashBackedAuthor < Hash
data/test/test_helper.rb CHANGED
@@ -1,16 +1,12 @@
1
- require 'rubygems'
2
1
  require 'bundler/setup'
3
2
 
4
- require 'test/unit'
5
- require 'mocha/setup'
3
+ require 'minitest/autorun'
6
4
 
7
5
  require 'active_model'
8
6
  require 'action_controller'
9
7
  require 'action_view'
10
8
  require 'action_view/template'
11
9
 
12
- # Rails 3.0.4 is missing this "deprecation" require.
13
- require 'active_support/core_ext/module/deprecation'
14
10
  require 'action_view/test_case'
15
11
 
16
12
  module Rails
@@ -47,31 +43,31 @@ class ActionView::TestCase
47
43
 
48
44
  def setup_new_user(options={})
49
45
  @user = User.new({
50
- :id => 1,
51
- :name => 'New in SimpleForm!',
52
- :description => 'Hello!',
53
- :created_at => Time.now
46
+ id: 1,
47
+ name: 'New in SimpleForm!',
48
+ description: 'Hello!',
49
+ created_at: Time.now
54
50
  }.merge(options))
55
51
 
56
52
  @validating_user = ValidatingUser.new({
57
- :id => 1,
58
- :name => 'New in SimpleForm!',
59
- :description => 'Hello!',
60
- :home_picture => 'Home picture',
61
- :created_at => Time.now,
62
- :age => 19,
63
- :amount => 15,
64
- :attempts => 1,
65
- :company => [1]
53
+ id: 1,
54
+ name: 'New in SimpleForm!',
55
+ description: 'Hello!',
56
+ home_picture: 'Home picture',
57
+ created_at: Time.now,
58
+ age: 19,
59
+ amount: 15,
60
+ attempts: 1,
61
+ company: [1]
66
62
  }.merge(options))
67
63
 
68
64
  @other_validating_user = OtherValidatingUser.new({
69
- :id => 1,
70
- :name => 'New in SimpleForm!',
71
- :description => 'Hello!',
72
- :created_at => Time.now,
73
- :age => 19,
74
- :company => 1
65
+ id: 1,
66
+ name: 'New in SimpleForm!',
67
+ description: 'Hello!',
68
+ created_at: Time.now,
69
+ age: 19,
70
+ company: 1
75
71
  }.merge(options))
76
72
  end
77
73
 
metadata CHANGED
@@ -1,8 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_form
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
5
- prerelease:
4
+ version: 3.0.0.rc
6
5
  platform: ruby
7
6
  authors:
8
7
  - José Valim
@@ -11,40 +10,48 @@ authors:
11
10
  autorequire:
12
11
  bindir: bin
13
12
  cert_chain: []
14
- date: 2013-02-26 00:00:00.000000000 Z
13
+ date: 2013-05-10 00:00:00.000000000 Z
15
14
  dependencies:
16
15
  - !ruby/object:Gem::Dependency
17
16
  name: activemodel
18
17
  requirement: !ruby/object:Gem::Requirement
19
- none: false
20
18
  requirements:
21
- - - ~>
19
+ - - '>='
22
20
  - !ruby/object:Gem::Version
23
- version: '3.0'
21
+ version: 4.0.0.rc1
22
+ - - <
23
+ - !ruby/object:Gem::Version
24
+ version: '4.1'
24
25
  type: :runtime
25
26
  prerelease: false
26
27
  version_requirements: !ruby/object:Gem::Requirement
27
- none: false
28
28
  requirements:
29
- - - ~>
29
+ - - '>='
30
+ - !ruby/object:Gem::Version
31
+ version: 4.0.0.rc1
32
+ - - <
30
33
  - !ruby/object:Gem::Version
31
- version: '3.0'
34
+ version: '4.1'
32
35
  - !ruby/object:Gem::Dependency
33
36
  name: actionpack
34
37
  requirement: !ruby/object:Gem::Requirement
35
- none: false
36
38
  requirements:
37
- - - ~>
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: 4.0.0.rc1
42
+ - - <
38
43
  - !ruby/object:Gem::Version
39
- version: '3.0'
44
+ version: '4.1'
40
45
  type: :runtime
41
46
  prerelease: false
42
47
  version_requirements: !ruby/object:Gem::Requirement
43
- none: false
44
48
  requirements:
45
- - - ~>
49
+ - - '>='
46
50
  - !ruby/object:Gem::Version
47
- version: '3.0'
51
+ version: 4.0.0.rc1
52
+ - - <
53
+ - !ruby/object:Gem::Version
54
+ version: '4.1'
48
55
  description: Forms made easy!
49
56
  email: contact@plataformatec.com.br
50
57
  executables: []
@@ -65,7 +72,6 @@ files:
65
72
  - lib/generators/simple_form/templates/README
66
73
  - lib/generators/simple_form/USAGE
67
74
  - lib/simple_form/action_view_extensions/builder.rb
68
- - lib/simple_form/action_view_extensions/builder.rb.orig
69
75
  - lib/simple_form/action_view_extensions/form_helper.rb
70
76
  - lib/simple_form/components/errors.rb
71
77
  - lib/simple_form/components/hints.rb
@@ -78,10 +84,8 @@ files:
78
84
  - lib/simple_form/components/placeholders.rb
79
85
  - lib/simple_form/components/readonly.rb
80
86
  - lib/simple_form/components.rb
81
- - lib/simple_form/core_ext/hash.rb
82
87
  - lib/simple_form/error_notification.rb
83
88
  - lib/simple_form/form_builder.rb
84
- - lib/simple_form/form_builder.rb.orig
85
89
  - lib/simple_form/helpers/autofocus.rb
86
90
  - lib/simple_form/helpers/disabled.rb
87
91
  - lib/simple_form/helpers/readonly.rb
@@ -108,8 +112,9 @@ files:
108
112
  - lib/simple_form/inputs/text_input.rb
109
113
  - lib/simple_form/inputs.rb
110
114
  - lib/simple_form/map_type.rb
115
+ - lib/simple_form/railtie.rb
116
+ - lib/simple_form/tags.rb
111
117
  - lib/simple_form/version.rb
112
- - lib/simple_form/version.rb.orig
113
118
  - lib/simple_form/wrappers/builder.rb
114
119
  - lib/simple_form/wrappers/many.rb
115
120
  - lib/simple_form/wrappers/root.rb
@@ -153,34 +158,28 @@ files:
153
158
  - test/support/models.rb
154
159
  - test/test_helper.rb
155
160
  homepage: https://github.com/plataformatec/simple_form
156
- licenses: []
161
+ licenses:
162
+ - MIT
163
+ metadata: {}
157
164
  post_install_message:
158
165
  rdoc_options: []
159
166
  require_paths:
160
167
  - lib
161
168
  required_ruby_version: !ruby/object:Gem::Requirement
162
- none: false
163
169
  requirements:
164
- - - ! '>='
170
+ - - '>='
165
171
  - !ruby/object:Gem::Version
166
172
  version: '0'
167
- segments:
168
- - 0
169
- hash: -389428450847468473
170
173
  required_rubygems_version: !ruby/object:Gem::Requirement
171
- none: false
172
174
  requirements:
173
- - - ! '>='
175
+ - - '>'
174
176
  - !ruby/object:Gem::Version
175
- version: '0'
176
- segments:
177
- - 0
178
- hash: -389428450847468473
177
+ version: 1.3.1
179
178
  requirements: []
180
179
  rubyforge_project: simple_form
181
- rubygems_version: 1.8.23
180
+ rubygems_version: 2.0.3
182
181
  signing_key:
183
- specification_version: 3
182
+ specification_version: 4
184
183
  summary: Forms made easy!
185
184
  test_files:
186
185
  - test/action_view_extensions/builder_test.rb
@@ -1,247 +0,0 @@
1
- module SimpleForm
2
- module ActionViewExtensions
3
- # A collection of methods required by simple_form but added to rails default form.
4
- # This means that you can use such methods outside simple_form context.
5
- module Builder
6
-
7
- # Wrapper for using SimpleForm inside a default rails form.
8
- # Example:
9
- #
10
- # form_for @user do |f|
11
- # f.simple_fields_for :posts do |posts_form|
12
- # # Here you have all simple_form methods available
13
- # posts_form.input :title
14
- # end
15
- # end
16
- def simple_fields_for(*args, &block)
17
- options = args.extract_options!
18
- options[:wrapper] = self.options[:wrapper] if options[:wrapper].nil?
19
- options[:defaults] ||= self.options[:defaults]
20
-
21
- if self.class < ActionView::Helpers::FormBuilder
22
- options[:builder] ||= self.class
23
- else
24
- options[:builder] ||= SimpleForm::FormBuilder
25
- end
26
- fields_for(*(args << options), &block)
27
- end
28
- end
29
- end
30
- end
31
-
32
- module SimpleForm
33
- module Tags
34
- module CollectionExtensions
35
- private
36
-
37
- def render_collection
38
- item_wrapper_tag = @options.fetch(:item_wrapper_tag, :span)
39
- item_wrapper_class = @options[:item_wrapper_class]
40
-
41
- @collection.map do |item|
42
- value = value_for_collection(item, @value_method)
43
- text = value_for_collection(item, @text_method)
44
- default_html_options = default_html_options_for_collection(item, value)
45
-
46
- rendered_item = yield item, value, text, default_html_options
47
-
48
- item_wrapper_tag ? @template_object.content_tag(item_wrapper_tag, rendered_item, :class => item_wrapper_class) : rendered_item
49
- end.join.html_safe
50
- end
51
-
52
- def wrap_rendered_collection(collection)
53
- wrapper_tag = @options[:collection_wrapper_tag]
54
-
55
- if wrapper_tag
56
- wrapper_class = @options[:collection_wrapper_class]
57
- @template_object.content_tag(wrapper_tag, collection, :class => wrapper_class)
58
- else
59
- collection
60
- end
61
- end
62
- end
63
-
64
- class CollectionRadioButtons < ActionView::Helpers::Tags::CollectionRadioButtons
65
- include CollectionExtensions
66
-
67
- def render
68
- wrap_rendered_collection(super)
69
- end
70
-
71
- private
72
-
73
- def render_component(builder)
74
- builder.radio_button + builder.label(:class => "collection_radio_buttons")
75
- end
76
- end
77
-
78
- class CollectionCheckBoxes < ActionView::Helpers::Tags::CollectionCheckBoxes
79
- include CollectionExtensions
80
-
81
- def render
82
- wrap_rendered_collection(super)
83
- end
84
-
85
- private
86
-
87
- def render_component(builder)
88
- builder.check_box + builder.label(:class => "collection_check_boxes")
89
- end
90
- end
91
- end
92
- end
93
-
94
- module ActionView::Helpers
95
- class FormBuilder
96
- include SimpleForm::ActionViewExtensions::Builder
97
- end
98
-
99
- <<<<<<< HEAD
100
- # Create a collection of radio inputs for the attribute. Basically this
101
- # helper will create a radio input associated with a label for each
102
- # text/value option in the collection, using value_method and text_method
103
- # to convert these text/value. You can give a symbol or a proc to both
104
- # value_method and text_method, that will be evaluated for each item in
105
- # the collection.
106
- #
107
- # == Examples
108
- #
109
- # form_for @user do |f|
110
- # f.collection_radio_buttons :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
111
- # end
112
- #
113
- # <input id="user_options_true" name="user[options]" type="radio" value="true" />
114
- # <label class="collection_radio_buttons" for="user_options_true">Yes</label>
115
- # <input id="user_options_false" name="user[options]" type="radio" value="false" />
116
- # <label class="collection_radio_buttons" for="user_options_false">No</label>
117
- #
118
- # It is also possible to give a block that should generate the radio +
119
- # label. To wrap the radio with the label, for instance:
120
- #
121
- # form_for @user do |f|
122
- # f.collection_radio_buttons(
123
- # :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
124
- # ) do |b|
125
- # b.label { b.radio_button + b.text }
126
- # end
127
- # end
128
- #
129
- # == Options
130
- #
131
- # Collection radio accepts some extra options:
132
- #
133
- # * checked => the value that should be checked initially.
134
- #
135
- # * disabled => the value or values that should be disabled. Accepts a single
136
- # item or an array of items.
137
- #
138
- # * collection_wrapper_tag => the tag to wrap the entire collection.
139
- #
140
- # * collection_wrapper_class => the CSS class to use for collection_wrapper_tag
141
- #
142
- # * item_wrapper_tag => the tag to wrap each item in the collection.
143
- #
144
- # * item_wrapper_class => the CSS class to use for item_wrapper_tag
145
- #
146
- # * a block => to generate the label + radio or any other component.
147
- def collection_radio_buttons(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
148
- SimpleForm::Tags::CollectionRadioButtons.new(@object_name, method, @template, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options)).render(&block)
149
- =======
150
- module FormOptionsHelper
151
- # Override Rails options_from_collection_for_select to handle lambdas/procs in
152
- # text and value methods, so it works the same way as collection_radio_buttons
153
- # and collection_check_boxes in SimpleForm. If none of text/value methods is a
154
- # callable object, then it just delegates back to original collection select.
155
- # FIXME: remove when support only Rails 4.0 forward
156
- # https://github.com/rails/rails/commit/9035324367526af0300477a58b6d3efc15d1a5a8
157
- alias :original_options_from_collection_for_select :options_from_collection_for_select
158
- def options_from_collection_for_select(collection, value_method, text_method, selected = nil)
159
- if value_method.respond_to?(:call) || text_method.respond_to?(:call)
160
- collection = collection.map do |item|
161
- value = value_for_collection(item, value_method)
162
- text = value_for_collection(item, text_method)
163
-
164
- [value, text]
165
- end
166
-
167
- value_method, text_method = :first, :last
168
- selected = extract_selected_and_disabled_and_call_procs selected, collection
169
- end
170
-
171
- original_options_from_collection_for_select collection, value_method, text_method, selected
172
- end
173
-
174
- private
175
-
176
- def extract_selected_and_disabled_and_call_procs(selected, collection)
177
- selected, disabled = extract_selected_and_disabled selected
178
- selected_disabled = { :selected => selected, :disabled => disabled }
179
-
180
- selected_disabled.each do |key, check|
181
- if check.is_a? Proc
182
- values = collection.map { |option| option.first if check.call(option.first) }
183
- selected_disabled[key] = values
184
- end
185
- end
186
- end
187
-
188
- def value_for_collection(item, value) #:nodoc:
189
- value.respond_to?(:call) ? value.call(item) : item.send(value)
190
- >>>>>>> master
191
- end
192
-
193
- # Creates a collection of check boxes for each item in the collection,
194
- # associated with a clickable label. Use value_method and text_method to
195
- # convert items in the collection for use as text/value in check boxes.
196
- # You can give a symbol or a proc to both value_method and text_method,
197
- # that will be evaluated for each item in the collection.
198
- #
199
- # == Examples
200
- #
201
- # form_for @user do |f|
202
- # f.collection_check_boxes :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
203
- # end
204
- #
205
- # <input name="user[options][]" type="hidden" value="" />
206
- # <input id="user_options_true" name="user[options][]" type="checkbox" value="true" />
207
- # <label class="collection_check_boxes" for="user_options_true">Yes</label>
208
- # <input name="user[options][]" type="hidden" value="" />
209
- # <input id="user_options_false" name="user[options][]" type="checkbox" value="false" />
210
- # <label class="collection_check_boxes" for="user_options_false">No</label>
211
- #
212
- # It is also possible to give a block that should generate the check box +
213
- # label. To wrap the check box with the label, for instance:
214
- #
215
- # form_for @user do |f|
216
- # f.collection_check_boxes(
217
- # :options, [[true, 'Yes'] ,[false, 'No']], :first, :last
218
- # ) do |b|
219
- # b.label { b.check_box + b.text }
220
- # end
221
- # end
222
- #
223
- # == Options
224
- #
225
- # Collection check box accepts some extra options:
226
- #
227
- # * checked => the value or values that should be checked initially. Accepts
228
- # a single item or an array of items. It overrides existing associations.
229
- #
230
- # * disabled => the value or values that should be disabled. Accepts a single
231
- # item or an array of items.
232
- #
233
- # * collection_wrapper_tag => the tag to wrap the entire collection.
234
- #
235
- # * collection_wrapper_class => the CSS class to use for collection_wrapper_tag. This option
236
- # is ignored if the :collection_wrapper_tag option is blank.
237
- #
238
- # * item_wrapper_tag => the tag to wrap each item in the collection.
239
- #
240
- # * item_wrapper_class => the CSS class to use for item_wrapper_tag
241
- #
242
- # * a block => to generate the label + check box or any other component.
243
- def collection_check_boxes(method, collection, value_method, text_method, options = {}, html_options = {}, &block)
244
- SimpleForm::Tags::CollectionCheckBoxes.new(@object_name, method, @template, collection, value_method, text_method, objectify_options(options), @default_options.merge(html_options)).render(&block)
245
- end
246
- end
247
- end
@@ -1,16 +0,0 @@
1
- # TODO: Delete this file when we drop support for Rails 3.0
2
- # This method is already implemented in active_support 3.1
3
-
4
- unless Hash.new.respond_to?(:deep_dup)
5
- class Hash
6
- # Returns a deep copy of hash.
7
- def deep_dup
8
- duplicate = self.dup
9
- duplicate.each_pair do |k,v|
10
- tv = duplicate[k]
11
- duplicate[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_dup : v
12
- end
13
- duplicate
14
- end
15
- end
16
- end