has_associations 0.0.1 → 0.0.2

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: dd029faa545adfba12e9df0164da7178ad05ac73
4
- data.tar.gz: 4946e8b1b998d2435f7d789efb4d538affa210d3
3
+ metadata.gz: d1622b7b49275aafa1daba01f43ad836e328e2cb
4
+ data.tar.gz: 362e2ee7777bd1fa3623b92089d87950deacee7d
5
5
  SHA512:
6
- metadata.gz: 8550b826bedd29a08a81f03c74ca25bdcc4664473cf632a7883f28231091ff1993fcef121d4a4f32f2d7f4341636ecb9dcd505fda3e28f5288ec2259fb1a1188
7
- data.tar.gz: 338a247a228495d30b07462e8f3c569d301fbef98286c7bbab45112b82b35dec8e9e0e6eae63ba51b929fdc2e21262ed2fff2704ef3750bf3b0f724f2ed369a2
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: :grandfather
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
- ### ::belongs_to?(`AR`), ::has_many?(`AR`), ::has_one?(`AR`), ::has_association?(`AR`)
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(`type`)
68
+ ### ::has_associations(`names_with_macro = nil`)
69
69
 
70
- Gathers the ActiveRecords associations (filtering results by `type`)
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
- # => [ {:name => :sons, :type => :has_many, :class_name => "Son"} ]
80
+ # => [:sons]
81
+ Father.has_associations true
82
+ # => [:grandfather, :sons]
80
83
  ```
81
84
 
82
85
  ### #has_(assoc)?(`record`)
@@ -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
@@ -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(type = nil)
10
- associations = reflect_on_all_associations.map { |a| {:name => a.name, :type => a.macro, :class_name => a.class_name} }
11
-
12
- if type
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(*associations)
22
- associations.map! &:to_sym
23
- assoc = has_associations
24
- assoc.select! { |a| associations.include? a[:name] } unless associations.empty?
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
- assoc.each do |a|
27
- name = a[:name].to_s
28
- if [:belongs_to, :has_one].include? a[:type]
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? #{a[:class_name]}
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? #{a[:class_name]}
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.association_class_name(object)
48
- if object.is_a? ActiveRecord::Base
45
+ def self.model_element_name(record)
46
+ if record.is_a? ActiveRecord::Base
49
47
  # if AR instance
50
- object = object.class
48
+ record = record.class
51
49
  end
52
50
 
53
- if object < ActiveRecord::Base
51
+ if record < ActiveRecord::Base
54
52
  # if AR class
55
- object.model_name.element.to_sym
56
- elsif object.is_a?(String) || object.is_a?(Symbol)
57
- object.to_sym
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 |type|
61
+ %w(belongs_to has_many has_one).each do |macro|
64
62
  class_eval <<-RUBY_EVAL, __FILE__, __LINE__+1
65
- def self.#{type}?(object)
66
- has_association? object, :#{type}
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?(name, type = nil)
72
- name = association_class_name name
73
- assoc = has_associations(type || true)
74
- assoc.include?(name) || assoc.include?(name.to_s.pluralize.to_sym)
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
@@ -1,3 +1,5 @@
1
- module HasAssociations
2
- VERSION = "0.0.1"
1
+ module ActiveRecord
2
+ module HasAssociations
3
+ VERSION = "0.0.2"
4
+ end
3
5
  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.1
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-06 00:00:00.000000000 Z
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