has_siblings 0.1.7 → 0.2.7
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 +13 -11
- data/lib/has_siblings/errors.rb +2 -2
- 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: b9ce12e0cc218d0ebe0fd3aa99a1218f64c2afff
|
4
|
+
data.tar.gz: f77daab3e5f7f63be62179bfec18d9b2d8e14a5b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d432e037773d05a4094c1e0ffe05511c593175ff0870d1e5cb09cab6850ed83687540246b2ac25dd2bbe2126e47af6777285c3ebe52a48b312fd51969ee15d77
|
7
|
+
data.tar.gz: 11f4e1dad7abae98f369a88f501c6041d56cd70c39edecc51e46b7a381171eca93195296ef85d68b708112e6f1e7d39b2a9a030d6d5ef256efe0825b020a77ec
|
@@ -8,26 +8,28 @@ module HasSiblings
|
|
8
8
|
*parents = options.fetch(:through)
|
9
9
|
name = options.fetch(:name, "siblings")
|
10
10
|
|
11
|
-
|
11
|
+
reflections = parents.map do |parent|
|
12
12
|
reflection = reflect_on_association(parent)
|
13
|
-
fail HasSiblings::
|
14
|
-
|
13
|
+
fail HasSiblings::ReflectionNotFoundError.new(parent, self) if reflection.nil?
|
14
|
+
reflection
|
15
15
|
end
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
16
|
+
where_scopes = reflections.map do |reflection|
|
17
|
+
foreign_key = reflection.foreign_key
|
18
|
+
foreign_type = reflection.foreign_type
|
19
|
+
|
20
|
+
if reflection.polymorphic?
|
21
|
+
"where(#{foreign_key}: #{foreign_key}, #{foreign_type}: #{foreign_type})"
|
22
|
+
else
|
23
|
+
"where(#{foreign_key}: #{foreign_key})"
|
24
|
+
end
|
22
25
|
end
|
23
|
-
first_parent_association = parent_association_name_pairs[0].join(".")
|
24
26
|
|
25
27
|
mixin = ActiveRecord.version.to_s >= "4.1" ? generated_association_methods : generated_feature_methods
|
26
28
|
|
27
29
|
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
|
28
30
|
def #{name}
|
29
31
|
return self.class.none if [#{parents.join(",")}].any?(&:nil?)
|
30
|
-
|
32
|
+
self.class.#{where_scopes.join(".")}.where.not(id: id)
|
31
33
|
end
|
32
34
|
CODE
|
33
35
|
end
|
data/lib/has_siblings/errors.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
module HasSiblings
|
2
|
-
class
|
2
|
+
class ReflectionNotFoundError < StandardError
|
3
3
|
def initialize(association_name, owner_class_name)
|
4
4
|
super("Could not find the association #{association_name} in model #{owner_class_name}")
|
5
5
|
end
|
6
6
|
end
|
7
7
|
|
8
|
-
class
|
8
|
+
class ForeignKeyNotFoundError < StandardError
|
9
9
|
def initialize(association_name, owner_class_name)
|
10
10
|
super("Could not find the inverse_of for the association #{association_name} in model #{owner_class_name}")
|
11
11
|
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.
|
4
|
+
version: 0.2.7
|
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-11-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|