por_relation_callbacks 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 +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +76 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/por_relation_callbacks.rb +73 -0
- data/lib/por_relation_callbacks/version.rb +3 -0
- data/por_relation_callbacks.gemspec +27 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5d3e5c8d9992ff72fd494f384302b35add564e9c
|
4
|
+
data.tar.gz: f7590fcc67760b3f70348ed5740daa2ddc1b3174
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 428b5f9eeb175c544ec3a1b6cd7e250aaeaf24c3c88cdd6bfae9d9c13997b41641b2968575f5042783fed87addd66b39086cd14011ee2d534529a7b55cfa589f
|
7
|
+
data.tar.gz: 7f21393b0ad2e3c7b5247a18df71aa3afd4c597ac51e1328991b141c2be8efc14869aa9948fb35f7d9db8735d34e66f504dcfeecb3605aa7bf64d2b63d216d0b
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 James Mac William
|
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,76 @@
|
|
1
|
+
# PorRelationCallbacks
|
2
|
+
|
3
|
+
This gem makes it easy to extract active record callbacks on relations
|
4
|
+
of an object that do not belong in the active record object. an example
|
5
|
+
would be any callback that does not manipulate the data of the base
|
6
|
+
object directly.
|
7
|
+
|
8
|
+
[![Build
|
9
|
+
Status](https://travis-ci.org/jamesmacwilliam/por_relation_callbacks.svg?branch=master)](https://travis-ci.org/jamesmacwilliam/por_relation_callbacks)
|
10
|
+
|
11
|
+
[![Code
|
12
|
+
Climate](https://codeclimate.com/github/jamesmacwilliam/por_relation_callbacks/badges/gpa.svg)](https://codeclimate.com/github/jamesmacwilliam/por_relation_callbacks)
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'por_relation_callbacks'
|
20
|
+
```
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
|
24
|
+
$ bundle
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install por_relation_callbacks
|
29
|
+
|
30
|
+
## Usage
|
31
|
+
|
32
|
+
- create a plain old ruby class to house callbacks on relations of an
|
33
|
+
object (after_add/after_remove) that do not belong in your model for
|
34
|
+
whatever reason (ie: callbacks aren't manipulating data on the base
|
35
|
+
model)
|
36
|
+
|
37
|
+
- specify your after_add/after_remove actions and which callback these
|
38
|
+
apply to, ie: if you want these to trigger when we add or subtract from
|
39
|
+
user.addresses then `callbacks_for :addresses`
|
40
|
+
|
41
|
+
- We instantiate the class by passing in the base record (in the above
|
42
|
+
example an instance of `User` and then pass the params to update the
|
43
|
+
record
|
44
|
+
|
45
|
+
- define the methods used for after_add/after_remove within the class
|
46
|
+
just like you would an active record object
|
47
|
+
|
48
|
+
- to use, instantiate the class and save it just like you would an
|
49
|
+
ActiveRecord Object, SomeClass.new(bas_record,
|
50
|
+
some_params).save
|
51
|
+
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
include PorRelationCallbacks
|
55
|
+
|
56
|
+
after_add :my_method_1, :my_method_2
|
57
|
+
after_remove :my_method_3
|
58
|
+
|
59
|
+
callbacks_for :my_relation
|
60
|
+
```
|
61
|
+
|
62
|
+
## Development
|
63
|
+
|
64
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
65
|
+
|
66
|
+
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`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
67
|
+
|
68
|
+
## Contributing
|
69
|
+
|
70
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jamesmacwilliam/por_relation_callbacks. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
71
|
+
|
72
|
+
|
73
|
+
## License
|
74
|
+
|
75
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
76
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "por_relation_callbacks"
|
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,73 @@
|
|
1
|
+
require "por_relation_callbacks/version"
|
2
|
+
require "active_record"
|
3
|
+
require "active_support"
|
4
|
+
|
5
|
+
module PorRelationCallbacks
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
cattr_accessor :_after_add, :_after_remove, :_watched_relation
|
10
|
+
attr_accessor :object, :prms
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def after_add(*methods)
|
15
|
+
self._after_add = methods
|
16
|
+
end
|
17
|
+
|
18
|
+
def after_remove(*methods)
|
19
|
+
self._after_remove = methods
|
20
|
+
end
|
21
|
+
|
22
|
+
def callbacks_for(rel)
|
23
|
+
self._watched_relation = rel
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize(object, prms)
|
28
|
+
@object = object
|
29
|
+
@prms = prms
|
30
|
+
|
31
|
+
self._after_add ||= []
|
32
|
+
self._after_remove ||= []
|
33
|
+
end
|
34
|
+
|
35
|
+
def save
|
36
|
+
return unless object
|
37
|
+
ActiveRecord::Base.transaction do
|
38
|
+
@existing_relation_state = object.send(_watched_relation).to_a
|
39
|
+
object.update_attributes(prms)
|
40
|
+
object.reload
|
41
|
+
@updated_relation_state = object.send(_watched_relation).to_a
|
42
|
+
hooks
|
43
|
+
end
|
44
|
+
true
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def hooks
|
50
|
+
after_add
|
51
|
+
after_remove
|
52
|
+
end
|
53
|
+
|
54
|
+
def after_add
|
55
|
+
return true if _after_add.empty?
|
56
|
+
trigger_callbacks (@updated_relation_state - @existing_relation_state),
|
57
|
+
_after_add
|
58
|
+
end
|
59
|
+
|
60
|
+
def after_remove
|
61
|
+
return true if _after_remove.empty?
|
62
|
+
trigger_callbacks (@existing_relation_state - @updated_relation_state),
|
63
|
+
_after_remove
|
64
|
+
end
|
65
|
+
|
66
|
+
def trigger_callbacks(records, callbacks)
|
67
|
+
records.each do |record|
|
68
|
+
callbacks.each do |method|
|
69
|
+
send(method, record)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'por_relation_callbacks/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "por_relation_callbacks"
|
8
|
+
spec.version = PorRelationCallbacks::VERSION
|
9
|
+
spec.authors = ["James Mac William"]
|
10
|
+
spec.email = ["jimmy.macwilliam@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Plain old ruby callbacks on active record relations}
|
13
|
+
spec.description = %q{Handles after_add and after_remove outside of the AR callback flow to keep things that aren't directly tied to the model outside of it}
|
14
|
+
spec.homepage = "https://github.com/jamesmacwilliam/por_relation_callbacks"
|
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.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_dependency "railties", ">= 4.1.0", "< 5.1"
|
25
|
+
spec.add_dependency "activerecord", ">= 4.0.0", "< 5.1"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: por_relation_callbacks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Mac William
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-17 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.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: railties
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 4.1.0
|
48
|
+
- - "<"
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '5.1'
|
51
|
+
type: :runtime
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 4.1.0
|
58
|
+
- - "<"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '5.1'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: activerecord
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 4.0.0
|
68
|
+
- - "<"
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '5.1'
|
71
|
+
type: :runtime
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: 4.0.0
|
78
|
+
- - "<"
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '5.1'
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: rspec
|
83
|
+
requirement: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
description: Handles after_add and after_remove outside of the AR callback flow to
|
96
|
+
keep things that aren't directly tied to the model outside of it
|
97
|
+
email:
|
98
|
+
- jimmy.macwilliam@gmail.com
|
99
|
+
executables: []
|
100
|
+
extensions: []
|
101
|
+
extra_rdoc_files: []
|
102
|
+
files:
|
103
|
+
- ".gitignore"
|
104
|
+
- ".rspec"
|
105
|
+
- ".travis.yml"
|
106
|
+
- CODE_OF_CONDUCT.md
|
107
|
+
- Gemfile
|
108
|
+
- LICENSE.txt
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- bin/console
|
112
|
+
- bin/setup
|
113
|
+
- lib/por_relation_callbacks.rb
|
114
|
+
- lib/por_relation_callbacks/version.rb
|
115
|
+
- por_relation_callbacks.gemspec
|
116
|
+
homepage: https://github.com/jamesmacwilliam/por_relation_callbacks
|
117
|
+
licenses:
|
118
|
+
- MIT
|
119
|
+
metadata: {}
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubyforge_project:
|
136
|
+
rubygems_version: 2.4.5
|
137
|
+
signing_key:
|
138
|
+
specification_version: 4
|
139
|
+
summary: Plain old ruby callbacks on active record relations
|
140
|
+
test_files: []
|
141
|
+
has_rdoc:
|