meta_search 0.3.0 → 0.5.0
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.
- data/.gitmodules +6 -0
- data/Gemfile +7 -0
- data/README.rdoc +165 -26
- data/Rakefile +18 -5
- data/VERSION +1 -1
- data/lib/meta_search.rb +28 -18
- data/lib/meta_search/builder.rb +178 -125
- data/lib/meta_search/exceptions.rb +1 -0
- data/lib/meta_search/helpers.rb +3 -0
- data/lib/meta_search/helpers/form_builder.rb +152 -0
- data/lib/meta_search/helpers/form_helper.rb +20 -0
- data/lib/meta_search/helpers/url_helper.rb +39 -0
- data/lib/meta_search/method.rb +129 -0
- data/lib/meta_search/model_compatibility.rb +36 -2
- data/lib/meta_search/searches/active_record.rb +88 -11
- data/lib/meta_search/utility.rb +1 -1
- data/lib/meta_search/where.rb +119 -61
- data/meta_search.gemspec +33 -13
- data/test/fixtures/company.rb +15 -2
- data/test/fixtures/data_types.yml +3 -3
- data/test/fixtures/developer.rb +3 -0
- data/test/helper.rb +10 -6
- data/test/test_search.rb +502 -288
- data/test/test_view_helpers.rb +152 -53
- metadata +82 -15
- data/lib/meta_search/helpers/action_view.rb +0 -168
- data/lib/meta_search/railtie.rb +0 -21
- data/lib/meta_search/searches/base.rb +0 -46
data/test/test_view_helpers.rb
CHANGED
@@ -3,14 +3,41 @@ require 'action_controller'
|
|
3
3
|
require 'action_view/test_case'
|
4
4
|
|
5
5
|
class TestViewHelpers < ActionView::TestCase
|
6
|
-
tests
|
6
|
+
tests MetaSearch::Helpers::FormHelper
|
7
|
+
include MetaSearch::Helpers::UrlHelper
|
8
|
+
|
9
|
+
router = ActionDispatch::Routing::RouteSet.new
|
10
|
+
router.draw do |map|
|
11
|
+
resources :developers
|
12
|
+
resources :companies
|
13
|
+
resources :projects
|
14
|
+
resources :notes
|
15
|
+
match ':controller(/:action(/:id(.:format)))'
|
16
|
+
end
|
17
|
+
include router.url_helpers
|
18
|
+
|
19
|
+
def setup
|
20
|
+
@controller = Class.new do
|
21
|
+
|
22
|
+
attr_reader :url_for_options
|
23
|
+
def url_for(options)
|
24
|
+
@url_for_options = options
|
25
|
+
"http://www.example.com"
|
26
|
+
end
|
27
|
+
|
28
|
+
def _router
|
29
|
+
@router ||= ActionDispatch::Routing::RouteSet.new
|
30
|
+
end
|
31
|
+
end
|
32
|
+
@controller = @controller.new
|
33
|
+
end
|
7
34
|
|
8
35
|
context "A previously-filled search form" do
|
9
36
|
setup do
|
10
37
|
@s = Company.search
|
11
38
|
@s.created_at_gte = [2001, 2, 3, 4, 5]
|
12
39
|
@s.name_contains = "bacon"
|
13
|
-
|
40
|
+
form_for @s do |f|
|
14
41
|
@f = f
|
15
42
|
end
|
16
43
|
end
|
@@ -31,7 +58,7 @@ class TestViewHelpers < ActionView::TestCase
|
|
31
58
|
context "A form using mutiparameter_field with default size option" do
|
32
59
|
setup do
|
33
60
|
@s = Developer.search
|
34
|
-
|
61
|
+
form_for @s do |f|
|
35
62
|
@f = f
|
36
63
|
end
|
37
64
|
end
|
@@ -51,35 +78,39 @@ class TestViewHelpers < ActionView::TestCase
|
|
51
78
|
context "A form using check_boxes with three choices" do
|
52
79
|
setup do
|
53
80
|
@s = Company.search
|
54
|
-
|
81
|
+
form_for @s do |f|
|
55
82
|
@f = f
|
56
83
|
end
|
57
84
|
end
|
58
85
|
|
59
|
-
should "
|
60
|
-
|
61
|
-
'type="checkbox" value="1" /><label for="search_id_in_1">One</label>' +
|
62
|
-
'<input id="search_id_in_2" name="search[id_in][]" ' +
|
63
|
-
'type="checkbox" value="2" /><label for="search_id_in_2">Two</label>' +
|
64
|
-
'<input id="search_id_in_3" name="search[id_in][]" ' +
|
65
|
-
'type="checkbox" value="3" /><label for="search_id_in_3">Three</label>',
|
66
|
-
@f.check_boxes(:id_in, [['One', 1], ['Two', 2], ['Three', 3]])
|
86
|
+
should "return an array of check boxes without a block" do
|
87
|
+
assert @f.check_boxes(:id_in, [['One', 1], ['Two', 2], ['Three', 3]]).all?{|c| c.is_a?(MetaSearch::Check)}
|
67
88
|
end
|
68
89
|
|
69
90
|
should "generate the expected HTML with a block" do
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
91
|
+
expected = <<-EXPECTED
|
92
|
+
<p>
|
93
|
+
<label for="search_id_in_1">One</label>
|
94
|
+
<input id="search_id_in_1" name="search[id_in][]" type="checkbox" value="1" />
|
95
|
+
</p>
|
96
|
+
<p>
|
97
|
+
<label for="search_id_in_2">Two</label>
|
98
|
+
<input id="search_id_in_2" name="search[id_in][]" type="checkbox" value="2" />
|
99
|
+
</p>
|
100
|
+
<p>
|
101
|
+
<label for="search_id_in_3">Three</label>
|
102
|
+
<input id="search_id_in_3" name="search[id_in][]" type="checkbox" value="3" />
|
103
|
+
</p>
|
104
|
+
EXPECTED
|
105
|
+
assert_dom_equal expected,
|
106
|
+
render(:to => :string, :inline => <<-ERB)
|
107
|
+
<%= @f.check_boxes(:id_in, [['One', 1], ['Two', 2], ['Three', 3]]) do |c| -%>
|
108
|
+
<p>
|
109
|
+
<%= c.label %>
|
110
|
+
<%= c.box %>
|
111
|
+
</p>
|
112
|
+
<% end -%>
|
113
|
+
ERB
|
83
114
|
end
|
84
115
|
end
|
85
116
|
|
@@ -87,54 +118,56 @@ class TestViewHelpers < ActionView::TestCase
|
|
87
118
|
setup do
|
88
119
|
@s = Company.search
|
89
120
|
@s.id_in = [1, 3]
|
90
|
-
|
121
|
+
form_for @s do |f|
|
91
122
|
@f = f
|
92
123
|
end
|
93
124
|
end
|
94
125
|
|
95
|
-
should "
|
96
|
-
|
97
|
-
'type="checkbox" value="1" /><label for="search_id_in_1">One</label>' +
|
98
|
-
'<input id="search_id_in_2" name="search[id_in][]" ' +
|
99
|
-
'type="checkbox" value="2" /><label for="search_id_in_2">Two</label>' +
|
100
|
-
'<input checked="checked" id="search_id_in_3" name="search[id_in][]" ' +
|
101
|
-
'type="checkbox" value="3" /><label for="search_id_in_3">Three</label>',
|
102
|
-
@f.check_boxes(:id_in, [['One', 1], ['Two', 2], ['Three', 3]])
|
126
|
+
should "return an array of check boxes without a block" do
|
127
|
+
assert @f.check_boxes(:id_in, [['One', 1], ['Two', 2], ['Three', 3]]).all?{|c| c.is_a?(MetaSearch::Check)}
|
103
128
|
end
|
104
129
|
|
105
130
|
should "generate the expected HTML with a block" do
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
131
|
+
expected = <<-EXPECTED
|
132
|
+
<p>
|
133
|
+
<label for="search_id_in_1">One</label>
|
134
|
+
<input id="search_id_in_1" name="search[id_in][]" type="checkbox" value="1" checked="checked" />
|
135
|
+
</p>
|
136
|
+
<p>
|
137
|
+
<label for="search_id_in_2">Two</label>
|
138
|
+
<input id="search_id_in_2" name="search[id_in][]" type="checkbox" value="2" />
|
139
|
+
</p>
|
140
|
+
<p>
|
141
|
+
<label for="search_id_in_3">Three</label>
|
142
|
+
<input id="search_id_in_3" name="search[id_in][]" type="checkbox" value="3" checked="checked" />
|
143
|
+
</p>
|
144
|
+
EXPECTED
|
145
|
+
assert_dom_equal expected,
|
146
|
+
render(:to => :string, :inline => <<-ERB)
|
147
|
+
<%= @f.check_boxes(:id_in, [['One', 1], ['Two', 2], ['Three', 3]]) do |c| -%>
|
148
|
+
<p>
|
149
|
+
<%= c.label %>
|
150
|
+
<%= c.box %>
|
151
|
+
</p>
|
152
|
+
<% end -%>
|
153
|
+
ERB
|
115
154
|
end
|
116
155
|
|
117
156
|
context "A form using collection_check_boxes with companies" do
|
118
157
|
setup do
|
119
158
|
@s = Company.search
|
120
|
-
|
159
|
+
form_for @s do |f|
|
121
160
|
@f = f
|
122
161
|
end
|
123
162
|
end
|
124
163
|
|
125
|
-
should "
|
126
|
-
|
127
|
-
'value="1" /><label for="search_id_in_1">Initech</label>' +
|
128
|
-
'<input id="search_id_in_2" name="search[id_in][]" type="checkbox" ' +
|
129
|
-
'value="2" /><label for="search_id_in_2">Advanced Optical Solutions</label>' +
|
130
|
-
'<input id="search_id_in_3" name="search[id_in][]" type="checkbox" ' +
|
131
|
-
'value="3" /><label for="search_id_in_3">Mission Data</label>',
|
132
|
-
@f.collection_check_boxes(:id_in, Company.all, :id, :name)
|
164
|
+
should "return an array of check boxes without a block" do
|
165
|
+
assert @f.collection_check_boxes(:id_in, Company.all, :id, :name).all?{|c| c.is_a?(MetaSearch::Check)}
|
133
166
|
end
|
134
167
|
|
135
168
|
should "generate the expected HTML with a block" do
|
136
169
|
@f.collection_check_boxes(:id_in, Company.all, :id, :name) do |c|
|
137
|
-
concat render :to => :string, :inline => "<p><%= c
|
170
|
+
concat render :to => :string, :inline => "<p><%= c.label %> <%= c.box %></p>", :locals => {:c => c}
|
138
171
|
end
|
139
172
|
assert_dom_equal output_buffer,
|
140
173
|
'<p><label for="search_id_in_1">Initech</label> ' +
|
@@ -146,4 +179,70 @@ class TestViewHelpers < ActionView::TestCase
|
|
146
179
|
end
|
147
180
|
end
|
148
181
|
end
|
182
|
+
|
183
|
+
context "A company search" do
|
184
|
+
setup do
|
185
|
+
@s = Company.search
|
186
|
+
end
|
187
|
+
|
188
|
+
context "sorted by name ascending" do
|
189
|
+
setup do
|
190
|
+
@s.meta_sort = 'name.asc'
|
191
|
+
end
|
192
|
+
|
193
|
+
should "generate a sort link with an up arrow for the sorted column" do
|
194
|
+
assert_match /Name ▲/,
|
195
|
+
sort_link(@s, :name, :controller => 'companies')
|
196
|
+
end
|
197
|
+
|
198
|
+
should "not generate a sort link with an up arrow for a non-sorted column" do
|
199
|
+
assert_no_match /Created at ▲/,
|
200
|
+
sort_link(@s, :created_at, :controller => 'companies')
|
201
|
+
end
|
202
|
+
|
203
|
+
context "with existing search options" do
|
204
|
+
setup do
|
205
|
+
@s.name_contains = 'a'
|
206
|
+
end
|
207
|
+
|
208
|
+
should "maintain previous search options in its sort links" do
|
209
|
+
assert_match /search\[name_contains\]=a/,
|
210
|
+
sort_link(@s, :name, :controller => 'companies')
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
context "A developer search" do
|
217
|
+
setup do
|
218
|
+
@s = Developer.search
|
219
|
+
end
|
220
|
+
|
221
|
+
context "sorted by company name descending" do
|
222
|
+
setup do
|
223
|
+
@s.meta_sort = 'company_name.desc'
|
224
|
+
end
|
225
|
+
|
226
|
+
should "generate a sort link with a down arrow for the sorted column" do
|
227
|
+
assert_match /Company name ▼/,
|
228
|
+
sort_link(@s, :company_name, :controller => 'developers')
|
229
|
+
end
|
230
|
+
|
231
|
+
should "not generate a sort link with a down arrow for a non-sorted column" do
|
232
|
+
assert_no_match /Created at ▼/,
|
233
|
+
sort_link(@s, :created_at, :controller => 'developers')
|
234
|
+
end
|
235
|
+
|
236
|
+
context "with existing search options" do
|
237
|
+
setup do
|
238
|
+
@s.name_contains = 'a'
|
239
|
+
end
|
240
|
+
|
241
|
+
should "maintain previous search options in its sort links" do
|
242
|
+
assert_match /search\[name_contains\]=a/,
|
243
|
+
sort_link(@s, :company_name, :controller => 'companies')
|
244
|
+
end
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
149
248
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 5
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.5.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ernie Miller
|
@@ -14,25 +14,86 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-06-08 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
name:
|
21
|
+
name: shoulda
|
22
22
|
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
24
25
|
requirements:
|
25
26
|
- - ">="
|
26
27
|
- !ruby/object:Gem::Version
|
27
28
|
segments:
|
28
|
-
- 3
|
29
|
-
- 0
|
30
29
|
- 0
|
31
|
-
|
32
|
-
version: 3.0.0.beta
|
30
|
+
version: "0"
|
33
31
|
type: :development
|
34
32
|
version_requirements: *id001
|
35
|
-
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: activerecord
|
35
|
+
prerelease: false
|
36
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 3
|
43
|
+
- 0
|
44
|
+
- 0
|
45
|
+
- beta4
|
46
|
+
version: 3.0.0.beta4
|
47
|
+
type: :runtime
|
48
|
+
version_requirements: *id002
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: activesupport
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
segments:
|
58
|
+
- 3
|
59
|
+
- 0
|
60
|
+
- 0
|
61
|
+
- beta4
|
62
|
+
version: 3.0.0.beta4
|
63
|
+
type: :runtime
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: actionpack
|
67
|
+
prerelease: false
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
segments:
|
74
|
+
- 3
|
75
|
+
- 0
|
76
|
+
- 0
|
77
|
+
- beta4
|
78
|
+
version: 3.0.0.beta4
|
79
|
+
type: :runtime
|
80
|
+
version_requirements: *id004
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: arel
|
83
|
+
prerelease: false
|
84
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
85
|
+
none: false
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
segments:
|
90
|
+
- 0
|
91
|
+
- 4
|
92
|
+
- 0
|
93
|
+
version: 0.4.0
|
94
|
+
type: :runtime
|
95
|
+
version_requirements: *id005
|
96
|
+
description: "\n Allows simple search forms to be created against an AR3 model\n and its associations, has useful view helpers for sort links\n and multiparameter fields as well.\n "
|
36
97
|
email: ernie@metautonomo.us
|
37
98
|
executables: []
|
38
99
|
|
@@ -44,6 +105,8 @@ extra_rdoc_files:
|
|
44
105
|
files:
|
45
106
|
- .document
|
46
107
|
- .gitignore
|
108
|
+
- .gitmodules
|
109
|
+
- Gemfile
|
47
110
|
- LICENSE
|
48
111
|
- README.rdoc
|
49
112
|
- Rakefile
|
@@ -51,11 +114,13 @@ files:
|
|
51
114
|
- lib/meta_search.rb
|
52
115
|
- lib/meta_search/builder.rb
|
53
116
|
- lib/meta_search/exceptions.rb
|
54
|
-
- lib/meta_search/helpers
|
117
|
+
- lib/meta_search/helpers.rb
|
118
|
+
- lib/meta_search/helpers/form_builder.rb
|
119
|
+
- lib/meta_search/helpers/form_helper.rb
|
120
|
+
- lib/meta_search/helpers/url_helper.rb
|
121
|
+
- lib/meta_search/method.rb
|
55
122
|
- lib/meta_search/model_compatibility.rb
|
56
|
-
- lib/meta_search/railtie.rb
|
57
123
|
- lib/meta_search/searches/active_record.rb
|
58
|
-
- lib/meta_search/searches/base.rb
|
59
124
|
- lib/meta_search/utility.rb
|
60
125
|
- lib/meta_search/where.rb
|
61
126
|
- meta_search.gemspec
|
@@ -75,7 +140,7 @@ files:
|
|
75
140
|
- test/test_search.rb
|
76
141
|
- test/test_view_helpers.rb
|
77
142
|
has_rdoc: true
|
78
|
-
homepage: http://metautonomo.us
|
143
|
+
homepage: http://metautonomo.us/projects/metasearch/
|
79
144
|
licenses: []
|
80
145
|
|
81
146
|
post_install_message:
|
@@ -84,6 +149,7 @@ rdoc_options:
|
|
84
149
|
require_paths:
|
85
150
|
- lib
|
86
151
|
required_ruby_version: !ruby/object:Gem::Requirement
|
152
|
+
none: false
|
87
153
|
requirements:
|
88
154
|
- - ">="
|
89
155
|
- !ruby/object:Gem::Version
|
@@ -91,6 +157,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
91
157
|
- 0
|
92
158
|
version: "0"
|
93
159
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
160
|
+
none: false
|
94
161
|
requirements:
|
95
162
|
- - ">="
|
96
163
|
- !ruby/object:Gem::Version
|
@@ -100,10 +167,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
167
|
requirements: []
|
101
168
|
|
102
169
|
rubyforge_project:
|
103
|
-
rubygems_version: 1.3.
|
170
|
+
rubygems_version: 1.3.7
|
104
171
|
signing_key:
|
105
172
|
specification_version: 3
|
106
|
-
summary: ActiveRecord 3 object-based searching.
|
173
|
+
summary: ActiveRecord 3 object-based searching for your form_for enjoyment.
|
107
174
|
test_files:
|
108
175
|
- test/fixtures/company.rb
|
109
176
|
- test/fixtures/data_type.rb
|
@@ -1,168 +0,0 @@
|
|
1
|
-
require 'action_view'
|
2
|
-
require 'action_dispatch'
|
3
|
-
|
4
|
-
module ActionDispatch::Http::FilterParameters #:nodoc:
|
5
|
-
protected
|
6
|
-
# Temp fix for Rails 3 beta buggy parameter filtering on arrays
|
7
|
-
def process_parameter_filter(original_params) #:nodoc:
|
8
|
-
return original_params.dup unless filtering_parameters?
|
9
|
-
|
10
|
-
filtered_params = {}
|
11
|
-
regexps, blocks = compile_parameter_filter
|
12
|
-
|
13
|
-
original_params.each do |key, value|
|
14
|
-
if regexps.find { |r| key =~ r }
|
15
|
-
value = '[FILTERED]'
|
16
|
-
elsif value.is_a?(Hash)
|
17
|
-
value = process_parameter_filter(value)
|
18
|
-
elsif value.is_a?(Array)
|
19
|
-
value = value.map { |v| v.is_a?(Hash) ? process_parameter_filter(v) : v }
|
20
|
-
elsif blocks.present?
|
21
|
-
key = key.dup
|
22
|
-
value = value.dup if value.duplicable?
|
23
|
-
blocks.each { |b| b.call(key, value) }
|
24
|
-
end
|
25
|
-
|
26
|
-
filtered_params[key] = value
|
27
|
-
end
|
28
|
-
|
29
|
-
filtered_params
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
module MetaSearch::Helpers
|
34
|
-
module FormBuilder
|
35
|
-
def self.enable! #:nodoc:
|
36
|
-
::ActionView::Helpers::FormBuilder.class_eval do
|
37
|
-
include FormBuilder
|
38
|
-
self.field_helpers += ['multiparameter_field', 'check_boxes', 'collection_check_boxes']
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
# Like other form_for field methods (text_field, hidden_field, password_field) etc,
|
43
|
-
# but takes a list of hashes between the +method+ parameter and the trailing option hash,
|
44
|
-
# if any, to specify a number of fields to create in multiparameter fashion.
|
45
|
-
#
|
46
|
-
# Each hash *must* contain a :field_type option, which specifies a form_for method, and
|
47
|
-
# _may_ contain an optional :type_cast option, with one of the typical multiparameter
|
48
|
-
# type cast characters. Any remaining options will be merged with the defaults specified
|
49
|
-
# in the trailing option hash and passed along when creating that field.
|
50
|
-
#
|
51
|
-
# For example...
|
52
|
-
#
|
53
|
-
# <%= f.multiparameter_field :moderations_value_between,
|
54
|
-
# {:field_type => :text_field, :class => 'first'},
|
55
|
-
# {:field_type => :text_field, :type_cast => 'i'},
|
56
|
-
# :size => 5 %>
|
57
|
-
#
|
58
|
-
# ...will create the following HTML:
|
59
|
-
#
|
60
|
-
# <input class="first" id="search_moderations_value_between(1)"
|
61
|
-
# name="search[moderations_value_between(1)]" size="5" type="text" />
|
62
|
-
#
|
63
|
-
# <input id="search_moderations_value_between(2i)"
|
64
|
-
# name="search[moderations_value_between(2i)]" size="5" type="text" />
|
65
|
-
#
|
66
|
-
# As with any multiparameter input fields, these will be concatenated into an
|
67
|
-
# array and passed to the attribute named by the first parameter for assignment.
|
68
|
-
def multiparameter_field(method, *args)
|
69
|
-
defaults = has_multiparameter_defaults?(args) ? args.pop : {}
|
70
|
-
raise ArgumentError, "No multiparameter fields specified" if args.blank?
|
71
|
-
html = ''.html_safe
|
72
|
-
args.each_with_index do |field, index|
|
73
|
-
type = field.delete(:field_type) || raise(ArgumentError, "No :field_type specified.")
|
74
|
-
cast = field.delete(:type_cast) || ''
|
75
|
-
opts = defaults.merge(field)
|
76
|
-
html.safe_concat(
|
77
|
-
@template.send(
|
78
|
-
type.to_s,
|
79
|
-
@object_name,
|
80
|
-
(method.to_s + "(#{index + 1}#{cast})"),
|
81
|
-
objectify_options(opts))
|
82
|
-
)
|
83
|
-
end
|
84
|
-
html
|
85
|
-
end
|
86
|
-
|
87
|
-
# Behaves almost exactly like the select method, but instead of generating a select tag,
|
88
|
-
# generates checkboxes. Since these checkboxes are just a checkbox and label with no
|
89
|
-
# additional formatting by default, this method can also take a block parameter.
|
90
|
-
#
|
91
|
-
# *Parameters:*
|
92
|
-
#
|
93
|
-
# * +method+ - The method name on the form_for object
|
94
|
-
# * +choices+ - An array of arrays, the first value in each element is the text for the
|
95
|
-
# label, and the last is the value for the checkbox
|
96
|
-
# * +options+ - An options hash to be passed through to the checkboxes
|
97
|
-
#
|
98
|
-
# If a block is supplied, rather than just rendering the checkboxes and labels, the block
|
99
|
-
# will receive a hash with two keys, :check_box and :label
|
100
|
-
#
|
101
|
-
# *Examples:*
|
102
|
-
#
|
103
|
-
# Simple usage:
|
104
|
-
#
|
105
|
-
# <%= f.check_boxes :number_of_heads_in,
|
106
|
-
# [['One', 1], ['Two', 2], ['Three', 3]], :class => 'checkboxy' %>
|
107
|
-
#
|
108
|
-
# This will result in three checkboxes, with the labels "One", "Two", and "Three", and
|
109
|
-
# corresponding numeric values, which will be sent as an array to the :number_of_heads_in
|
110
|
-
# attribute of the form_for object.
|
111
|
-
#
|
112
|
-
# Additional formatting:
|
113
|
-
#
|
114
|
-
# <table>
|
115
|
-
# <th colspan="2">How many heads?</th>
|
116
|
-
# <% f.check_boxes :number_of_heads_in,
|
117
|
-
# [['One', 1], ['Two', 2], ['Three', 3]], :class => 'checkboxy' do |c| %>
|
118
|
-
# <tr>
|
119
|
-
# <td><%= c[:check_box] %></td>
|
120
|
-
# <td><%= c[:label] %></td>
|
121
|
-
# </tr>
|
122
|
-
# <% end %>
|
123
|
-
# </table>
|
124
|
-
#
|
125
|
-
# This example will output the checkboxes and labels in a tabular format. You get the idea.
|
126
|
-
def check_boxes(method, choices = [], options = {}, &block)
|
127
|
-
unless choices.first.respond_to?(:first) && choices.first.respond_to?(:last)
|
128
|
-
raise ArgumentError, 'invalid choice array specified'
|
129
|
-
end
|
130
|
-
collection_check_boxes(method, choices, :last, :first, options, &block)
|
131
|
-
end
|
132
|
-
|
133
|
-
# Just like +check_boxes+, but this time you can pass in a collection, value, and text method,
|
134
|
-
# as with collection_select.
|
135
|
-
#
|
136
|
-
# Example:
|
137
|
-
#
|
138
|
-
# <%= f.collection_check_boxes :head_sizes_in, HeadSize.all,
|
139
|
-
# :id, :name, :class => 'head-check' %>
|
140
|
-
def collection_check_boxes(method, collection, value_method, text_method, options = {}, &block)
|
141
|
-
html = ''.html_safe
|
142
|
-
collection.each do |choice|
|
143
|
-
text = choice.send(text_method)
|
144
|
-
value = choice.send(value_method)
|
145
|
-
c = {}
|
146
|
-
c[:check_box] = @template.check_box_tag(
|
147
|
-
"#{@object_name}[#{method}][]",
|
148
|
-
value,
|
149
|
-
[@object.send(method)].flatten.include?(value),
|
150
|
-
options.merge(:id => [@object_name, method.to_s, value.to_s.underscore].join('_'))
|
151
|
-
)
|
152
|
-
c[:label] = @template.label_tag([@object_name, method.to_s, value.to_s.underscore].join('_'),
|
153
|
-
text)
|
154
|
-
yield c if block_given?
|
155
|
-
html.safe_concat(c[:check_box] + c[:label])
|
156
|
-
end
|
157
|
-
html
|
158
|
-
end
|
159
|
-
|
160
|
-
private
|
161
|
-
|
162
|
-
# If the last element of the arguments to multiparameter_field has no :field_type
|
163
|
-
# key, we assume it's got some defaults to be used in the other hashes.
|
164
|
-
def has_multiparameter_defaults?(args)
|
165
|
-
args.size > 1 && args.last.is_a?(Hash) && !args.last.has_key?(:field_type)
|
166
|
-
end
|
167
|
-
end
|
168
|
-
end
|