has_siblings 0.1.4 → 0.1.5
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/lib/has_siblings/class_methods.rb +12 -4
- data/lib/has_siblings/errors.rb +6 -0
- data/lib/has_siblings/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9f9a89302f89bb8250b6244b893be2f0778db9fe
|
4
|
+
data.tar.gz: 8b0f86c625db09473ab791ed9d684165da592686
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 98caa42fb0fa17325fa3ba68fea9423a5ed8345db64c123bcc0f41372cd3eee0b8300f384b1a742b8e8b0fa3561299b0d752251bf14d5ecda9c582af1cf652e5
|
7
|
+
data.tar.gz: a584ddf70c1b38769faecff09ecd9939263ad6972cc81eb3fe32847d2af29fc279ff02971845409a66207329e2bfa5d03bd26be91e5e59982f2eb7123208253c
|
@@ -8,17 +8,25 @@ module HasSiblings
|
|
8
8
|
*parents = options.fetch(:through)
|
9
9
|
name = options.fetch(:name, "siblings")
|
10
10
|
|
11
|
-
|
11
|
+
parent_association_pairs = parents.map do |parent|
|
12
12
|
reflection = reflect_on_association(parent)
|
13
13
|
fail HasSiblings::ThroughAssociationNotFoundError.new(parent, self) if reflection.nil?
|
14
|
-
|
15
|
-
"where(#{foreign_key}: #{foreign_key})"
|
14
|
+
[parent, reflection]
|
16
15
|
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(".")
|
17
24
|
|
18
25
|
mixin = ActiveRecord.version.to_s >= "4.1" ? generated_association_methods : generated_feature_methods
|
26
|
+
|
19
27
|
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
|
20
28
|
def #{name}
|
21
|
-
|
29
|
+
#{([first_parent_association] + merge_scopes).join(".")}.where.not(id: id)
|
22
30
|
end
|
23
31
|
CODE
|
24
32
|
end
|
data/lib/has_siblings/errors.rb
CHANGED
@@ -4,4 +4,10 @@ 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
|
7
13
|
end
|
data/lib/has_siblings/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_siblings
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Octavian Neamtu
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-08-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|