search_object 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. data/.gitignore +17 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +8 -0
  4. data/CHANGELOG.md +5 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +283 -0
  8. data/Rakefile +6 -0
  9. data/example/.gitignore +16 -0
  10. data/example/.rspec +1 -0
  11. data/example/Gemfile +11 -0
  12. data/example/README.md +34 -0
  13. data/example/Rakefile +6 -0
  14. data/example/app/assets/javascripts/application.js +5 -0
  15. data/example/app/assets/stylesheets/application.css.scss +40 -0
  16. data/example/app/assets/stylesheets/reset.css +43 -0
  17. data/example/app/controllers/application_controller.rb +3 -0
  18. data/example/app/controllers/posts_controller.rb +5 -0
  19. data/example/app/models/.keep +0 -0
  20. data/example/app/models/post.rb +13 -0
  21. data/example/app/models/post_search.rb +44 -0
  22. data/example/app/models/user.rb +5 -0
  23. data/example/app/views/layouts/application.html.slim +12 -0
  24. data/example/app/views/posts/index.html.slim +48 -0
  25. data/example/bin/bundle +3 -0
  26. data/example/bin/rails +4 -0
  27. data/example/bin/rake +4 -0
  28. data/example/config.ru +4 -0
  29. data/example/config/application.rb +27 -0
  30. data/example/config/boot.rb +4 -0
  31. data/example/config/database.yml +12 -0
  32. data/example/config/environment.rb +5 -0
  33. data/example/config/environments/development.rb +29 -0
  34. data/example/config/environments/test.rb +37 -0
  35. data/example/config/initializers/filter_parameter_logging.rb +4 -0
  36. data/example/config/initializers/secret_token.rb +12 -0
  37. data/example/config/initializers/session_store.rb +3 -0
  38. data/example/config/initializers/wrap_parameters.rb +14 -0
  39. data/example/config/routes.rb +3 -0
  40. data/example/db/migrate/20131102130117_create_users.rb +10 -0
  41. data/example/db/migrate/20131102130413_create_posts.rb +18 -0
  42. data/example/db/schema.rb +40 -0
  43. data/example/db/seeds.rb +37 -0
  44. data/example/log/.keep +0 -0
  45. data/example/screenshot.png +0 -0
  46. data/example/spec/models/post_search_spec.rb +81 -0
  47. data/example/spec/spec_helper.rb +19 -0
  48. data/lib/search_object.rb +20 -0
  49. data/lib/search_object/base.rb +64 -0
  50. data/lib/search_object/helper.rb +36 -0
  51. data/lib/search_object/plugin/kaminari.rb +18 -0
  52. data/lib/search_object/plugin/model.rb +16 -0
  53. data/lib/search_object/plugin/paging.rb +42 -0
  54. data/lib/search_object/plugin/sorting.rb +54 -0
  55. data/lib/search_object/plugin/will_paginate.rb +17 -0
  56. data/lib/search_object/search.rb +26 -0
  57. data/lib/search_object/version.rb +3 -0
  58. data/search_object.gemspec +31 -0
  59. data/spec/search_object/base_spec.rb +237 -0
  60. data/spec/search_object/helper_spec.rb +30 -0
  61. data/spec/search_object/plugin/kaminari_spec.rb +50 -0
  62. data/spec/search_object/plugin/model_spec.rb +22 -0
  63. data/spec/search_object/plugin/paging_spec.rb +43 -0
  64. data/spec/search_object/plugin/sorting_spec.rb +139 -0
  65. data/spec/search_object/plugin/will_paginate_spec.rb +51 -0
  66. data/spec/search_object/search_spec.rb +72 -0
  67. data/spec/spec_helper.rb +13 -0
  68. data/spec/spec_helper_active_record.rb +19 -0
  69. data/spec/support/kaminari_setup.rb +7 -0
  70. metadata +292 -0
