/
filter_form_builder_spec.rb
272 lines (219 loc) · 8.88 KB
/
filter_form_builder_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
require 'spec_helper'
describe ActiveAdmin::Filters::ViewHelper do
# Setup an ActionView::Base object which can be used for
# generating the form for.
let(:helpers) do
view = action_view
def view.collection_path
"/posts"
end
def view.protect_against_forgery?
false
end
def view.a_helper_method
"A Helper Method"
end
view
end
def render_filter(search, filters)
render_arbre_component({:filter_args => [search, filters]}, helpers) do
text_node active_admin_filters_form_for(*assigns[:filter_args])
end
end
def filter(name, options = {})
render_filter Post.search, @filters.push(options.merge(:attribute => name))
end
before(:each) { @filters = [] }
describe "the form in general" do
let(:body) { filter :title }
it "should generate a form which submits via get" do
body.should have_tag("form", :attributes => { :method => 'get', :class => 'filter_form' })
end
it "should generate a filter button" do
body.should have_tag("input", :attributes => { :type => "submit",
:value => "Filter" })
end
it "should only generate the form once" do
body.to_s.scan(/q\[title_contains\]/).size.should == 1
end
it "should generate a clear filters link" do
body.should have_tag("a", "Clear Filters", :attributes => { :class => "clear_filters_btn" })
end
end
describe "string attribute" do
let(:body) { filter :title }
it "should generate a search field for a string attribute" do
body.should have_tag("input", :attributes => { :name => "q[title_contains]"})
end
it "should label a text field with search" do
body.should have_tag('label', 'Search Title')
end
it "should translate the label for text field" do
begin
I18n.backend.store_translations(:en, :activerecord => { :attributes => { :post => { :title => "Name" } } })
body.should have_tag('label', 'Search Name')
ensure
I18n.backend.reload!
end
end
context "using starts_with and as" do
let(:body) { filter :title_starts_with, :as => :string }
it "should generate a search field for a string attribute with query starts_with" do
body.should have_tag("input", :attributes => { :name => "q[title_starts_with]" })
end
end
context "using ends_with and as" do
let(:body) { filter :title_ends_with, :as => :string }
it "should generate a search field for a string attribute with query starts_with" do
body.should have_tag("input", :attributes => { :name => "q[title_ends_with]" })
end
end
end
describe "text attribute" do
let(:body) { filter :body }
it "should generate a search field for a text attribute" do
body.should have_tag("input", :attributes => { :name => "q[body_contains]"})
end
it "should label a text field with search" do
body.should have_tag('label', 'Search Body')
end
end
describe "datetime attribute" do
let(:body) { filter :created_at }
it "should generate a date greater than" do
body.should have_tag("input", :attributes => { :name => "q[created_at_gte]", :class => "datepicker"})
end
it "should generate a seperator" do
body.should have_tag("span", :attributes => { :class => "seperator"})
end
it "should generate a date less than" do
body.should have_tag("input", :attributes => { :name => "q[created_at_lte]", :class => "datepicker"})
end
end
describe "integer attribute" do
let(:body) { filter :id }
it "should generate a select option for equal to" do
body.should have_tag("option", "Equal To", :attributes => { :value => 'id_eq' })
end
it "should generate a select option for greater than" do
body.should have_tag("option", "Greater Than")
end
it "should generate a select option for less than" do
body.should have_tag("option", "Less Than")
end
it "should generate a text field for input" do
body.should have_tag("input", :attributes => {
:name => /q\[(id_eq|id_equals)\]/ })
end
it "should select the option which is currently being filtered"
end
describe "boolean attribute" do
context "boolean datatypes" do
let(:body) { filter :starred }
it "should create a check box for equals to" do
body.should have_tag("input", :attributes => {
:name => "q[starred_eq]",
:type => "checkbox" })
end
end
context "non-boolean data types" do
let(:body) { filter :title_is_present, :as => :boolean }
it "should create a check box for equals to" do
body.should have_tag("input", :attributes => {
:name => "q[title_is_present]",
:type => "checkbox" })
end
end
end
describe "belong to" do
before do
@john = User.create :first_name => "John", :last_name => "Doe", :username => "john_doe"
@jane = User.create :first_name => "Jane", :last_name => "Doe", :username => "jane_doe"
end
context "when given as the _id attribute name" do
let(:body) { filter :author_id }
it "should not render as an integer" do
body.should_not have_tag "input", :attributes => { :name => "q[author_id_eq]" }
end
it "should render as belongs to select" do
body.should have_tag "select", :attributes => { :name => "q[author_id_eq]" }
body.should have_tag "option", "John Doe", :attributes => { :value => @john.id }
body.should have_tag "option", "Jane Doe", :attributes => { :value => @jane.id }
end
end
context "when given as the name of the relationship" do
let(:body) { filter :author }
it "should generate a select" do
body.should have_tag "select", :attributes => { :name => "q[author_id_eq]" }
end
it "should set the default text to 'Any'" do
body.should have_tag "option", "Any", :attributes => { :value => "" }
end
it "should create an option for each related object" do
body.should have_tag "option", "John Doe", :attributes => { :value => @john.id }
body.should have_tag "option", "Jane Doe", :attributes => { :value => @jane.id }
end
context "with a proc" do
let :body do
filter :title, :as => :select, :collection => proc{ ['Title One', 'Title Two'] }
end
it "should use call the proc as the collection" do
body.should have_tag("option", "Title One")
body.should have_tag("option", "Title Two")
end
it "should render the collection in the context of the view" do
body = filter(:title, :as => :select, :collection => proc{[a_helper_method]})
body.should have_tag("option", "A Helper Method")
end
end
end
context "as check boxes" do
let(:body) { filter :author, :as => :check_boxes }
it "should create a check box for each related object" do
body.should have_tag("input", :attributes => {
:name => "q[author_id_in][]",
:type => "checkbox",
:value => @john.id })
body.should have_tag("input", :attributes => {
:name => "q[author_id_in][]",
:type => "checkbox",
:value => @jane.id })
end
end
context "when polymorphic relationship" do
let(:body) do
search = ActiveAdmin::Comment.search
render_filter(search, [{:attribute => :resource}])
end
it "should not generate any field" do
body.should have_tag("form", :attributes => { :method => 'get' })
end
end
end # belongs to
describe "conditional display" do
context "with :if block" do
let(:body) do
filter :body, :if => proc{true}
filter :author, :if => proc{false}
end
it "should be displayed if true" do
body.should have_tag("input", :attributes => { :name => "q[body_contains]"})
end
it "should NOT be displayed if false" do
body.should_not have_tag("input", :attributes => { :name => "q[author_id_eq]"})
end
end
context "with :unless block" do
let(:body) do
filter :created_at, :unless => proc{false}
filter :updated_at, :unless => proc{true}
end
it "should be displayed if false" do
body.should have_tag("input", :attributes => { :name => "q[created_at_gte]"})
end
it "should NOT be displayed if true" do
body.should_not have_tag("input", :attributes => { :name => "q[updated_at_gte]"})
end
end
end
end