shiftable 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/.rspec +3 -0
- data/.rubocop.yml +100 -0
- data/.rubocop_todo.yml +46 -0
- data/CHANGELOG.md +10 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +116 -0
- data/Rakefile +12 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/shiftable/collection.rb +93 -0
- data/lib/shiftable/single.rb +101 -0
- data/lib/shiftable/version.rb +5 -0
- data/lib/shiftable.rb +9 -0
- metadata +286 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2aee0ddcd18b646324ef8ec890c7fc50aaf2af9c6b630c5267d3ec0143a7dedb
|
4
|
+
data.tar.gz: c6bd0a6a3ddc61517dc9e162c4cf9e9861e23f832baaf8e013c3bbb12e97d868
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6e6b917230a28123480e5032766ee0e26dfc44c887f177d59b6f44fc9c1659eec0695a1513415f126a3de168b4315498eb7b26905e07350c16d72cec6e17234e
|
7
|
+
data.tar.gz: 0f4765f281ec4cf5bd97e874fe6ee63dc171df5f178d028d99a1c89d886e00ea5906931e1f192467ddcd1206c2d473c8a6d738db0a0db3f8b9bd10454f171da1
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
NewCops: enable
|
5
|
+
TargetRubyVersion: 2.6
|
6
|
+
|
7
|
+
Gemspec/DateAssignment: # new in 1.10
|
8
|
+
Enabled: true
|
9
|
+
Layout/LineEndStringConcatenationIndentation: # new in 1.18
|
10
|
+
Enabled: true
|
11
|
+
Layout/SpaceBeforeBrackets: # new in 1.7
|
12
|
+
Enabled: true
|
13
|
+
Lint/AmbiguousAssignment: # new in 1.7
|
14
|
+
Enabled: true
|
15
|
+
Lint/AmbiguousOperatorPrecedence: # new in 1.21
|
16
|
+
Enabled: true
|
17
|
+
Lint/AmbiguousRange: # new in 1.19
|
18
|
+
Enabled: true
|
19
|
+
Lint/DeprecatedConstants: # new in 1.8
|
20
|
+
Enabled: true
|
21
|
+
Lint/DuplicateBranch: # new in 1.3
|
22
|
+
Enabled: true
|
23
|
+
Lint/DuplicateRegexpCharacterClassElement: # new in 1.1
|
24
|
+
Enabled: true
|
25
|
+
Lint/EmptyBlock: # new in 1.1
|
26
|
+
Enabled: true
|
27
|
+
Lint/EmptyClass: # new in 1.3
|
28
|
+
Enabled: true
|
29
|
+
Lint/EmptyInPattern: # new in 1.16
|
30
|
+
Enabled: true
|
31
|
+
Lint/IncompatibleIoSelectWithFiberScheduler: # new in 1.21
|
32
|
+
Enabled: true
|
33
|
+
Lint/LambdaWithoutLiteralBlock: # new in 1.8
|
34
|
+
Enabled: true
|
35
|
+
Lint/NoReturnInBeginEndBlocks: # new in 1.2
|
36
|
+
Enabled: true
|
37
|
+
Lint/NumberedParameterAssignment: # new in 1.9
|
38
|
+
Enabled: true
|
39
|
+
Lint/OrAssignmentToConstant: # new in 1.9
|
40
|
+
Enabled: true
|
41
|
+
Lint/RedundantDirGlobSort: # new in 1.8
|
42
|
+
Enabled: true
|
43
|
+
Lint/RequireRelativeSelfPath: # new in 1.22
|
44
|
+
Enabled: true
|
45
|
+
Lint/SymbolConversion: # new in 1.9
|
46
|
+
Enabled: true
|
47
|
+
Lint/ToEnumArguments: # new in 1.1
|
48
|
+
Enabled: true
|
49
|
+
Lint/TripleQuotes: # new in 1.9
|
50
|
+
Enabled: true
|
51
|
+
Lint/UnexpectedBlockArity: # new in 1.5
|
52
|
+
Enabled: true
|
53
|
+
Lint/UnmodifiedReduceAccumulator: # new in 1.1
|
54
|
+
Enabled: true
|
55
|
+
Security/IoMethods: # new in 1.22
|
56
|
+
Enabled: true
|
57
|
+
Style/ArgumentsForwarding: # new in 1.1
|
58
|
+
Enabled: true
|
59
|
+
Style/CollectionCompact: # new in 1.2
|
60
|
+
Enabled: true
|
61
|
+
Style/DocumentDynamicEvalDefinition: # new in 1.1
|
62
|
+
Enabled: true
|
63
|
+
Style/EndlessMethod: # new in 1.8
|
64
|
+
Enabled: true
|
65
|
+
Style/HashConversion: # new in 1.10
|
66
|
+
Enabled: true
|
67
|
+
Style/HashExcept: # new in 1.7
|
68
|
+
Enabled: true
|
69
|
+
Style/IfWithBooleanLiteralBranches: # new in 1.9
|
70
|
+
Enabled: true
|
71
|
+
Style/InPatternThen: # new in 1.16
|
72
|
+
Enabled: true
|
73
|
+
Style/MultilineInPatternThen: # new in 1.16
|
74
|
+
Enabled: true
|
75
|
+
Style/NegatedIfElseCondition: # new in 1.2
|
76
|
+
Enabled: true
|
77
|
+
Style/NilLambda: # new in 1.3
|
78
|
+
Enabled: true
|
79
|
+
Style/NumberedParameters: # new in 1.22
|
80
|
+
Enabled: true
|
81
|
+
Style/NumberedParametersLimit: # new in 1.22
|
82
|
+
Enabled: true
|
83
|
+
Style/QuotedSymbols: # new in 1.16
|
84
|
+
Enabled: true
|
85
|
+
Style/RedundantArgument: # new in 1.4
|
86
|
+
Enabled: true
|
87
|
+
Style/RedundantSelfAssignmentBranch: # new in 1.19
|
88
|
+
Enabled: true
|
89
|
+
Style/SelectByRegexp: # new in 1.22
|
90
|
+
Enabled: true
|
91
|
+
Style/StringChars: # new in 1.12
|
92
|
+
Enabled: true
|
93
|
+
Style/StringLiterals:
|
94
|
+
Enabled: true
|
95
|
+
EnforcedStyle: double_quotes
|
96
|
+
Style/StringLiteralsInInterpolation:
|
97
|
+
Enabled: true
|
98
|
+
EnforcedStyle: double_quotes
|
99
|
+
Style/SwapValues: # new in 1.1
|
100
|
+
Enabled: true
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2021-10-23 07:00:27 UTC using RuboCop version 1.22.2.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
8
|
+
|
9
|
+
# Offense count: 1
|
10
|
+
# Configuration parameters: IgnoredMethods, CountRepeatedAttributes.
|
11
|
+
Metrics/AbcSize:
|
12
|
+
Max: 27
|
13
|
+
|
14
|
+
# Offense count: 7
|
15
|
+
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
|
16
|
+
# IgnoredMethods: refine
|
17
|
+
Metrics/BlockLength:
|
18
|
+
Max: 58
|
19
|
+
|
20
|
+
# Offense count: 1
|
21
|
+
# Configuration parameters: IgnoredMethods.
|
22
|
+
Metrics/CyclomaticComplexity:
|
23
|
+
Max: 11
|
24
|
+
|
25
|
+
# Offense count: 2
|
26
|
+
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
|
27
|
+
Metrics/MethodLength:
|
28
|
+
Max: 25
|
29
|
+
|
30
|
+
# Offense count: 1
|
31
|
+
# Configuration parameters: IgnoredMethods.
|
32
|
+
Metrics/PerceivedComplexity:
|
33
|
+
Max: 11
|
34
|
+
|
35
|
+
# Offense count: 2
|
36
|
+
# Cop supports --auto-correct.
|
37
|
+
Style/EvalWithLocation:
|
38
|
+
Exclude:
|
39
|
+
- 'spec/shiftable/single_spec.rb'
|
40
|
+
|
41
|
+
# Offense count: 2
|
42
|
+
# Cop supports --auto-correct.
|
43
|
+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
|
44
|
+
# URISchemes: http, https
|
45
|
+
Layout/LineLength:
|
46
|
+
Max: 130
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders 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, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at peter.boling@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Peter Boling
|
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,116 @@
|
|
1
|
+
# Shiftable
|
2
|
+
|
3
|
+
Do your Spaceships belong to Captains, but sometimes a Captain will retire, and you need to reassign the spaceship?
|
4
|
+
|
5
|
+
We've all been there. This gem provides structure around the process of "shifting" your records from one associated
|
6
|
+
record to a new record.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'shiftable'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle install
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install shiftable
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
You are a spaceship captain (who isn't?!) so you have a spaceship (duh!)
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
|
30
|
+
class Captain < ActiveRecord::Base
|
31
|
+
has_one :spaceship
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Spaceships belong to the Captain.
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
|
39
|
+
class Spaceship < ActiveRecord::Base
|
40
|
+
belongs_to :captain
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
But because you can't afford fuel, and this dystopian future continues to burn carbon, in episode 11, you need to sell
|
45
|
+
the spaceship to your arch-nemesis Captain Sturgle.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
|
49
|
+
class Captain < ActiveRecord::Base
|
50
|
+
has_one :spaceship
|
51
|
+
|
52
|
+
def sell_spaceship_to(nemesis_captain)
|
53
|
+
Spaceship.shift_single(shift_to: nemesis_captain, shift_from: self)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
58
|
+
But how can you accomplish this? If you used the `shiftable` gem, won't take but a line(s) of code...
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
|
62
|
+
class Spaceship < ActiveRecord::Base
|
63
|
+
belongs_to :captain
|
64
|
+
extend Shiftable::Single.new(
|
65
|
+
belongs_to: :captain,
|
66
|
+
has_one: :spaceship,
|
67
|
+
preflight_checks: true,
|
68
|
+
before_shift: ->(shifting:, shift_to:, shift_from:) { shifting.ownership_changes += 1 }
|
69
|
+
)
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
### Multiple association on a single class
|
74
|
+
|
75
|
+
What if the Captain is really all that though? What if the captain and the spaceship have a boss... the space
|
76
|
+
federation!
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
|
80
|
+
class Spaceship < ActiveRecord::Base
|
81
|
+
belongs_to :captain
|
82
|
+
belongs_to :space_federation
|
83
|
+
end
|
84
|
+
|
85
|
+
class SpaceFederation < ActiveRecord::Base
|
86
|
+
has_many :captains
|
87
|
+
has_many :spaceships
|
88
|
+
|
89
|
+
def sell_spaceship_to(nemesis_federation)
|
90
|
+
Spaceship.shift_cx(shift_to: nemesis_federation, shift_from: self)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
... stay tuned!
|
96
|
+
|
97
|
+
## Development
|
98
|
+
|
99
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
|
100
|
+
also run `bin/console` for an interactive prompt that will allow you to experiment.
|
101
|
+
|
102
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
|
103
|
+
version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
|
104
|
+
push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
105
|
+
|
106
|
+
## Contributing
|
107
|
+
|
108
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/pboling/shiftable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/pboling/shiftable/blob/master/CODE_OF_CONDUCT.md).
|
109
|
+
|
110
|
+
## License
|
111
|
+
|
112
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
113
|
+
|
114
|
+
## Code of Conduct
|
115
|
+
|
116
|
+
Everyone interacting in the Shiftable project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/pboling/shiftable/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "bundler/setup"
|
5
|
+
require "shiftable"
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require "irb"
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# Usage:
|
5
|
+
#
|
6
|
+
# class BlasterRounds < ActiveRecord::Base
|
7
|
+
# belongs_to :space_federation
|
8
|
+
# extend Shiftable::Collection.new(
|
9
|
+
# belongs_to: :space_federation,
|
10
|
+
# has_many: :blaster_rounds,
|
11
|
+
# method_prefix: "banana"
|
12
|
+
# )
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
module Shiftable
|
16
|
+
# Inheriting from Module is a powerful pattern. If you like it checkout the debug_logging gem!
|
17
|
+
class Collection < Module
|
18
|
+
def initialize(belongs_to:, has_many:, method_prefix: nil, preflight_checks: true, before_save: nil)
|
19
|
+
super()
|
20
|
+
raise ArgumentError, "belongs_to must be a symbol" unless belongs_to.is_a?(Symbol)
|
21
|
+
raise ArgumentError, "has_many must be a symbol" unless has_many.is_a?(Symbol)
|
22
|
+
|
23
|
+
# For the following, imagine you are a Spaceship Captain, the Spaceship belongs_to you, and it has only one Captain.
|
24
|
+
# But you have to sell it to your nemesis!
|
25
|
+
#
|
26
|
+
# The name of the belongs_to association, defined on the shifting model, e.g. Spaceship
|
27
|
+
# Normally a camel-cased, symbolized, version of the class name.
|
28
|
+
# In the case where Spaceship belongs_to: :captain, this is :captain.
|
29
|
+
@belongs_to = belongs_to
|
30
|
+
|
31
|
+
# The name of the has_one association, defined on the shift_to/shift_from model, e.g. Captain.
|
32
|
+
# Normally a camel-cased, symbolized, version of the class name.
|
33
|
+
# In the case where Captain has_one: :spaceship, this is :spaceship.
|
34
|
+
@has_many = has_many
|
35
|
+
|
36
|
+
@method_prefix = method_prefix
|
37
|
+
|
38
|
+
# will prevent the save if it returns false
|
39
|
+
# allows for any custom logic to be run, such as setting shift_from attributes, prior to the shift is saved.
|
40
|
+
@before_save = before_save
|
41
|
+
end
|
42
|
+
|
43
|
+
def extended(base)
|
44
|
+
shift_cx_modulizer = ShiftCollectionModulizer.to_mod(@belongs_to, @has_many, @method_prefix,
|
45
|
+
@before_save)
|
46
|
+
base.singleton_class.send(:prepend, shift_cx_modulizer)
|
47
|
+
end
|
48
|
+
|
49
|
+
def included(base)
|
50
|
+
shift_cx_modulizer = ShiftCollectionModulizer.to_mod(@belongs_to, @has_many, @method_prefix,
|
51
|
+
@before_save)
|
52
|
+
base.send(:prepend, shift_cx_modulizer)
|
53
|
+
end
|
54
|
+
|
55
|
+
# Creates anonymous Ruby Modules, containing dynamically built methods
|
56
|
+
module ShiftCollectionModulizer
|
57
|
+
def to_mod(belongs_to, has_many, mepr, before_save)
|
58
|
+
Module.new do
|
59
|
+
define_method(:"#{mepr}shift_cx_column") do
|
60
|
+
reflection = reflect_on_association(belongs_to).klass.reflect_on_association(has_many)
|
61
|
+
reflection.foreign_key
|
62
|
+
end
|
63
|
+
define_method(:"#{mepr}shift_cx_relation") do |id|
|
64
|
+
return nil unless id
|
65
|
+
|
66
|
+
where(send("#{mepr}shift_cx_column") => id)
|
67
|
+
end
|
68
|
+
define_method(:"#{mepr}shift_cx_safe_relation") do |shift_to:, shift_from:|
|
69
|
+
return false if shift_from&.id.nil?
|
70
|
+
return false if shift_to&.id.nil?
|
71
|
+
|
72
|
+
send("#{mepr}shift_cx_relation", shift_from.id)
|
73
|
+
end
|
74
|
+
define_method(:"#{mepr}shift_cx") do |shift_to:, shift_from:|
|
75
|
+
shifting_rel = send("#{mepr}shift_cx_safe_relation", shift_to: shift_to, shift_from: shift_from)
|
76
|
+
return false unless shifting_rel && shifting_rel.any?
|
77
|
+
|
78
|
+
shifting_rel.each do |shifting|
|
79
|
+
shifting.send("#{send("#{mepr}shift_cx_column")}=", shift_to.id)
|
80
|
+
end
|
81
|
+
before_save.call(shifting_rel: shifting_rel, shift_to: shift_to, shift_from: shift_from) if before_save
|
82
|
+
shifting_rel.each do |shifting|
|
83
|
+
shifting.save
|
84
|
+
end
|
85
|
+
shifting_rel
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
module_function :to_mod
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Targets classes that have:
|
4
|
+
# 1. belongs_to association with a shift_to Model, e.g. Spaceship belongs to Captain, and
|
5
|
+
# 2. the shift_to Model, e.g. Captain, has_one Spaceship record
|
6
|
+
# When the shift_to records (e.g. people) are merged,
|
7
|
+
# these belongs_to/has_one associations must be carried over.
|
8
|
+
#
|
9
|
+
# Usage:
|
10
|
+
#
|
11
|
+
# class Spaceship < ActiveRecord::Base
|
12
|
+
# belongs_to :captain
|
13
|
+
# extend Shiftable::Single.new(
|
14
|
+
# belongs_to: :captain,
|
15
|
+
# has_one: :spaceship,
|
16
|
+
# preflight_checks: true,
|
17
|
+
# before_shift: ->(shifting:, shift_to:, shift_from:) { shifting.ownership_changes += 1 }
|
18
|
+
# )
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
module Shiftable
|
22
|
+
# Inheriting from Module is a powerful pattern. If you like it checkout the debug_logging gem!
|
23
|
+
class Single < Module
|
24
|
+
def initialize(belongs_to:, has_one:, method_prefix: nil, preflight_checks: true, before_save: nil)
|
25
|
+
super()
|
26
|
+
raise ArgumentError, "belongs_to must be a symbol" unless belongs_to.is_a?(Symbol)
|
27
|
+
raise ArgumentError, "has_one must be a symbol" unless has_one.is_a?(Symbol)
|
28
|
+
|
29
|
+
# For the following, imagine you are a Spaceship Captain, the Spaceship belongs_to you, and it has only one Captain.
|
30
|
+
# But you have to sell it to your nemesis!
|
31
|
+
#
|
32
|
+
# The name of the belongs_to association, defined on the shifting model, e.g. Spaceship
|
33
|
+
# Normally a camel-cased, symbolized, version of the class name.
|
34
|
+
# In the case where Spaceship belongs_to: :captain, this is :captain.
|
35
|
+
@belongs_to = belongs_to
|
36
|
+
|
37
|
+
# The name of the has_one association, defined on the shift_to/shift_from model, e.g. Captain.
|
38
|
+
# Normally a camel-cased, symbolized, version of the class name.
|
39
|
+
# In the case where Captain has_one: :spaceship, this is :spaceship.
|
40
|
+
@has_one = has_one
|
41
|
+
|
42
|
+
@method_prefix = method_prefix
|
43
|
+
|
44
|
+
# Do not move record if a record already exists (we are shifting a "has_one" association, after all)
|
45
|
+
@preflight_checks = preflight_checks
|
46
|
+
|
47
|
+
# will prevent the save if it returns false
|
48
|
+
# allows for any custom logic to be run, such as setting shift_from attributes, prior to the shift is saved.
|
49
|
+
@before_save = before_save
|
50
|
+
end
|
51
|
+
|
52
|
+
def extended(base)
|
53
|
+
shift_single_modulizer = ShiftSingleModulizer.to_mod(@belongs_to, @has_one, @method_prefix, @preflight_checks,
|
54
|
+
@before_save)
|
55
|
+
base.singleton_class.send(:prepend, shift_single_modulizer)
|
56
|
+
end
|
57
|
+
|
58
|
+
def included(base)
|
59
|
+
shift_single_modulizer = ShiftSingleModulizer.to_mod(@belongs_to, @has_one, @method_prefix, @preflight_checks,
|
60
|
+
@before_save)
|
61
|
+
base.send(:prepend, shift_single_modulizer)
|
62
|
+
end
|
63
|
+
|
64
|
+
# Creates anonymous Ruby Modules, containing dynamically built methods
|
65
|
+
module ShiftSingleModulizer
|
66
|
+
def to_mod(belongs_to, has_one, mepr, preflight_checks, before_save)
|
67
|
+
Module.new do
|
68
|
+
define_method(:"#{mepr}shift_column") do
|
69
|
+
reflection = reflect_on_association(belongs_to).klass.reflect_on_association(has_one)
|
70
|
+
reflection.foreign_key
|
71
|
+
end
|
72
|
+
define_method(:"#{mepr}shift_find") do |id|
|
73
|
+
return nil unless id
|
74
|
+
|
75
|
+
find_by(send("#{mepr}shift_column") => id)
|
76
|
+
end
|
77
|
+
define_method(:"#{mepr}shift_safe_find") do |shift_to:, shift_from:|
|
78
|
+
return false if shift_from&.id.nil?
|
79
|
+
return false if shift_to&.id.nil?
|
80
|
+
|
81
|
+
if preflight_checks
|
82
|
+
already_exists = shift_to.send(has_one)
|
83
|
+
return false if already_exists
|
84
|
+
end
|
85
|
+
|
86
|
+
send("#{mepr}shift_find", shift_from.id)
|
87
|
+
end
|
88
|
+
define_method(:"#{mepr}shift_single") do |shift_to:, shift_from:|
|
89
|
+
shifting = send("#{mepr}shift_safe_find", shift_to: shift_to, shift_from: shift_from)
|
90
|
+
return false unless shifting
|
91
|
+
|
92
|
+
shifting.send("#{send("#{mepr}shift_column")}=", shift_to.id)
|
93
|
+
shifting.save if before_save.nil? || before_save.call(shifting: shifting, shift_to: shift_to, shift_from: shift_from)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
module_function :to_mod
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
data/lib/shiftable.rb
ADDED
metadata
ADDED
@@ -0,0 +1,286 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shiftable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Peter Boling
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-10-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: byebug
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: database_cleaner-active_record
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: factory_bot
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: faker
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '13'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '13'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rspec-block_is_expected
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rubocop
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '1.2'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '1.2'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: rubocop-md
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: rubocop-performance
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - ">="
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '0'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - ">="
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '0'
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: rubocop-rake
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - ">="
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '0'
|
174
|
+
type: :development
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - ">="
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '0'
|
181
|
+
- !ruby/object:Gem::Dependency
|
182
|
+
name: rubocop-rspec
|
183
|
+
requirement: !ruby/object:Gem::Requirement
|
184
|
+
requirements:
|
185
|
+
- - "~>"
|
186
|
+
- !ruby/object:Gem::Version
|
187
|
+
version: '2.0'
|
188
|
+
type: :development
|
189
|
+
prerelease: false
|
190
|
+
version_requirements: !ruby/object:Gem::Requirement
|
191
|
+
requirements:
|
192
|
+
- - "~>"
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
version: '2.0'
|
195
|
+
- !ruby/object:Gem::Dependency
|
196
|
+
name: simplecov
|
197
|
+
requirement: !ruby/object:Gem::Requirement
|
198
|
+
requirements:
|
199
|
+
- - ">="
|
200
|
+
- !ruby/object:Gem::Version
|
201
|
+
version: '0'
|
202
|
+
type: :development
|
203
|
+
prerelease: false
|
204
|
+
version_requirements: !ruby/object:Gem::Requirement
|
205
|
+
requirements:
|
206
|
+
- - ">="
|
207
|
+
- !ruby/object:Gem::Version
|
208
|
+
version: '0'
|
209
|
+
- !ruby/object:Gem::Dependency
|
210
|
+
name: sqlite3
|
211
|
+
requirement: !ruby/object:Gem::Requirement
|
212
|
+
requirements:
|
213
|
+
- - ">="
|
214
|
+
- !ruby/object:Gem::Version
|
215
|
+
version: '0'
|
216
|
+
type: :development
|
217
|
+
prerelease: false
|
218
|
+
version_requirements: !ruby/object:Gem::Requirement
|
219
|
+
requirements:
|
220
|
+
- - ">="
|
221
|
+
- !ruby/object:Gem::Version
|
222
|
+
version: '0'
|
223
|
+
- !ruby/object:Gem::Dependency
|
224
|
+
name: yard
|
225
|
+
requirement: !ruby/object:Gem::Requirement
|
226
|
+
requirements:
|
227
|
+
- - ">="
|
228
|
+
- !ruby/object:Gem::Version
|
229
|
+
version: 0.9.20
|
230
|
+
type: :development
|
231
|
+
prerelease: false
|
232
|
+
version_requirements: !ruby/object:Gem::Requirement
|
233
|
+
requirements:
|
234
|
+
- - ">="
|
235
|
+
- !ruby/object:Gem::Version
|
236
|
+
version: 0.9.20
|
237
|
+
description: Move single records (has_one) or collections (has_many) from one parent
|
238
|
+
(belongs_to) to another
|
239
|
+
email:
|
240
|
+
- peter.boling@gmail.com
|
241
|
+
executables: []
|
242
|
+
extensions: []
|
243
|
+
extra_rdoc_files: []
|
244
|
+
files:
|
245
|
+
- ".rspec"
|
246
|
+
- ".rubocop.yml"
|
247
|
+
- ".rubocop_todo.yml"
|
248
|
+
- CHANGELOG.md
|
249
|
+
- CODE_OF_CONDUCT.md
|
250
|
+
- Gemfile
|
251
|
+
- LICENSE.txt
|
252
|
+
- README.md
|
253
|
+
- Rakefile
|
254
|
+
- bin/console
|
255
|
+
- bin/setup
|
256
|
+
- lib/shiftable.rb
|
257
|
+
- lib/shiftable/collection.rb
|
258
|
+
- lib/shiftable/single.rb
|
259
|
+
- lib/shiftable/version.rb
|
260
|
+
homepage: https://railsbling.com/tags/shiftable
|
261
|
+
licenses:
|
262
|
+
- MIT
|
263
|
+
metadata:
|
264
|
+
homepage_uri: https://railsbling.com/tags/shiftable
|
265
|
+
source_code_uri: https://github.com/pboling/shiftable
|
266
|
+
changelog_uri: https://github.com/pboling/shiftable/blob/main/CHANGELOG.md
|
267
|
+
post_install_message:
|
268
|
+
rdoc_options: []
|
269
|
+
require_paths:
|
270
|
+
- lib
|
271
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
272
|
+
requirements:
|
273
|
+
- - ">="
|
274
|
+
- !ruby/object:Gem::Version
|
275
|
+
version: 2.6.0
|
276
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
277
|
+
requirements:
|
278
|
+
- - ">="
|
279
|
+
- !ruby/object:Gem::Version
|
280
|
+
version: '0'
|
281
|
+
requirements: []
|
282
|
+
rubygems_version: 3.2.22
|
283
|
+
signing_key:
|
284
|
+
specification_version: 4
|
285
|
+
summary: Easily shift record associations
|
286
|
+
test_files: []
|