mighty_associations 0.1.0

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: 32198de8f0eb7424cf0f245df9d0f292059e8470
4
+ data.tar.gz: aafc53eee987ad388a2c58838ba830804376330a
5
+ SHA512:
6
+ metadata.gz: 74fe6e0c854069b1e6bb812c5c8e0719e5504d0f51e416a8e12aa185f2a678014529222a64d5ec974e40357d125fd622a545687b8a533cf97f1541d2f0c3ea04
7
+ data.tar.gz: 9f16d8a415c23423f2e27ced5cf870cf5f9224acee05461e84c5037c9960771f6c324130248010ab192c42519e91c024d331c078287c539e50032ecefbe6ef1f
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ rdoc/
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,48 @@
1
+ = Mighty Associations
2
+
3
+ Extends many of your associations for free. Given the following models:
4
+
5
+ class Task < ActiveRecord::Base
6
+ belongs_to :project
7
+ end
8
+
9
+ class Project < ActiveRecord::Base
10
+ has_many :tasks
11
+ belongs_to :company
12
+
13
+ named_scope :five_letters, :conditions => "LENGTH(name) = 5"
14
+ end
15
+
16
+ class Company < ActiveRecord::Base
17
+ has_many :projects
18
+ has_many :tasks, :through => :project
19
+
20
+ named_scope :five_letters, :conditions => "LENGTH(name) = 5"
21
+ end
22
+
23
+ You can:
24
+
25
+ Get all projects belonging to a company:
26
+
27
+ Company.projects
28
+
29
+ Get all projects belonging to a company with a 5-letter name:
30
+
31
+ Company.five_letters.projects
32
+
33
+ It also works with has_many :through associations:
34
+
35
+ Company.tasks
36
+ Company.five_letters.tasks
37
+
38
+ Return value is a scope, so you can still chain it:
39
+
40
+ Company.five_letters.projects.find_by_name("Wadus")
41
+
42
+ The same applies for belongs_to associations, for example you can get the companies with a five-letter project:
43
+
44
+ Project.five_letter.companies
45
+
46
+
47
+
48
+ Copyright (c) 2009 BeBanjo, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+ require 'rdoc/task'
5
+
6
+ desc 'Default: run unit tests.'
7
+ task :default => :test
8
+
9
+ desc 'Test the Mighty Associations plugin.'
10
+ Rake::TestTask.new(:test) do |t|
11
+ t.libs << 'lib'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the Mighty Associations plugin.'
17
+ RDoc::Task.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'Mighty Associations'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README.rdoc')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'mighty_associations'
data/install.rb ADDED
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,43 @@
1
+ module MightyAssociations
2
+ end
3
+
4
+ class ActiveRecord::Base
5
+
6
+ class << self
7
+ def has_many_with_class_methods(*args, &block)
8
+ has_many_without_class_methods(*args, &block)
9
+ reflection = reflections[args.first]
10
+ if reflection.options[:through]
11
+ through_association = reflection.through_reflection.options[:as] ? reflection.through_reflection.name : reflection.through_reflection.name.to_s.singularize
12
+ class_eval <<-RUBY, __FILE__, __LINE__
13
+ def self.#{reflection.name}
14
+ ::#{reflection.class_name}.scoped(
15
+ :include => :#{through_association},
16
+ :conditions => {"#{reflection.through_reflection.table_name}.#{reflection.through_reflection.foreign_key}" => all(:select => :id)})
17
+ end
18
+ RUBY
19
+ else
20
+ class_eval <<-RUBY, __FILE__, __LINE__
21
+ def self.#{reflection.name}
22
+ ::#{reflection.class_name}.scoped(:conditions => {:#{reflection.foreign_key} => all(:select => :id)})
23
+ end
24
+ RUBY
25
+ end
26
+ end
27
+ alias_method_chain :has_many, :class_methods
28
+
29
+ def belongs_to_with_class_methods(*args, &block)
30
+ belongs_to_without_class_methods(*args, &block)
31
+ reflection = reflections[args.first]
32
+ class_eval <<-RUBY, __FILE__, __LINE__
33
+ def self.#{reflection.name.to_s.pluralize}
34
+ ::#{reflection.class_name}.scoped(:conditions => {:#{reflection.active_record.primary_key} => all(:select => :#{reflection.foreign_key}).map(&:#{reflection.foreign_key})})
35
+ end
36
+ RUBY
37
+ end
38
+ alias_method_chain :belongs_to, :class_methods
39
+ end
40
+
41
+ # Alternative approach:
42
+ # Asset.scoped(:joins => :scheduled_title, :conditions => scope(:find)[:conditions])
43
+ end
@@ -0,0 +1,3 @@
1
+ module MightyAssociations
2
+ VERSION="0.1.0"
3
+ end
@@ -0,0 +1,55 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ $:.unshift File.expand_path('../lib', __FILE__)
4
+ require 'mighty_associations/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{mighty_associations}
8
+ s.version = MightyAssociations::VERSION
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Sergio Gil", "Luismi Cavallé", "Paco Guzmán"]
12
+ s.homepage = "https://github.com//mighty_associations"
13
+ s.license = "MIT"
14
+ s.email = %q{ballsbreaking@bebanjo.com}
15
+ s.extra_rdoc_files = [
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".gitignore",
20
+ "MIT-LICENSE",
21
+ "README.rdoc",
22
+ "Rakefile",
23
+ "init.rb",
24
+ "install.rb",
25
+ "lib/mighty_associations.rb",
26
+ "lib/mighty_associations/version.rb",
27
+ "mighty_associations.gemspec",
28
+ "test/mighty_associations_test.rb",
29
+ "test/test_helper.rb",
30
+ "uninstall.rb"
31
+ ]
32
+ s.has_rdoc = true
33
+ s.homepage = %q{http://github.com/bebanjo/mighty_associations}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.2}
37
+ s.summary = %q{Traversing superpowers for your ActiveRecord associations}
38
+ s.test_files = [
39
+ "test/mighty_associations_test.rb",
40
+ "test/test_helper.rb"
41
+ ]
42
+
43
+ s.add_runtime_dependency 'activerecord', '>= 3.1.0', '< 4.0.0'
44
+ s.add_development_dependency 'sqlite3'
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
51
+ else
52
+ end
53
+ else
54
+ end
55
+ end
@@ -0,0 +1,84 @@
1
+ require File.dirname(__FILE__) + '/test_helper'
2
+
3
+ class MightyAssociationsTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ empty_tables
7
+ end
8
+
9
+ def test_schema_loaded
10
+ assert_equal([], Company.all)
11
+ assert_equal([], Project.all)
12
+ assert_equal([], Task.all)
13
+ assert_equal([], Item.all)
14
+ end
15
+
16
+ def test_has_many_in_class
17
+ project1 = Project.create!(:name => 'Wadus', :company => Company.create!(:name => 'Wadus'))
18
+ project2 = Project.create!(:name => 'Wadus')
19
+ assert_equal([project1], Company.projects)
20
+ end
21
+
22
+ def test_has_many_in_scope
23
+ project1 = Project.create!(:name => 'Wadus', :company => Company.create!(:name => 'Wadus'))
24
+ project2 = Project.create!(:name => 'Wadus', :company => Company.create!(:name => 'WadusWadus'))
25
+ assert_equal([project1], Company.five_letters.projects)
26
+ end
27
+
28
+ def test_has_many_in_association
29
+ task1 = Task.create(:name => 'Wadus', :project => Project.create!(:name => 'Wadus', :company => Company.create!(:name => 'Wadus')))
30
+ task2 = Task.create(:name => 'Wadus', :project => Project.create!(:name => 'Wadus', :company => Company.create!(:name => 'WadusWadus')))
31
+ assert_equal([task1], Company.find_by_name("Wadus").projects.tasks)
32
+ end
33
+
34
+ def test_has_many_through_in_class
35
+ task1 = Task.create!(:name => 'Wadus', :project => Project.create!(:name => 'Wadus', :company => Company.create!(:name => 'Wadus')))
36
+ task2 = Task.create!(:name => 'Wadus', :project => Project.create!(:name => 'Wadus'))
37
+ task3 = Task.create!(:name => 'Wadus')
38
+ assert_equal([task1], Company.tasks)
39
+ end
40
+
41
+ def test_has_many_through_in_scope
42
+ task1 = Task.create!(:name => 'Wadus', :project => Project.create!(:name => 'Wadus', :company => Company.create!(:name => 'Wadus')))
43
+ task2 = Task.create!(:name => 'Wadus', :project => Project.create!(:name => 'Wadus', :company => Company.create!(:name => 'WadusWadus')))
44
+ assert_equal([task1], Company.five_letters.tasks)
45
+ end
46
+
47
+ def test_has_many_through_in_association
48
+ item1 = Item.create!(:name => 'Wadus', :task => Task.create(:name => 'Wadus', :project => Project.create!(:name => 'Wadus', :company => Company.create!(:name => 'Wadus'))))
49
+ item2 = Item.create!(:name => 'Wadus', :task => Task.create(:name => 'Wadus', :project => Project.create!(:name => 'Wadus', :company => Company.create!(:name => 'WadusWadus'))))
50
+ assert_equal([item1], Company.find_by_name("Wadus").projects.items)
51
+ end
52
+
53
+ def test_has_many_is_chainable
54
+ assert Company.projects.respond_to?(:find)
55
+ end
56
+
57
+ def test_pluralized_belongs_to_in_class
58
+ company1 = Company.create!(:name => 'Wadus')
59
+ company2 = Company.create!(:name => 'WadusWadus')
60
+ company3 = Company.create!(:name => 'WadusWadusWadus')
61
+ Project.create!(:company => company1, :name => 'Wadus')
62
+ Project.create!(:company => company2, :name => 'WadusWadus')
63
+ assert_equal([company1, company2], Project.companies)
64
+ end
65
+
66
+ def test_pluralized_belongs_to_in_scope
67
+ company1 = Company.create!(:name => 'Wadus')
68
+ company2 = Company.create!(:name => 'WadusWadus')
69
+ company3 = Company.create!(:name => 'WadusWadusWadus')
70
+ Project.create!(:company => company1, :name => 'Wadus')
71
+ Project.create!(:company => company2, :name => 'WadusWadus')
72
+ assert_equal([company1], Project.five_letters.companies)
73
+ end
74
+
75
+ def test_polymorphic
76
+ company1 = Company.create!(:name => 'Wadus')
77
+ company2 = Company.create!(:name => 'WadusWadus')
78
+ tag = Tag.create!(:name => 'wadus')
79
+ Tagging.create!(:tag => tag, :taggable => company1)
80
+ assert_equal([tag], Company.tags)
81
+ end
82
+
83
+
84
+ end
@@ -0,0 +1,78 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'active_record'
4
+ require 'active_support/all'
5
+ #require 'ruby-debug'
6
+
7
+ ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ':memory:'}}
8
+ ActiveRecord::Base.establish_connection('test')
9
+
10
+ ActiveRecord::Schema.verbose = false
11
+
12
+ ActiveRecord::Schema.define(:version => 1) do
13
+ create_table :companies do |t|
14
+ t.string :name
15
+ end
16
+ create_table :projects do |t|
17
+ t.integer :company_id
18
+ t.string :name
19
+ end
20
+ create_table :tasks do |t|
21
+ t.integer :project_id
22
+ t.string :name
23
+ end
24
+ create_table :items do |t|
25
+ t.integer :task_id
26
+ t.string :name
27
+ end
28
+ create_table :tags do |t|
29
+ t.string :name
30
+ end
31
+ create_table :taggings do |t|
32
+ t.integer :tag_id, :taggable_id
33
+ t.string :taggable_type
34
+ end
35
+ end
36
+
37
+ def empty_tables
38
+ [ Company, Project, Task, Item ].each(&:destroy_all)
39
+ end
40
+
41
+ require File.dirname(__FILE__) + '/../init'
42
+
43
+ class Task < ActiveRecord::Base
44
+ belongs_to :project
45
+ has_many :items
46
+ end
47
+
48
+ class Item < ActiveRecord::Base
49
+ belongs_to :task
50
+ end
51
+
52
+ class Project < ActiveRecord::Base
53
+ has_many :tasks
54
+ has_many :items, :through => :tasks
55
+ belongs_to :company
56
+
57
+ scope :five_letters, lambda{ where("LENGTH(name) = 5") }
58
+ end
59
+
60
+ class Tag < ActiveRecord::Base
61
+ has_many :taggings
62
+ end
63
+
64
+ class Tagging < ActiveRecord::Base
65
+ belongs_to :tag
66
+ belongs_to :taggable, :polymorphic => true
67
+ end
68
+
69
+ class Company < ActiveRecord::Base
70
+ has_many :projects
71
+ has_many :tasks, :through => :projects
72
+
73
+ scope :five_letters, lambda { where("LENGTH(name) = 5") }
74
+
75
+ has_many :taggings, :as => :taggable, :dependent => :destroy, :include => :tag
76
+ has_many :tags, :through => :taggings
77
+ end
78
+
data/uninstall.rb ADDED
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mighty_associations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergio Gil
8
+ - Luismi Cavallé
9
+ - Paco Guzmán
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2014-07-04 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
22
+ - - <
23
+ - !ruby/object:Gem::Version
24
+ version: 4.0.0
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - '>='
30
+ - !ruby/object:Gem::Version
31
+ version: 3.1.0
32
+ - - <
33
+ - !ruby/object:Gem::Version
34
+ version: 4.0.0
35
+ - !ruby/object:Gem::Dependency
36
+ name: sqlite3
37
+ requirement: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ type: :development
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ description:
50
+ email: ballsbreaking@bebanjo.com
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files:
54
+ - README.rdoc
55
+ files:
56
+ - .gitignore
57
+ - MIT-LICENSE
58
+ - README.rdoc
59
+ - Rakefile
60
+ - init.rb
61
+ - install.rb
62
+ - lib/mighty_associations.rb
63
+ - lib/mighty_associations/version.rb
64
+ - mighty_associations.gemspec
65
+ - test/mighty_associations_test.rb
66
+ - test/test_helper.rb
67
+ - uninstall.rb
68
+ homepage: http://github.com/bebanjo/mighty_associations
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --charset=UTF-8
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.0.14
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Traversing superpowers for your ActiveRecord associations
93
+ test_files:
94
+ - test/mighty_associations_test.rb
95
+ - test/test_helper.rb