with_filters 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. data/.gitignore +8 -0
  2. data/.yardopts +1 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE +22 -0
  5. data/README.md +63 -0
  6. data/Rakefile +1 -0
  7. data/app/views/with_filters/_filter_form.html.erb +14 -0
  8. data/app/views/with_filters/filter/_check_box.html.erb +2 -0
  9. data/app/views/with_filters/filter/_check_boxes.html.erb +5 -0
  10. data/app/views/with_filters/filter/_radio.html.erb +5 -0
  11. data/app/views/with_filters/filter/_select.html.erb +2 -0
  12. data/app/views/with_filters/filter/_select_range.html.erb +4 -0
  13. data/app/views/with_filters/filter/_text.html.erb +2 -0
  14. data/app/views/with_filters/filter/_text_range.html.erb +4 -0
  15. data/changelog.md +2 -0
  16. data/lib/generators/with_filters/theme/theme_generator.rb +43 -0
  17. data/lib/with_filters/action_view_extension.rb +110 -0
  18. data/lib/with_filters/active_record_extension.rb +26 -0
  19. data/lib/with_filters/active_record_model_extension.rb +163 -0
  20. data/lib/with_filters/engine.rb +5 -0
  21. data/lib/with_filters/hash_extraction.rb +31 -0
  22. data/lib/with_filters/models/action.rb +14 -0
  23. data/lib/with_filters/models/filter/base.rb +36 -0
  24. data/lib/with_filters/models/filter/base_range.rb +42 -0
  25. data/lib/with_filters/models/filter/check_box.rb +20 -0
  26. data/lib/with_filters/models/filter/choice.rb +23 -0
  27. data/lib/with_filters/models/filter/collection.rb +28 -0
  28. data/lib/with_filters/models/filter/filter.rb +59 -0
  29. data/lib/with_filters/models/filter/radio.rb +7 -0
  30. data/lib/with_filters/models/filter/select.rb +22 -0
  31. data/lib/with_filters/models/filter/select_range.rb +15 -0
  32. data/lib/with_filters/models/filter/text.rb +30 -0
  33. data/lib/with_filters/models/filter/text_range.rb +15 -0
  34. data/lib/with_filters/models/filter_form.rb +93 -0
  35. data/lib/with_filters/value_prep/boolean_prep.rb +10 -0
  36. data/lib/with_filters/value_prep/date_prep.rb +10 -0
  37. data/lib/with_filters/value_prep/date_time_prep.rb +51 -0
  38. data/lib/with_filters/value_prep/default_prep.rb +88 -0
  39. data/lib/with_filters/value_prep/value_prep.rb +28 -0
  40. data/lib/with_filters/version.rb +3 -0
  41. data/lib/with_filters.rb +32 -0
  42. data/spec/active_record_model_extension_spec.rb +435 -0
  43. data/spec/dummy/README.rdoc +261 -0
  44. data/spec/dummy/Rakefile +7 -0
  45. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  46. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  47. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  48. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  49. data/spec/dummy/app/mailers/.gitkeep +0 -0
  50. data/spec/dummy/app/models/.gitkeep +0 -0
  51. data/spec/dummy/app/models/date_time_tester.rb +2 -0
  52. data/spec/dummy/app/models/field_format_tester.rb +2 -0
  53. data/spec/dummy/app/models/nobel_prize.rb +3 -0
  54. data/spec/dummy/app/models/nobel_prize_winner.rb +3 -0
  55. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  56. data/spec/dummy/config/application.rb +56 -0
  57. data/spec/dummy/config/boot.rb +10 -0
  58. data/spec/dummy/config/database.yml +25 -0
  59. data/spec/dummy/config/environment.rb +5 -0
  60. data/spec/dummy/config/environments/development.rb +37 -0
  61. data/spec/dummy/config/environments/production.rb +67 -0
  62. data/spec/dummy/config/environments/test.rb +37 -0
  63. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  64. data/spec/dummy/config/initializers/inflections.rb +15 -0
  65. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  66. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  67. data/spec/dummy/config/initializers/session_store.rb +8 -0
  68. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  69. data/spec/dummy/config/locales/en.yml +5 -0
  70. data/spec/dummy/config/routes.rb +58 -0
  71. data/spec/dummy/config.ru +4 -0
  72. data/spec/dummy/db/migrate/20111227224959_additional_columns.rb +73 -0
  73. data/spec/dummy/db/migrate/20120127203225_create_nobel_prize_winners.rb +30 -0
  74. data/spec/dummy/db/migrate/20120203212237_create_nobel_prizes.rb +34 -0
  75. data/spec/dummy/db/migrate/20120209051208_modify_updated_at.rb +13 -0
  76. data/spec/dummy/db/migrate/20120210163052_change_created_at.rb +17 -0
  77. data/spec/dummy/db/migrate/20120214172946_fix_einstein.rb +9 -0
  78. data/spec/dummy/db/migrate/20120227200013_create_date_time_testers.rb +25 -0
  79. data/spec/dummy/db/migrate/20120309202722_create_field_format_testers.rb +22 -0
  80. data/spec/dummy/db/migrate/20120310195447_update_field_format_testers.rb +15 -0
  81. data/spec/dummy/db/schema.rb +51 -0
  82. data/spec/dummy/db/test.sqlite3 +0 -0
  83. data/spec/dummy/lib/assets/.gitkeep +0 -0
  84. data/spec/dummy/log/.gitkeep +0 -0
  85. data/spec/dummy/public/404.html +26 -0
  86. data/spec/dummy/public/422.html +26 -0
  87. data/spec/dummy/public/500.html +25 -0
  88. data/spec/dummy/public/favicon.ico +0 -0
  89. data/spec/dummy/script/rails +6 -0
  90. data/spec/generators/with_filters_theme_spec.rb +33 -0
  91. data/spec/hash_extraction_spec.rb +51 -0
  92. data/spec/helpers/action_view_extension_spec.rb +345 -0
  93. data/spec/models/action.rb +20 -0
  94. data/spec/models/filter/base_range_spec.rb +32 -0
  95. data/spec/models/filter/base_spec.rb +76 -0
  96. data/spec/models/filter/check_box_spec.rb +36 -0
  97. data/spec/models/filter/choice_spec.rb +24 -0
  98. data/spec/models/filter/collection_spec.rb +72 -0
  99. data/spec/models/filter/filter_spec.rb +35 -0
  100. data/spec/models/filter/select_spec.rb +12 -0
  101. data/spec/models/filter/text_spec.rb +16 -0
  102. data/spec/models/filter_form_spec.rb +212 -0
  103. data/spec/spec_helper.rb +12 -0
  104. data/spec/value_prep/boolean_prep_spec.rb +13 -0
  105. data/spec/value_prep/date_prep_spec.rb +28 -0
  106. data/spec/value_prep/date_time_prep_spec.rb +106 -0
  107. data/spec/value_prep/default_prep_spec.rb +43 -0
  108. data/with_filters.gemspec +27 -0
  109. metadata +280 -0
