scopable 1.0.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 +7 -0
- data/.gitignore +16 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +22 -0
- data/README.md +54 -0
- data/Rakefile +2 -0
- data/lib/scopable.rb +60 -0
- data/lib/scopable/version.rb +3 -0
- data/scopable.gemspec +25 -0
- metadata +100 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a92105be0449f880038bc7d58e0af2d2a1873531
|
4
|
+
data.tar.gz: 2718a557dda1ec4d724ea0f9ab231fe0ffd45a43
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7c0cadb182a7e2171f6e4d97db016bcad90e6d6a2abc7d9f5146827643699bc21190e5a3ac92c7f977ed5ae3b103e3eb9228d2aca4fb89bfb2f259501a988971
|
7
|
+
data.tar.gz: dbcb6bffaeea5d1101b0385ccc9537604552506e73fa97a3c3a3c607618aabd57ff85b384b5908d79bad23bf651112d68c5b411dd3793390ec0f7d9a03ff5eaa
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Arthur Corenzan
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Scopable
|
2
|
+
|
3
|
+
> Apply scopes to your query based on available parameters
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'scopable'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install scopable
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Configure scopes in your controller:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
class PostsController < ApplicationController
|
27
|
+
include Scopable
|
28
|
+
|
29
|
+
scope :search, :param => :q
|
30
|
+
|
31
|
+
# ...
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Then apply it to your model:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
def index
|
39
|
+
@posts = scoped(Post, params)
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
Now, whenever the parameter `q` is present at `params`, it will call `#search` on your model passing its value as argument.
|
44
|
+
|
45
|
+
TODO: Add more examples.
|
46
|
+
TODO: Layout `scope` options.
|
47
|
+
|
48
|
+
## Contributing
|
49
|
+
|
50
|
+
1. Fork it ( https://github.com/[my-github-username]/scopable/fork )
|
51
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
52
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
53
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/lib/scopable.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require "scopable/version"
|
2
|
+
|
3
|
+
module Scopable
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
cattr_reader :scopes do
|
8
|
+
Hash.new
|
9
|
+
end
|
10
|
+
|
11
|
+
helper_method :active_scopes
|
12
|
+
end
|
13
|
+
|
14
|
+
def active_scopes
|
15
|
+
scopes.map do |name, scope|
|
16
|
+
name if scope[:active]
|
17
|
+
end.compact
|
18
|
+
end
|
19
|
+
|
20
|
+
def scoped(resource, params)
|
21
|
+
scopes.reduce(resource) do |resource, scope|
|
22
|
+
name, scope = *scope
|
23
|
+
|
24
|
+
param, force, default, except, only, fn = scope.values_at(:param, :force, :default, :except, :only, :fn)
|
25
|
+
|
26
|
+
force = self.instance_exec(&force) if force.respond_to?(:call)
|
27
|
+
|
28
|
+
param = param || name
|
29
|
+
value = force || params[param] || default
|
30
|
+
|
31
|
+
value = nil if except.present? && Array.wrap(except).map(&:to_s).include?(action_name)
|
32
|
+
value = nil unless only.nil? || Array.wrap(only).map(&:to_s).include?(action_name)
|
33
|
+
|
34
|
+
if value.nil? || value.to_s =~ /\A(false|no|off)\z/
|
35
|
+
scope.update(:active => false)
|
36
|
+
resource
|
37
|
+
else
|
38
|
+
scope.update(:active => true)
|
39
|
+
|
40
|
+
value = nil if value == 'nil'
|
41
|
+
|
42
|
+
if fn
|
43
|
+
self.instance_exec(resource, value, &fn)
|
44
|
+
else
|
45
|
+
if value.to_s =~ /\A(true|yes|on)\z/
|
46
|
+
resource.send(name)
|
47
|
+
else
|
48
|
+
resource.send(name, value)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
module ClassMethods
|
56
|
+
def scope(name, opts = {}, &fn)
|
57
|
+
scopes.store name, opts.merge(:fn => fn)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/scopable.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'scopable/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "scopable"
|
8
|
+
spec.version = Scopable::VERSION
|
9
|
+
spec.authors = ["Arthur Corenzan"]
|
10
|
+
spec.email = ["arthur@corenzan.com"]
|
11
|
+
spec.summary = %q{Apply scopes to your query based on available parameters}
|
12
|
+
# spec.description = %q{}
|
13
|
+
spec.homepage = "https://github.com/haggen/scopable"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "activesupport", ">= 3.2", "< 5.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: scopable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Arthur Corenzan
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '5.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.2'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '5.0'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: bundler
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.7'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1.7'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '10.0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '10.0'
|
61
|
+
description:
|
62
|
+
email:
|
63
|
+
- arthur@corenzan.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- ".gitignore"
|
69
|
+
- Gemfile
|
70
|
+
- LICENSE.txt
|
71
|
+
- README.md
|
72
|
+
- Rakefile
|
73
|
+
- lib/scopable.rb
|
74
|
+
- lib/scopable/version.rb
|
75
|
+
- scopable.gemspec
|
76
|
+
homepage: https://github.com/haggen/scopable
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.4.5
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Apply scopes to your query based on available parameters
|
100
|
+
test_files: []
|