has_siblings 0.1.2 → 0.1.4

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: fdb7a296d7243a90699d3d70111ecb77f989898c
4
- data.tar.gz: f1b80a9be1854ae337d05d5d44c9ec9631bd3e45
3
+ metadata.gz: 0c9a478422e44130d48e912d90859c85cf39e0fe
4
+ data.tar.gz: 4cf1bf638ed49bc03f30045f8d6918f721ded0fb
5
5
  SHA512:
6
- metadata.gz: b54bfbde0d868e15c6ca34464d033aa65638794690f2249039550c31ea351debde55929ec37789855114ed6ba7a83eaf0b9474c5698fa30ebe7fd4f4a193f64f
7
- data.tar.gz: e913c7a5b30d733553c703371c5810be368d5ab7888e8140a631f32f79635592ac54644310d8e04bcbf2d6dc284f52f29af43b32a6205e41b037ea0f278156f5
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
- class Mother < ActiveRecord::Base
30
- has_many :children
31
- end
29
+ class Mother < ActiveRecord::Base
30
+ has_many :children
31
+ end
32
32
 
33
- class Father < ActiveRecord::Base
34
- has_many :children
35
- end
33
+ class Father < ActiveRecord::Base
34
+ has_many :children
35
+ end
36
36
 
37
- class Child < ActiveRecord::Base
38
- belongs_to :mother, inverse_of: :children
39
- belongs_to :father, inverse_of: :children
37
+ class Child < ActiveRecord::Base
38
+ belongs_to :mother, inverse_of: :children
39
+ belongs_to :father, inverse_of: :children
40
40
 
41
- has_siblings through: [:mother, :father]
42
- has_siblings through: :mother, name: :step_siblings
43
- end
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
- [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>]
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
- parent_association_pairs = parents.map do |parent|
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
- [parent, reflection]
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
- #{([first_parent_association] + merge_scopes).join(".")}.where.not(id: id)
21
+ self.class.#{where_scopes.join(".")}.where.not(id: id)
30
22
  end
31
23
  CODE
32
24
  end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module HasSiblings
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_siblings
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Octavian Neamtu