@@ -0,0 +1,435 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'WithFilters::ActiveRecordModelExtention' do
4
+ describe '#with_filters(params = nil, options = {})' do
5
+ context 'filters using fields' do
6
+ context 'where value is a string' do
7
+ it 'filters based on the string value' do
8
+ npw = NobelPrizeWinner.with_filters({nobel_prize_winners: {first_name: 'Albert'}})
9
+ npw.length.should == 1
10
+ npw.first.first_name.should == 'Albert'
11
+ end
12
+
13
+ it 'skips an empty value' do
14
+ npw = NobelPrizeWinner.with_filters({nobel_prize_winners: {first_name: ''}})
15
+ npw.where_values.should == []
16
+ end
17
+ end
18
+
19
+ context 'where value is an array' do
20
+ it 'filters based on the array values' do
21
+ npw = NobelPrizeWinner.with_filters({nobel_prize_winners: {first_name: ['Albert', 'Marie']}}).order('first_name ASC')
22
+ npw.length.should == 2
23
+ npw.first.first_name.should == 'Albert'
24
+ npw.last.first_name.should == 'Marie'
25
+ end
26
+
27
+ it 'skips blank array values' do
28
+ npw = NobelPrizeWinner.with_filters({nobel_prize_winners: {first_name: ['Albert', 'Marie', '']}}).order('first_name ASC')
29
+ npw.length.should == 2
30
+ npw.first.first_name.should == 'Albert'
31
+ npw.last.first_name.should == 'Marie'
32
+ npw.where_values.should == ["nobel_prize_winners.\"first_name\" LIKE 'Albert' OR nobel_prize_winners.\"first_name\" LIKE 'Marie'"]
33
+ end
34
+
35
+ it 'skips empty arrays' do
36
+ npw = NobelPrizeWinner.with_filters({nobel_prize_winners: {first_name: []}})
37
+ npw.where_values.should == []
38
+ end
39
+ end
40
+
41
+ context 'where value is a :start and :stop range' do
42
+ it 'filters between :start and :stop' do
43
+ np = NobelPrize.with_filters({nobel_prizes: {year: {start: 1900, stop: 1930}}})
44
+ np.length.should == 4
45
+ end
46
+
47
+ it 'discards the range if :start or :stop are empty' do
48
+ np = NobelPrize.with_filters({nobel_prizes: {year: {start: 1900, stop: ''}}})
49
+ np.where_values.should == []
50
+
51
+ np = NobelPrize.with_filters({nobel_prizes: {year: {stop: 1930}}})
52
+ np.where_values.should == []
53
+ end
54
+ end
55
+
56
+ context 'where value is a boolean (and the column on the table is a :boolean)' do
57
+ it 'filters when "on" or "off" is passed' do
58
+ np = NobelPrize.with_filters({nobel_prizes: {shared: 'on'}})
59
+ np.length.should == 7
60
+
61
+ np = NobelPrize.with_filters({nobel_prizes: {shared: 'off'}})
62
+ np.length.should == 9
63
+ end
64
+ end
65
+
66
+ context 'where value is a date' do
67
+ context 'and the column on the table is a :date' do
68
+ it 'filters on the date value' do
69
+ npw = NobelPrizeWinner.with_filters({nobel_prize_winners: {birthdate: '19140325'}})
70
+ npw.length.should == 1
71
+ npw.first.birthdate.should == '19140325'.to_date
72
+ end
73
+ end
74
+
75
+ context 'and the column on the table is a :datetime or :timestamp' do
76
+ it 'filters on the date value' do
77
+ date = '2012-01-01'
78
+ ddt = DateTimeTester.with_filters({date_time_testers: {test: date}}).order('test ASC')
79
+ ddt.length.should == 8
80
+ ddt.first.test.to_s.should =~ /^#{date}/
81
+ ddt.last.test.to_s.should =~ /^#{date}/
82
+ end
83
+ end
84
+ end
85
+
86
+ context 'where value is a date range' do
87
+ context 'and the column on the table is a :date' do
88
+ it 'filters between :start and :stop' do
89
+ start_date = '1914-03-25'
90
+ stop_date = '1928-04-06'
91
+ npw = NobelPrizeWinner.
92
+ with_filters({nobel_prize_winners: {birthdate: {start: start_date, stop: stop_date}}}).
93
+ order('birthdate ASC')
94
+ npw.length.should == 4
95
+ npw.first.birthdate.should == start_date.to_date
96
+ npw.last.birthdate.should == stop_date.to_date
97
+ end
98
+ end
99
+
100
+ context 'and the column on the table is a :datetime or :timestamp' do
101
+ it 'filters between :start and :stop' do
102
+ start_date = '2012-01-01'
103
+ stop_date = '2012-01-01'
104
+ ddt = DateTimeTester.
105
+ with_filters({date_time_testers: {test: {start: start_date, stop: stop_date}}}).
106
+ order('test ASC')
107
+ ddt.length.should == 8
108
+ ddt.first.test.to_s.should =~ /^#{start_date}/
109
+ ddt.last.test.to_s.should =~ /^#{stop_date}/
110
+ end
111
+ end
112
+ end
113
+
114
+ context 'where value is a datetime with microseconds (and the column on the table is a :datetime or :timestamp)' do
115
+ it 'filters on the datetime value' do
116
+ time = '2012-01-01 00:00:01.654321'
117
+ ddt = DateTimeTester.with_filters({date_time_testers: {test: time}})
118
+ ddt.length.should == 1
119
+ ddt.first.test.to_s.should == Time.parse(time).to_s
120
+ end
121
+ end
122
+
123
+ context 'where value is a datetime (and the column on the table is a :datetime or :timestamp)' do
124
+ it 'filters on the datetime value' do
125
+ time = '2012-01-01 00:00:01'
126
+ ddt = DateTimeTester.with_filters({date_time_testers: {test: time}}).order('test ASC')
127
+ ddt.length.should == 4
128
+ ddt.map(&:test).each do |test|
129
+ test.to_s.should =~ /^#{time}/
130
+ end
131
+ end
132
+ end
133
+
134
+ context 'where value is a datetime range with microseconds (and the column on the table is a :datetime or :timestamp)' do
135
+ it 'filters between :start and :stop' do
136
+ start_time = '2012-01-01 00:00:01.123456'
137
+ stop_time = '2012-01-01 00:00:01.654300'
138
+ ddt = DateTimeTester.
139
+ with_filters({date_time_testers: {test: {start: start_time, stop: stop_time}}}).
140
+ order('test ASC')
141
+ ddt.length.should == 2
142
+ ddt.first.test.to_s.should == Time.parse(start_time).to_s
143
+ ddt.last.test.to_s.should == Time.parse(stop_time).to_s
144
+ end
145
+ end
146
+
147
+ context 'where value is a datetime range (and the column on the table is a :datetime or :timestamp)' do
148
+ it 'filters between :start and :stop' do
149
+ start_time = '2012-01-01 00:00:01'
150
+ stop_time = '2012-01-01 00:00:02'
151
+ ddt = DateTimeTester.
152
+ with_filters({date_time_testers: {test: {start: start_time, stop: stop_time}}}).
153
+ order('test ASC')
154
+ ddt.length.should == 5
155
+ ddt.first.test.to_s.should =~ /^#{start_time}/
156
+ ddt.last.test.to_s.should =~ /^#{stop_time}/
157
+ end
158
+ end
159
+
160
+ it 'accepts more than one field' do
161
+ np = NobelPrize.with_filters({nobel_prizes: {year: {start: 1900, stop: 1930}, category: 'Physics'}})
162
+ np.length.should == 3
163
+ end
164
+ end
165
+
166
+ context 'options' do
167
+ context ':param_namespace' do
168
+ it 'finds the params from the hash using the namespace' do
169
+ npw = NobelPrizeWinner.with_filters({foo: {first_name: 'Albert'}}, {param_namespace: :foo})
170
+ npw.with_filters_data[:param_namespace].should == :foo
171
+ end
172
+ end
173
+ context 'no :param_namespace' do
174
+ it 'defaults to the primary table name' do
175
+ npw = NobelPrizeWinner.with_filters({nobel_prize_winners: {first_name: 'Albert'}})
176
+ npw.with_filters_data[:param_namespace].should == :nobel_prize_winners
177
+ end
178
+ end
179
+
180
+ context ':fields' do
181
+ context 'value is a hash of options' do
182
+ context ':column' do
183
+ it 'uses the passed column name' do
184
+ npw = NobelPrizeWinner.with_filters({nobel_prize_winners: {fname: 'Albert'}}, {
185
+ fields: {
186
+ fname: {column: :first_name}
187
+ }
188
+ })
189
+ npw.length.should == 1
190
+ npw.first.first_name.should == 'Albert'
191
+
192
+ npw = NobelPrizeWinner.with_filters({nobel_prize_winners: {fname: 'Albert'}}, {
193
+ fields: {
194
+ fname: {column: 'nobel_prize_winners.first_name'}
195
+ }
196
+ })
197
+ npw.length.should == 1
198
+ npw.first.first_name.should == 'Albert'
199
+ end
200
+ end
201
+
202
+ context ':match' do
203
+ context ':exact' do
204
+ it 'handles matches for a single entry' do
205
+ npw = NobelPrizeWinner.with_filters(
206
+ {nobel_prize_winners: {first_name: 'Paul'}},
207
+ {fields: {
208
+ first_name: {match: :exact}
209
+ }}
210
+ ).order('first_name ASC')
211
+ npw.length.should == 2
212
+ npw.each do |n|
213
+ n.first_name.should == 'Paul'
214
+ end
215
+ end
216
+
217
+ it 'handles matches for a multiple entries' do
218
+ npw = NobelPrizeWinner.with_filters(
219
+ {nobel_prize_winners: {first_name: ['Paul', 'Erwin']}},
220
+ {fields: {
221
+ first_name: {match: :exact}
222
+ }}
223
+ ).order('first_name ASC')
224
+ npw.length.should == 3
225
+ npw.first.first_name.should == 'Erwin'
226
+ npw.second.first_name.should == 'Paul'
227
+ npw.last.first_name.should == 'Paul'
228
+ end
229
+ end
230
+
231
+ context ':contains' do
232
+ it 'handles matches for a single entry' do
233
+ npw = NobelPrizeWinner.with_filters(
234
+ {nobel_prize_winners: {first_name: 'el'}},
235
+ {fields: {
236
+ first_name: {match: :contains}
237
+ }}
238
+ ).order('first_name ASC')
239
+ npw.length.should == 3
240
+ npw.first.first_name.should == 'Nelson'
241
+ npw.second.first_name.should == 'Niels'
242
+ npw.last.first_name.should == 'Samuel'
243
+ end
244
+
245
+ it 'handles matches for a multiple entries' do
246
+ npw = NobelPrizeWinner.with_filters(
247
+ {nobel_prize_winners: {first_name: ['ert', 'mu'] }},
248
+ {fields: {
249
+ first_name: {match: :contains}
250
+ }}
251
+ ).order('first_name ASC')
252
+ npw.length.should == 3
253
+ npw.first.first_name.should == 'Albert'
254
+ npw.second.first_name.should == 'Bertrand'
255
+ npw.last.first_name.should == 'Samuel'
256
+ end
257
+ end
258
+
259
+ context ':begins_with' do
260
+ it 'handles matches for a single entry' do
261
+ npw = NobelPrizeWinner.with_filters(
262
+ {nobel_prize_winners: {first_name: 'ja'}},
263
+ {fields: {
264
+ first_name: {match: :begins_with}
265
+ }}
266
+ ).order('first_name ASC')
267
+ npw.length.should == 2
268
+ npw.first.first_name.should == 'Jacques'
269
+ npw.last.first_name.should == 'James'
270
+ end
271
+
272
+ it 'handles matches for a multiple entries' do
273
+ npw = NobelPrizeWinner.with_filters(
274
+ {nobel_prize_winners: {first_name: ['ja', 'ri']}},
275
+ {fields: {
276
+ first_name: {match: :begins_with}
277
+ }}
278
+ ).order('first_name ASC')
279
+ npw.length.should == 3
280
+ npw.first.first_name.should == 'Jacques'
281
+ npw.second.first_name.should == 'James'
282
+ npw.last.first_name.should == 'Richard'
283
+ end
284
+ end
285
+
286
+ context ':ends_with' do
287
+ it 'handles matches for a single entry' do
288
+ npw = NobelPrizeWinner.with_filters(
289
+ {nobel_prize_winners: {first_name: 'es'}},
290
+ {fields: {
291
+ first_name: {match: :ends_with}
292
+ }}
293
+ ).order('first_name ASC')
294
+ npw.length.should == 2
295
+ npw.first.first_name.should == 'Jacques'
296
+ npw.last.first_name.should == 'James'
297
+ end
298
+
299
+ it 'handles matches for a multiple entries' do
300
+ npw = NobelPrizeWinner.with_filters(
301
+ {nobel_prize_winners: {first_name: ['es', 'ie']}},
302
+ {fields: {
303
+ first_name: {match: :ends_with}
304
+ }}
305
+ ).order('first_name ASC')
306
+ npw.length.should == 3
307
+ npw.first.first_name.should == 'Jacques'
308
+ npw.second.first_name.should == 'James'
309
+ npw.last.first_name.should == 'Marie'
310
+ end
311
+ end
312
+ end
313
+ end
314
+
315
+ context 'value is a Proc' do
316
+ it 'returns the value from the proc' do
317
+ npw = NobelPrizeWinner.with_filters(
318
+ {nobel_prize_winners: {full_name: 'Albert Einstein'}},
319
+ {fields: {
320
+ full_name: ->(value, scope) {
321
+ first_word, second_word = value.strip.split(/\s+/)
322
+
323
+ if second_word
324
+ scope.where(['first_name LIKE ? OR last_name LIKE ?', first_word, first_word])
325
+ else
326
+ scope.where(['first_name LIKE ? AND last_name LIKE ?', first_word, second_word])
327
+ end
328
+ }
329
+ }}
330
+ )
331
+ npw.length.should == 1
332
+ npw.first.first_name.should == 'Albert'
333
+ npw.first.last_name.should == 'Einstein'
334
+ end
335
+ end
336
+ end
337
+ end
338
+
339
+ context 'provides with_filters_data attr' do
340
+ it 'has :param_namespace' do
341
+ npw = NobelPrizeWinner.with_filters({nobel_prize_winners: {first_name: 'Albert'}})
342
+ npw.with_filters_data[:param_namespace].should == :nobel_prize_winners
343
+ end
344
+
345
+ context 'has :column_types' do
346
+ let(:column_types) {
347
+ {
348
+ id: :integer,
349
+ nobel_prize_winner_id: :integer,
350
+ category: :string,
351
+ year: :integer,
352
+ shared: :boolean,
353
+ first_name: :string,
354
+ last_name: :string,
355
+ birthdate: :date,
356
+ created_at: :datetime,
357
+ updated_at: :datetime
358
+ }
359
+ }
360
+
361
+ it 'joins an association' do
362
+ npw = NobelPrizeWinner.
363
+ joins(:nobel_prizes).
364
+ with_filters({nobel_prize_winners: {first_name: 'Albert'}})
365
+
366
+ npw.with_filters_data[:column_types].should == column_types
367
+ end
368
+
369
+ it 'manually joins a table' do
370
+ npw = NobelPrizeWinner.joins('join nobel_prizes ON nobel_prizes.nobel_prize_winner_id = nobel_prize_winners.id').with_filters
371
+
372
+ npw.with_filters_data[:column_types].should == column_types
373
+ end
374
+
375
+ it 'includes an association' do
376
+ npw = NobelPrizeWinner.includes(:nobel_prizes).with_filters
377
+
378
+ npw.with_filters_data[:column_types].should == column_types
379
+ end
380
+
381
+ context 'selects the column type when the field is aliased' do
382
+ it 'is alias to a field name' do
383
+ npw = NobelPrizeWinner.with_filters({}, fields: {foo: {column: :birthdate}})
384
+ npw.with_filters_data[:column_types][:foo].should == :date
385
+ end
386
+
387
+ it 'is aliased to a field on the primary table' do
388
+ npw = NobelPrizeWinner.with_filters({}, fields: {foo: {column: 'nobel_prize_winners.birthdate'}})
389
+ npw.with_filters_data[:column_types][:foo].should == :date
390
+ end
391
+
392
+ it 'is aliased to a field on a joined table' do
393
+ npw = NobelPrizeWinner.joins(:nobel_prizes).with_filters({}, fields: {foo: {column: 'nobel_prizes.year'}})
394
+ npw.with_filters_data[:column_types][:foo].should == :integer
395
+ end
396
+ end
397
+ end
398
+
399
+ it 'stays when converted to an array' do
400
+ npw = NobelPrizeWinner.with_filters({nobel_prize_winners: {first_name: 'Albert'}}).to_a
401
+ npw.with_filters_data[:param_namespace].should == :nobel_prize_winners
402
+ npw.with_filters_data[:column_types].should == {
403
+ id: :integer,
404
+ first_name: :string,
405
+ last_name: :string,
406
+ birthdate: :date,
407
+ created_at: :datetime,
408
+ updated_at: :datetime
409
+ }
410
+ end
411
+ end
412
+
413
+ context 'limit the need for specifying table names to resolve ambiguity' do
414
+ it 'prepends the table name to the field if the field is in the primary table' do
415
+ npw = NobelPrizeWinner.joins(:nobel_prizes).with_filters({nobel_prize_winners: {birthdate: '19140325'}})
416
+ npw.where_values.first.should =~ /^#{npw.table_name}\./
417
+ end
418
+
419
+ it 'does not affect non-primary fields' do
420
+ npw = NobelPrizeWinner.joins(:nobel_prizes).with_filters({nobel_prize_winners: {year: '1903'}})
421
+ npw.where_values.first.should =~ /^#{npw.connection.quote_column_name('year')}/
422
+ end
423
+ end
424
+
425
+ it 'quotes column names' do
426
+ npw = NobelPrizeWinner.joins(:nobel_prizes).with_filters({nobel_prize_winners: {year: '1903'}})
427
+ npw.where_values.first.should =~ /^#{npw.connection.quote_column_name('year')}/
428
+ end
429
+
430
+ it 'does not break the chain' do
431
+ npw = NobelPrizeWinner.with_filters.limit(1)
432
+ npw.length.should == 1
433
+ end
434
+ end
435
+ end
@@ -0,0 +1,261 @@
1
+ == Welcome to Rails
2
+
3
+ Rails is a web-application framework that includes everything needed to create
4
+ database-backed web applications according to the Model-View-Control pattern.
5
+
6
+ This pattern splits the view (also called the presentation) into "dumb"
7
+ templates that are primarily responsible for inserting pre-built data in between
8
+ HTML tags. The model contains the "smart" domain objects (such as Account,
9
+ Product, Person, Post) that holds all the business logic and knows how to
10
+ persist themselves to a database. The controller handles the incoming requests
11
+ (such as Save New Account, Update Product, Show Post) by manipulating the model
12
+ and directing data to the view.
13
+
14
+ In Rails, the model is handled by what's called an object-relational mapping
15
+ layer entitled Active Record. This layer allows you to present the data from
16
+ database rows as objects and embellish these data objects with business logic
17
+ methods. You can read more about Active Record in
18
+ link:files/vendor/rails/activerecord/README.html.
19
+
20
+ The controller and view are handled by the Action Pack, which handles both
21
+ layers by its two parts: Action View and Action Controller. These two layers
22
+ are bundled in a single package due to their heavy interdependence. This is
23
+ unlike the relationship between the Active Record and Action Pack that is much
24
+ more separate. Each of these packages can be used independently outside of
25
+ Rails. You can read more about Action Pack in
26
+ link:files/vendor/rails/actionpack/README.html.
27
+
28
+
29
+ == Getting Started
30
+
31
+ 1. At the command prompt, create a new Rails application:
32
+ <tt>rails new myapp</tt> (where <tt>myapp</tt> is the application name)
33
+
34
+ 2. Change directory to <tt>myapp</tt> and start the web server:
35
+ <tt>cd myapp; rails server</tt> (run with --help for options)
36
+
37
+ 3. Go to http://localhost:3000/ and you'll see:
38
+ "Welcome aboard: You're riding Ruby on Rails!"
39
+
40
+ 4. Follow the guidelines to start developing your application. You can find
41
+ the following resources handy:
42
+
43
+ * The Getting Started Guide: http://guides.rubyonrails.org/getting_started.html
44
+ * Ruby on Rails Tutorial Book: http://www.railstutorial.org/
45
+
46
+
47
+ == Debugging Rails
48
+
49
+ Sometimes your application goes wrong. Fortunately there are a lot of tools that
50
+ will help you debug it and get it back on the rails.
51
+
52
+ First area to check is the application log files. Have "tail -f" commands
53
+ running on the server.log and development.log. Rails will automatically display
54
+ debugging and runtime information to these files. Debugging info will also be
55
+ shown in the browser on requests from 127.0.0.1.
56
+
57
+ You can also log your own messages directly into the log file from your code
58
+ using the Ruby logger class from inside your controllers. Example:
59
+
60
+ class WeblogController < ActionController::Base
61
+ def destroy
62
+ @weblog = Weblog.find(params[:id])
63
+ @weblog.destroy
64
+ logger.info("#{Time.now} Destroyed Weblog ID ##{@weblog.id}!")
65
+ end
66
+ end
67
+
68
+ The result will be a message in your log file along the lines of:
69
+
70
+ Mon Oct 08 14:22:29 +1000 2007 Destroyed Weblog ID #1!
71
+
72
+ More information on how to use the logger is at http://www.ruby-doc.org/core/
73
+
74
+ Also, Ruby documentation can be found at http://www.ruby-lang.org/. There are
75
+ several books available online as well:
76
+
77
+ * Programming Ruby: http://www.ruby-doc.org/docs/ProgrammingRuby/ (Pickaxe)
78
+ * Learn to Program: http://pine.fm/LearnToProgram/ (a beginners guide)
79
+
80
+ These two books will bring you up to speed on the Ruby language and also on
81
+ programming in general.
82
+
83
+
84
+ == Debugger
85
+
86
+ Debugger support is available through the debugger command when you start your
87
+ Mongrel or WEBrick server with --debugger. This means that you can break out of
88
+ execution at any point in the code, investigate and change the model, and then,
89
+ resume execution! You need to install ruby-debug to run the server in debugging
90
+ mode. With gems, use <tt>sudo gem install ruby-debug</tt>. Example:
91
+
92
+ class WeblogController < ActionController::Base
93
+ def index
94
+ @posts = Post.all
95
+ debugger
96
+ end
97
+ end
98
+
99
+ So the controller will accept the action, run the first line, then present you
100
+ with a IRB prompt in the server window. Here you can do things like:
101
+
102
+ >> @posts.inspect
103
+ => "[#<Post:0x14a6be8
104
+ @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>,
105
+ #<Post:0x14a6620
106
+ @attributes={"title"=>"Rails", "body"=>"Only ten..", "id"=>"2"}>]"
107
+ >> @posts.first.title = "hello from a debugger"
108
+ => "hello from a debugger"
109
+
110
+ ...and even better, you can examine how your runtime objects actually work:
111
+
112
+ >> f = @posts.first
113
+ => #<Post:0x13630c4 @attributes={"title"=>nil, "body"=>nil, "id"=>"1"}>
114
+ >> f.
115
+ Display all 152 possibilities? (y or n)
116
+
117
+ Finally, when you're ready to resume execution, you can enter "cont".
118
+
119
+
120
+ == Console
121
+
122
+ The console is a Ruby shell, which allows you to interact with your
123
+ application's domain model. Here you'll have all parts of the application
124
+ configured, just like it is when the application is running. You can inspect
125
+ domain models, change values, and save to the database. Starting the script
126
+ without arguments will launch it in the development environment.
127
+
128
+ To start the console, run <tt>rails console</tt> from the application
129
+ directory.
130
+
131
+ Options:
132
+
133
+ * Passing the <tt>-s, --sandbox</tt> argument will rollback any modifications
134
+ made to the database.
135
+ * Passing an environment name as an argument will load the corresponding
136
+ environment. Example: <tt>rails console production</tt>.
137
+
138
+ To reload your controllers and models after launching the console run
139
+ <tt>reload!</tt>
140
+
141
+ More information about irb can be found at:
142
+ link:http://www.rubycentral.org/pickaxe/irb.html
143
+
144
+
145
+ == dbconsole
146
+
147
+ You can go to the command line of your database directly through <tt>rails
148
+ dbconsole</tt>. You would be connected to the database with the credentials
149
+ defined in database.yml. Starting the script without arguments will connect you
150
+ to the development database. Passing an argument will connect you to a different
151
+ database, like <tt>rails dbconsole production</tt>. Currently works for MySQL,
152
+ PostgreSQL and SQLite 3.
153
+
154
+ == Description of Contents
155
+
156
+ The default directory structure of a generated Ruby on Rails application:
157
+
158
+ |-- app
159
+ | |-- assets
160
+ | |-- images
161
+ | |-- javascripts
162
+ | `-- stylesheets
163
+ | |-- controllers
164
+ | |-- helpers
165
+ | |-- mailers
166
+ | |-- models
167
+ | `-- views
168
+ | `-- layouts
169
+ |-- config
170
+ | |-- environments
171
+ | |-- initializers
172
+ | `-- locales
173
+ |-- db
174
+ |-- doc
175
+ |-- lib
176
+ | `-- tasks
177
+ |-- log
178
+ |-- public
179
+ |-- script
180
+ |-- test
181
+ | |-- fixtures
182
+ | |-- functional
183
+ | |-- integration
184
+ | |-- performance
185
+ | `-- unit
186
+ |-- tmp
187
+ | |-- cache
188
+ | |-- pids
189
+ | |-- sessions
190
+ | `-- sockets
191
+ `-- vendor
192
+ |-- assets
193
+ `-- stylesheets
194
+ `-- plugins
195
+
196
+ app
197
+ Holds all the code that's specific to this particular application.
198
+
199
+ app/assets
200
+ Contains subdirectories for images, stylesheets, and JavaScript files.
201
+
202
+ app/controllers
203
+ Holds controllers that should be named like weblogs_controller.rb for
204
+ automated URL mapping. All controllers should descend from
205
+ ApplicationController which itself descends from ActionController::Base.
206
+
207
+ app/models
208
+ Holds models that should be named like post.rb. Models descend from
209
+ ActiveRecord::Base by default.
210
+
211
+ app/views
212
+ Holds the template files for the view that should be named like
213
+ weblogs/index.html.erb for the WeblogsController#index action. All views use
214
+ eRuby syntax by default.
215
+
216
+ app/views/layouts
217
+ Holds the template files for layouts to be used with views. This models the
218
+ common header/footer method of wrapping views. In your views, define a layout
219
+ using the <tt>layout :default</tt> and create a file named default.html.erb.
220
+ Inside default.html.erb, call <% yield %> to render the view using this
221
+ layout.
222
+
223
+ app/helpers
224
+ Holds view helpers that should be named like weblogs_helper.rb. These are
225
+ generated for you automatically when using generators for controllers.
226
+ Helpers can be used to wrap functionality for your views into methods.
227
+
228
+ config
229
+ Configuration files for the Rails environment, the routing map, the database,
230
+ and other dependencies.
231
+
232
+ db
233
+ Contains the database schema in schema.rb. db/migrate contains all the
234
+ sequence of Migrations for your schema.
235
+
236
+ doc
237
+ This directory is where your application documentation will be stored when
238
+ generated using <tt>rake doc:app</tt>
239
+
240
+ lib
241
+ Application specific libraries. Basically, any kind of custom code that
242
+ doesn't belong under controllers, models, or helpers. This directory is in
243
+ the load path.
244
+
245
+ public
246
+ The directory available for the web server. Also contains the dispatchers and the
247
+ default HTML files. This should be set as the DOCUMENT_ROOT of your web
248
+ server.
249
+
250
+ script
251
+ Helper scripts for automation and generation.
252
+
253
+ test
254
+ Unit and functional tests along with fixtures. When using the rails generate
255
+ command, template test files will be generated for you and placed in this
256
+ directory.
257
+
258
+ vendor
259
+ External libraries that the application depends on. Also includes the plugins
260
+ subdirectory. If the app has frozen rails, those gems also go here, under
261
+ vendor/rails/. This directory is in the load path.