has_siblings 0.1.2 → 0.1.4
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/README.md +29 -29
- data/lib/has_siblings/class_methods.rb +4 -12
- data/lib/has_siblings/errors.rb +0 -6
- data/lib/has_siblings/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0c9a478422e44130d48e912d90859c85cf39e0fe
|
4
|
+
data.tar.gz: 4cf1bf638ed49bc03f30045f8d6918f721ded0fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5bd05456d26bff338d5d314eb25812ba46ef8583c0a7fd36eedb47abb0cbbf40fa2380ff5ee53dc41619d32561ce41ebc394d6d06e50fef26c52378472fec3ef
|
7
|
+
data.tar.gz: 2bfad7ee8d04dad790949a7418720acea104fba99f55c7fef6f0d7f18df1269840b4a295488bf06d3efa6c7994da95a67d1ff9190a844315e3a04bc4fb4a9796
|
data/README.md
CHANGED
@@ -26,43 +26,43 @@ Add has_siblings to a child model to get a siblings method that returns all of t
|
|
26
26
|
other children.
|
27
27
|
|
28
28
|
```ruby
|
29
|
-
|
30
|
-
|
31
|
-
|
29
|
+
class Mother < ActiveRecord::Base
|
30
|
+
has_many :children
|
31
|
+
end
|
32
32
|
|
33
|
-
|
34
|
-
|
35
|
-
|
33
|
+
class Father < ActiveRecord::Base
|
34
|
+
has_many :children
|
35
|
+
end
|
36
36
|
|
37
|
-
|
38
|
-
|
39
|
-
|
37
|
+
class Child < ActiveRecord::Base
|
38
|
+
belongs_to :mother, inverse_of: :children
|
39
|
+
belongs_to :father, inverse_of: :children
|
40
40
|
|
41
|
-
|
42
|
-
|
43
|
-
|
41
|
+
has_siblings through: [:mother, :father]
|
42
|
+
has_siblings through: :mother, name: :step_siblings
|
43
|
+
end
|
44
44
|
```
|
45
45
|
|
46
46
|
An example of what it returns:
|
47
47
|
|
48
48
|
```ruby
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
49
|
+
[1] pry> child
|
50
|
+
=> #<Child:0x007fe5b3371c20 id: 6, mother_id: 3, father_id: 2>
|
51
|
+
[2] pry> sister
|
52
|
+
=> #<Child:0x007fe5b3379e70 id: 7, mother_id: 3, father_id: 2>
|
53
|
+
[3] pry> brother
|
54
|
+
=> #<Child:0x007fe5b337e0b0 id: 8, mother_id: 3, father_id: 2>
|
55
|
+
[4] pry> half_sister
|
56
|
+
=> #<Child:0x007fe5b3386300 id: 9, mother_id: 3, father_id: nil>
|
57
|
+
[5] pry> just_a_friend
|
58
|
+
=> #<Child:0x007fe5b338c390 id: 10, mother_id: 4, father_id: nil>
|
59
|
+
[6] pry> child.siblings
|
60
|
+
=> [#<Child:0x007fe5b33dc6b0 id: 7, mother_id: 3, father_id: 2>,
|
61
|
+
#<Child:0x007fe5b33dc3e0 id: 8, mother_id: 3, father_id: 2>]
|
62
|
+
[7] pry> child.step_siblings
|
63
|
+
=> [#<Child:0x007fe5b335fcc8 id: 7, mother_id: 3, father_id: 2>,
|
64
|
+
#<Child:0x007fe5b335f9d0 id: 8, mother_id: 3, father_id: 2>,
|
65
|
+
#<Child:0x007fe5b335f598 id: 9, mother_id: 3, father_id: nil>]
|
66
66
|
```
|
67
67
|
|
68
68
|
## Options
|
@@ -8,25 +8,17 @@ module HasSiblings
|
|
8
8
|
*parents = options.fetch(:through)
|
9
9
|
name = options.fetch(:name, "siblings")
|
10
10
|
|
11
|
-
|
11
|
+
where_scopes = parents.map do |parent|
|
12
12
|
reflection = reflect_on_association(parent)
|
13
13
|
fail HasSiblings::ThroughAssociationNotFoundError.new(parent, self) if reflection.nil?
|
14
|
-
|
14
|
+
foreign_key = reflection.foreign_key
|
15
|
+
"where(#{foreign_key}: #{foreign_key})"
|
15
16
|
end
|
16
|
-
parent_association_name_pairs = parent_association_pairs.map do |parent, association|
|
17
|
-
fail HasSiblings::InverseOfNotFoundError.new(parent, self) if association.inverse_of.nil?
|
18
|
-
[parent, association.inverse_of.name]
|
19
|
-
end
|
20
|
-
merge_scopes = parent_association_name_pairs[1..-1].map do |parent, association_name|
|
21
|
-
"merge(#{parent}.#{association_name})"
|
22
|
-
end
|
23
|
-
first_parent_association = parent_association_name_pairs[0].join(".")
|
24
17
|
|
25
18
|
mixin = ActiveRecord.version.to_s >= "4.1" ? generated_association_methods : generated_feature_methods
|
26
|
-
|
27
19
|
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
|
28
20
|
def #{name}
|
29
|
-
|
21
|
+
self.class.#{where_scopes.join(".")}.where.not(id: id)
|
30
22
|
end
|
31
23
|
CODE
|
32
24
|
end
|
data/lib/has_siblings/errors.rb
CHANGED
@@ -4,10 +4,4 @@ module HasSiblings
|
|
4
4
|
super("Could not find the association #{association_name} in model #{owner_class_name}")
|
5
5
|
end
|
6
6
|
end
|
7
|
-
|
8
|
-
class InverseOfNotFoundError < StandardError
|
9
|
-
def initialize(association_name, owner_class_name)
|
10
|
-
super("Could not find the inverse_of for the association #{association_name} in model #{owner_class_name}")
|
11
|
-
end
|
12
|
-
end
|
13
7
|
end
|
data/lib/has_siblings/version.rb
CHANGED