search_object 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cc173628e2536b5964d951dbf6dd41b193cd52b8
4
+ data.tar.gz: 82b714d47dc48c540c64503589d50872f9156446
5
+ SHA512:
6
+ metadata.gz: e3e9f0fae74e4a5a6d620c6f6db1f8fbc744ac85bc918b98ed30c2c91920b06d7213c94af0f1fb823e54ea681c162018db4c0073513f60e8eec11ade31db443d
7
+ data.tar.gz: 4c5fed9dc2f8dae633b1cf8507ff66a79c620e356bdde2a9ebddcc455256ecf49823acc93cf2e9f4d24e8fc76fcea05d064e902707a6f88f67f2a5768a9d01d4
@@ -2,7 +2,7 @@ language: ruby
2
2
  rvm:
3
3
  - 1.9.3
4
4
  - 2.0
5
- - rbx-19mode
5
+ - 2.1
6
6
  script: bundle exec rspec spec
7
7
  notifications:
8
8
  email: false
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## Version 0.2
4
+
5
+ * Added `.results` shortcut for `new(*arg).results`
6
+
7
+ * Fix wrong limit and offset in default paging
8
+
3
9
  ## Version 0.1
4
10
 
5
- * initial release
11
+ * Initial release
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 Radoslav Stankov
1
+ Copyright (c) 2013-2014 Radoslav Stankov
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -1,16 +1,19 @@
1
+ [![Gem Version](https://badge.fury.io/rb/search_object.png)](http://badge.fury.io/rb/search_object)
1
2
  [![Code Climate](https://codeclimate.com/github/RStankov/SearchObject.png)](https://codeclimate.com/github/RStankov/SearchObject)
2
3
  [![Build Status](https://secure.travis-ci.org/RStankov/SearchObject.png)](http://travis-ci.org/RStankov/SearchObject)
3
4
  [![Code coverage](https://coveralls.io/repos/RStankov/SearchObject/badge.png?branch=master)](https://coveralls.io/r/RStankov/SearchObject)
4
5
 
5
6
  # SearchObject
6
7
 
7
- In many of my projects I've needed an object that performs several fairly complicated queries. Or, just some multi-field search forms. Most times I hand-coded them, but they would get complicated over time when other concerns were added like sorting, pagination and so on. So I decided to abstract this away and created ```SearchObject```, a DSL for creating such objects.
8
+ In many of my projects I needed an object that performs several fairly complicated queries. Or, just some multi-field search forms. Most times I hand-coded them, but they would get complicated over time when other concerns were added like sorting, pagination and so on. So I decided to abstract this away and created ```SearchObject```, a DSL for creating such objects.
8
9
 
9
10
  ## Installation
10
11
 
11
12
  Add this line to your application's Gemfile:
12
13
 
13
- gem 'search_object'
14
+ ```ruby
15
+ gem 'search_object'
16
+ ```
14
17
 
15
18
  And then execute:
16
19
 
@@ -52,13 +55,13 @@ search.results? # => is there any results found
52
55
  search.results # => found results
53
56
 
54
57
  # params for url generations
55
- search.params # => option values
56
- search.params opened: false # => overwrites the 'opened' option
58
+ search.params # => option values
59
+ search.params opened: false # => overwrites the 'opened' option
57
60
  ```
58
61
 
59
62
  ## Example
60
63
 
61
- You can find example of most imporatant features and plugins - [here](https://github.com/RStankov/SearchObject/tree/master/example).
64
+ You can find example of most important features and plugins - [here](https://github.com/RStankov/SearchObject/tree/master/example).
62
65
 
63
66
  ## Plugins
64
67
 
@@ -68,7 +71,7 @@ Plugins are just plain Ruby modules, which are included with ```SearchObject.mod
68
71
 
69
72
  ### Paginate plugin
70
73
 
71
- Really simple paginte plugin, which uses the plain ```.limit``` and ```.offset``` methods.
74
+ Really simple paginate plugin, which uses the plain ```.limit``` and ```.offset``` methods.
72
75
 
73
76
  ```ruby
74
77
  class ProductSearch
@@ -113,7 +116,8 @@ end
113
116
  ```
114
117
 
115
118
  ```erb
116
- # in some view:
119
+ <%# in some view: %>
120
+
117
121
  <%= form_for ProductSearch.new do |form| %>
118
122
  <% form.label :name %>
119
123
  <% form.text_field :name %>
@@ -168,7 +172,7 @@ end
168
172
 
169
173
  # first arguments is treated as scope (if no scope option is provided)
170
174
  search = ProductSearch.new(Product.visible, params[:f])
171
- search.results #=> products
175
+ search.results # => products
172
176
  ```
173
177
 
174
178
 
@@ -10,7 +10,7 @@ module SearchObject
10
10
  end
11
11
 
12
12
  def initialize(*args)
13
- @search = self.class.search args
13
+ @search = self.class.build_internal_search args
14
14
  end
15
15
 
16
16
  def results
@@ -40,7 +40,8 @@ module SearchObject
40
40
  end
41
41
 
42
42
  module ClassMethods
43
- def search(args)
43
+ # :api: private
44
+ def build_internal_search(args)
44
45
  scope = (@scope && @scope.call) || args.shift
45
46
  params = @defaults.merge(Helper.select_keys Helper.stringify_keys(args.shift || {}), @actions.keys)
46
47
 
@@ -59,6 +60,10 @@ module SearchObject
59
60
 
60
61
  define_method(name) { @search.param name }
61
62
  end
63
+
64
+ def results(*args)
65
+ new(*args).results
66
+ end
62
67
  end
63
68
  end
64
69
  end
@@ -25,7 +25,7 @@ module SearchObject
25
25
  end
26
26
 
27
27
  def apply_paging(scope)
28
- scope.limit(page * per_page).offset(per_page)
28
+ scope.limit(per_page).offset(page * per_page)
29
29
  end
30
30
 
31
31
  module ClassMethods
@@ -1,3 +1,3 @@
1
1
  module SearchObject
2
- VERSION = '0.1'
2
+ VERSION = '0.2'
3
3
  end
@@ -25,7 +25,6 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency 'activerecord', '>= 3.0.0'
26
26
  spec.add_development_dependency 'sqlite3'
27
27
  spec.add_development_dependency 'coveralls'
28
- spec.add_development_dependency 'active_model_lint-rspec'
29
28
  spec.add_development_dependency 'will_paginate'
30
29
  spec.add_development_dependency 'kaminari'
31
30
  end
@@ -2,8 +2,8 @@ require 'spec_helper'
2
2
 
3
3
  module SearchObject
4
4
  describe Base do
5
- def new_search(default_scope = [], filters = {}, &block)
6
- search_class = Class.new do
5
+ def search_class(default_scope = [], &block)
6
+ Class.new do
7
7
  include SearchObject.module
8
8
 
9
9
  scope { default_scope }
@@ -16,8 +16,10 @@ module SearchObject
16
16
  class_eval &block
17
17
  end
18
18
  end
19
+ end
19
20
 
20
- search_class.new filters
21
+ def new_search(default_scope = [], filters = {}, &block)
22
+ search_class(default_scope, &block).new filters
21
23
  end
22
24
 
23
25
  it "can had its #initialize method overwritten" do
@@ -129,6 +131,13 @@ module SearchObject
129
131
  end
130
132
  end
131
133
 
134
+ describe ".results" do
135
+ it "shortcut for creating new search and immediately returning results" do
136
+ klass = search_class [1 ,2 ,3]
137
+ expect(klass.results(value: 1)).to eq [1]
138
+ end
139
+ end
140
+
132
141
  describe "#results" do
133
142
  it "returns only the filtered search results" do
134
143
  search = new_search [1 ,2 ,3], value: 1
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- require 'active_model_lint-rspec'
3
+ require 'active_model/lint'
4
4
 
5
5
  module SearchObject
6
6
  module Plugin
@@ -8,15 +8,31 @@ module SearchObject
8
8
  include SearchObject.module(:model)
9
9
 
10
10
  # Fake errors
11
- # Since SearchObject is focused to plain search forms,
12
- # which don't have validation most of the time
11
+ # Since SearchObject is focused on plain search forms,
12
+ # validations are not needed most of the time
13
13
  def errors
14
14
  Hash.new([])
15
15
  end
16
16
  end
17
17
 
18
18
  describe ExtendedModel do
19
- it_behaves_like 'an ActiveModel'
19
+ include ActiveModel::Lint::Tests
20
+
21
+ before do
22
+ @model = subject
23
+ end
24
+
25
+ def assert(condition, message = nil)
26
+ expect(condition).to be_true, message
27
+ end
28
+
29
+ def assert_kind_of(expected_kind, object, message = nil)
30
+ expect(object).to be_kind_of(expected_kind), message
31
+ end
32
+
33
+ ActiveModel::Lint::Tests.public_instance_methods.map { |method| method.to_s }.grep(/^test/).each do |method|
34
+ example(method.gsub('_', ' ')) { send method }
35
+ end
20
36
  end
21
37
  end
22
38
  end
metadata CHANGED
@@ -1,174 +1,139 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: search_object
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
5
- prerelease:
4
+ version: '0.2'
6
5
  platform: ruby
7
6
  authors:
8
7
  - Radoslav Stankov
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-11-09 00:00:00.000000000 Z
11
+ date: 2014-09-04 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ~>
17
+ - - "~>"
20
18
  - !ruby/object:Gem::Version
21
19
  version: '1.3'
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ~>
24
+ - - "~>"
28
25
  - !ruby/object:Gem::Version
29
26
  version: '1.3'
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rspec
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ~>
45
+ - - "~>"
52
46
  - !ruby/object:Gem::Version
53
47
  version: '2.14'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ~>
52
+ - - "~>"
60
53
  - !ruby/object:Gem::Version
61
54
  version: '2.14'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: rspec-mocks
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - ">="
68
60
  - !ruby/object:Gem::Version
69
61
  version: 2.12.3
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - ">="
76
67
  - !ruby/object:Gem::Version
77
68
  version: 2.12.3
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: activerecord
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
- - - ! '>='
73
+ - - ">="
84
74
  - !ruby/object:Gem::Version
85
75
  version: 3.0.0
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
- - - ! '>='
80
+ - - ">="
92
81
  - !ruby/object:Gem::Version
93
82
  version: 3.0.0
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: sqlite3
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
- - - ! '>='
87
+ - - ">="
100
88
  - !ruby/object:Gem::Version
101
89
  version: '0'
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
- - - ! '>='
94
+ - - ">="
108
95
  - !ruby/object:Gem::Version
109
96
  version: '0'
110
97
  - !ruby/object:Gem::Dependency
111
98
  name: coveralls
112
99
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
100
  requirements:
115
- - - ! '>='
101
+ - - ">="
116
102
  - !ruby/object:Gem::Version
117
103
  version: '0'
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
107
  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
- - - ! '>='
108
+ - - ">="
140
109
  - !ruby/object:Gem::Version
141
110
  version: '0'
142
111
  - !ruby/object:Gem::Dependency
143
112
  name: will_paginate
144
113
  requirement: !ruby/object:Gem::Requirement
145
- none: false
146
114
  requirements:
147
- - - ! '>='
115
+ - - ">="
148
116
  - !ruby/object:Gem::Version
149
117
  version: '0'
150
118
  type: :development
151
119
  prerelease: false
152
120
  version_requirements: !ruby/object:Gem::Requirement
153
- none: false
154
121
  requirements:
155
- - - ! '>='
122
+ - - ">="
156
123
  - !ruby/object:Gem::Version
157
124
  version: '0'
158
125
  - !ruby/object:Gem::Dependency
159
126
  name: kaminari
160
127
  requirement: !ruby/object:Gem::Requirement
161
- none: false
162
128
  requirements:
163
- - - ! '>='
129
+ - - ">="
164
130
  - !ruby/object:Gem::Version
165
131
  version: '0'
166
132
  type: :development
167
133
  prerelease: false
168
134
  version_requirements: !ruby/object:Gem::Requirement
169
- none: false
170
135
  requirements:
171
- - - ! '>='
136
+ - - ">="
172
137
  - !ruby/object:Gem::Version
173
138
  version: '0'
174
139
  description: Search object DSL
@@ -178,9 +143,9 @@ executables: []
178
143
  extensions: []
179
144
  extra_rdoc_files: []
180
145
  files:
181
- - .gitignore
182
- - .rspec
183
- - .travis.yml
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".travis.yml"
184
149
  - CHANGELOG.md
185
150
  - Gemfile
186
151
  - LICENSE.txt
@@ -250,33 +215,26 @@ files:
250
215
  homepage: https://github.com/RStankov/SearchObject
251
216
  licenses:
252
217
  - MIT
218
+ metadata: {}
253
219
  post_install_message:
254
220
  rdoc_options: []
255
221
  require_paths:
256
222
  - lib
257
223
  required_ruby_version: !ruby/object:Gem::Requirement
258
- none: false
259
224
  requirements:
260
- - - ! '>='
225
+ - - ">="
261
226
  - !ruby/object:Gem::Version
262
227
  version: '0'
263
- segments:
264
- - 0
265
- hash: -2271564720281692758
266
228
  required_rubygems_version: !ruby/object:Gem::Requirement
267
- none: false
268
229
  requirements:
269
- - - ! '>='
230
+ - - ">="
270
231
  - !ruby/object:Gem::Version
271
232
  version: '0'
272
- segments:
273
- - 0
274
- hash: -2271564720281692758
275
233
  requirements: []
276
234
  rubyforge_project:
277
- rubygems_version: 1.8.25
235
+ rubygems_version: 2.2.2
278
236
  signing_key:
279
- specification_version: 3
237
+ specification_version: 4
280
238
  summary: Provides DSL for creating search objects
281
239
  test_files:
282
240
  - spec/search_object/base_spec.rb
@@ -290,3 +248,4 @@ test_files:
290
248
  - spec/spec_helper.rb
291
249
  - spec/spec_helper_active_record.rb
292
250
  - spec/support/kaminari_setup.rb
251
+ has_rdoc: