scope_delegation 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +31 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +101 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/scope_delegation.rb +13 -0
- data/lib/scope_delegation/version.rb +3 -0
- data/scope_delegation.gemspec +24 -0
- metadata +86 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0b8e3d5202a9186bfd041a1781364431c35b464e
|
4
|
+
data.tar.gz: 271216f634f0b014733b5980afebb5e7d2a56369
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8c096298fa45258c8a3cae991c60e7a3e2eebbc90b73f7437d7fa4dfd4e53f2b6a6fd74016fe94d04f240595d172d60dc79f43e2618d33719ab2504e29702977
|
7
|
+
data.tar.gz: 42688d4cfe528ec6d2e5d4d4c219c1bc7ecbad26f403b37473902599431cd5bb1d885aa98fa535bf738ff4f47319f86d6d1b7ee49b409f607e1ec46479ed8807
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all
|
4
|
+
people who contribute through reporting issues, posting feature requests,
|
5
|
+
updating documentation, submitting pull requests or patches, and other
|
6
|
+
activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, or religion.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include the use of sexual
|
14
|
+
language or imagery, derogatory comments or personal attacks, trolling, public
|
15
|
+
or private harassment, insults, or other unprofessional conduct.
|
16
|
+
|
17
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
18
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
19
|
+
that are not aligned to this Code of Conduct. Project maintainers who do not
|
20
|
+
follow the Code of Conduct may be removed from the project team.
|
21
|
+
|
22
|
+
This code of conduct applies both within project spaces and in public spaces
|
23
|
+
when an individual is representing the project or its community.
|
24
|
+
|
25
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
26
|
+
reported by opening an issue or contacting one or more of the project
|
27
|
+
maintainers.
|
28
|
+
|
29
|
+
This Code of Conduct is adapted from the Contributor Covenant
|
30
|
+
(http://contributor-covenant.org), version 1.1.0, available at
|
31
|
+
http://contributor-covenant.org/version/1/1/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Jon Evans
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
# ScopeDelegation
|
2
|
+
|
3
|
+
Every application needs to be able to scope data, and every developer hates
|
4
|
+
writing boilerplate code.
|
5
|
+
|
6
|
+
With `ScopeDelegation`, you can easily define scopes
|
7
|
+
in one model that rely on scopes that are defined on an associated model.
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
Let's look at an example.
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
class Whole < ActiveRecord::Base
|
15
|
+
extend ScopeDelegation
|
16
|
+
|
17
|
+
has_many :parts
|
18
|
+
|
19
|
+
delegate_scope :named, to: :parts, prefix: :part
|
20
|
+
end
|
21
|
+
|
22
|
+
class Part < ActiveRecord::Base
|
23
|
+
belongs_to :whole
|
24
|
+
|
25
|
+
def self.named(name)
|
26
|
+
where(name: name)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
This now enables you to call `Whole.part_named("some name")` to easily scope
|
32
|
+
your `Whole`s.
|
33
|
+
|
34
|
+
The `prefix` option is optional, and you can also specify the `scope` on the
|
35
|
+
associated model if it does not match the first parameter.
|
36
|
+
|
37
|
+
## Explanation
|
38
|
+
|
39
|
+
In a previous life, you might have been tempted to do something like:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
# whole.rb
|
43
|
+
def self.part_named(name)
|
44
|
+
joins(:parts).where(name: name)
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
This is unfortunate because now `Whole` knows about a column name for `Part`,
|
49
|
+
which is none of its business. Isolating knowledge about the persistence for a
|
50
|
+
given model to that model itself makes changing things easier.
|
51
|
+
|
52
|
+
We can get around this with `merge`, like so:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
# whole.rb
|
56
|
+
def self.part_named(name)
|
57
|
+
joins(:parts).merge(Part.named(name))
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
This is what `ScopeDelegation` does, but with a friendly DSL that allows you to
|
62
|
+
avoid writing boilerplate while discouraging knowing things about your neighbor
|
63
|
+
that you shouldn't.
|
64
|
+
|
65
|
+
## Installation
|
66
|
+
|
67
|
+
Add this line to your application's Gemfile:
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
gem 'scope_delegation'
|
71
|
+
```
|
72
|
+
|
73
|
+
And then execute:
|
74
|
+
|
75
|
+
$ bundle
|
76
|
+
|
77
|
+
Or install it yourself as:
|
78
|
+
|
79
|
+
$ gem install scope_delegation
|
80
|
+
|
81
|
+
## Development
|
82
|
+
|
83
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
|
84
|
+
|
85
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
86
|
+
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
1. Fork it ( https://github.com/[my-github-username]/scope_delegation/fork )
|
90
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
91
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
92
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
93
|
+
5. Create a new Pull Request
|
94
|
+
|
95
|
+
## About Foraker Labs
|
96
|
+
|
97
|
+
<img src="http://assets.foraker.com/foraker_logo.png" width="400" height="62">
|
98
|
+
|
99
|
+
This project is maintained by Foraker Labs. The names and logos of Foraker Labs are fully owned and copyright Foraker Design, LLC.
|
100
|
+
|
101
|
+
Foraker Labs is a Boulder-based Ruby on Rails and iOS development shop. Please reach out if we can [help build your product](http://www.foraker.com).
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "scope_delegation"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "scope_delegation/version"
|
2
|
+
|
3
|
+
module ScopeDelegation
|
4
|
+
def delegate_scope(name, options)
|
5
|
+
source = options[:scope] || name
|
6
|
+
association = reflect_on_association(options[:to]) || raise("Unknown association")
|
7
|
+
name = [options[:prefix], name].compact.join("_")
|
8
|
+
|
9
|
+
scope name, Proc.new { |*args|
|
10
|
+
joins(association.name).merge(association.klass.send(source, *args))
|
11
|
+
}
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'scope_delegation/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "scope_delegation"
|
8
|
+
spec.version = ScopeDelegation::VERSION
|
9
|
+
spec.authors = ["Foraker"]
|
10
|
+
spec.email = ["jle@foraker.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Easily delegate scopes to associations in Rails.}
|
13
|
+
spec.description = %q{Helps to reduce boilerplate for joining and merging scopes. Write fewer characters and be happier.}
|
14
|
+
spec.homepage = "https://github.com/foraker/scope_delegation"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: scope_delegation
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Foraker
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: Helps to reduce boilerplate for joining and merging scopes. Write fewer
|
42
|
+
characters and be happier.
|
43
|
+
email:
|
44
|
+
- jle@foraker.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- ".gitignore"
|
50
|
+
- ".rspec"
|
51
|
+
- ".travis.yml"
|
52
|
+
- CODE_OF_CONDUCT.md
|
53
|
+
- Gemfile
|
54
|
+
- LICENSE.txt
|
55
|
+
- README.md
|
56
|
+
- Rakefile
|
57
|
+
- bin/console
|
58
|
+
- bin/setup
|
59
|
+
- lib/scope_delegation.rb
|
60
|
+
- lib/scope_delegation/version.rb
|
61
|
+
- scope_delegation.gemspec
|
62
|
+
homepage: https://github.com/foraker/scope_delegation
|
63
|
+
licenses:
|
64
|
+
- MIT
|
65
|
+
metadata: {}
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - ">="
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0'
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 2.4.6
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Easily delegate scopes to associations in Rails.
|
86
|
+
test_files: []
|