acts_as_belongable 2.1.2 → 3.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b80c9a93bf4a2ab6180cc848d9fcf43084daeb88dd56bfc8995c2955eaae8cda
4
- data.tar.gz: 0e384ccdd8fc420f4e3263697aec4bfd067d426b741ffec420405808b71bcbca
3
+ metadata.gz: 7aca36fe4eeefedfea025f84b497f6153c8582b4cb5df5d2708da5739356a312
4
+ data.tar.gz: bf8ddc58424da5e420b7daf867dd6148c1f5af1a9ed7e501e10e1a001004e288
5
5
  SHA512:
6
- metadata.gz: ab7d4a5f9d99fc1eae0120f7eb96fc16f5b95146ce24c779a96bb73c53e52285c357a04d26b6bd8c1e9d7d25f306ecf2507b7e43c5a24dd0cec7ae4cd2225201
7
- data.tar.gz: af99d1b73b2349ef830ccaf095b87bd9410374ddf43cfcf52bfba4c93bad60c174646bdb7399a031ff956eb3fb2f748b27c2c26eb68f4c28b9a670e617b2362e
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
- [![Gem Version](https://badge.fury.io/rb/acts_as_belongable.svg)](https://badge.fury.io/rb/acts_as_belongable) <img src="https://travis-ci.org/jonhue/acts_as_belongable.svg?branch=master" />
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
- * [To Do](#to-do)
19
- * [Contributing](#contributing)
20
- * [Contributors](#contributors)
21
- * [Semantic versioning](#semantic-versioning)
22
- * [License](#license)
23
-
24
- ---
25
-
26
- ## Installation
27
-
28
- acts_as_belongable works with Rails 5 onwards. You can add it to your `Gemfile` with:
29
-
30
- ```ruby
31
- gem 'acts_as_belongable'
32
- ```
33
-
34
- And then execute:
35
-
36
- $ bundle
37
-
38
- Or install it yourself as:
39
-
40
- $ gem install acts_as_belongable
41
-
42
- If you always want to be up to date fetch the latest from GitHub in your `Gemfile`:
43
-
44
- ```ruby
45
- gem 'acts_as_belongable', github: 'jonhue/acts_as_belongable'
46
- ```
47
-
48
- Now run the generator:
49
-
50
- $ rails g acts_as_belongable
51
-
52
- To wrap things up, migrate the changes into your database:
53
-
54
- $ rails db:migrate
55
-
56
- ---
57
-
58
- ## Usage
59
-
60
- ### Associations
61
-
62
- ```ruby
63
- class User < ApplicationRecord
64
- acts_as_belongable
65
- belongable :attending, 'Event'
66
- end
67
-
68
- class Event < ApplicationRecord
69
- acts_as_belonger
70
- acts_as_belongable
71
- belonger :attendees, 'User'
72
- belongable :conferences, 'Conference'
73
- end
74
-
75
- class Conference < ApplicationRecord
76
- acts_as_belonger
77
- belonger :events, 'Event'
78
- end
79
- ```
80
-
81
- ### acts_as_belonger
82
-
83
- `acts_as_belonger` makes the following methods available:
84
-
85
- ```ruby
86
- c = Conference.first
87
-
88
- # Returns all belongables associated with this record
89
- c.belongables
90
-
91
- # Adds a belongable to this record
92
- c.add_belongable Event.first
93
- # c.add_belongable! Event.first
94
-
95
- # Creates a belongable record and adds it to this record
96
- c.create_belongable 'Event', options
97
- # c.create_belongable! Event, options
98
- ```
99
-
100
- ### acts_as_belongable
101
-
102
- `acts_as_belongable` makes the following methods available:
103
-
104
- ```ruby
105
- e = Event.first
106
-
107
- # Returns all belongers associated with this record
108
- e.belongers
109
-
110
- # Adds this record to a belonger
111
- c.add_to_belonger Conference.first
112
- # c.add_to_belonger! Conference.first
113
-
114
- # Adds this record to a newly created belonger
115
- c.create_belonger 'Conference', options
116
- # c.create_belonger! Conference, options
117
- ```
118
-
119
- ### acts_as_list
120
-
121
- acts_as_belongable integrates with [acts_as_list](). It adds a `position` column to `Belonging`:
122
-
123
- ```ruby
124
- c.add_belongable Event.first, position: 1
125
- ```
126
-
127
- ### Scopes
128
-
129
- You can use scopes to add details to an relation:
130
-
131
- ```ruby
132
- u = User.first
133
- e = u.create_belongable 'Event'
134
- c = u.create_belongable 'Conference'
135
- u = User.last
136
- u.add_belongable 'Event', scope: 'collaboration'
137
- u.add_belongable 'Conference', scope: 'collaboration'
138
-
139
- # Get all belongables with a specific scope
140
- u.belongables_with_scope :collaboration
141
-
142
- # Get `Event` belongables with a specific scope
143
- u.belongables_with_scope :collaboration, 'Event'
144
-
145
- # Get all belongers with a specific scope
146
- e.belongers_with_scope :collaboration
147
-
148
- # Get `User` belongers with a specific scope
149
- e.belongers_with_scope :collaboration, 'User'
150
- ```
151
-
152
- You are also able to restrict associations to specific scopes:
153
-
154
- ```ruby
155
- class User < ApplicationRecord
156
- acts_as_belonger
157
- belonger :conference_collaborations, 'Conference', scope: :collaboration
158
- belonger :conference_attendings, 'Conference', scope: :membership
159
- end
160
-
161
- class Conference < ApplicationRecord
162
- acts_as_belongable
163
- belongable :collaborators, 'User', scope: :collaboration
164
- belongable :attendees, 'User', scope: :membership
165
- end
166
- ```
167
-
168
- ---
169
-
170
- ## To Do
171
-
172
- [Here](https://github.com/jonhue/acts_as_belongable/projects/1) is the full list of current projects.
173
-
174
- To propose your ideas, initiate the discussion by adding a [new issue](https://github.com/jonhue/acts_as_belongable/issues/new).
175
-
176
- ---
177
-
178
- ## Contributing
179
-
180
- We hope that you will consider contributing to acts_as_belongable. Please read this short overview for some information about how to get started:
181
-
182
- [Learn more about contributing to this repository](CONTRIBUTING.md), [Code of Conduct](CODE_OF_CONDUCT.md)
183
-
184
- ### Contributors
185
-
186
- Give the people some :heart: who are working on this project. See them all at:
187
-
188
- https://github.com/jonhue/acts_as_belongable/graphs/contributors
189
-
190
- ### Semantic Versioning
191
-
192
- acts_as_belongable follows Semantic Versioning 2.0 as defined at http://semver.org.
193
-
194
- ## License
195
-
196
- MIT License
197
-
198
- Copyright (c) 2018 Jonas Hübotter
199
-
200
- Permission is hereby granted, free of charge, to any person obtaining a copy
201
- of this software and associated documentation files (the "Software"), to deal
202
- in the Software without restriction, including without limitation the rights
203
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
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
+ [![Gem Version](https://badge.fury.io/rb/acts_as_belongable.svg)](https://badge.fury.io/rb/acts_as_belongable) ![Travis](https://travis-ci.org/jonhue/acts_as_belongable.svg?branch=master)
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
- module ActsAsBelongable
2
- module Belongable
3
-
4
- extend ActiveSupport::Concern
5
-
6
- module ClassMethods
7
- def acts_as_belongable
8
- has_many :belongers, through: :belongable_belongings, source: :belonger
9
- has_many :belongable_belongings, as: :belongable, class_name: 'Belonging', dependent: :destroy
10
- end
11
-
12
- def belongable name, source_type, options = {}
13
- if options.has_key? :scope
14
- belongable_belongings_with_scope options[:scope]
15
- through = "belongable_belongings_#{options[:scope]}".to_sym
16
- end
17
- through ||= :belongable_belongings
18
- has_many name.to_sym, through: through, source: :belonger, source_type: source_type.to_s
19
- end
20
- def belongable_belongings_with_scope scope
21
- has_many "belongable_belongings_#{scope}".to_sym, -> { where(scope: scope.to_s) }, as: :belongable, class_name: 'Belonging', dependent: :destroy
22
- end
23
- end
24
-
25
- def add_to_belonger belonger, options = {}
26
- options[:belonger] = belonger
27
- self.belongable_belongings.create options
28
- end
29
- def add_to_belonger! belonger, options = {}
30
- options[:belonger] = belonger
31
- self.belongable_belongings.create! options
32
- end
33
-
34
- def create_belonger _class, options = {}
35
- belonging_options = options.delete(:belonging) || {}
36
- object = _class.create options
37
- self.add_to_belonger object, belonging_options
38
- object
39
- end
40
- def create_belonger! _class, options = {}
41
- belonging_options = options.delete(:belonging) || {}
42
- object = _class.create! options
43
- self.add_to_belonger! object, belonging_options
44
- object
45
- end
46
-
47
- def belongers_with_scope scope, source_type = nil
48
- self.belongable_belongings.where(scope: scope.to_s).map { |belonging| source_type.nil? || belonging.belonger.class.name == source_type ? belonging.belonger : nil }.compact
49
- end
50
-
51
- end
52
- end
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
- module ActsAsBelongable
2
- module Belonger
3
-
4
- extend ActiveSupport::Concern
5
-
6
- module ClassMethods
7
- def acts_as_belonger
8
- has_many :belongables, through: :belonger_belongings, source: :belongable
9
- has_many :belonger_belongings, as: :belonger, class_name: 'Belonging', dependent: :destroy
10
- end
11
-
12
- def belonger name, source_type, options = {}
13
- if options.has_key? :scope
14
- belonger_belongings_with_scope options[:scope]
15
- through = "belonger_belongings_#{options[:scope]}".to_sym
16
- end
17
- through ||= :belonger_belongings
18
- has_many name.to_sym, through: through, source: :belongable, source_type: source_type.to_s
19
- end
20
- def belonger_belongings_with_scope scope
21
- has_many "belonger_belongings_#{scope}".to_sym, -> { where(scope: scope.to_s) }, as: :belonger, class_name: 'Belonging', dependent: :destroy
22
- end
23
- end
24
-
25
- def add_belongable belongable, options = {}
26
- options[:belongable] = belongable
27
- self.belonger_belongings.create options
28
- end
29
- def add_belongable! belongable, options = {}
30
- options[:belongable] = belongable
31
- self.belonger_belongings.create! options
32
- end
33
-
34
- def create_belongable _class, options = {}
35
- belonging_options = options.delete(:belonging) || {}
36
- object = _class.create options
37
- self.add_belongable object, belonging_options
38
- object
39
- end
40
- def create_belongable! _class, options = {}
41
- belonging_options = options.delete(:belonging) || {}
42
- object = _class.create! options
43
- self.add_belongable! object, belonging_options
44
- object
45
- end
46
-
47
- def belongables_with_scope scope, source_type = nil
48
- self.belonger_belongings.where(scope: scope.to_s).map { |belonging| source_type.nil? || belonging.belongable.class.name == source_type ? belonging.belongable : nil }.compact
49
- end
50
-
51
- end
52
- end
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
@@ -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
- require 'acts_as_belongable/railtie'
7
- require 'acts_as_belongable/engine'
8
-
7
+ require 'acts_as_belongable/railtie'
8
+ require 'acts_as_belongable/engine'
9
9
  end
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails/railtie'
2
4
 
3
5
  module ActsAsBelongable
4
- class Engine < Rails::Engine
5
- end
6
+ class Engine < Rails::Engine
7
+ end
6
8
  end
@@ -1,14 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rails/railtie'
2
4
 
3
5
  module ActsAsBelongable
4
- class Railtie < Rails::Railtie
5
-
6
- initializer 'acts_as_belongable.active_record' do
7
- ActiveSupport.on_load :active_record do
8
- include ActsAsBelongable::Belonger
9
- include ActsAsBelongable::Belongable
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
- module ActsAsBelongable
2
-
3
- VERSION = '2.1.2'
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
- include Rails::Generators::Migration
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
- def self.next_migration_number dirname
12
- if ActiveRecord::Base.timestamped_migrations
13
- Time.now.utc.strftime '%Y%m%d%H%M%S'
14
- else
15
- "%.3d" % (current_migration_number(dirname) + 1)
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
- def create_migration_file
20
- migration_template 'migration.rb.erb', 'db/migrate/acts_as_belongable_migration.rb', migration_version: migration_version
21
- end
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
- def create_model
24
- template 'model.rb', 'app/models/belonging.rb'
25
- end
26
+ def create_model
27
+ template 'model.rb', 'app/models/belonging.rb'
28
+ end
26
29
 
27
- def show_readme
28
- readme 'README.md'
29
- end
30
+ def show_readme
31
+ readme 'README.md'
32
+ end
30
33
 
31
- private
34
+ private
32
35
 
33
- def migration_version
34
- if Rails.version >= '5.0.0'
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
- class ActsAsBelongableMigration < ActiveRecord::Migration<%= migration_version %>
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
- t.string :scope
9
- t.integer :position
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
- t.timestamps null: false
9
+ t.string :scope
10
+ t.integer :position
12
11
 
13
- end
12
+ t.timestamps null: false
14
13
  end
14
+ end
15
15
  end
@@ -1,8 +1,8 @@
1
- class Belonging < ApplicationRecord
2
-
3
- acts_as_list :belonger
1
+ # frozen_string_literal: true
4
2
 
5
- belongs_to :belonger, polymorphic: true
6
- belongs_to :belongable, polymorphic: true
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: 2.1.2
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-05-24 00:00:00.000000000 Z
11
+ date: 2018-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: railties
14
+ name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '5.0'
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.0'
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.0'
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.0'
40
+ version: '5.2'
41
41
  - !ruby/object:Gem::Dependency
42
- name: activerecord
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: '5.0'
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: '5.0'
54
+ version: '0.9'
55
55
  - !ruby/object:Gem::Dependency
56
- name: acts_as_list
56
+ name: railties
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0.9'
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: '0.9'
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.4
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