scopie 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 81e654437e834ed3e40a66765512129901f09d97
4
- data.tar.gz: ecf9f776853e9bc5f7f4a2eb9c43382a5c6be232
3
+ metadata.gz: e4dc1d9c52050baf32e9de66d52508da37b2b6d2
4
+ data.tar.gz: 6c5abc0c7f5a27e0784db9607cb1e9331c757788
5
5
  SHA512:
6
- metadata.gz: e9507e247d4134b177dbe1d469cbb4108919c78f55e1bf67f91a8093234ef9819c67bf0b19d143739422b4025be0f1180652093549c1fa931adfdb0dcaae8e78
7
- data.tar.gz: 2927c25d5d98354ae2e9523fdbf6d09e206204ae2dd8cffb4fec759f94ddb582a9c50ff8f75a48a104b8a148150cfe9ce199982e70286e38e65b31be285225f2
6
+ metadata.gz: 86efafe4d7843c496ce6dd6c1b6e13f5600184cec5773e4dd90f087f4edf61df0e1cebf94429f4adb5ee4af62b219e34f8609966ec963428a892383f39e191d1
7
+ data.tar.gz: ded02f636fa57c71e24bf0bdc70b17d9ceafa0f58d7f5372db265e1887430cbc9d5f785a3d65dad83fb6a23e46fc730fa0b55a1161d005689663b8bdf2d5bb10
data/README.md CHANGED
@@ -2,6 +2,13 @@
2
2
  Minimal mapping of incoming parameters to named scopes in your resources through OO design
3
3
 
4
4
  Scopie allows you to map incoming controller parameters to named scopes in your resources.
5
+
6
+ Scopie is the yet another implementation of [has_scope](http://github.com/plataformatec/has_scope). The key differences is dedicated class where
7
+ the scopes are defined. Also, it doesn't depend from ActiveSupport or ActionPack. Please have, a look at [scopie_rails](http://github.com/beorc/scopie_rails)
8
+ if you are using Ruby on Rails framework.
9
+ To override default mapping behavior you don't need to pass a block - just define a method with the same name as scope.
10
+ 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.
11
+
5
12
  Imagine the following model called graduations:
6
13
 
7
14
  ```ruby
@@ -17,8 +24,9 @@ You can use those named scopes as filters by declaring them on your scopie:
17
24
  ```ruby
18
25
  class Scopies::GraduationsScopie < Scopie::Base
19
26
  has_scope :featured, type: :boolean
20
- has_scope :by_degree
21
- has_scope :by_period
27
+ has_scope :by_degree, :by_period
28
+ has_scope :page, default: 1
29
+ has_scope :per, default: 30
22
30
 
23
31
  def by_period(scope, value, _hash)
24
32
  scope.by_period(value[:started_at], value[:ended_at])
@@ -57,7 +65,7 @@ Then for each request:
57
65
  Add `scopie` to your Gemfile or install it from Rubygems.
58
66
 
59
67
  ```ruby
60
- gem 'scopie
68
+ gem 'scopie'
61
69
  ```
62
70
 
63
71
  ## Options
@@ -70,4 +78,12 @@ Scopie supports several options:
70
78
 
71
79
  * `:except` - In which actions the scope is not applied.
72
80
 
81
+ * `:as` - The key in the params hash expected to find the scope. Defaults to the scope name.
82
+
73
83
  * `:default` - Default value for the scope. Whenever supplied the scope is always called.
84
+
85
+ ## Thanks
86
+
87
+ Scopie was inspired by [has_scope](http://github.com/plataformatec/has_scope) and [pundit](http://github.com/elabs/pubdit).
88
+
89
+ Thanks to both.
data/lib/scopie/base.rb CHANGED
@@ -31,8 +31,15 @@ class Scopie::Base
31
31
 
32
32
  private
33
33
 
34
+ def key_name(scope_name, options)
35
+ key_name = scope_name
36
+ key_name = options[:as] if options.key?(:as)
37
+ key_name
38
+ end
39
+
34
40
  def scope_value(scope_name, options, hash)
35
- return coerce_value_type(hash[scope_name], options[:type]) if hash.key?(scope_name)
41
+ key_name = key_name(scope_name, options)
42
+ return coerce_value_type(hash[key_name], options[:type]) if hash.key?(key_name)
36
43
  options[:default]
37
44
  end
38
45
 
@@ -54,7 +61,8 @@ class Scopie::Base
54
61
  return false if methods_white_list.any? && !methods_white_list.include?(method)
55
62
  end
56
63
 
57
- hash.key?(scope_name) || options.key?(:default)
64
+ key_name = key_name(scope_name, options)
65
+ hash.key?(key_name) || options.key?(:default)
58
66
  end
59
67
 
60
68
  def self.reset_scopes_configuration!
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Scopie
3
- VERSION = '0.3.0'
3
+ VERSION = '0.4.0'
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scopie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.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-22 00:00:00.000000000 Z
11
+ date: 2016-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec