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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 99724979e1ee56f88f57d31b9fccae9fc557d6f5c03f3e09feecbbbb58587122
4
- data.tar.gz: 8ea8c8e9c2b4de93e9a2db1ad9e5a56cde09c6b9e2fc953a236aaecd422607ad
3
+ metadata.gz: 0e1b930cccce191245c3a42a9fa2393b3750b12226accd0704b8f0b0db773181
4
+ data.tar.gz: a016181335cf789ca4e51241430d81cbc0ee892e9e6bafdb9f2ee555ecde83f6
5
5
  SHA512:
6
- metadata.gz: a135d7308b3fbaf37d83b639b9c2962180d6211a57ca904c22995e2b6264b1a4a0cf6028a6f8aedd5423e3446ea72eec78bfa73a54fa48b18d5e44e6ae10c781
7
- data.tar.gz: 01f1e856c96e217f41d8c6973006d8c8e478158d5a830fb5d27cdf315fb598883d81279445888f177206bb0b07815f886c621d177224e58ee09625ba35c57b5a
6
+ metadata.gz: c1d863ddd63d603e99b3beff19866f4a426066c3758b68a456f1a5231934797536d7c8718d87142e6bc57a636c2620a0a5973f2194ec776632331e3e5109c11a
7
+ data.tar.gz: f4ec1b730bf5795c5fcaadbd90fda303623ac5b4791d4545573e12d0fd83019117a27875bd63247db4b51f7050422799147701b479599695cbc11c13240f684a
data/CHANGELOG.md CHANGED
@@ -4,6 +4,11 @@
4
4
 
5
5
  * nothing yet
6
6
 
7
+ ### 1.1.0 - 2018/01/20
8
+
9
+ * features
10
+ * add scopes
11
+
7
12
  ### 1.0.0 - 2018/01/20
8
13
 
9
14
  * initial release
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
- has_many name.to_sym, through: :belongable_belongings, source: :belonger, source_type: source_type.to_s
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
- has_many name.to_sym, through: :belonger_belongings, source: :belongable, source_type: source_type.to_s
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
@@ -1,5 +1,5 @@
1
1
  module ActsAsBelongable
2
2
 
3
- VERSION = '1.0.0'
3
+ VERSION = '1.1.0'
4
4
 
5
5
  end
@@ -5,6 +5,7 @@ class ActsAsBelongableMigration < ActiveRecord::Migration<%= migration_version %
5
5
  t.references :belonger, polymorphic: true, index: true
6
6
  t.references :belonging, polymorphic: true, index: true
7
7
 
8
+ t.string :scope
8
9
  t.integer :position
9
10
 
10
11
  t.timestamps null: false
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_belongable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Hübotter