@@ -0,0 +1,51 @@
1
+ require 'spec_helper_active_record'
2
+
3
+ require 'will_paginate'
4
+ require 'will_paginate/active_record'
5
+
6
+ module SearchObject
7
+ module Plugin
8
+ describe Paging do
9
+ def search_class
10
+ Class.new do
11
+ include SearchObject.module(:will_paginate)
12
+
13
+ scope { Product }
14
+
15
+ per_page 2
16
+ end
17
+ end
18
+
19
+ after do
20
+ Product.delete_all
21
+ end
22
+
23
+ it "paginates" do
24
+ 10.times { |i| Product.create name: "product_#{i}" }
25
+ search = search_class.new({}, 2)
26
+ expect(search.results.map(&:name)).to eq %w(product_2 product_3)
27
+ end
28
+
29
+ it "uses will paginate" do
30
+ search = search_class.new
31
+ expect(search.results.respond_to? :total_entries).to be_true
32
+ end
33
+
34
+ it "treats nil page as 0" do
35
+ search = search_class.new({}, nil)
36
+ expect(search.page).to eq 0
37
+ end
38
+
39
+ it "treats negative page numbers as positive" do
40
+ search = search_class.new({}, -1)
41
+ expect(search.page).to eq 1
42
+ end
43
+
44
+ it "gives the real count" do
45
+ 10.times { |i| Product.create name: "product_#{i}" }
46
+ search = search_class.new({}, 1)
47
+ expect(search.count).to eq 10
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ module SearchObject
4
+ describe Search do
5
+
6
+ describe ".params" do
7
+ it "returns the passed params" do
8
+ search = Search.new('scope', 'params', {})
9
+ expect(search.params).to eq 'params'
10
+ end
11
+ end
12
+
13
+ describe ".param" do
14
+ it "returns the param value" do
15
+ search = Search.new('scope', {name: 'value'}, {})
16
+ expect(search.param(:name)).to eq 'value'
17
+ end
18
+ end
19
+
20
+ describe ".query" do
21
+ it "returns filtered result" do
22
+ actions = {
23
+ :min => ->(scope, min) { scope.select { |v| v > min } },
24
+ }
25
+
26
+ search = Search.new [1,2,3], {min: 2}, actions
27
+ expect(search.query(Object.new)).to eq [3]
28
+ end
29
+
30
+ it "applies actions to params" do
31
+ actions = {
32
+ :min => ->(scope, min) { scope.select { |v| v > min } },
33
+ :max => ->(scope, max) { scope.select { |v| v < max } },
34
+ }
35
+
36
+ search = Search.new [1,2,3,4,5], {min: 2, max: 5}, actions
37
+ expect(search.query(Object.new)).to eq [3,4]
38
+ end
39
+
40
+ it "handles nil returned from action" do
41
+ actions = {
42
+ :odd => ->(scope, odd) { scope.select(&:odd?) if odd }
43
+ }
44
+
45
+ search = Search.new [1,2,3,4,5], {odd: false}, actions
46
+ expect(search.query(Object.new)).to eq [1,2,3,4,5]
47
+ end
48
+
49
+ it "executes action in the passed context" do
50
+ actions = {
51
+ :search => ->(scope, _) { scope.select { |v| v == target_value } }
52
+ }
53
+
54
+ context = double target_value: 2
55
+
56
+ search = Search.new [1,2,3,4,5], {search: true}, actions
57
+ expect(search.query(context)).to eq [2]
58
+ end
59
+ end
60
+
61
+ describe ".count" do
62
+ it "counts the results of the query" do
63
+ actions = {
64
+ :value => ->(scope, value) { scope.select { |v| v == value} }
65
+ }
66
+
67
+ search = Search.new [1,2,3], {value: 2}, actions
68
+ expect(search.count(Object.new)).to eq 1
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,13 @@
1
+ require 'bundler/setup'
2
+
3
+ if ENV['TRAVIS']
4
+ require 'coveralls'
5
+ Coveralls.wear!
6
+ end
7
+
8
+ require 'search_object'
9
+
10
+ RSpec.configure do |config|
11
+ config.expect_with(:rspec) { |c| c.syntax = :expect }
12
+ end
13
+
@@ -0,0 +1,19 @@
1
+ require_relative 'spec_helper'
2
+ require 'active_record'
3
+
4
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
5
+
6
+ ActiveRecord::Schema.define do
7
+ self.verbose = false
8
+
9
+ create_table :products, force: true do |t|
10
+ t.string :name
11
+ t.string :category_name
12
+ t.integer :price
13
+
14
+ t.timestamps
15
+ end
16
+ end
17
+
18
+ class Product < ActiveRecord::Base
19
+ end
@@ -0,0 +1,7 @@
1
+ module Grape
2
+ end
3
+ require 'kaminari'
4
+ Kaminari::Hooks.init
5
+ require 'kaminari/models/active_record_extension'
6
+ ::ActiveRecord::Base.send :include, Kaminari::ActiveRecordExtension
7
+
metadata ADDED
@@ -0,0 +1,292 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: search_object
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Radoslav Stankov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2.14'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.14'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec-mocks
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 2.12.3
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: 2.12.3
78
+ - !ruby/object:Gem::Dependency
79
+ name: activerecord
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: 3.0.0
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: 3.0.0
94
+ - !ruby/object:Gem::Dependency
95
+ name: sqlite3
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: coveralls
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: active_model_lint-rspec
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: will_paginate
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ type: :development
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ - !ruby/object:Gem::Dependency
159
+ name: kaminari
160
+ requirement: !ruby/object:Gem::Requirement
161
+ none: false
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ type: :development
167
+ prerelease: false
168
+ version_requirements: !ruby/object:Gem::Requirement
169
+ none: false
170
+ requirements:
171
+ - - ! '>='
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ description: Search object DSL
175
+ email:
176
+ - rstankov@gmail.com
177
+ executables: []
178
+ extensions: []
179
+ extra_rdoc_files: []
180
+ files:
181
+ - .gitignore
182
+ - .rspec
183
+ - .travis.yml
184
+ - CHANGELOG.md
185
+ - Gemfile
186
+ - LICENSE.txt
187
+ - README.md
188
+ - Rakefile
189
+ - example/.gitignore
190
+ - example/.rspec
191
+ - example/Gemfile
192
+ - example/README.md
193
+ - example/Rakefile
194
+ - example/app/assets/javascripts/application.js
195
+ - example/app/assets/stylesheets/application.css.scss
196
+ - example/app/assets/stylesheets/reset.css
197
+ - example/app/controllers/application_controller.rb
198
+ - example/app/controllers/posts_controller.rb
199
+ - example/app/models/.keep
200
+ - example/app/models/post.rb
201
+ - example/app/models/post_search.rb
202
+ - example/app/models/user.rb
203
+ - example/app/views/layouts/application.html.slim
204
+ - example/app/views/posts/index.html.slim
205
+ - example/bin/bundle
206
+ - example/bin/rails
207
+ - example/bin/rake
208
+ - example/config.ru
209
+ - example/config/application.rb
210
+ - example/config/boot.rb
211
+ - example/config/database.yml
212
+ - example/config/environment.rb
213
+ - example/config/environments/development.rb
214
+ - example/config/environments/test.rb
215
+ - example/config/initializers/filter_parameter_logging.rb
216
+ - example/config/initializers/secret_token.rb
217
+ - example/config/initializers/session_store.rb
218
+ - example/config/initializers/wrap_parameters.rb
219
+ - example/config/routes.rb
220
+ - example/db/migrate/20131102130117_create_users.rb
221
+ - example/db/migrate/20131102130413_create_posts.rb
222
+ - example/db/schema.rb
223
+ - example/db/seeds.rb
224
+ - example/log/.keep
225
+ - example/screenshot.png
226
+ - example/spec/models/post_search_spec.rb
227
+ - example/spec/spec_helper.rb
228
+ - lib/search_object.rb
229
+ - lib/search_object/base.rb
230
+ - lib/search_object/helper.rb
231
+ - lib/search_object/plugin/kaminari.rb
232
+ - lib/search_object/plugin/model.rb
233
+ - lib/search_object/plugin/paging.rb
234
+ - lib/search_object/plugin/sorting.rb
235
+ - lib/search_object/plugin/will_paginate.rb
236
+ - lib/search_object/search.rb
237
+ - lib/search_object/version.rb
238
+ - search_object.gemspec
239
+ - spec/search_object/base_spec.rb
240
+ - spec/search_object/helper_spec.rb
241
+ - spec/search_object/plugin/kaminari_spec.rb
242
+ - spec/search_object/plugin/model_spec.rb
243
+ - spec/search_object/plugin/paging_spec.rb
244
+ - spec/search_object/plugin/sorting_spec.rb
245
+ - spec/search_object/plugin/will_paginate_spec.rb
246
+ - spec/search_object/search_spec.rb
247
+ - spec/spec_helper.rb
248
+ - spec/spec_helper_active_record.rb
249
+ - spec/support/kaminari_setup.rb
250
+ homepage: https://github.com/RStankov/SearchObject
251
+ licenses:
252
+ - MIT
253
+ post_install_message:
254
+ rdoc_options: []
255
+ require_paths:
256
+ - lib
257
+ required_ruby_version: !ruby/object:Gem::Requirement
258
+ none: false
259
+ requirements:
260
+ - - ! '>='
261
+ - !ruby/object:Gem::Version
262
+ version: '0'
263
+ segments:
264
+ - 0
265
+ hash: -2271564720281692758
266
+ required_rubygems_version: !ruby/object:Gem::Requirement
267
+ none: false
268
+ requirements:
269
+ - - ! '>='
270
+ - !ruby/object:Gem::Version
271
+ version: '0'
272
+ segments:
273
+ - 0
274
+ hash: -2271564720281692758
275
+ requirements: []
276
+ rubyforge_project:
277
+ rubygems_version: 1.8.25
278
+ signing_key:
279
+ specification_version: 3
280
+ summary: Provides DSL for creating search objects
281
+ test_files:
282
+ - spec/search_object/base_spec.rb
283
+ - spec/search_object/helper_spec.rb
284
+ - spec/search_object/plugin/kaminari_spec.rb
285
+ - spec/search_object/plugin/model_spec.rb
286
+ - spec/search_object/plugin/paging_spec.rb
287
+ - spec/search_object/plugin/sorting_spec.rb
288
+ - spec/search_object/plugin/will_paginate_spec.rb
289
+ - spec/search_object/search_spec.rb
290
+ - spec/spec_helper.rb
291
+ - spec/spec_helper_active_record.rb
292
+ - spec/support/kaminari_setup.rb