has_associations 0.0.1 → 0.0.2
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/README.md +12 -9
- data/has_associations.gemspec +4 -1
- data/lib/has_associations.rb +27 -29
- data/lib/has_associations/version.rb +4 -2
- metadata +30 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d1622b7b49275aafa1daba01f43ad836e328e2cb
|
|
4
|
+
data.tar.gz: 362e2ee7777bd1fa3623b92089d87950deacee7d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: f136240543289fb001be4e18ba450eb447360c8ac92660a32ada1adcfcff190eefa755b0ebd8c01d10b4a99de58d6ad4a552ae17afafa31573a590d73311b686
|
|
7
|
+
data.tar.gz: 22d1c4c9f9fc7e28463fd2bcd1c355e37bd0112f2639e54e8d386731b30fd7ee43cd94be6fea83d481f44661dc23008a783a11200ced91146ebd4bd0959fa29c
|
data/README.md
CHANGED
|
@@ -35,7 +35,7 @@ end
|
|
|
35
35
|
|
|
36
36
|
class Son < ActiveRecord::Base
|
|
37
37
|
belongs_to :father
|
|
38
|
-
has_one :grandfather, through: :
|
|
38
|
+
has_one :grandfather, through: :father
|
|
39
39
|
has_association_helpers
|
|
40
40
|
end
|
|
41
41
|
```
|
|
@@ -54,7 +54,7 @@ grandfather.has_father? father
|
|
|
54
54
|
# => NameError: undefined local variable or method `has_father?`
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
-
### ::
|
|
57
|
+
### ::has_association?(`AR`), ::belongs_to?(`AR`), ::has_many?(`AR`), ::has_one?(`AR`)
|
|
58
58
|
|
|
59
59
|
Verifies the ActiveRecord association exists
|
|
60
60
|
|
|
@@ -65,18 +65,21 @@ Son.has_one? Father # false
|
|
|
65
65
|
Son.has_one? Grandfather # true
|
|
66
66
|
```
|
|
67
67
|
|
|
68
|
-
### ::has_associations(`
|
|
68
|
+
### ::has_associations(`names_with_macro = nil`)
|
|
69
69
|
|
|
70
|
-
Gathers the ActiveRecords associations
|
|
70
|
+
Gathers the ActiveRecords associations
|
|
71
|
+
* `names_with_macro`: Used to return only the names of associations.
|
|
72
|
+
If `Symbol`, returns names of associations with macro (ie. `:has_many` or `:belongs_to`).
|
|
73
|
+
If `true`, returns names of all associations.
|
|
74
|
+
If `nil` or `false`, returns all AssociationReflection(s)
|
|
71
75
|
|
|
72
76
|
```ruby
|
|
73
77
|
Father.has_associations
|
|
74
|
-
# => [
|
|
75
|
-
# {:name => :sons, :type => :has_many, :class_name => "Son"},
|
|
76
|
-
# {:name => :grandfather, :type => :belongs_to, :class_name => "Grandfather"}
|
|
77
|
-
# ]
|
|
78
|
+
# => [#<AssociationReflection @name=:grandfather, @macro=:belongs_to>, #<AssociationReflection @name=:sons, @macro=:has_many>]
|
|
78
79
|
Father.has_associations :has_many
|
|
79
|
-
# => [
|
|
80
|
+
# => [:sons]
|
|
81
|
+
Father.has_associations true
|
|
82
|
+
# => [:grandfather, :sons]
|
|
80
83
|
```
|
|
81
84
|
|
|
82
85
|
### #has_(assoc)?(`record`)
|
data/has_associations.gemspec
CHANGED
|
@@ -5,7 +5,7 @@ require 'has_associations/version'
|
|
|
5
5
|
|
|
6
6
|
Gem::Specification.new do |spec|
|
|
7
7
|
spec.name = "has_associations"
|
|
8
|
-
spec.version = HasAssociations::VERSION
|
|
8
|
+
spec.version = ActiveRecord::HasAssociations::VERSION
|
|
9
9
|
spec.authors = ["Bradyn Poulsen"]
|
|
10
10
|
spec.email = ["bradyn@bradynpoulsen.com"]
|
|
11
11
|
spec.summary = %q{ActiveRecord helpers for checking associations}
|
|
@@ -18,6 +18,9 @@ Gem::Specification.new do |spec|
|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
19
|
spec.require_paths = ["lib"]
|
|
20
20
|
|
|
21
|
+
spec.add_dependency "activesupport", ">= 3.2"
|
|
22
|
+
spec.add_dependency "activerecord", ">= 3.2"
|
|
23
|
+
|
|
21
24
|
spec.add_development_dependency "bundler", "~> 1.5"
|
|
22
25
|
spec.add_development_dependency "rake", "~> 0"
|
|
23
26
|
end
|
data/lib/has_associations.rb
CHANGED
|
@@ -6,36 +6,34 @@ module ActiveRecord
|
|
|
6
6
|
|
|
7
7
|
included do
|
|
8
8
|
## Gather associations for the current ActiveRecord model
|
|
9
|
-
def self.has_associations(
|
|
10
|
-
associations = reflect_on_all_associations
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
associations.select! { |a| a[:type] == type.to_sym } if type.respond_to? :to_sym
|
|
14
|
-
associations.map { |a| a[:name] }
|
|
9
|
+
def self.has_associations(names_with_macro = nil)
|
|
10
|
+
associations = reflect_on_all_associations names_with_macro.is_a?(Symbol) ? names_with_macro : nil
|
|
11
|
+
if names_with_macro
|
|
12
|
+
associations.map { |association| association.name }
|
|
15
13
|
else
|
|
16
14
|
associations
|
|
17
15
|
end
|
|
18
16
|
end
|
|
19
17
|
|
|
20
18
|
## Create association helpers (for use in ActiveRecord model)
|
|
21
|
-
def self.has_association_helpers(*
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
19
|
+
def self.has_association_helpers(*only_associations)
|
|
20
|
+
only_associations.map! &:to_sym
|
|
21
|
+
associations = has_associations
|
|
22
|
+
associations.select! { |association| only_associations.include? association.name } unless only_associations.empty?
|
|
25
23
|
|
|
26
|
-
|
|
27
|
-
name =
|
|
28
|
-
if [:belongs_to, :has_one].include?
|
|
24
|
+
associations.each do |association|
|
|
25
|
+
name = association.name.to_s
|
|
26
|
+
if [:belongs_to, :has_one].include? association.type
|
|
29
27
|
class_eval <<-RUBY_EVAL, __FILE__, __LINE__+1
|
|
30
28
|
def has_#{name.singularize}?(object)
|
|
31
|
-
return nil unless object.is_a? #{
|
|
29
|
+
return nil unless object.is_a? #{association.class_name}
|
|
32
30
|
#{name}.id == object.id
|
|
33
31
|
end
|
|
34
32
|
RUBY_EVAL
|
|
35
33
|
else
|
|
36
34
|
class_eval <<-RUBY_EVAL, __FILE__, __LINE__+1
|
|
37
35
|
def has_#{name.singularize}?(object)
|
|
38
|
-
return nil unless object.is_a? #{
|
|
36
|
+
return nil unless object.is_a? #{association.class_name}
|
|
39
37
|
#{name}.where(id: object.id).exists?
|
|
40
38
|
end
|
|
41
39
|
RUBY_EVAL
|
|
@@ -44,34 +42,34 @@ module ActiveRecord
|
|
|
44
42
|
end
|
|
45
43
|
|
|
46
44
|
## Process class name of object as ActiveRecord or String/Symbol
|
|
47
|
-
def self.
|
|
48
|
-
if
|
|
45
|
+
def self.model_element_name(record)
|
|
46
|
+
if record.is_a? ActiveRecord::Base
|
|
49
47
|
# if AR instance
|
|
50
|
-
|
|
48
|
+
record = record.class
|
|
51
49
|
end
|
|
52
50
|
|
|
53
|
-
if
|
|
51
|
+
if record < ActiveRecord::Base
|
|
54
52
|
# if AR class
|
|
55
|
-
|
|
56
|
-
elsif
|
|
57
|
-
|
|
53
|
+
record.model_name.element.to_sym
|
|
54
|
+
elsif record.is_a?(String) || record.is_a?(Symbol)
|
|
55
|
+
record.to_sym
|
|
58
56
|
else
|
|
59
57
|
false
|
|
60
58
|
end
|
|
61
59
|
end
|
|
62
60
|
|
|
63
|
-
%w(belongs_to has_many has_one).each do |
|
|
61
|
+
%w(belongs_to has_many has_one).each do |macro|
|
|
64
62
|
class_eval <<-RUBY_EVAL, __FILE__, __LINE__+1
|
|
65
|
-
def self.#{
|
|
66
|
-
has_association?
|
|
63
|
+
def self.#{macro}?(record)
|
|
64
|
+
has_association? record, :#{macro}
|
|
67
65
|
end
|
|
68
66
|
RUBY_EVAL
|
|
69
67
|
end
|
|
70
68
|
|
|
71
|
-
def self.has_association?(
|
|
72
|
-
name =
|
|
73
|
-
|
|
74
|
-
|
|
69
|
+
def self.has_association?(record, macro = nil)
|
|
70
|
+
name = model_element_name record
|
|
71
|
+
associations = has_associations macro || true
|
|
72
|
+
associations.include?(name) || associations.include?(name.to_s.pluralize.to_sym)
|
|
75
73
|
end
|
|
76
74
|
end
|
|
77
75
|
end
|
metadata
CHANGED
|
@@ -1,15 +1,43 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: has_associations
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Bradyn Poulsen
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2014-04-
|
|
11
|
+
date: 2014-04-08 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: activesupport
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">="
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '3.2'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">="
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '3.2'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: activerecord
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '3.2'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '3.2'
|
|
13
41
|
- !ruby/object:Gem::Dependency
|
|
14
42
|
name: bundler
|
|
15
43
|
requirement: !ruby/object:Gem::Requirement
|