acts_as_belongable 2.1.2 → 3.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 +4 -4
- data/LICENSE +21 -21
- data/README.md +203 -216
- data/app/models/concerns/acts_as_belongable/belongable.rb +63 -52
- data/app/models/concerns/acts_as_belongable/belonger.rb +63 -52
- data/lib/acts_as_belongable.rb +4 -4
- data/lib/acts_as_belongable/engine.rb +4 -2
- data/lib/acts_as_belongable/railtie.rb +9 -9
- data/lib/acts_as_belongable/version.rb +5 -5
- data/lib/generators/acts_as_belongable_generator.rb +27 -25
- data/lib/generators/templates/README.md +1 -1
- data/lib/generators/templates/migration.rb.erb +10 -10
- data/lib/generators/templates/model.rb +5 -5
- metadata +35 -25
- data/CHANGELOG.md +0 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7aca36fe4eeefedfea025f84b497f6153c8582b4cb5df5d2708da5739356a312
|
4
|
+
data.tar.gz: bf8ddc58424da5e420b7daf867dd6148c1f5af1a9ed7e501e10e1a001004e288
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5970fcb586b055d9cb73b0ce9f8a9d033137840bc85def79bdb4f7d1c358c88635d12dd6e2911e0077c9af1a8fa691d863fdd9200cd3990d4eeba22035335ee5
|
7
|
+
data.tar.gz: b8a1acfeb32af6dbff8293cac790fd9b9c8e23d1c062a3317898644107a8cc12b7cc6c823d4b03db1d41e6f4e1feb5bed7a64a5f0328e716c26f1681c2850af5
|
data/LICENSE
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
MIT License
|
2
|
-
|
3
|
-
Copyright (c) 2018 Jonas Hübotter
|
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 all
|
13
|
-
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 THE
|
21
|
-
SOFTWARE.
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2018 Jonas Hübotter
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -1,216 +1,203 @@
|
|
1
|
-
# acts_as_belongable
|
2
|
-
|
3
|
-
[](https://badge.fury.io/rb/acts_as_belongable)
|
4
|
-
|
5
|
-
acts_as_belongable is a Rubygem that provides an associations engine for Rails apps. It's primary use case is to simplify `has_many through: ...` relations.
|
6
|
-
|
7
|
-
---
|
8
|
-
|
9
|
-
## Table of Contents
|
10
|
-
|
11
|
-
* [Installation](#installation)
|
12
|
-
* [Usage](#usage)
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
* [
|
19
|
-
* [
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
c.add_belongable Event.first
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
c.create_belongable
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
c.add_to_belonger Conference.first
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
c.create_belonger
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
copies of the Software, and to permit persons to whom the Software is
|
205
|
-
furnished to do so, subject to the following conditions:
|
206
|
-
|
207
|
-
The above copyright notice and this permission notice shall be included in all
|
208
|
-
copies or substantial portions of the Software.
|
209
|
-
|
210
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
211
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
212
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
213
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
214
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
215
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
216
|
-
SOFTWARE.
|
1
|
+
# acts_as_belongable
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/acts_as_belongable) 
|
4
|
+
|
5
|
+
acts_as_belongable is a Rubygem that provides an associations engine for Rails apps. It's primary use case is to simplify `has_many through: ...` relations.
|
6
|
+
|
7
|
+
---
|
8
|
+
|
9
|
+
## Table of Contents
|
10
|
+
|
11
|
+
* [Installation](#installation)
|
12
|
+
* [Usage](#usage)
|
13
|
+
* [Associations](#associations)
|
14
|
+
* [acts_as_belonger](#acts_as_belonger)
|
15
|
+
* [acts_as_belongable](#acts_as_belongable)
|
16
|
+
* [acts_as_list](#acts_as_list)
|
17
|
+
* [Scopes](#scopes)
|
18
|
+
* [Testing](#testing)
|
19
|
+
* [To Do](#to-do)
|
20
|
+
* [Contributing](#contributing)
|
21
|
+
* [Semantic versioning](#semantic-versioning)
|
22
|
+
|
23
|
+
---
|
24
|
+
|
25
|
+
## Installation
|
26
|
+
|
27
|
+
acts_as_belongable works with Rails 5.0 onwards. You can add it to your `Gemfile` with:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
gem 'acts_as_belongable'
|
31
|
+
```
|
32
|
+
|
33
|
+
And then execute:
|
34
|
+
|
35
|
+
$ bundle
|
36
|
+
|
37
|
+
Or install it yourself as:
|
38
|
+
|
39
|
+
$ gem install acts_as_belongable
|
40
|
+
|
41
|
+
If you always want to be up to date fetch the latest from GitHub in your `Gemfile`:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
gem 'acts_as_belongable', github: 'jonhue/acts_as_belongable'
|
45
|
+
```
|
46
|
+
|
47
|
+
Now run the generator:
|
48
|
+
|
49
|
+
$ rails g acts_as_belongable
|
50
|
+
|
51
|
+
To wrap things up, migrate the changes into your database:
|
52
|
+
|
53
|
+
$ rails db:migrate
|
54
|
+
|
55
|
+
---
|
56
|
+
|
57
|
+
## Usage
|
58
|
+
|
59
|
+
### Associations
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
class User < ApplicationRecord
|
63
|
+
acts_as_belongable
|
64
|
+
belongable :attending, 'Event'
|
65
|
+
end
|
66
|
+
|
67
|
+
class Event < ApplicationRecord
|
68
|
+
acts_as_belonger
|
69
|
+
acts_as_belongable
|
70
|
+
belonger :attendees, 'User'
|
71
|
+
belongable :conferences, 'Conference'
|
72
|
+
end
|
73
|
+
|
74
|
+
class Conference < ApplicationRecord
|
75
|
+
acts_as_belonger
|
76
|
+
belonger :events, 'Event'
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
### acts_as_belonger
|
81
|
+
|
82
|
+
`acts_as_belonger` makes the following methods available:
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
conference = Conference.first
|
86
|
+
|
87
|
+
# Returns all belongables associated with this record
|
88
|
+
conference.belongables
|
89
|
+
|
90
|
+
# Adds a belongable to this record
|
91
|
+
conference.add_belongable(Event.first)
|
92
|
+
# c.add_belongable! Event.first
|
93
|
+
|
94
|
+
# Creates a belongable record and adds it to this record
|
95
|
+
conference.create_belongable(Event, options)
|
96
|
+
# c.create_belongable! Event, options
|
97
|
+
```
|
98
|
+
|
99
|
+
### acts_as_belongable
|
100
|
+
|
101
|
+
`acts_as_belongable` makes the following methods available:
|
102
|
+
|
103
|
+
```ruby
|
104
|
+
event = Event.first
|
105
|
+
|
106
|
+
# Returns all belongers associated with this record
|
107
|
+
event.belongers
|
108
|
+
|
109
|
+
# Adds this record to a belonger
|
110
|
+
conference.add_to_belonger(Conference.first)
|
111
|
+
# c.add_to_belonger! Conference.first
|
112
|
+
|
113
|
+
# Adds this record to a newly created belonger
|
114
|
+
conference.create_belonger(Conference, options)
|
115
|
+
# c.create_belonger! Conference, options
|
116
|
+
```
|
117
|
+
|
118
|
+
### acts_as_list
|
119
|
+
|
120
|
+
acts_as_belongable integrates with [acts_as_list](). It adds a `position` column to `Belonging`:
|
121
|
+
|
122
|
+
```ruby
|
123
|
+
conference.add_belongable(Event.first, position: 1)
|
124
|
+
```
|
125
|
+
|
126
|
+
### Scopes
|
127
|
+
|
128
|
+
You can use scopes to add details to an relation:
|
129
|
+
|
130
|
+
```ruby
|
131
|
+
user = User.first
|
132
|
+
event = user.create_belongable(Event)
|
133
|
+
conference = user.create_belongable(Conference)
|
134
|
+
user = User.last
|
135
|
+
user.add_belongable(Event, scope: 'collaboration')
|
136
|
+
user.add_belongable(Conference, scope: 'collaboration')
|
137
|
+
|
138
|
+
# Get all belongables with a specific scope
|
139
|
+
user.belongables_with_scope(:collaboration)
|
140
|
+
|
141
|
+
# Get `Event` belongables with a specific scope
|
142
|
+
user.belongables_with_scope(:collaboration, belongable_type: 'Event')
|
143
|
+
|
144
|
+
# Get all belongers with a specific scope
|
145
|
+
event.belongers_with_scope(:collaboration)
|
146
|
+
|
147
|
+
# Get `User` belongers with a specific scope
|
148
|
+
event.belongers_with_scope(:collaboration, belonger_type: 'User')
|
149
|
+
```
|
150
|
+
|
151
|
+
You are also able to restrict associations to specific scopes:
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
class User < ApplicationRecord
|
155
|
+
acts_as_belonger
|
156
|
+
belonger :conference_collaborations, 'Conference', scope: :collaboration
|
157
|
+
belonger :conference_attendings, 'Conference', scope: :membership
|
158
|
+
end
|
159
|
+
|
160
|
+
class Conference < ApplicationRecord
|
161
|
+
acts_as_belongable
|
162
|
+
belongable :collaborators, 'User', scope: :collaboration
|
163
|
+
belongable :attendees, 'User', scope: :membership
|
164
|
+
end
|
165
|
+
```
|
166
|
+
|
167
|
+
---
|
168
|
+
|
169
|
+
## Testing
|
170
|
+
|
171
|
+
1. Fork this repository
|
172
|
+
2. Clone your forked git locally
|
173
|
+
3. Install dependencies
|
174
|
+
|
175
|
+
`$ bundle install`
|
176
|
+
|
177
|
+
4. Run specs
|
178
|
+
|
179
|
+
`$ bundle exec rspec`
|
180
|
+
|
181
|
+
5. Run RuboCop
|
182
|
+
|
183
|
+
`$ bundle exec rubocop`
|
184
|
+
|
185
|
+
---
|
186
|
+
|
187
|
+
## To Do
|
188
|
+
|
189
|
+
We use [GitHub projects](https://github.com/jonhue/acts_as_belongable/projects/1) to coordinate the work on this project.
|
190
|
+
|
191
|
+
To propose your ideas, initiate the discussion by adding a [new issue](https://github.com/jonhue/acts_as_belongable/issues/new).
|
192
|
+
|
193
|
+
---
|
194
|
+
|
195
|
+
## Contributing
|
196
|
+
|
197
|
+
We hope that you will consider contributing to acts_as_belongable. Please read this short overview for some information about how to get started:
|
198
|
+
|
199
|
+
[Learn more about contributing to this repository](CONTRIBUTING.md), [Code of Conduct](CODE_OF_CONDUCT.md)
|
200
|
+
|
201
|
+
### Semantic Versioning
|
202
|
+
|
203
|
+
acts_as_belongable follows Semantic Versioning 2.0 as defined at http://semver.org.
|
@@ -1,52 +1,63 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActsAsBelongable
|
4
|
+
module Belongable
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def acts_as_belongable
|
9
|
+
has_many :belongers, through: :belongable_belongings, source: :belonger
|
10
|
+
has_many :belongable_belongings, as: :belongable,
|
11
|
+
class_name: 'Belonging',
|
12
|
+
dependent: :destroy
|
13
|
+
end
|
14
|
+
|
15
|
+
def belongable(name, source_type, options = {})
|
16
|
+
if options.key?(:scope)
|
17
|
+
belongable_belongings_with_scope(options[:scope])
|
18
|
+
through = "belongable_belongings_#{options[:scope]}".to_sym
|
19
|
+
end
|
20
|
+
|
21
|
+
has_many name.to_sym, through: through || :belongable_belongings,
|
22
|
+
source: :belonger,
|
23
|
+
source_type: source_type
|
24
|
+
end
|
25
|
+
|
26
|
+
def belongable_belongings_with_scope(scope)
|
27
|
+
# rubocop:disable Metrics/LineLength
|
28
|
+
has_many "belongable_belongings_#{scope}".to_sym, -> { where(scope: scope) }, as: :belongable, class_name: 'Belonging', dependent: :destroy
|
29
|
+
# rubocop:enable Metrics/LineLength
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def add_to_belonger(belonger, options = {})
|
34
|
+
options[:belonger] = belonger
|
35
|
+
belongable_belongings.create(options)
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_to_belonger!(belonger, options = {})
|
39
|
+
options[:belonger] = belonger
|
40
|
+
belongable_belongings.create!(options)
|
41
|
+
end
|
42
|
+
|
43
|
+
def create_belonger(klass, options = {})
|
44
|
+
belonging_options = options.delete(:belonging) || {}
|
45
|
+
object = klass.create(options)
|
46
|
+
add_to_belonger(object, belonging_options)
|
47
|
+
object
|
48
|
+
end
|
49
|
+
|
50
|
+
def create_belonger!(klass, options = {})
|
51
|
+
belonging_options = options.delete(:belonging) || {}
|
52
|
+
object = klass.create!(options)
|
53
|
+
add_to_belonger!(object, belonging_options)
|
54
|
+
object
|
55
|
+
end
|
56
|
+
|
57
|
+
def belongers_with_scope(scope, source_type: nil)
|
58
|
+
return belongable_belongings.where(scope: scope) if source_type.nil?
|
59
|
+
|
60
|
+
belongable_belongings.where(scope: scope, source_type: source_type)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -1,52 +1,63 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActsAsBelongable
|
4
|
+
module Belonger
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def acts_as_belonger
|
9
|
+
has_many :belongables, through: :belonger_belongings,
|
10
|
+
source: :belongable
|
11
|
+
has_many :belonger_belongings, as: :belonger, class_name: 'Belonging',
|
12
|
+
dependent: :destroy
|
13
|
+
end
|
14
|
+
|
15
|
+
def belonger(name, source_type, options = {})
|
16
|
+
if options.key?(:scope)
|
17
|
+
belonger_belongings_with_scope(options[:scope])
|
18
|
+
through = "belonger_belongings_#{options[:scope]}".to_sym
|
19
|
+
end
|
20
|
+
|
21
|
+
has_many name.to_sym, through: through || :belonger_belongings,
|
22
|
+
source: :belongable,
|
23
|
+
source_type: source_type
|
24
|
+
end
|
25
|
+
|
26
|
+
def belonger_belongings_with_scope(scope)
|
27
|
+
# rubocop:disable Metrics/LineLength
|
28
|
+
has_many "belonger_belongings_#{scope}".to_sym, -> { where(scope: scope) }, as: :belonger, class_name: 'Belonging', dependent: :destroy
|
29
|
+
# rubocop:enable Metrics/LineLength
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def add_belongable(belongable, options = {})
|
34
|
+
options[:belongable] = belongable
|
35
|
+
belonger_belongings.create(options)
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_belongable!(belongable, options = {})
|
39
|
+
options[:belongable] = belongable
|
40
|
+
belonger_belongings.create!(options)
|
41
|
+
end
|
42
|
+
|
43
|
+
def create_belongable(klass, options = {})
|
44
|
+
belonging_options = options.delete(:belonging) || {}
|
45
|
+
object = klass.create(options)
|
46
|
+
add_belongable(object, belonging_options)
|
47
|
+
object
|
48
|
+
end
|
49
|
+
|
50
|
+
def create_belongable!(klass, options = {})
|
51
|
+
belonging_options = options.delete(:belonging) || {}
|
52
|
+
object = klass.create!(options)
|
53
|
+
add_belongable!(object, belonging_options)
|
54
|
+
object
|
55
|
+
end
|
56
|
+
|
57
|
+
def belongables_with_scope(scope, source_type: nil)
|
58
|
+
return belonger_belongings.where(scope: scope) if source_type.nil?
|
59
|
+
|
60
|
+
belonger_belongings.where(scope: scope, source_type: source_type)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/lib/acts_as_belongable.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'acts_as_list'
|
2
4
|
require 'acts_as_belongable/version'
|
3
5
|
|
4
6
|
module ActsAsBelongable
|
5
|
-
|
6
|
-
|
7
|
-
require 'acts_as_belongable/engine'
|
8
|
-
|
7
|
+
require 'acts_as_belongable/railtie'
|
8
|
+
require 'acts_as_belongable/engine'
|
9
9
|
end
|
@@ -1,14 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'rails/railtie'
|
2
4
|
|
3
5
|
module ActsAsBelongable
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
6
|
+
class Railtie < Rails::Railtie
|
7
|
+
initializer 'acts_as_belongable.active_record' do
|
8
|
+
ActiveSupport.on_load :active_record do
|
9
|
+
include ActsAsBelongable::Belonger
|
10
|
+
include ActsAsBelongable::Belongable
|
11
|
+
end
|
13
12
|
end
|
13
|
+
end
|
14
14
|
end
|
@@ -1,5 +1,5 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActsAsBelongable
|
4
|
+
VERSION = '3.0.0'
|
5
|
+
end
|
@@ -1,39 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'rails/generators'
|
2
4
|
require 'rails/generators/migration'
|
3
5
|
|
4
6
|
class ActsAsBelongableGenerator < Rails::Generators::Base
|
7
|
+
include Rails::Generators::Migration
|
5
8
|
|
6
|
-
|
7
|
-
|
8
|
-
source_root File.join File.dirname(__FILE__), 'templates'
|
9
|
-
desc 'Install acts_as_belongable'
|
9
|
+
source_root File.join File.dirname(__FILE__), 'templates'
|
10
|
+
desc 'Install acts_as_belongable'
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
12
|
+
def self.next_migration_number(dirname)
|
13
|
+
if ActiveRecord::Base.timestamped_migrations
|
14
|
+
Time.now.utc.strftime '%Y%m%d%H%M%S'
|
15
|
+
else
|
16
|
+
format('%.3d', current_migration_number(dirname) + 1)
|
17
17
|
end
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
def create_migration_file
|
21
|
+
migration_template 'migration.rb.erb',
|
22
|
+
'db/migrate/acts_as_belongable_migration.rb',
|
23
|
+
migration_version: migration_version
|
24
|
+
end
|
22
25
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
+
def create_model
|
27
|
+
template 'model.rb', 'app/models/belonging.rb'
|
28
|
+
end
|
26
29
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
+
def show_readme
|
31
|
+
readme 'README.md'
|
32
|
+
end
|
30
33
|
|
31
|
-
|
34
|
+
private
|
32
35
|
|
33
|
-
|
34
|
-
|
35
|
-
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
36
|
-
end
|
37
|
-
end
|
36
|
+
def migration_version
|
37
|
+
return unless Rails.version >= '5.0.0'
|
38
38
|
|
39
|
+
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
40
|
+
end
|
39
41
|
end
|
@@ -1 +1 @@
|
|
1
|
-
Now run `rails db:migrate` to add acts_as_belongable to your database.
|
1
|
+
Now run `rails db:migrate` to add acts_as_belongable to your database.
|
@@ -1,15 +1,15 @@
|
|
1
|
-
|
2
|
-
def change
|
3
|
-
create_table :belongings do |t|
|
4
|
-
|
5
|
-
t.references :belonger, polymorphic: true, index: true
|
6
|
-
t.references :belongable, polymorphic: true, index: true
|
1
|
+
# frozen_string_literal: true
|
7
2
|
|
8
|
-
|
9
|
-
|
3
|
+
class ActsAsBelongableMigration < ActiveRecord::Migration<%= migration_version %>
|
4
|
+
def change
|
5
|
+
create_table :belongings do |t|
|
6
|
+
t.references :belonger, polymorphic: true, index: true
|
7
|
+
t.references :belongable, polymorphic: true, index: true
|
10
8
|
|
11
|
-
|
9
|
+
t.string :scope
|
10
|
+
t.integer :position
|
12
11
|
|
13
|
-
|
12
|
+
t.timestamps null: false
|
14
13
|
end
|
14
|
+
end
|
15
15
|
end
|
@@ -1,8 +1,8 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
acts_as_list :belonger
|
1
|
+
# frozen_string_literal: true
|
4
2
|
|
5
|
-
|
6
|
-
|
3
|
+
class Belonging < ApplicationRecord
|
4
|
+
acts_as_list :belonger
|
7
5
|
|
6
|
+
belongs_to :belonger, polymorphic: true
|
7
|
+
belongs_to :belongable, polymorphic: true
|
8
8
|
end
|
metadata
CHANGED
@@ -1,71 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_belongable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 3.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonas Hübotter
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-12-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: activerecord
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '5.
|
19
|
+
version: '5.2'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - "
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '5.
|
26
|
+
version: '5.2'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activesupport
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '5.
|
33
|
+
version: '5.2'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '5.
|
40
|
+
version: '5.2'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: acts_as_list
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '0.9'
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '0.9'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: railties
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '5.2'
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '5.2'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rspec
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -94,6 +94,20 @@ dependencies:
|
|
94
94
|
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0.52'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop-rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
97
111
|
description: 'acts_as_belongable is a Rubygem that provides an associations engine
|
98
112
|
for Rails apps. It''s primary use case is to simplify `has_many through: ...` relations.'
|
99
113
|
email: me@jonhue.me
|
@@ -101,7 +115,6 @@ executables: []
|
|
101
115
|
extensions: []
|
102
116
|
extra_rdoc_files: []
|
103
117
|
files:
|
104
|
-
- CHANGELOG.md
|
105
118
|
- LICENSE
|
106
119
|
- README.md
|
107
120
|
- app/models/concerns/acts_as_belongable/belongable.rb
|
@@ -118,10 +131,7 @@ homepage: https://github.com/jonhue/acts_as_belongable
|
|
118
131
|
licenses:
|
119
132
|
- MIT
|
120
133
|
metadata: {}
|
121
|
-
post_install_message:
|
122
|
-
**Thank you for installing acts_as_belongable!**
|
123
|
-
Get started by running `rails g acts_as_belongable`.
|
124
|
-
Learn more at https://github.com/jonhue/acts_as_belongable.
|
134
|
+
post_install_message:
|
125
135
|
rdoc_options: []
|
126
136
|
require_paths:
|
127
137
|
- lib
|
@@ -137,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
137
147
|
version: '0'
|
138
148
|
requirements: []
|
139
149
|
rubyforge_project:
|
140
|
-
rubygems_version: 2.7.
|
150
|
+
rubygems_version: 2.7.6
|
141
151
|
signing_key:
|
142
152
|
specification_version: 4
|
143
153
|
summary: Associations engine for Rails
|
data/CHANGELOG.md
DELETED
@@ -1,31 +0,0 @@
|
|
1
|
-
# Changelog
|
2
|
-
|
3
|
-
### master
|
4
|
-
|
5
|
-
* nothing yet
|
6
|
-
|
7
|
-
### 2.1.2 - 2018/05/24
|
8
|
-
|
9
|
-
* enhancements
|
10
|
-
* pass classes to methods instead of their names
|
11
|
-
* bugfixes
|
12
|
-
* fixed `undefined method '[]=' for nil:NilClass`
|
13
|
-
|
14
|
-
### 1.1.2 - 2018/02/05
|
15
|
-
|
16
|
-
* bugfixes
|
17
|
-
* require acts_as_list
|
18
|
-
|
19
|
-
### 1.1.1 - 2018/01/21
|
20
|
-
|
21
|
-
* bugfixes
|
22
|
-
* fixed naming error in generated migration files
|
23
|
-
|
24
|
-
### 1.1.0 - 2018/01/20
|
25
|
-
|
26
|
-
* features
|
27
|
-
* add scopes
|
28
|
-
|
29
|
-
### 1.0.0 - 2018/01/20
|
30
|
-
|
31
|
-
* initial release
|