scoped_from 0.8.0 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4289bd8c3bbfa67e32434e234a812d6c7355722d
4
- data.tar.gz: f8f519eba2ed19382bd26dac18ed29235b2f2f36
3
+ metadata.gz: 420ecbe54d29a250ed5827afd36cb68f00658acd
4
+ data.tar.gz: 70718fa6858992001f51ac154f512ff5e87a0bc6
5
5
  SHA512:
6
- metadata.gz: d8b4baaf4d2624f44422b79bf2fe124e7f8f21dcfd9d097638f97e7b4832135eef8bcdf70d803317acb3a5f59dffc7de73f05747718d161473e3e7853f524afd
7
- data.tar.gz: 5921566963a8c2e17538acd47b5a91f1fe4007405f0297f2310d67341c2a361fa0d4a7f550f35434fe9f1d2b4e7148ba60b135dcb9c9fb17aa78f114b2872233
6
+ metadata.gz: 478022cae387bc459a21330454c4cb9dad0a0355686e4c3ec0c07be1b8e3f4ab9a2a457952771cb326536a698d4dc443db4245138c8f43dd51feabeeacfdfe9a
7
+ data.tar.gz: f603d42533c9685f23996e114e7995cd1407d8eb1cf5b4a623d5ef633d398eb8e3087ea3eabd31a5e8a03c3615d4dd2216b2f555a887e8044397e777c02459a1
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gemspec
3
+ gemspec
data/Gemfile.lock CHANGED
@@ -1,50 +1,50 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- scoped_from (0.8.0)
4
+ scoped_from (0.8.1)
5
5
  activerecord (~> 4.0.0)
6
6
  activesupport (~> 4.0.0)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activemodel (4.0.0)
12
- activesupport (= 4.0.0)
11
+ activemodel (4.0.2)
12
+ activesupport (= 4.0.2)
13
13
  builder (~> 3.1.0)
14
- activerecord (4.0.0)
15
- activemodel (= 4.0.0)
14
+ activerecord (4.0.2)
15
+ activemodel (= 4.0.2)
16
16
  activerecord-deprecated_finders (~> 1.0.2)
17
- activesupport (= 4.0.0)
17
+ activesupport (= 4.0.2)
18
18
  arel (~> 4.0.0)
19
19
  activerecord-deprecated_finders (1.0.3)
20
- activesupport (4.0.0)
20
+ activesupport (4.0.2)
21
21
  i18n (~> 0.6, >= 0.6.4)
22
22
  minitest (~> 4.2)
23
23
  multi_json (~> 1.3)
24
24
  thread_safe (~> 0.1)
25
25
  tzinfo (~> 0.3.37)
26
- arel (4.0.0)
27
- atomic (1.1.10)
26
+ arel (4.0.1)
27
+ atomic (1.1.14)
28
28
  builder (3.1.4)
29
- diff-lcs (1.2.4)
30
- i18n (0.6.4)
29
+ diff-lcs (1.2.5)
30
+ i18n (0.6.9)
31
31
  minitest (4.7.5)
32
- multi_json (1.7.7)
32
+ multi_json (1.8.2)
33
33
  rake (10.1.0)
34
34
  rspec (2.14.1)
35
35
  rspec-core (~> 2.14.0)
36
36
  rspec-expectations (~> 2.14.0)
37
37
  rspec-mocks (~> 2.14.0)
38
- rspec-core (2.14.3)
39
- rspec-expectations (2.14.0)
38
+ rspec-core (2.14.7)
39
+ rspec-expectations (2.14.4)
40
40
  diff-lcs (>= 1.1.3, < 2.0)
41
- rspec-mocks (2.14.1)
42
- sqlite3 (1.3.7)
41
+ rspec-mocks (2.14.4)
42
+ sqlite3 (1.3.8)
43
43
  sqlite3-ruby (1.3.3)
44
44
  sqlite3 (>= 1.3.3)
45
- thread_safe (0.1.0)
45
+ thread_safe (0.1.3)
46
46
  atomic
47
- tzinfo (0.3.37)
47
+ tzinfo (0.3.38)
48
48
 
49
49
  PLATFORMS
50
50
  ruby
data/README.mdown CHANGED
@@ -1,7 +1,7 @@
1
1
  # ScopedFrom
2
2
 
3
3
  Provides a simple mapping between scopes and controller parameters for
