has_siblings 0.1.4 → 0.2.8

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
- SHA1:
3
- metadata.gz: 0c9a478422e44130d48e912d90859c85cf39e0fe
4
- data.tar.gz: 4cf1bf638ed49bc03f30045f8d6918f721ded0fb
2
+ SHA256:
3
+ metadata.gz: 74413b0cd5fbe98208341f508142e4e8c083dd7fb7a3676c19c99e679b36672c
4
+ data.tar.gz: 2bfc9e15371ce603dc2ae124b1ab05e0c11be55f0bb1600d601f358f84255fbd
5
5
  SHA512:
6
- metadata.gz: 5bd05456d26bff338d5d314eb25812ba46ef8583c0a7fd36eedb47abb0cbbf40fa2380ff5ee53dc41619d32561ce41ebc394d6d06e50fef26c52378472fec3ef
7
- data.tar.gz: 2bfad7ee8d04dad790949a7418720acea104fba99f55c7fef6f0d7f18df1269840b4a295488bf06d3efa6c7994da95a67d1ff9190a844315e3a04bc4fb4a9796
6
+ metadata.gz: f14c41f5d1bb0cabc447590fc085ab330528877ab00a14093f0c13c6c97338857926de1a056fc2b05831ee695a1cd59df8b3f0d7e86a8a6acdd41d952fa2ac74
7
+ data.tar.gz: e52ab634d35ef47b1654c4c5226de60670d32d3c8f99dc3f9ab419497c7773a5053b42a552f8f88ec11182178f7070fc06ce565b86ba925876b8503ede8fa717
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Annkissam
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -69,7 +69,10 @@ An example of what it returns:
69
69
 
70
70
  `has_siblings` accepts a `through: [...]` option that specifies the parents and can
71
71
  be an array or a single symbol, and an optional `name` that will be the name of
72
- the siblings method (default to `siblings`).
72
+ the siblings method (default to `siblings`). By default, when specifying an array of
73
+ parents, any `nil` association will result in an empty sibling collection, if parents
74
+ can optionally be blank setting `allow_nil: true` will include the present parents and
75
+ and the missing parent as conditions to the sibling scope.
73
76
 
74
77
  ## Compatibility
75
78
 
data/has_siblings.gemspec CHANGED
@@ -12,6 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.summary = %q{Easily define a siblings method for a child model instance.}
13
13
  spec.description = %q{ parent.children.where.not(id: self.id), extended to n parents }
14
14
  spec.homepage = "https://github.com/annkissam/has_siblings"
15
+ spec.license = "MIT"
15
16
 
16
17
  # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
17
18
  # delete this section to allow pushing this gem to any host.
@@ -27,8 +28,8 @@ Gem::Specification.new do |spec|
27
28
  spec.require_paths = ["lib"]
28
29
 
29
30
  spec.add_dependency "activerecord", ">= 4.0"
30
- spec.add_development_dependency "bundler", "~> 1.10"
31
- spec.add_development_dependency "rake", "~> 10.0"
31
+ spec.add_development_dependency "bundler", ">= 1.17"
32
+ spec.add_development_dependency "rake", ">= 12.3.3"
32
33
  spec.add_development_dependency "rspec"
33
34
  spec.add_development_dependency "rspec-its"
34
35
  spec.add_development_dependency "sqlite3"
@@ -7,17 +7,32 @@ module HasSiblings
7
7
  def has_siblings(options = {})
8
8
  *parents = options.fetch(:through)
9
9
  name = options.fetch(:name, "siblings")
10
+ allow_nil = options.fetch(:allow_nil, false)
10
11
 
11
- where_scopes = parents.map do |parent|
12
+ reflections = parents.map do |parent|
12
13
  reflection = reflect_on_association(parent)
13
- fail HasSiblings::ThroughAssociationNotFoundError.new(parent, self) if reflection.nil?
14
+ fail HasSiblings::ReflectionNotFoundError.new(parent, self) if reflection.nil?
15
+ reflection
16
+ end
17
+ where_scopes = reflections.map do |reflection|
14
18
  foreign_key = reflection.foreign_key
15
- "where(#{foreign_key}: #{foreign_key})"
19
+ foreign_type = reflection.foreign_type
20
+
21
+ if reflection.polymorphic?
22
+ "where(#{foreign_key}: #{foreign_key}, #{foreign_type}: #{foreign_type})"
23
+ else
24
+ "where(#{foreign_key}: #{foreign_key})"
25
+ end
16
26
  end
17
27
 
18
28
  mixin = ActiveRecord.version.to_s >= "4.1" ? generated_association_methods : generated_feature_methods
29
+
19
30
  mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
20
31
  def #{name}
32
+ unless #{allow_nil}
33
+ return self.class.none if [#{parents.join(",")}].any?(&:nil?)
34
+ end
35
+
21
36
  self.class.#{where_scopes.join(".")}.where.not(id: id)
22
37
  end
23
38
  CODE
@@ -1,7 +1,13 @@
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
+
8
+ class ForeignKeyNotFoundError < 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.2.8"
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.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Octavian Neamtu
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-07-31 00:00:00.000000000 Z
11
+ date: 2021-04-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -28,30 +28,30 @@ dependencies:
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '1.10'
33
+ version: '1.17'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '1.10'
40
+ version: '1.17'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: 12.3.3
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: 12.3.3
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -134,6 +134,7 @@ files:
134
134
  - ".travis.yml"
135
135
  - Appraisals
136
136
  - Gemfile
137
+ - LICENSE.txt
137
138
  - README.md
138
139
  - Rakefile
139
140
  - bin/console
@@ -150,10 +151,11 @@ files:
150
151
  - lib/has_siblings/errors.rb
151
152
  - lib/has_siblings/version.rb
152
153
  homepage: https://github.com/annkissam/has_siblings
153
- licenses: []
154
+ licenses:
155
+ - MIT
154
156
  metadata:
155
157
  allowed_push_host: https://rubygems.org
156
- post_install_message:
158
+ post_install_message:
157
159
  rdoc_options: []
158
160
  require_paths:
159
161
  - lib
@@ -168,9 +170,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
168
170
  - !ruby/object:Gem::Version
169
171
  version: '0'
170
172
  requirements: []
171
- rubyforge_project:
172
- rubygems_version: 2.2.3
173
- signing_key:
173
+ rubyforge_project:
174
+ rubygems_version: 2.7.6
175
+ signing_key:
174
176
  specification_version: 4
175
177
  summary: Easily define a siblings method for a child model instance.
176
178
  test_files: []