has_associations 0.0.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dd029faa545adfba12e9df0164da7178ad05ac73
4
+ data.tar.gz: 4946e8b1b998d2435f7d789efb4d538affa210d3
5
+ SHA512:
6
+ metadata.gz: 8550b826bedd29a08a81f03c74ca25bdcc4664473cf632a7883f28231091ff1993fcef121d4a4f32f2d7f4341636ecb9dcd505fda3e28f5288ec2259fb1a1188
7
+ data.tar.gz: 338a247a228495d30b07462e8f3c569d301fbef98286c7bbab45112b82b35dec8e9e0e6eae63ba51b929fdc2e21262ed2fff2704ef3750bf3b0f724f2ed369a2
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in has_associations.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Bradyn Poulsen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,134 @@
1
+ # HasAssociations
2
+
3
+ ActiveRecord association helpers
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'has_associations'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install has_associations
18
+
19
+ ## Method Overview
20
+
21
+ Refer to these ActiveRecord models for overview examples
22
+
23
+ ```ruby
24
+ class Grandfather < ActiveRecord::Base
25
+ has_many :fathers
26
+ has_many :sons, through: :father
27
+ has_association_helpers :sons
28
+ end
29
+
30
+ class Father < ActiveRecord::Base
31
+ has_many :sons
32
+ belongs_to :grandfather
33
+ has_association_helpers
34
+ end
35
+
36
+ class Son < ActiveRecord::Base
37
+ belongs_to :father
38
+ has_one :grandfather, through: :grandfather
39
+ has_association_helpers
40
+ end
41
+ ```
42
+
43
+ ### ::has_association_helpers(`*associations`)
44
+
45
+ Creates `has_(assoc)?` helpers for `associations`
46
+ ```ruby
47
+ grandfather = Grandfather.create
48
+ father = Father.create :grandfather => grandfather
49
+ son = Son.create :father => father
50
+
51
+ grandfather.has_son? son
52
+ # => true
53
+ grandfather.has_father? father
54
+ # => NameError: undefined local variable or method `has_father?`
55
+ ```
56
+
57
+ ### ::belongs_to?(`AR`), ::has_many?(`AR`), ::has_one?(`AR`), ::has_association?(`AR`)
58
+
59
+ Verifies the ActiveRecord association exists
60
+
61
+ ```ruby
62
+ Son.belongs_to? Father # true
63
+ Father.has_many? Son # true
64
+ Son.has_one? Father # false
65
+ Son.has_one? Grandfather # true
66
+ ```
67
+
68
+ ### ::has_associations(`type`)
69
+
70
+ Gathers the ActiveRecords associations (filtering results by `type`)
71
+
72
+ ```ruby
73
+ Father.has_associations
74
+ # => [
75
+ # {:name => :sons, :type => :has_many, :class_name => "Son"},
76
+ # {:name => :grandfather, :type => :belongs_to, :class_name => "Grandfather"}
77
+ # ]
78
+ Father.has_associations :has_many
79
+ # => [ {:name => :sons, :type => :has_many, :class_name => "Son"} ]
80
+ ```
81
+
82
+ ### #has_(assoc)?(`record`)
83
+
84
+ Verifies the current ActiveRecord has the specified relationship with `record`;
85
+ returns `nil` if relationship does not exist. Method(s) created with `has_association_helpers`
86
+
87
+ ```ruby
88
+ bob = Grandfather.create :name => 'Jerry'
89
+ dave = Father.create :name => 'Dave', :grandfather => bob
90
+ james = Son.create :name => 'James', :father => dave
91
+
92
+ bob.has_son? james # true
93
+ bob.has_father? dave # NameError: undefined local variable or method `has_father?`
94
+ dave.has_son? james # true
95
+ james.has_father? dave # true
96
+ james.has_father? bob # nil
97
+ ```
98
+
99
+ ## Example Usage
100
+
101
+ ```ruby
102
+ class Sport < ActiveRecord::Base
103
+ has_many :teams
104
+ has_association_helpers
105
+ end
106
+
107
+ class Team < ActiveRecord::Base
108
+ belongs_to :sport
109
+ has_association_helpers
110
+ end
111
+
112
+ Sport.has_many? Team # true
113
+ Team.belongs_to? Sport # true
114
+ Sport.has_one? Team # false
115
+
116
+ strikers = Team.create :name => 'Strikers' # #<Team id: 1, name: 'Strikers'>
117
+ soccer = Sport.create :name => 'Soccer' # #<Soccer id: 1, name: 'Soccer'>
118
+
119
+ soccer.has_team? strikers # false
120
+ strikers.has_sport? soccer # false
121
+
122
+ strikers.update_attributes :sport => soccer
123
+
124
+ soccer.has_team? strikers # true
125
+ strikers.has_sport? soccer # true
126
+ ```
127
+
128
+ ## Contributing
129
+
130
+ 1. Fork it ( http://github.com/bradynpoulsen/has_associations/fork )
131
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
132
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
133
+ 4. Push to the branch (`git push origin my-new-feature`)
134
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'has_associations/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "has_associations"
8
+ spec.version = HasAssociations::VERSION
9
+ spec.authors = ["Bradyn Poulsen"]
10
+ spec.email = ["bradyn@bradynpoulsen.com"]
11
+ spec.summary = %q{ActiveRecord helpers for checking associations}
12
+ spec.description = ""
13
+ spec.homepage = "https://github.com/bradynpoulsen/has_associations"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake", "~> 0"
23
+ end
@@ -0,0 +1,3 @@
1
+ module HasAssociations
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,80 @@
1
+ require "has_associations/version"
2
+
3
+ module ActiveRecord
4
+ module HasAssociations
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
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] }
15
+ else
16
+ associations
17
+ end
18
+ end
19
+
20
+ ## 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?
25
+
26
+ assoc.each do |a|
27
+ name = a[:name].to_s
28
+ if [:belongs_to, :has_one].include? a[:type]
29
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__+1
30
+ def has_#{name.singularize}?(object)
31
+ return nil unless object.is_a? #{a[:class_name]}
32
+ #{name}.id == object.id
33
+ end
34
+ RUBY_EVAL
35
+ else
36
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__+1
37
+ def has_#{name.singularize}?(object)
38
+ return nil unless object.is_a? #{a[:class_name]}
39
+ #{name}.where(id: object.id).exists?
40
+ end
41
+ RUBY_EVAL
42
+ end
43
+ end
44
+ end
45
+
46
+ ## Process class name of object as ActiveRecord or String/Symbol
47
+ def self.association_class_name(object)
48
+ if object.is_a? ActiveRecord::Base
49
+ # if AR instance
50
+ object = object.class
51
+ end
52
+
53
+ if object < ActiveRecord::Base
54
+ # if AR class
55
+ object.model_name.element.to_sym
56
+ elsif object.is_a?(String) || object.is_a?(Symbol)
57
+ object.to_sym
58
+ else
59
+ false
60
+ end
61
+ end
62
+
63
+ %w(belongs_to has_many has_one).each do |type|
64
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__+1
65
+ def self.#{type}?(object)
66
+ has_association? object, :#{type}
67
+ end
68
+ RUBY_EVAL
69
+ end
70
+
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)
75
+ end
76
+ end
77
+ end
78
+ end
79
+
80
+ ActiveRecord::Base.send :include, ActiveRecord::HasAssociations
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_associations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bradyn Poulsen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: ''
42
+ email:
43
+ - bradyn@bradynpoulsen.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - has_associations.gemspec
54
+ - lib/has_associations.rb
55
+ - lib/has_associations/version.rb
56
+ homepage: https://github.com/bradynpoulsen/has_associations
57
+ licenses:
58
+ - MIT
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.2.2
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: ActiveRecord helpers for checking associations
80
+ test_files: []
81
+ has_rdoc: