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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0c9a478422e44130d48e912d90859c85cf39e0fe
4
- data.tar.gz: 4cf1bf638ed49bc03f30045f8d6918f721ded0fb
3
+ metadata.gz: 9f9a89302f89bb8250b6244b893be2f0778db9fe
4
+ data.tar.gz: 8b0f86c625db09473ab791ed9d684165da592686
5
5
  SHA512:
6
- metadata.gz: 5bd05456d26bff338d5d314eb25812ba46ef8583c0a7fd36eedb47abb0cbbf40fa2380ff5ee53dc41619d32561ce41ebc394d6d06e50fef26c52378472fec3ef
7
- data.tar.gz: 2bfad7ee8d04dad790949a7418720acea104fba99f55c7fef6f0d7f18df1269840b4a295488bf06d3efa6c7994da95a67d1ff9190a844315e3a04bc4fb4a9796
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
- where_scopes = parents.map do |parent|
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
- foreign_key = reflection.foreign_key
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
- self.class.#{where_scopes.join(".")}.where.not(id: id)
29
+ #{([first_parent_association] + merge_scopes).join(".")}.where.not(id: id)
22
30
  end
23
31
  CODE
24
32
  end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module HasSiblings
2
- VERSION = "0.1.4"
2
+ VERSION = "0.1.5"
3
3
  end
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
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-07-31 00:00:00.000000000 Z
11
+ date: 2015-08-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord