has_siblings 0.1.7 → 0.2.7

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
  SHA1:
3
- metadata.gz: 1b9d1f499235250537032de08d3fa519a4c1e604
4
- data.tar.gz: 327722172be38d3ae65980a655f4895830fc21fc
3
+ metadata.gz: b9ce12e0cc218d0ebe0fd3aa99a1218f64c2afff
4
+ data.tar.gz: f77daab3e5f7f63be62179bfec18d9b2d8e14a5b
5
5
  SHA512:
6
- metadata.gz: f396ab071b76d64499c9a1e6d9d597f4c03ca9be06f49d95b40f611305aa1ea7a1bd03c8edfc3424036086906e3ff086b2da2076af91940612d5a03cdf465a3c
7
- data.tar.gz: 0ba16e8cccddff04f4c4b29f615513b66bcb8853cc2758d8b3e5182330e81704f21a47fcdea00fa3f5bb973d3445404d5c31eda63b1c2bc4e783142b1e849fb3
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
- parent_association_pairs = parents.map do |parent|
11
+ reflections = parents.map do |parent|
12
12
  reflection = reflect_on_association(parent)
13
- fail HasSiblings::ThroughAssociationNotFoundError.new(parent, self) if reflection.nil?
14
- [parent, reflection]
13
+ fail HasSiblings::ReflectionNotFoundError.new(parent, self) if reflection.nil?
14
+ reflection
15
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})"
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
- #{([first_parent_association] + merge_scopes).join(".")}.where.not(id: id)
32
+ self.class.#{where_scopes.join(".")}.where.not(id: id)
31
33
  end
32
34
  CODE
33
35
  end
@@ -1,11 +1,11 @@
1
1
  module HasSiblings
2
- class ThroughAssociationNotFoundError < StandardError
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 InverseOfNotFoundError < StandardError
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
@@ -1,3 +1,3 @@
1
1
  module HasSiblings
2
- VERSION = "0.1.7"
2
+ VERSION = "0.2.7"
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.7
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-08-03 00:00:00.000000000 Z
11
+ date: 2015-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord