simple_form 3.0.0.beta1 → 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.
Potentially problematic release.
This version of simple_form might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/CHANGELOG.md +11 -0
- data/README.md +16 -10
- data/lib/generators/simple_form/templates/config/initializers/simple_form.rb +5 -2
- data/lib/simple_form.rb +8 -0
- data/lib/simple_form/action_view_extensions/builder.rb +1 -1
- data/lib/simple_form/action_view_extensions/form_helper.rb +1 -8
- data/lib/simple_form/components/html5.rb +4 -1
- data/lib/simple_form/components/pattern.rb +2 -2
- data/lib/simple_form/form_builder.rb +4 -4
- data/lib/simple_form/version.rb +1 -1
- data/test/action_view_extensions/builder_test.rb +8 -8
- data/test/action_view_extensions/form_helper_test.rb +9 -0
- data/test/form_builder/association_test.rb +16 -6
- data/test/form_builder/general_test.rb +16 -10
- data/test/form_builder/hint_test.rb +1 -1
- data/test/inputs/collection_check_boxes_input_test.rb +7 -1
- data/test/inputs/collection_radio_buttons_input_test.rb +11 -5
- data/test/inputs/collection_select_input_test.rb +32 -7
- data/test/inputs/datetime_input_test.rb +6 -0
- data/test/inputs/hidden_input_test.rb +2 -1
- data/test/inputs/priority_input_test.rb +6 -0
- data/test/inputs/string_input_test.rb +5 -0
- data/test/support/misc_helpers.rb +25 -3
- data/test/support/models.rb +27 -20
- data/test/test_helper.rb +1 -5
- metadata +17 -28
- data/lib/simple_form/action_view_extensions/builder.rb.orig +0 -247
- data/lib/simple_form/form_builder.rb.orig +0 -486
- data/lib/simple_form/version.rb.orig +0 -7
@@ -136,7 +136,7 @@ class CollectionSelectInputTest < ActionView::TestCase
|
|
136
136
|
|
137
137
|
test 'input should allow overriding label and value method using a lambda for collection selects' do
|
138
138
|
with_input_for @user, :name, :select,
|
139
|
-
collection: ['Jose'
|
139
|
+
collection: ['Jose', 'Carlos'],
|
140
140
|
label_method: lambda { |i| i.upcase },
|
141
141
|
value_method: lambda { |i| i.downcase }
|
142
142
|
assert_select 'select option[value=jose]', "JOSE"
|
@@ -145,7 +145,7 @@ class CollectionSelectInputTest < ActionView::TestCase
|
|
145
145
|
|
146
146
|
test 'input should allow overriding only label but not value method using a lambda for collection select' do
|
147
147
|
with_input_for @user, :name, :select,
|
148
|
-
collection: ['Jose'
|
148
|
+
collection: ['Jose', 'Carlos'],
|
149
149
|
label_method: lambda { |i| i.upcase }
|
150
150
|
assert_select 'select option[value=Jose]', "JOSE"
|
151
151
|
assert_select 'select option[value=Carlos]', "CARLOS"
|
@@ -153,7 +153,7 @@ class CollectionSelectInputTest < ActionView::TestCase
|
|
153
153
|
|
154
154
|
test 'input should allow overriding only value but not label method using a lambda for collection select' do
|
155
155
|
with_input_for @user, :name, :select,
|
156
|
-
collection: ['Jose'
|
156
|
+
collection: ['Jose', 'Carlos'],
|
157
157
|
value_method: lambda { |i| i.downcase }
|
158
158
|
assert_select 'select option[value=jose]', "Jose"
|
159
159
|
assert_select 'select option[value=carlos]', "Carlos"
|
@@ -167,29 +167,54 @@ class CollectionSelectInputTest < ActionView::TestCase
|
|
167
167
|
end
|
168
168
|
|
169
169
|
test 'collection input with select type should generate required html attribute only with blank option' do
|
170
|
-
with_input_for @user, :name, :select, include_blank: true, collection: ['Jose'
|
170
|
+
with_input_for @user, :name, :select, include_blank: true, collection: ['Jose', 'Carlos']
|
171
171
|
assert_select 'select.required'
|
172
172
|
assert_select 'select[required]'
|
173
173
|
end
|
174
174
|
|
175
175
|
test 'collection input with select type should not generate required html attribute without blank option' do
|
176
|
-
with_input_for @user, :name, :select, include_blank: false, collection: ['Jose'
|
176
|
+
with_input_for @user, :name, :select, include_blank: false, collection: ['Jose', 'Carlos']
|
177
177
|
assert_select 'select.required'
|
178
178
|
assert_no_select 'select[required]'
|
179
|
+
assert_no_select 'select[aria-required=true]'
|
179
180
|
end
|
180
181
|
|
181
182
|
test 'collection input with select type with multiple attribute should generate required html attribute without blank option' do
|
182
|
-
with_input_for @user, :name, :select, include_blank: false, input_html: {multiple: true}, collection: ['Jose'
|
183
|
+
with_input_for @user, :name, :select, include_blank: false, input_html: { multiple: true }, collection: ['Jose', 'Carlos']
|
183
184
|
assert_select 'select.required'
|
184
185
|
assert_select 'select[required]'
|
185
186
|
end
|
186
187
|
|
187
188
|
test 'collection input with select type with multiple attribute should generate required html attribute with blank option' do
|
188
|
-
with_input_for @user, :name, :select, include_blank: true, input_html: {multiple: true}, collection: ['Jose'
|
189
|
+
with_input_for @user, :name, :select, include_blank: true, input_html: { multiple: true }, collection: ['Jose', 'Carlos']
|
189
190
|
assert_select 'select.required'
|
190
191
|
assert_select 'select[required]'
|
191
192
|
end
|
192
193
|
|
194
|
+
test 'with a blank option, a collection input of type select has an aria-required html attribute' do
|
195
|
+
with_input_for @user, :name, :select, include_blank: true, collection: ['Jose', 'Carlos']
|
196
|
+
assert_select 'select.required'
|
197
|
+
assert_select 'select[aria-required=true]'
|
198
|
+
end
|
199
|
+
|
200
|
+
test 'without a blank option, a collection input of type select does not have an aria-required html attribute' do
|
201
|
+
with_input_for @user, :name, :select, include_blank: false, collection: ['Jose', 'Carlos']
|
202
|
+
assert_select 'select.required'
|
203
|
+
assert_no_select 'select[aria-required]'
|
204
|
+
end
|
205
|
+
|
206
|
+
test 'without a blank option and with a multiple option, a collection input of type select has an aria-required html attribute' do
|
207
|
+
with_input_for @user, :name, :select, include_blank: false, input_html: { multiple: true }, collection: ['Jose', 'Carlos']
|
208
|
+
assert_select 'select.required'
|
209
|
+
assert_select 'select[aria-required=true]'
|
210
|
+
end
|
211
|
+
|
212
|
+
test 'with a blank option and a multiple option, a collection input of type select has an aria-required html attribute' do
|
213
|
+
with_input_for @user, :name, :select, include_blank: true, input_html: { multiple: true }, collection: ['Jose', 'Carlos']
|
214
|
+
assert_select 'select.required'
|
215
|
+
assert_select 'select[aria-required]'
|
216
|
+
end
|
217
|
+
|
193
218
|
test 'input should allow disabled options with a lambda for collection select' do
|
194
219
|
with_input_for @user, :name, :select, collection: ["Carlos", "Antonio"],
|
195
220
|
disabled: lambda { |x| x == "Carlos" }
|
@@ -96,4 +96,10 @@ class DateTimeInputTest < ActionView::TestCase
|
|
96
96
|
assert_select 'select.required'
|
97
97
|
assert_select 'select[required]'
|
98
98
|
end
|
99
|
+
|
100
|
+
test 'date time input has an aria-required html attribute' do
|
101
|
+
with_input_for @user, :delivery_time, :time, required: true
|
102
|
+
assert_select 'select.required'
|
103
|
+
assert_select 'select[aria-required=true]'
|
104
|
+
end
|
99
105
|
end
|
@@ -20,10 +20,11 @@ class HiddenInputTest < ActionView::TestCase
|
|
20
20
|
assert_no_select 'label'
|
21
21
|
end
|
22
22
|
|
23
|
-
test 'required/optional options should not be generated for hidden inputs' do
|
23
|
+
test 'required/aria-required/optional options should not be generated for hidden inputs' do
|
24
24
|
with_input_for @user, :name, :hidden
|
25
25
|
assert_no_select 'input.required'
|
26
26
|
assert_no_select 'input[required]'
|
27
|
+
assert_no_select 'input[aria-required]'
|
27
28
|
assert_no_select 'input.optional'
|
28
29
|
assert_select 'input.hidden#user_name'
|
29
30
|
end
|
@@ -40,4 +40,10 @@ class PriorityInputTest < ActionView::TestCase
|
|
40
40
|
assert_select 'select.required'
|
41
41
|
assert_no_select 'select[required]'
|
42
42
|
end
|
43
|
+
|
44
|
+
test 'priority input should not generate invalid aria-required html attribute' do
|
45
|
+
with_input_for @user, :country, :country
|
46
|
+
assert_select 'select.required'
|
47
|
+
assert_no_select 'select[aria-required]'
|
48
|
+
end
|
43
49
|
end
|
@@ -90,6 +90,11 @@ class StringInputTest < ActionView::TestCase
|
|
90
90
|
assert_select 'input[pattern="\d+"]'
|
91
91
|
end
|
92
92
|
|
93
|
+
test 'input should not use pattern if model has :without validation option' do
|
94
|
+
with_input_for @other_validating_user, :description, :string, pattern: true
|
95
|
+
assert_no_select 'input[pattern="\d+"]'
|
96
|
+
end
|
97
|
+
|
93
98
|
test 'input should use i18n to translate placeholder text' do
|
94
99
|
store_translations(:en, simple_form: { placeholders: { user: {
|
95
100
|
name: 'Name goes here'
|
@@ -24,6 +24,28 @@ module MiscHelpers
|
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
27
|
+
def stub_any_instance(klass, method, value)
|
28
|
+
klass.class_eval do
|
29
|
+
alias_method :"new_#{method}", method
|
30
|
+
|
31
|
+
define_method(method) do
|
32
|
+
if value.respond_to?(:call)
|
33
|
+
value.call
|
34
|
+
else
|
35
|
+
value
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
yield
|
41
|
+
ensure
|
42
|
+
klass.class_eval do
|
43
|
+
undef_method method
|
44
|
+
alias_method method, :"new_#{method}"
|
45
|
+
undef_method :"new_#{method}"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
27
49
|
def swap_wrapper(name=:default, wrapper=self.custom_wrapper)
|
28
50
|
old = SimpleForm.wrappers[name]
|
29
51
|
SimpleForm.wrappers[name] = wrapper
|
@@ -91,11 +113,11 @@ module MiscHelpers
|
|
91
113
|
end
|
92
114
|
|
93
115
|
def custom_form_for(object, *args, &block)
|
94
|
-
simple_form_for(object, *
|
116
|
+
simple_form_for(object, *args, { builder: CustomFormBuilder }, &block)
|
95
117
|
end
|
96
118
|
|
97
119
|
def custom_mapping_form_for(object, *args, &block)
|
98
|
-
simple_form_for(object, *
|
120
|
+
simple_form_for(object, *args, { builder: CustomMapTypeFormBuilder }, &block)
|
99
121
|
end
|
100
122
|
|
101
123
|
def with_concat_form_for(*args, &block)
|
@@ -129,7 +151,7 @@ end
|
|
129
151
|
|
130
152
|
class CustomFormBuilder < SimpleForm::FormBuilder
|
131
153
|
def input(attribute_name, *args, &block)
|
132
|
-
super(attribute_name, *
|
154
|
+
super(attribute_name, *args, { input_html: { class: 'custom' } }, &block)
|
133
155
|
end
|
134
156
|
end
|
135
157
|
|
data/test/support/models.rb
CHANGED
@@ -1,27 +1,38 @@
|
|
1
1
|
Association = Struct.new(:klass, :name, :macro, :options)
|
2
2
|
|
3
|
-
|
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
|
-
|
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
|
-
|
15
|
-
|
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.
|
24
|
-
|
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
|
-
|
39
|
-
end
|
45
|
+
TagGroup = Struct.new(:id, :name, :tags)
|
40
46
|
|
41
47
|
class User
|
42
48
|
extend ActiveModel::Naming
|
@@ -187,15 +193,16 @@ class OtherValidatingUser < User
|
|
187
193
|
only_integer: true
|
188
194
|
validates_numericality_of :amount,
|
189
195
|
greater_than: Proc.new { |user| user.age },
|
190
|
-
less_than: Proc.new { |user| user.age + 100},
|
196
|
+
less_than: Proc.new { |user| user.age + 100 },
|
191
197
|
only_integer: true
|
192
198
|
validates_numericality_of :attempts,
|
193
199
|
greater_than_or_equal_to: Proc.new { |user| user.age },
|
194
|
-
less_than_or_equal_to: Proc.new { |user| user.age + 100},
|
200
|
+
less_than_or_equal_to: Proc.new { |user| user.age + 100 },
|
195
201
|
only_integer: true
|
196
202
|
|
197
203
|
validates_format_of :country, with: /\w+/
|
198
204
|
validates_format_of :name, with: Proc.new { /\w+/ }
|
205
|
+
validates_format_of :description, without: /\d+/
|
199
206
|
end
|
200
207
|
|
201
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 '
|
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
|
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: 3.0.0.
|
5
|
-
prerelease: 6
|
4
|
+
version: 3.0.0.rc
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- José Valim
|
@@ -11,49 +10,45 @@ authors:
|
|
11
10
|
autorequire:
|
12
11
|
bindir: bin
|
13
12
|
cert_chain: []
|
14
|
-
date: 2013-
|
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: 4.0.0.
|
21
|
+
version: 4.0.0.rc1
|
24
22
|
- - <
|
25
23
|
- !ruby/object:Gem::Version
|
26
24
|
version: '4.1'
|
27
25
|
type: :runtime
|
28
26
|
prerelease: false
|
29
27
|
version_requirements: !ruby/object:Gem::Requirement
|
30
|
-
none: false
|
31
28
|
requirements:
|
32
|
-
- -
|
29
|
+
- - '>='
|
33
30
|
- !ruby/object:Gem::Version
|
34
|
-
version: 4.0.0.
|
31
|
+
version: 4.0.0.rc1
|
35
32
|
- - <
|
36
33
|
- !ruby/object:Gem::Version
|
37
34
|
version: '4.1'
|
38
35
|
- !ruby/object:Gem::Dependency
|
39
36
|
name: actionpack
|
40
37
|
requirement: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
38
|
requirements:
|
43
|
-
- -
|
39
|
+
- - '>='
|
44
40
|
- !ruby/object:Gem::Version
|
45
|
-
version: 4.0.0.
|
41
|
+
version: 4.0.0.rc1
|
46
42
|
- - <
|
47
43
|
- !ruby/object:Gem::Version
|
48
44
|
version: '4.1'
|
49
45
|
type: :runtime
|
50
46
|
prerelease: false
|
51
47
|
version_requirements: !ruby/object:Gem::Requirement
|
52
|
-
none: false
|
53
48
|
requirements:
|
54
|
-
- -
|
49
|
+
- - '>='
|
55
50
|
- !ruby/object:Gem::Version
|
56
|
-
version: 4.0.0.
|
51
|
+
version: 4.0.0.rc1
|
57
52
|
- - <
|
58
53
|
- !ruby/object:Gem::Version
|
59
54
|
version: '4.1'
|
@@ -77,7 +72,6 @@ files:
|
|
77
72
|
- lib/generators/simple_form/templates/README
|
78
73
|
- lib/generators/simple_form/USAGE
|
79
74
|
- lib/simple_form/action_view_extensions/builder.rb
|
80
|
-
- lib/simple_form/action_view_extensions/builder.rb.orig
|
81
75
|
- lib/simple_form/action_view_extensions/form_helper.rb
|
82
76
|
- lib/simple_form/components/errors.rb
|
83
77
|
- lib/simple_form/components/hints.rb
|
@@ -92,7 +86,6 @@ files:
|
|
92
86
|
- lib/simple_form/components.rb
|
93
87
|
- lib/simple_form/error_notification.rb
|
94
88
|
- lib/simple_form/form_builder.rb
|
95
|
-
- lib/simple_form/form_builder.rb.orig
|
96
89
|
- lib/simple_form/helpers/autofocus.rb
|
97
90
|
- lib/simple_form/helpers/disabled.rb
|
98
91
|
- lib/simple_form/helpers/readonly.rb
|
@@ -122,7 +115,6 @@ files:
|
|
122
115
|
- lib/simple_form/railtie.rb
|
123
116
|
- lib/simple_form/tags.rb
|
124
117
|
- lib/simple_form/version.rb
|
125
|
-
- lib/simple_form/version.rb.orig
|
126
118
|
- lib/simple_form/wrappers/builder.rb
|
127
119
|
- lib/simple_form/wrappers/many.rb
|
128
120
|
- lib/simple_form/wrappers/root.rb
|
@@ -166,31 +158,28 @@ files:
|
|
166
158
|
- test/support/models.rb
|
167
159
|
- test/test_helper.rb
|
168
160
|
homepage: https://github.com/plataformatec/simple_form
|
169
|
-
licenses:
|
161
|
+
licenses:
|
162
|
+
- MIT
|
163
|
+
metadata: {}
|
170
164
|
post_install_message:
|
171
165
|
rdoc_options: []
|
172
166
|
require_paths:
|
173
167
|
- lib
|
174
168
|
required_ruby_version: !ruby/object:Gem::Requirement
|
175
|
-
none: false
|
176
169
|
requirements:
|
177
|
-
- -
|
170
|
+
- - '>='
|
178
171
|
- !ruby/object:Gem::Version
|
179
172
|
version: '0'
|
180
|
-
segments:
|
181
|
-
- 0
|
182
|
-
hash: 2771371477718210215
|
183
173
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
184
|
-
none: false
|
185
174
|
requirements:
|
186
|
-
- -
|
175
|
+
- - '>'
|
187
176
|
- !ruby/object:Gem::Version
|
188
177
|
version: 1.3.1
|
189
178
|
requirements: []
|
190
179
|
rubyforge_project: simple_form
|
191
|
-
rubygems_version:
|
180
|
+
rubygems_version: 2.0.3
|
192
181
|
signing_key:
|
193
|
-
specification_version:
|
182
|
+
specification_version: 4
|
194
183
|
summary: Forms made easy!
|
195
184
|
test_files:
|
196
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
|