scopie_rails 0.2.0 → 0.3.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: d709cd41d5dbae30fa15a4dfc02d956433688079
4
- data.tar.gz: ed3c1303ce2903a37d8c144edf7b8f23046f71de
3
+ metadata.gz: 3e56563f6e72158575391b7da2f3a82e22737a46
4
+ data.tar.gz: 21af9647d32257149b14ddd4878720039d461dc5
5
5
  SHA512:
6
- metadata.gz: 7d34cd601c277d93720362a3c2a211ad9874f113ff55018b210434c6505c1f0fcb899787ac23c04f8c2e058cbe8bc718af6f281aaf4ef2cb8831945ef4d1b943
7
- data.tar.gz: 3c84b48a12da15d8e7fc1955f498bcb44232ab0f135e54e7f676eb0b70edb8b4f4cc16ad9c609a2217f7dfb4c6906c6429f2005550833fb1e98a2a929eedd9cb
6
+ metadata.gz: c8de2a74842924f0a452664058ee620a676132c75e2dede33bfa10d364a88cf2f173aa3c311dd7f7688f4bc8a3673129cabc4092809d13a1db3369d9f7c12d68
7
+ data.tar.gz: 0fda698f097b7d25b4e3728e83bfe8ea5536e0f06bc5c7f08615f483be4406035b09fad9b23155fa24f08918e80ad3c9f09ec79b18462a504b54bd46f02e8bd5
data/README.md CHANGED
@@ -1,2 +1,75 @@
1
- # scopie_rails
1
+ ## ScopieRails
2
2
  Scopie for Rails
3
+
4
+ ScopieRails allows you to map incoming controller parameters to named scopes in your resources.
5
+ Imagine the following model called graduations:
6
+
7
+ ```ruby
8
+ class Graduation < ActiveRecord::Base
9
+ scope :featured, -> { where(:featured => true) }
10
+ scope :by_degree, -> degree { where(:degree => degree) }
11
+ scope :by_period, -> started_at, ended_at { where("started_at = ? AND ended_at = ?", started_at, ended_at) }
12
+ end
13
+ ```
14
+
15
+ You can use those named scopes as filters by declaring them on your scopie:
16
+
17
+ ```ruby
18
+ class Scopies::GraduationsScopie < ScopieRails::Base
19
+ has_scope :featured, type: :boolean
20
+ has_scope :by_degree
21
+ has_scope :by_period
22
+
23
+ def by_period(scope, value, _hash)
24
+ scope.by_period(value[:started_at], value[:ended_at])
25
+ end
26
+ end
27
+ ```
28
+
29
+ Now, if you want to apply them to an specific resource, you just need to call `apply_scopes`:
30
+
31
+ ```ruby
32
+ class GraduationsController < ApplicationController
33
+ include ScopieRails::Controller
34
+
35
+ def index
36
+ @graduations = apply_scopes(Graduation).all
37
+ end
38
+ end
39
+ ```
40
+
41
+ Then for each request:
42
+
43
+ ```
44
+ /graduations
45
+ #=> acts like a normal request
46
+
47
+ /graduations?featured=true
48
+ #=> calls the named scope and bring featured graduations
49
+
50
+ /graduations?by_period[started_at]=20100701&by_period[ended_at]=20101013
51
+ #=> brings graduations in the given period
52
+
53
+ /graduations?featured=true&by_degree=phd
54
+ #=> brings featured graduations with phd degree
55
+ ```
56
+
57
+ ## Installation
58
+
59
+ Add `scopie_rails` to your Gemfile or install it from Rubygems.
60
+
61
+ ```ruby
62
+ gem 'scopie_rails'
63
+ ```
64
+
65
+ ## Options
66
+
67
+ Scopie supports several options:
68
+
69
+ * `:type` - Checks the type of the parameter sent.
70
+
71
+ * `:only` - In which actions the scope is applied.
72
+
73
+ * `:except` - In which actions the scope is not applied.
74
+
75
+ * `:default` - Default value for the scope. Whenever supplied the scope is always called.
data/lib/scopie_rails.rb CHANGED
@@ -8,10 +8,10 @@ require 'active_support/core_ext/string/inflections'
8
8
 
9
9
  module ScopieRails
10
10
 
11
- SCOPIE_PREFIX = 'Scopies'.freeze
12
- SCOPIE_SUFFIX = 'Scopie'.freeze
13
- CLASS_NAME_DELIMETER = '::'.freeze
14
- CONTROLLER_DELIMETER = 'Controller'.freeze
11
+ SCOPIE_PREFIX = 'Scopies'
12
+ SCOPIE_SUFFIX = 'Scopie'
13
+ CLASS_NAME_DELIMETER = '::'
14
+ CONTROLLER_DELIMETER = 'Controller'
15
15
 
16
16
  require 'scopie_rails/base'
17
17
  require 'scopie_rails/controller'
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module ScopieRails::Controller
2
3
  extend ActiveSupport::Concern
3
4
 
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module ScopieRails
3
- VERSION = '0.2.0'
3
+ VERSION = '0.3.0'
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scopie_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yury Kotov