sorted-activerecord 0.1.0 → 0.1.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: 73c6a1710bf109f92c675c82bfb51adbae2e83fe
4
- data.tar.gz: f839fef7e88e8ca7632f3e9055467a217d7e2a96
3
+ metadata.gz: dc58014f05b2a80f9238bb33d98e76e3dec941c5
4
+ data.tar.gz: 1ead6ecd557dd19f0c3a03ee11a154fc90c86665
5
5
  SHA512:
6
- metadata.gz: be76ff4089fe1a2062ad37b001f1084f76b92401ca8011b356a480a8499036b4de359fc3ae6cf1e6b62da7a9655e914b805a0c028eba102758ec0145dbb0c853
7
- data.tar.gz: d65155d4a62a460667c0e38e8fd1a3832bba1e0f6f9b0e43a837848f21d2e92f6610bed1bcaa286d769b9ee7c01682d59faf71580f0806f578042fefe48f88a1
6
+ metadata.gz: f7a8c3e36ddc370b777204b0eb58fbcc3851d59d4532c8b3295c939fc5f4dac841a3269677e6d6cdaca959e597b0abf388feec5ae1e72bcb298bd737d2c84317
7
+ data.tar.gz: 1379753406f3ea0d17e7046487417b8572d13bb899e7aadfb5a15d0fd433dee73010a8451e608847b74dcb097b8ec09cd3bb9cb5511f477aee1ec2f51c7691b3
data/README.md CHANGED
@@ -31,8 +31,7 @@ except that if you provide a string to the sort argument it expects a
31
31
  `Sorted::URIQuery` encoded string:
32
32
 
33
33
  ```ruby
34
- @users = User.sorted(sort: 'created_asc!orders_count_asc'
35
- order: 'orders_count ASC, created_at DESC')
34
+ @users = User.sorted(sort: 'created_asc!orders_count_asc', order: 'orders_count ASC, created_at DESC')
36
35
  ```
37
36
 
38
37
  See https://github.com/mynameisrufus/sorted-actionview for a view helper to
@@ -42,16 +41,14 @@ A `resorted` method is also available and works the same way as the `reorder`
42
41
  method in Rails. It forces the order to be the one passed in:
43
42
 
44
43
  ```ruby
45
- @users = User.order(:id).sorted(order: 'name DESC')
46
- .resorted(sort: params[:sort], order: 'email ASC')
44
+ @users = User.order(:id).sorted(order: 'name DESC').resorted(sort: params[:sort], order: 'email ASC')
47
45
  ```
48
46
 
49
47
  If you want to prevent people creating 500s by messing with the sort url string
50
48
  you can use a white list:
51
49
 
52
50
  ```ruby
53
- @users = User.sorted(sort: 'created_asc!explode_me_asc'
54
- whitelist: %w(created_at))
51
+ @users = User.sorted(sort: 'created_asc!explode_me_asc', whitelist: %w(created_at))
55
52
  ```
56
53
 
57
54
  ## Contributing
@@ -17,6 +17,7 @@ module Sorted
17
17
  end
18
18
 
19
19
  def parse_sort(sort)
20
+ return ::Sorted::Set.new if sort.nil?
20
21
  case sort.class.name
21
22
  when 'String'
22
23
  ::Sorted::URIQuery.parse(sort)
@@ -28,6 +29,7 @@ module Sorted
28
29
  end
29
30
 
30
31
  def parse_order(order)
32
+ return ::Sorted::Set.new if order.nil?
31
33
  case order.class.name
32
34
  when 'String'
33
35
  ::Sorted::SQLQuery.parse(order)
@@ -55,6 +57,12 @@ module Sorted
55
57
  memo
56
58
  end
57
59
  end
60
+
61
+ # We return sym here becuase rails 4.0.0 does not like string for values
62
+ # and keys.
63
+ def to_hash
64
+ @set.to_a.inject({}) { |a, e| a.merge(Hash[e[0].to_sym, e[1].to_sym]) }
65
+ end
58
66
  end
59
67
  end
60
68
  end
@@ -12,14 +12,14 @@ module Sorted
12
12
  builder = ::Sorted::ActiveRecord::Builder.new(sort: sort,
13
13
  order: order,
14
14
  whitelist: whitelist)
15
- order(builder.set.to_hash)
15
+ order(builder.to_hash)
16
16
  end
17
17
 
18
18
  def self.resorted(sort: [], order: [], whitelist: [])
19
19
  builder = ::Sorted::ActiveRecord::Builder.new(sort: sort,
20
20
  order: order,
21
21
  whitelist: whitelist)
22
- reorder(builder.set.to_hash)
22
+ reorder(builder.to_hash)
23
23
  end
24
24
  end
25
25
  end
@@ -1,5 +1,5 @@
1
1
  module Sorted
2
2
  module Activerecord
3
- VERSION = '0.1.0'
3
+ VERSION = '0.1.1'
4
4
  end
5
5
  end
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_dependency 'activerecord', '>= 4.0.0'
22
22
  spec.add_dependency 'activesupport'
23
- spec.add_dependency 'sorted', '~> 2.0.0'
23
+ spec.add_dependency 'sorted', '~> 2.0.2'
24
24
 
25
25
  spec.add_development_dependency 'bundler', '~> 1.7'
26
26
  spec.add_development_dependency 'rake', '~> 10.0'
@@ -28,6 +28,5 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency 'rubocop'
29
29
  spec.add_development_dependency 'rspec'
30
30
  spec.add_development_dependency 'guard-rspec'
31
- # spec.add_development_dependency 'activerecord-jdbcsqlite3-adapter'
32
31
  spec.add_development_dependency 'sqlite3', '>= 1.3.5'
33
32
  end
@@ -80,4 +80,12 @@ describe Sorted::ActiveRecord do
80
80
  expect(subject.sorted(sort: 'orders_count_asc!created_at_desc',
81
81
  whitelist: %(created_at)).to_sql).to match(order_by)
82
82
  end
83
+
84
+ it 'should not raise error with nil value' do
85
+ expect(-> { subject.sorted(sort: nil) }).to_not raise_error
86
+ end
87
+
88
+ it 'should not raise error with empty string' do
89
+ expect(-> { subject.sorted(sort: '') }).to_not raise_error
90
+ end
83
91
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sorted-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rufus Post
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-28 00:00:00.000000000 Z
11
+ date: 2015-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: 2.0.0
47
+ version: 2.0.2
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: 2.0.0
54
+ version: 2.0.2
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement