scopie_rails 0.7.0 → 0.8.0

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: 637fa6d935550c14ae68b73f8af1d4e41ec140d2
4
- data.tar.gz: d198127d9506a406623f76c0c901cb6b81b5616c
3
+ metadata.gz: 3bb41290b07a829393e1f6a02793b320afdce972
4
+ data.tar.gz: 5152212ca1822242d3aed6b7a8fa144e6ddc2502
5
5
  SHA512:
6
- metadata.gz: fe4ec28928e8c38321cec9f5a1b48bbe2fc9cbe4ae6048c95254ac410c6f0b9ab05feb473a4419a667481cb725d7c61b5061d7ad8d35182fab86ed0c91d85ee9
7
- data.tar.gz: d327ebde39eb70724d32fa882f68fc57faaf06bff4a397c001830421cc76236c77765489e091fb6bb45fde0e3cc4344158a2e8af3a2c9d3a5f5066ec3f7081cf
6
+ metadata.gz: 80512b1d6eb25594df3a92956eb3d28a2df5923a5e4f9eeb77a84f92ee8eab383704f07cd76c370aa1387a031925b695213da6dc0aa469e4acdde79844411db1
7
+ data.tar.gz: 2540e408c676d420b38a5a45aabd63835aa7fe579c68fac8cf9352d6cd6b91e0b756ec42151492b6bfc9c04245d87dddba8a62309bf880ce1d026c586bd2860e
data/README.md CHANGED
@@ -1,33 +1,69 @@
1
1
  ## ScopieRails
2
+
3
+ [![Code Climate](https://codeclimate.com/github/beorc/scopie_rails/badges/gpa.svg)](https://codeclimate.com/github/beorc/scopie_rails)
4
+
2
5
  [Scopie][s] for Rails
3
6
 
4
- ScopieRails allows you to map incoming controller parameters to named scopes in your resources and decouple mapping logic from controller.
5
- ScopieRails is the yet another implementation of [has_scope](http://github.com/plataformatec/has_scope). The key difference is dedicated class where
6
- the scopes are defined. To override default mapping behavior you don't need to pass a block - just define a method with the same name as scope.
7
- You can DRY your custom scopes mapping logic by using helper methods defined in scopie class and use the same scopie class in multiple controllers.
7
+ It is similar to [has_scope](http://github.com/plataformatec/has_scope).
8
+
9
+ The key differences are:
10
+
11
+ * Dedicated class where the scopes are defined, so that your controller will be skinny.
12
+ * To override default mapping behavior you don't need to pass a block - just define a method with the same name as scope.
13
+ * You can DRY your custom scopes mapping logic by using helper methods defined in scopie class and use the same scopie class in multiple controllers.
8
14
 
9
15
  Imagine the following model called graduations:
10
16
 
11
17
  ```ruby
12
18
  class Graduation < ActiveRecord::Base
19
+
13
20
  scope :featured, -> { where(featured: true) }
14
21
  scope :by_degree, -> (degree) { where(degree: degree) }
15
22
  scope :by_period, -> (started_at, ended_at) { where('started_at = ? AND ended_at = ?', started_at, ended_at) }
23
+
24
+ scope :created_at_greater_than, ->(date) { where('entities.created_at >= ?', date.beginning_of_day) }
25
+ scope :created_at_less_than, ->(date) { where('entities.created_at <= ?', date.end_of_day) }
26
+ scope :updated_at_greater_than, ->(date) { where('entities.updated_at >= ?', date.beginning_of_day) }
27
+ scope :updated_at_less_than, ->(date) { where('entities.updated_at <= ?', date.end_of_day) }
28
+
16
29
  end
17
30
  ```
18
31
 
19
32
  You can use those named scopes as filters by declaring them on your scopie:
20
33
 
21
34
  ```ruby
22
- class Scopies::GraduationsScopie < ScopieRails::Base
35
+ class GraduationsScopie < ScopieRails::Base
36
+
23
37
  has_scope :featured, type: :boolean
24
38
  has_scope :by_degree, :by_period
39
+
40
+ has_scope :created_at_greater_than, in: :created_at, as: :start_at
41
+ has_scope :created_at_less_than, in: :created_at, as: :end_at
42
+
25
43
  has_scope :page, default: 1
26
44
  has_scope :per, default: 30
27
45
 
28
46
  def by_period(scope, value, _hash)
29
- scope.by_period(value[:started_at], value[:ended_at])
47
+ started_at = value[:started_at]
48
+ ended_at = value[:ended_at]
49
+
50
+ started_at && ended_at && scope.by_period(started_at, ended_at)
51
+ end
52
+
53
+ def created_at_greater_than(scope, value, _hash)
54
+ scope.created_at_greater_than(parse_date(value))
55
+ end
56
+
57
+ def created_at_less_than(scope, value, _hash)
58
+ scope.created_at_less_than(parse_date(value))
30
59
  end
60
+
61
+ private
62
+
63
+ def parse_date(value)
64
+ Date.parse(value)
65
+ end
66
+
31
67
  end
32
68
  ```
33
69
 
@@ -55,6 +91,9 @@ Then for each request:
55
91
  /graduations?by_period[started_at]=20100701&by_period[ended_at]=20101013
56
92
  #=> brings graduations in the given period
57
93
 
94
+ /graduations?created_at[start_at]=2016-06-01&created_at[end_at]=2016-06-02
95
+ #=> brings graduations created in the given period
96
+
58
97
  /graduations?featured=true&by_degree=phd
59
98
  #=> brings featured graduations with phd degree
60
99
  ```
@@ -71,7 +110,7 @@ gem 'scopie_rails'
71
110
 
72
111
  Scopie supports several options:
73
112
 
74
- * `:type` - Coerces the type of the parameter sent.
113
+ * `:type` - Coerces the type of the parameter sent. Available options: boolean, integer, date.
75
114
 
76
115
  * `:only` - In which actions the scope is applied.
77
116
 
@@ -1,3 +1,6 @@
1
+ # frozen_string_literal: true
1
2
  class ScopieRails::Engine < ::Rails::Engine
3
+
2
4
  config.autoload_paths += Dir["#{config.root}/app/scopies"]
5
+
3
6
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  module ScopieRails
3
- VERSION = '0.7.0'
4
+ VERSION = '0.8.0'
4
5
  end
data/lib/scopie_rails.rb CHANGED
@@ -9,9 +9,7 @@ module ScopieRails
9
9
 
10
10
  SCOPIE_SUFFIX = '_scopie'
11
11
 
12
- if defined?(Rails)
13
- require 'scopie_rails/engine'
14
- end
12
+ require 'scopie_rails/engine' if defined?(Rails)
15
13
  require 'scopie_rails/base'
16
14
  require 'scopie_rails/controller'
17
15
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scopie_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yury Kotov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-24 00:00:00.000000000 Z
11
+ date: 2016-06-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: scopie
@@ -30,7 +30,7 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '4.1'
33
+ version: '3.2'
34
34
  - - "<"
35
35
  - !ruby/object:Gem::Version
36
36
  version: '5.1'
@@ -40,7 +40,7 @@ dependencies:
40
40
  requirements:
41
41
  - - ">="
42
42
  - !ruby/object:Gem::Version
43
- version: '4.1'
43
+ version: '3.2'
44
44
  - - "<"
45
45
  - !ruby/object:Gem::Version
46
46
  version: '5.1'
@@ -50,7 +50,7 @@ dependencies:
50
50
  requirements:
51
51
  - - ">="
52
52
  - !ruby/object:Gem::Version
53
- version: '4.1'
53
+ version: '3.2'
54
54
  - - "<"
55
55
  - !ruby/object:Gem::Version
56
56
  version: '5.1'
@@ -60,24 +60,10 @@ dependencies:
60
60
  requirements:
61
61
  - - ">="
62
62
  - !ruby/object:Gem::Version
63
- version: '4.1'
63
+ version: '3.2'
64
64
  - - "<"
65
65
  - !ruby/object:Gem::Version
66
66
  version: '5.1'
67
- - !ruby/object:Gem::Dependency
68
- name: railties
69
- requirement: !ruby/object:Gem::Requirement
70
- requirements:
71
- - - ">="
72
- - !ruby/object:Gem::Version
73
- version: '0'
74
- type: :runtime
75
- prerelease: false
76
- version_requirements: !ruby/object:Gem::Requirement
77
- requirements:
78
- - - ">="
79
- - !ruby/object:Gem::Version
80
- version: '0'
81
67
  - !ruby/object:Gem::Dependency
82
68
  name: rspec
83
69
  requirement: !ruby/object:Gem::Requirement