acts_as_belongable 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +42 -0
- data/app/models/concerns/acts_as_belongable/belongable.rb +20 -4
- data/app/models/concerns/acts_as_belongable/belonger.rb +20 -4
- data/lib/acts_as_belongable/version.rb +1 -1
- data/lib/generators/templates/migration.rb.erb +1 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0e1b930cccce191245c3a42a9fa2393b3750b12226accd0704b8f0b0db773181
|
4
|
+
data.tar.gz: a016181335cf789ca4e51241430d81cbc0ee892e9e6bafdb9f2ee555ecde83f6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1d863ddd63d603e99b3beff19866f4a426066c3758b68a456f1a5231934797536d7c8718d87142e6bc57a636c2620a0a5973f2194ec776632331e3e5109c11a
|
7
|
+
data.tar.gz: f4ec1b730bf5795c5fcaadbd90fda303623ac5b4791d4545573e12d0fd83019117a27875bd63247db4b51f7050422799147701b479599695cbc11c13240f684a
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -14,6 +14,7 @@ acts_as_belongable is a Rubygem that provides an associations engine for Rails a
|
|
14
14
|
* [acts_as_belonger](#acts_as_belonger)
|
15
15
|
* [acts_as_belongable](#acts_as_belongable)
|
16
16
|
* [acts_as_list](#acts_as_list)
|
17
|
+
* [Scopes](#scopes)
|
17
18
|
* [To Do](#to-do)
|
18
19
|
* [Contributing](#contributing)
|
19
20
|
* [Contributors](#contributors)
|
@@ -123,6 +124,47 @@ acts_as_belongable integrates with [acts_as_list](). It adds a `position` column
|
|
123
124
|
c.add_belongable Event.first, position: 1
|
124
125
|
```
|
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, 'User', 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
|
+
|
126
168
|
---
|
127
169
|
|
128
170
|
## To Do
|
@@ -9,8 +9,16 @@ module ActsAsBelongable
|
|
9
9
|
has_many :belongable_belongings, as: :belongable, class_name: 'Belonging', dependent: :destroy
|
10
10
|
end
|
11
11
|
|
12
|
-
def belongable name, source_type
|
13
|
-
|
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
|
14
22
|
end
|
15
23
|
end
|
16
24
|
|
@@ -24,12 +32,20 @@ module ActsAsBelongable
|
|
24
32
|
end
|
25
33
|
|
26
34
|
def create_belonger class_name, options = {}
|
35
|
+
belonging_options = options.delete :belonging
|
27
36
|
object = class_name.constantize.create options
|
28
|
-
self.add_to_belonger object
|
37
|
+
self.add_to_belonger object, belonging_options
|
38
|
+
object
|
29
39
|
end
|
30
40
|
def create_belonger! class_name, options = {}
|
41
|
+
belonging_options = options.delete :belonging
|
31
42
|
object = class_name.constantize.create! options
|
32
|
-
self.add_to_belonger! object
|
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
|
33
49
|
end
|
34
50
|
|
35
51
|
end
|
@@ -9,8 +9,16 @@ module ActsAsBelongable
|
|
9
9
|
has_many :belonger_belongings, as: :belonger, class_name: 'Belonging', dependent: :destroy
|
10
10
|
end
|
11
11
|
|
12
|
-
def belonger name, source_type
|
13
|
-
|
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
|
14
22
|
end
|
15
23
|
end
|
16
24
|
|
@@ -24,12 +32,20 @@ module ActsAsBelongable
|
|
24
32
|
end
|
25
33
|
|
26
34
|
def create_belongable class_name, options = {}
|
35
|
+
belonging_options = options.delete :belonging
|
27
36
|
object = class_name.constantize.create options
|
28
|
-
self.add_belongable object
|
37
|
+
self.add_belongable object, belonging_options
|
38
|
+
object
|
29
39
|
end
|
30
40
|
def create_belongable! class_name, options = {}
|
41
|
+
belonging_options = options.delete :belonging
|
31
42
|
object = class_name.constantize.create! options
|
32
|
-
self.add_belongable! object
|
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
|
33
49
|
end
|
34
50
|
|
35
51
|
end
|