4
- [Ruby On Rails 3](http://rubyonrails.org/).
4
+ [Ruby On Rails 4](http://rubyonrails.org/).
5
5
 
6
6
  ## Installation
7
7
 
@@ -16,35 +16,35 @@ Then, just run `bundle install`.
16
16
  First, a model with some scopes:
17
17
 
18
18
  class Post < ActiveRecord::Base
19
-
19
+
20
20
  scope :commented, where('comments_count > 0')
21
21
 
22
22
  scope :created_between, lambda { |after, before|
23
23
  where('created_at >= ? AND created_at <= ?', after, before)
24
24
  }
25
-
25
+
26
26
  scope :search, lambda { |pattern|
27
27
  where('body LIKE ?', "%#{pattern}%")
28
28
  }
29
-
29
+
30
30
  scope :with_category, lambda { |category_id|
31
31
  where(:category_id, category_id)
32
32
  }
33
-
33
+
34
34
  end
35
-
35
+
36
36
  After, a controller:
37
-
37
+
38
38
  class PostsController < ActionController::Base
39
-
39
+
40
40
  def index
41
41
  @posts = Post.scoped_from(params)
42
42
  end
43
-
43
+
44
44
  end
45
-
45
+
46
46
  Then, it just filter your model from params:
47
-
47
+
48
48
  /posts?commented=1
49
49
  /posts?search=rails
50
50
  /posts?search=rails&commented=1&with_category=42
@@ -77,7 +77,7 @@ If you need to map an SQL order, just pass `order` parameter:
77
77
  @posts = Post.scoped_from(order: 'created_at')
78
78
 
79
79
  Order direction can be specified using a dot, space or `:` as delimiter:
80
-
80
+
81
81
  @posts = Post.scoped_from(order: 'created_at.desc')
82
82
 
83
83
  Note that order is SQL safe with `scoped_from` method (columns names are
@@ -89,9 +89,9 @@ If your provide an array as parameter value, scope is invoked with each item
89
89
  of the array:
90
90
 
91
91
  @posts = Post.scoped_from(search: ['bar', 'foo'])
92
-
92
+
93
93
  is equivalent to
94
-
94
+
95
95
  @posts = Post.search('bar').search('foo')
96
96
 
97
97
  You may also not want to filter on columns, just specify `:exclude_columns`
@@ -118,15 +118,15 @@ subclass of `ScopedFrom::Query` named `#{RecordClassName}Query`. Here is an
118
118
  example:
119
119
 
120
120
  class PostQuery < ScopedFrom::Query
121
-
121
+
122
122
  def category
123
123
  Category.find_by_id(params[:with_category]) if params[:with_category]
124
124
  end
125
-
125
+
126
126
  end
127
127
 
128
128
  This class has to be in load path.
129
-
129
+
130
130
  Then into a view:
131
131
 
132
132
  <% if @query.category %>
data/Rakefile CHANGED
@@ -7,4 +7,4 @@ desc 'Default: runs specs.'
7
7
  task default: :spec
8
8
 
9
9
  desc 'Run all specs in spec directory.'
10
- RSpec::Core::RakeTask.new(:spec)
10
+ RSpec::Core::RakeTask.new(:spec)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.8.0
1
+ 0.8.1
data/lib/scoped_from.rb CHANGED
@@ -18,4 +18,4 @@ lib_path = "#{__dir__}/scoped_from"
18
18
  require "#{lib_path}/active_record"
19
19
  require "#{lib_path}/query"
20
20
 
21
- ActiveRecord::Base.send(:include, ScopedFrom::ActiveRecord)
21
+ ActiveRecord::Base.send(:include, ScopedFrom::ActiveRecord)
@@ -34,4 +34,4 @@ module ScopedFrom
34
34
 
35
35
  end
36
36
 
37
- end
37
+ end
@@ -110,7 +110,7 @@ module ScopedFrom
110
110
 
111
111
  def parse_orders(values)
112
112
  [].tap do |orders|
113
- values.reverse.each do |value|
113
+ values.each do |value|
114
114
  order = parse_order(value)
115
115
  orders << order if order.present? && !orders.any? { |o| o[:column] == order[:column] }
116
116
  end
@@ -127,4 +127,4 @@ module ScopedFrom
127
127
 
128
128
  end
129
129
 
130
- end
130
+ end
data/scoped_from.gemspec CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |s|
6
6
  s.email = 'al@alweb.org'
7
7
  s.homepage = 'https://github.com/alexistoulotte/scoped_from'
8
8
  s.summary = 'Mapping between scopes and parameters for Rails'
9
- s.description = 'Provides a simple mapping between Active Record scopes and controller parameters for Ruby On Rails 3'
9
+ s.description = 'Provides a simple mapping between Active Record scopes and controller parameters for Ruby On Rails 4'
10
10
  s.license = 'MIT'
11
11
 
12
12
  s.rubyforge_project = 'scoped_from'
@@ -1,2 +1,2 @@
1
1
  class Comment < ActiveRecord::Base
2
- end
2
+ end
@@ -1,2 +1,2 @@
1
1
  class CommentQuery < ScopedFrom::Query
2
- end
2
+ end
data/spec/mocks/post.rb CHANGED
@@ -1,2 +1,2 @@
1
1
  class Post < ActiveRecord::Base
2
- end
2
+ end
data/spec/mocks/user.rb CHANGED
@@ -5,4 +5,4 @@ class User < ActiveRecord::Base
5
5
  scope :created_between, -> (after, before) { where('created_at >= ? AND created_at <= ?', after, before) }
6
6
  scope :latest, -> { where('created_at >= ?', 1.week.ago) }
7
7
 
8
- end
8
+ end
@@ -1,2 +1,2 @@
1
1
  class UserQuery < String
2
- end
2
+ end
data/spec/mocks/vote.rb CHANGED
@@ -1,2 +1,2 @@
1
1
  class Vote < ActiveRecord::Base
2
- end
2
+ end
@@ -1,2 +1,2 @@
1
1
  module VoteQuery
2
- end
2
+ end
@@ -171,17 +171,17 @@ describe ScopedFrom::Query do
171
171
  describe '#params' do
172
172
 
173
173
  it 'returns params specified at initialization' do
174
- query(User, :search => 'foo', 'enabled' => true).params.should == { 'search' => 'foo', 'enabled' => true }
174
+ query(User, search: 'foo', 'enabled' => true).params.should == { 'search' => 'foo', 'enabled' => true }
175
175
  end
176
176
 
177
177
  it 'returns an hash with indifferent access' do
178
178
  query(User, 'search' => 'bar').params.should be_a(ActiveSupport::HashWithIndifferentAccess)
179
179
  query(User, 'search' => 'bar').params[:search].should == 'bar'
180
- query(User, :search => 'bar').params['search'].should == 'bar'
180
+ query(User, search: 'bar').params['search'].should == 'bar'
181
181
  end
182
182
 
183
183
  it 'can be converted to query string' do
184
- query(User, :search => ['foo', 'bar'], 'enabled' => '1').params.to_query.should == 'enabled=true&search%5B%5D=foo&search%5B%5D=bar'
184
+ query(User, search: ['foo', 'bar'], 'enabled' => '1').params.to_query.should == 'enabled=true&search%5B%5D=foo&search%5B%5D=bar'
185
185
  end
186
186
 
187
187
  end
@@ -193,7 +193,7 @@ describe ScopedFrom::Query do
193
193
  end
194
194
 
195
195
  it 'removes values that are not scopes' do
196
- query(User, :foo => 'bar', 'search' => 'foo', :enabled => true).params.should == { 'search' => 'foo', 'enabled' => true }
196
+ query(User, foo: 'bar', 'search' => 'foo', enabled: true).params.should == { 'search' => 'foo', 'enabled' => true }
197
197
  end
198
198
 
199
199
  it 'is case sensitive' do
@@ -293,16 +293,16 @@ describe ScopedFrom::Query do
293
293
 
294
294
  it 'includes column values' do
295
295
  query(User, 'firstname' => 'Jane', 'foo' => 'bar').params.should == { 'firstname' => 'Jane' }
296
- query(User, :firstname => 'Jane', 'foo' => 'bar').params.should == { 'firstname' => 'Jane' }
296
+ query(User, firstname: 'Jane', 'foo' => 'bar').params.should == { 'firstname' => 'Jane' }
297
297
  end
298
298
 
299
299
  it 'exclude column values if :exclude_columns option is specified' do
300
- query(User, { :enabled => true, 'firstname' => 'Jane', 'foo' => 'bar' }, :exclude_columns => true).params.should == { 'enabled' => true }
301
- query(User, { :enabled => true, :firstname => 'Jane', :foo => 'bar' }, :exclude_columns => true).params.should == { 'enabled' => true }
300
+ query(User, { enabled: true, 'firstname' => 'Jane', 'foo' => 'bar' }, exclude_columns: true).params.should == { 'enabled' => true }
301
+ query(User, { enabled: true, firstname: 'Jane', foo: 'bar' }, exclude_columns: true).params.should == { 'enabled' => true }
302
302
  end
303
303
 
304
304
  it 'scopes have priority on columns' do
305
- query(User, :enabled => false).params.should == {}
305
+ query(User, enabled: false).params.should == {}
306
306
  end
307
307
 
308
308
  it 'maps an "order"' do
@@ -331,7 +331,7 @@ describe ScopedFrom::Query do
331
331
  end
332
332
 
333
333
  it 'order can be specified as symbol' do
334
- query(User, { :order => 'firstname.desc' }).params.should == { 'order' => 'firstname.desc' }
334
+ query(User, { order: 'firstname.desc' }).params.should == { 'order' => 'firstname.desc' }
335
335
  end
336
336
 
337
337
  it "order is case sensitive" do
@@ -339,10 +339,10 @@ describe ScopedFrom::Query do
339
339
  end
340
340
 
341
341
  it 'many order can be specified' do
342
- query(User, { 'order' => ['firstname.Asc', 'lastname.DESC'] }).params.should == { 'order' => ['lastname.desc', 'firstname.asc'] }
343
- query(User, { 'order' => ['firstname.Asc', 'firstname.desc'] }).params.should == { 'order' => 'firstname.desc' }
344
- query(User, { 'order' => ['firstname.Asc', 'lastname.DESC', 'firstname.desc'] }).params.should == { 'order' => ['firstname.desc', 'lastname.desc'] }
345
- query(User, { 'order' => ['firstname.Asc', 'foo', 'lastname.DESC', 'firstname.desc'] }).params.should == { 'order' => ['firstname.desc', 'lastname.desc'] }
342
+ query(User, { 'order' => ['firstname.Asc', 'lastname.DESC'] }).params.should == { 'order' => ['firstname.asc', 'lastname.desc'] }
343
+ query(User, { 'order' => ['firstname.Asc', 'firstname.desc'] }).params.should == { 'order' => 'firstname.asc' }
344
+ query(User, { 'order' => ['firstname.Asc', 'lastname.DESC', 'firstname.desc'] }).params.should == { 'order' => ['firstname.asc', 'lastname.desc'] }
345
+ query(User, { 'order' => ['firstname.Asc', 'foo', 'lastname.DESC', 'firstname.desc'] }).params.should == { 'order' => ['firstname.asc', 'lastname.desc'] }
346
346
  end
347
347
 
348
348
  it 'order can be delimited by a space' do
@@ -368,18 +368,18 @@ describe ScopedFrom::Query do
368
368
 
369
369
  it 'does not execute any query' do
370
370
  User.should_not_receive(:connection)
371
- query(User, :enabled => true).relation
371
+ query(User, enabled: true).relation
372
372
  end
373
373
 
374
374
  it 'works with scopes with a lambda without arguments' do
375
375
  users(:jane).update_attribute(:created_at, 10.days.ago)
376
- query(User, :latest => true).relation.should == [users(:john)]
377
- query(User, :latest => false).relation.should == [users(:john), users(:jane)]
376
+ query(User, latest: true).relation.should == [users(:john)]
377
+ query(User, latest: false).relation.should == [users(:john), users(:jane)]
378
378
  end
379
379
 
380
380
  it 'does not modify relation specified at initialization' do
381
381
  relation = User.search('foo')
382
- q = query(relation, :enabled => true)
382
+ q = query(relation, enabled: true)
383
383
  expect {
384
384
  expect {
385
385
  q.relation
@@ -393,27 +393,27 @@ describe ScopedFrom::Query do
393
393
  end
394
394
 
395
395
  it 'invokes many times relation if an array is given' do
396
- query(User, :search => ['John', 'Doe']).relation.should == [users(:john)]
397
- query(User, :search => ['John', 'Done']).relation.should == []
398
- query(User, :search => ['John', 'Doe']).params.should == { 'search' => ['John', 'Doe'] }
396
+ query(User, search: ['John', 'Doe']).relation.should == [users(:john)]
397
+ query(User, search: ['John', 'Done']).relation.should == []
398
+ query(User, search: ['John', 'Doe']).params.should == { 'search' => ['John', 'Doe'] }
399
399
  end
400
400
 
401
401
  it 'invokes many times relation if given twice (as string & symbol)' do
402
- query(User, :search => 'John', 'search' => 'Done').params['search'].size.should be(2)
403
- query(User, :search => 'John', 'search' => 'Done').params['search'].should include('John', 'Done')
402
+ query(User, search: 'John', 'search' => 'Done').params['search'].size.should be(2)
403
+ query(User, search: 'John', 'search' => 'Done').params['search'].should include('John', 'Done')
404
404
 
405
405
 
406
- query(User, :search => 'John', 'search' => ['Did', 'Done']).params['search'].size.should be(3)
407
- query(User, :search => 'John', 'search' => ['Did', 'Done']).params['search'].should include('John', 'Did', 'Done')
406
+ query(User, search: 'John', 'search' => ['Did', 'Done']).params['search'].size.should be(3)
407
+ query(User, search: 'John', 'search' => ['Did', 'Done']).params['search'].should include('John', 'Did', 'Done')
408
408
  end
409
409
 
410
410
  it 'invokes last order if an array is given' do
411
411
  create_user(:jane2, firstname: 'Jane', lastname: 'Zoe')
412
412
 
413
- query(User, :order => ['lastname', 'firstname']).relation.should == [users(:jane), users(:john), users(:jane2)]
414
- query(User, :order => ['lastname', 'firstname.desc']).relation.should == [users(:john), users(:jane), users(:jane2)]
415
- query(User, :order => ['firstname', 'lastname.desc']).relation.should == [users(:jane2), users(:jane), users(:john)]
416
- query(User, :order => ['firstname.desc', 'lastname']).relation.order_values.should == [{ 'firstname' => :desc }, { 'lastname' => :asc }]
413
+ query(User, order: ['lastname', 'firstname']).relation.should == [users(:jane), users(:john), users(:jane2)]
414
+ query(User, order: ['lastname', 'firstname.desc']).relation.should == [users(:john), users(:jane), users(:jane2)]
415
+ query(User, order: ['firstname', 'lastname.desc']).relation.should == [users(:jane2), users(:jane), users(:john)]
416
+ query(User, order: ['firstname.desc', 'lastname']).relation.order_values.should == [{ 'firstname' => :desc }, { 'lastname' => :asc }]
417
417
  end
418
418
 
419
419
  it 'defines #query method on returned relation' do
data/spec/spec_helper.rb CHANGED
@@ -20,4 +20,4 @@ RSpec.configure do |config|
20
20
  create_user(:john, firstname: 'John', lastname: 'Doe', enabled: true, admin: true)
21
21
  create_user(:jane, firstname: 'Jane', lastname: 'Doe', enabled: false, admin: false)
22
22
  end
23
- end
23
+ end
@@ -9,4 +9,4 @@ ActiveRecord::Base.connection.create_table(:users, force: true) do |t|
9
9
  t.boolean :admin, null: false, default: false
10
10
  t.timestamps
11
11
  end
12
- ActiveRecord::Base.connection.create_table(:votes, force: true)
12
+ ActiveRecord::Base.connection.create_table(:votes, force: true)
@@ -10,4 +10,4 @@ module UserMacro
10
10
  USERS[label]
11
11
  end
12
12
 
13
- end
13
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scoped_from
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alexis Toulotte
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-18 00:00:00.000000000 Z
11
+ date: 2013-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -81,7 +81,7 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: 1.3.0
83
83
  description: Provides a simple mapping between Active Record scopes and controller
84
- parameters for Ruby On Rails 3
84
+ parameters for Ruby On Rails 4
85
85
  email: al@alweb.org
86
86
  executables: []
87
87
  extensions: []
@@ -132,7 +132,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
132
132
  version: '0'
133
133
  requirements: []
134
134
  rubyforge_project: scoped_from
135
- rubygems_version: 2.0.3
135
+ rubygems_version: 2.0.14
136
136
  signing_key:
137
137
  specification_version: 4
138
138
  summary: Mapping between scopes and parameters for Rails