polyamorous 0.5.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.
- data/.gitignore +4 -0
- data/Gemfile +31 -0
- data/LICENSE +20 -0
- data/README.md +5 -0
- data/Rakefile +17 -0
- data/lib/polyamorous.rb +19 -0
- data/lib/polyamorous/join.rb +64 -0
- data/lib/polyamorous/join_association.rb +72 -0
- data/lib/polyamorous/join_dependency.rb +75 -0
- data/lib/polyamorous/version.rb +3 -0
- data/polyamorous.gemspec +36 -0
- data/spec/blueprints/articles.rb +5 -0
- data/spec/blueprints/comments.rb +5 -0
- data/spec/blueprints/notes.rb +3 -0
- data/spec/blueprints/people.rb +4 -0
- data/spec/blueprints/tags.rb +3 -0
- data/spec/helpers/polyamorous_helper.rb +11 -0
- data/spec/polyamorous/join_association_spec.rb +6 -0
- data/spec/polyamorous/join_dependency_spec.rb +52 -0
- data/spec/polyamorous/join_spec.rb +6 -0
- data/spec/spec_helper.rb +34 -0
- data/spec/support/schema.rb +103 -0
- metadata +135 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
gemspec
|
3
|
+
|
4
|
+
gem 'rake'
|
5
|
+
|
6
|
+
rails = ENV['RAILS'] || 'master'
|
7
|
+
arel = ENV['AREL'] || 'master'
|
8
|
+
|
9
|
+
arel_opts = case arel
|
10
|
+
when /\// # A path
|
11
|
+
{:path => arel}
|
12
|
+
when /^v/ # A tagged version
|
13
|
+
{:git => 'git://github.com/rails/arel.git', :tag => arel}
|
14
|
+
else
|
15
|
+
{:git => 'git://github.com/rails/arel.git', :branch => arel}
|
16
|
+
end
|
17
|
+
|
18
|
+
gem 'arel', arel_opts
|
19
|
+
|
20
|
+
case rails
|
21
|
+
when /\// # A path
|
22
|
+
gem 'activerecord', :path => "#{rails}/activerecord"
|
23
|
+
when /^v/ # A tagged version
|
24
|
+
git 'git://github.com/rails/rails.git', :tag => rails do
|
25
|
+
gem 'activerecord'
|
26
|
+
end
|
27
|
+
else
|
28
|
+
git 'git://github.com/rails/rails.git', :branch => rails do
|
29
|
+
gem 'activerecord'
|
30
|
+
end
|
31
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Ernie Miller
|
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.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new(:spec) do |rspec|
|
5
|
+
rspec.rspec_opts = ['--backtrace']
|
6
|
+
end
|
7
|
+
|
8
|
+
task :default => :spec
|
9
|
+
|
10
|
+
desc "Open an irb session with Ransack and the sample data used in specs"
|
11
|
+
task :console do
|
12
|
+
require 'irb'
|
13
|
+
require 'irb/completion'
|
14
|
+
require 'console'
|
15
|
+
ARGV.clear
|
16
|
+
IRB.start
|
17
|
+
end
|
data/lib/polyamorous.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "polyamorous/version"
|
2
|
+
|
3
|
+
module Polyamorous
|
4
|
+
if defined?(::ActiveRecord::Associations::JoinDependency)
|
5
|
+
JoinDependency = ::ActiveRecord::Associations::JoinDependency
|
6
|
+
JoinAssociation = ::ActiveRecord::Associations::JoinDependency::JoinAssociation
|
7
|
+
else
|
8
|
+
JoinDependency = ::ActiveRecord::Associations::ClassMethods::JoinDependency
|
9
|
+
JoinAssociation = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
require 'active_record'
|
14
|
+
require 'polyamorous/join'
|
15
|
+
require 'polyamorous/join_association'
|
16
|
+
require 'polyamorous/join_dependency'
|
17
|
+
|
18
|
+
Polyamorous::JoinDependency.send(:include, Polyamorous::JoinDependencyExtensions)
|
19
|
+
Polyamorous::JoinAssociation.send(:include, Polyamorous::JoinAssociationExtensions)
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module Polyamorous
|
2
|
+
class Join
|
3
|
+
attr_accessor :name
|
4
|
+
attr_reader :type, :klass
|
5
|
+
|
6
|
+
def initialize(name, type = Arel::InnerJoin, klass = nil)
|
7
|
+
@name = name
|
8
|
+
@type = convert_to_arel_join_type(type)
|
9
|
+
@klass = convert_to_class(klass) if klass
|
10
|
+
end
|
11
|
+
|
12
|
+
def klass=(klass)
|
13
|
+
@klass = convert_to_class(klass) if klass
|
14
|
+
end
|
15
|
+
|
16
|
+
def type=(type)
|
17
|
+
@type = convert_to_arel_join_type(type) if type
|
18
|
+
end
|
19
|
+
|
20
|
+
def hash
|
21
|
+
[@name, @type, @klass].hash
|
22
|
+
end
|
23
|
+
|
24
|
+
def eql?(other)
|
25
|
+
self.class == other.class &&
|
26
|
+
self.name == other.name &&
|
27
|
+
self.type == other.type &&
|
28
|
+
self.klass == other.klass
|
29
|
+
end
|
30
|
+
|
31
|
+
alias :== :eql?
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def convert_to_arel_join_type(type)
|
36
|
+
case type
|
37
|
+
when 'inner', :inner
|
38
|
+
Arel::InnerJoin
|
39
|
+
when 'outer', :outer
|
40
|
+
Arel::OuterJoin
|
41
|
+
when Class
|
42
|
+
if [Arel::InnerJoin, Arel::OuterJoin].include? type
|
43
|
+
type
|
44
|
+
else
|
45
|
+
raise ArgumentError, "#{type} cannot be converted to an ARel join type"
|
46
|
+
end
|
47
|
+
else
|
48
|
+
raise ArgumentError, "#{type} cannot be converted to an ARel join type"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def convert_to_class(value)
|
53
|
+
case value
|
54
|
+
when String, Symbol
|
55
|
+
Kernel.const_get(value)
|
56
|
+
when Class
|
57
|
+
value
|
58
|
+
else
|
59
|
+
raise ArgumentError, "#{value} cannot be converted to a Class"
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Polyamorous
|
2
|
+
module JoinAssociationExtensions
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
alias_method_chain :initialize, :polymorphism
|
7
|
+
alias_method :equality_without_polymorphism, :==
|
8
|
+
alias_method :==, :equality_with_polymorphism
|
9
|
+
|
10
|
+
if ActiveRecord::VERSION::STRING =~ /^3\.0\./
|
11
|
+
alias_method_chain :association_join, :polymorphism
|
12
|
+
else
|
13
|
+
alias_method_chain :build_constraint, :polymorphism
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize_with_polymorphism(reflection, join_dependency, parent = nil, polymorphic_class = nil)
|
19
|
+
if polymorphic_class && ::ActiveRecord::Base > polymorphic_class
|
20
|
+
swapping_reflection_klass(reflection, polymorphic_class) do |reflection|
|
21
|
+
initialize_without_polymorphism(reflection, join_dependency, parent)
|
22
|
+
end
|
23
|
+
else
|
24
|
+
initialize_without_polymorphism(reflection, join_dependency, parent)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def swapping_reflection_klass(reflection, klass)
|
29
|
+
reflection = reflection.clone
|
30
|
+
original_polymorphic = reflection.options.delete(:polymorphic)
|
31
|
+
reflection.instance_variable_set(:@klass, klass)
|
32
|
+
yield reflection
|
33
|
+
ensure
|
34
|
+
reflection.options[:polymorphic] = original_polymorphic
|
35
|
+
end
|
36
|
+
|
37
|
+
def equality_with_polymorphism(other)
|
38
|
+
equality_without_polymorphism(other) && active_record == other.active_record
|
39
|
+
end
|
40
|
+
|
41
|
+
def build_constraint_with_polymorphism(reflection, table, key, foreign_table, foreign_key)
|
42
|
+
if reflection.options[:polymorphic]
|
43
|
+
build_constraint_without_polymorphism(reflection, table, key, foreign_table, foreign_key).and(
|
44
|
+
foreign_table[reflection.foreign_type].eq(reflection.klass.name)
|
45
|
+
)
|
46
|
+
else
|
47
|
+
build_constraint_without_polymorphism(reflection, table, key, foreign_table, foreign_key)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def association_join_with_polymorphism
|
52
|
+
return @join if @Join
|
53
|
+
|
54
|
+
@join = association_join_without_polymorphism
|
55
|
+
|
56
|
+
if reflection.macro == :belongs_to && reflection.options[:polymorphic]
|
57
|
+
aliased_table = Arel::Table.new(table_name, :as => @aliased_table_name,
|
58
|
+
:engine => arel_engine,
|
59
|
+
:columns => klass.columns)
|
60
|
+
|
61
|
+
parent_table = Arel::Table.new(parent.table_name, :as => parent.aliased_table_name,
|
62
|
+
:engine => arel_engine,
|
63
|
+
:columns => parent.active_record.columns)
|
64
|
+
|
65
|
+
@join << parent_table[reflection.options[:foreign_type]].eq(reflection.klass.name)
|
66
|
+
end
|
67
|
+
|
68
|
+
@join
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module Polyamorous
|
2
|
+
module JoinDependencyExtensions
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
alias_method_chain :build, :polymorphism
|
7
|
+
alias_method_chain :graft, :polymorphism
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def graft_with_polymorphism(*associations)
|
12
|
+
associations.each do |association|
|
13
|
+
unless join_associations.detect {|a| association == a}
|
14
|
+
if association.reflection.options[:polymorphic]
|
15
|
+
build(Join.new(association.reflection.name, association.join_type, association.reflection.klass),
|
16
|
+
association.find_parent_in(self) || join_base, association.join_type)
|
17
|
+
else
|
18
|
+
build(association.reflection.name, association.find_parent_in(self) || join_base, association.join_type)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
if ActiveRecord::VERSION::STRING =~ /^3\.0\./
|
26
|
+
def _join_parts
|
27
|
+
@joins
|
28
|
+
end
|
29
|
+
else
|
30
|
+
def _join_parts
|
31
|
+
@join_parts
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def build_with_polymorphism(associations, parent = nil, join_type = Arel::InnerJoin)
|
36
|
+
case associations
|
37
|
+
when Join
|
38
|
+
parent ||= _join_parts.last
|
39
|
+
reflection = parent.reflections[associations.name] or
|
40
|
+
raise ::ActiveRecord::ConfigurationError, "Association named '#{ associations.name }' was not found; perhaps you misspelled it?"
|
41
|
+
|
42
|
+
unless join_association = find_join_association_respecting_polymorphism(reflection, parent, associations.klass)
|
43
|
+
@reflections << reflection
|
44
|
+
join_association = build_join_association_respecting_polymorphism(reflection, parent, associations.klass)
|
45
|
+
join_association.join_type = associations.type
|
46
|
+
_join_parts << join_association
|
47
|
+
cache_joined_association(join_association)
|
48
|
+
end
|
49
|
+
|
50
|
+
join_association
|
51
|
+
else
|
52
|
+
build_without_polymorphism(associations, parent, join_type)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def find_join_association_respecting_polymorphism(reflection, parent, klass)
|
57
|
+
if association = find_join_association(reflection, parent)
|
58
|
+
unless reflection.options[:polymorphic]
|
59
|
+
association
|
60
|
+
else
|
61
|
+
association if association.active_record == klass
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def build_join_association_respecting_polymorphism(reflection, parent, klass)
|
67
|
+
if reflection.options[:polymorphic] && klass
|
68
|
+
JoinAssociation.new(reflection, self, parent, klass)
|
69
|
+
else
|
70
|
+
JoinAssociation.new(reflection, self, parent)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
data/polyamorous.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "polyamorous/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "polyamorous"
|
7
|
+
s.version = Polyamorous::VERSION
|
8
|
+
s.authors = ["Ernie Miller"]
|
9
|
+
s.email = ["ernie@metautonomo.us"]
|
10
|
+
s.homepage = "http://github.com/ernie/polyamorous"
|
11
|
+
s.summary = %q{
|
12
|
+
Loves/is loved by polymorphic belongs_to associations, Ransack, Squeel, MetaSearch...
|
13
|
+
}
|
14
|
+
s.description = %q{
|
15
|
+
This is just an extraction from Ransack/Squeel. You probably don't want to use this
|
16
|
+
directly. It extends ActiveRecord's associations to support polymorphic belongs_to
|
17
|
+
associations.
|
18
|
+
}
|
19
|
+
|
20
|
+
s.rubyforge_project = "polyamorous"
|
21
|
+
|
22
|
+
s.add_dependency 'activerecord', '~> 3.0'
|
23
|
+
s.add_development_dependency 'rspec', '~> 2.6.0'
|
24
|
+
s.add_development_dependency 'machinist', '~> 1.0.6'
|
25
|
+
s.add_development_dependency 'faker', '~> 0.9.5'
|
26
|
+
s.add_development_dependency 'sqlite3', '~> 1.3.3'
|
27
|
+
|
28
|
+
s.files = `git ls-files`.split("\n")
|
29
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
30
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
31
|
+
s.require_paths = ["lib"]
|
32
|
+
|
33
|
+
# specify any dependencies here; for example:
|
34
|
+
# s.add_development_dependency "rspec"
|
35
|
+
# s.add_runtime_dependency "rest-client"
|
36
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module PolyamorousHelper
|
2
|
+
|
3
|
+
def new_join_dependency(klass, associations = {})
|
4
|
+
Polyamorous::JoinDependency.new klass, associations, []
|
5
|
+
end
|
6
|
+
|
7
|
+
def new_join(name, type = Arel::InnerJoin, klass = nil)
|
8
|
+
Polyamorous::Join.new name, type, klass
|
9
|
+
end
|
10
|
+
|
11
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Polyamorous
|
4
|
+
describe JoinDependency do
|
5
|
+
|
6
|
+
context 'with symbol joins' do
|
7
|
+
subject { new_join_dependency Person, :articles => :comments }
|
8
|
+
|
9
|
+
specify { subject.join_associations.should have(2).associations }
|
10
|
+
specify { subject.join_associations.should be_all { |a| a.join_type == Arel::InnerJoin } }
|
11
|
+
end
|
12
|
+
|
13
|
+
context 'with has_many :through association' do
|
14
|
+
subject { new_join_dependency Person, :authored_article_comments }
|
15
|
+
|
16
|
+
specify { subject.join_associations.should have(1).association }
|
17
|
+
specify { subject.join_associations.first.table_name.should eq 'comments' }
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'with outer join' do
|
21
|
+
subject { new_join_dependency Person, new_join(:articles, :outer) }
|
22
|
+
|
23
|
+
specify { subject.join_associations.should have(1).association }
|
24
|
+
specify { subject.join_associations.should be_all { |a| a.join_type == Arel::OuterJoin } }
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'with nested outer joins' do
|
28
|
+
subject { new_join_dependency Person, new_join(:articles, :outer) => new_join(:comments, :outer) }
|
29
|
+
|
30
|
+
specify { subject.join_associations.should have(2).associations }
|
31
|
+
specify { subject.join_associations.should be_all { |a| a.join_type == Arel::OuterJoin } }
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'with polymorphic belongs_to join' do
|
35
|
+
subject { new_join_dependency Note, new_join(:notable, :inner, Person) }
|
36
|
+
|
37
|
+
specify { subject.join_associations.should have(1).association }
|
38
|
+
specify { subject.join_associations.should be_all { |a| a.join_type == Arel::InnerJoin } }
|
39
|
+
specify { subject.join_associations.first.table_name.should eq 'people' }
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'with polymorphic belongs_to join and nested symbol join' do
|
43
|
+
subject { new_join_dependency Note, new_join(:notable, :inner, Person) => :comments }
|
44
|
+
|
45
|
+
specify { subject.join_associations.should have(2).association }
|
46
|
+
specify { subject.join_associations.should be_all { |a| a.join_type == Arel::InnerJoin } }
|
47
|
+
specify { subject.join_associations.first.table_name.should eq 'people' }
|
48
|
+
specify { subject.join_associations[1].table_name.should eq 'comments' }
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'machinist/active_record'
|
2
|
+
require 'sham'
|
3
|
+
require 'faker'
|
4
|
+
require 'polyamorous'
|
5
|
+
|
6
|
+
Time.zone = 'Eastern Time (US & Canada)'
|
7
|
+
|
8
|
+
Dir[File.expand_path('../{helpers,support,blueprints}/*.rb', __FILE__)].each do |f|
|
9
|
+
require f
|
10
|
+
end
|
11
|
+
|
12
|
+
Sham.define do
|
13
|
+
name { Faker::Name.name }
|
14
|
+
title { Faker::Lorem.sentence }
|
15
|
+
body { Faker::Lorem.paragraph }
|
16
|
+
salary {|index| 30000 + (index * 1000)}
|
17
|
+
tag_name { Faker::Lorem.words(3).join(' ') }
|
18
|
+
note { Faker::Lorem.words(7).join(' ') }
|
19
|
+
end
|
20
|
+
|
21
|
+
RSpec.configure do |config|
|
22
|
+
config.before(:suite) { Schema.create }
|
23
|
+
config.before(:all) { Sham.reset(:before_all) }
|
24
|
+
config.before(:each) { Sham.reset(:before_each) }
|
25
|
+
|
26
|
+
config.include PolyamorousHelper
|
27
|
+
end
|
28
|
+
|
29
|
+
RSpec::Matchers.define :be_like do |expected|
|
30
|
+
match do |actual|
|
31
|
+
actual.gsub(/^\s+|\s+$/, '').gsub(/\s+/, ' ').strip ==
|
32
|
+
expected.gsub(/^\s+|\s+$/, '').gsub(/\s+/, ' ').strip
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
|
3
|
+
ActiveRecord::Base.establish_connection(
|
4
|
+
:adapter => 'sqlite3',
|
5
|
+
:database => ':memory:'
|
6
|
+
)
|
7
|
+
|
8
|
+
class Person < ActiveRecord::Base
|
9
|
+
belongs_to :parent, :class_name => 'Person', :foreign_key => :parent_id
|
10
|
+
has_many :children, :class_name => 'Person', :foreign_key => :parent_id
|
11
|
+
has_many :articles
|
12
|
+
has_many :comments
|
13
|
+
has_many :authored_article_comments, :through => :articles,
|
14
|
+
:foreign_key => :person_id, :source => :comments
|
15
|
+
has_many :notes, :as => :notable
|
16
|
+
end
|
17
|
+
|
18
|
+
class Article < ActiveRecord::Base
|
19
|
+
belongs_to :person
|
20
|
+
has_many :comments
|
21
|
+
has_and_belongs_to_many :tags
|
22
|
+
has_many :notes, :as => :notable
|
23
|
+
end
|
24
|
+
|
25
|
+
class Comment < ActiveRecord::Base
|
26
|
+
belongs_to :article
|
27
|
+
belongs_to :person
|
28
|
+
end
|
29
|
+
|
30
|
+
class Tag < ActiveRecord::Base
|
31
|
+
has_and_belongs_to_many :articles
|
32
|
+
end
|
33
|
+
|
34
|
+
class Note < ActiveRecord::Base
|
35
|
+
belongs_to :notable, :polymorphic => true
|
36
|
+
end
|
37
|
+
|
38
|
+
module Schema
|
39
|
+
def self.create
|
40
|
+
ActiveRecord::Base.silence do
|
41
|
+
ActiveRecord::Migration.verbose = false
|
42
|
+
|
43
|
+
ActiveRecord::Schema.define do
|
44
|
+
create_table :people, :force => true do |t|
|
45
|
+
t.integer :parent_id
|
46
|
+
t.string :name
|
47
|
+
t.integer :salary
|
48
|
+
t.boolean :awesome, :default => false
|
49
|
+
t.timestamps
|
50
|
+
end
|
51
|
+
|
52
|
+
create_table :articles, :force => true do |t|
|
53
|
+
t.integer :person_id
|
54
|
+
t.string :title
|
55
|
+
t.text :body
|
56
|
+
end
|
57
|
+
|
58
|
+
create_table :comments, :force => true do |t|
|
59
|
+
t.integer :article_id
|
60
|
+
t.integer :person_id
|
61
|
+
t.text :body
|
62
|
+
end
|
63
|
+
|
64
|
+
create_table :tags, :force => true do |t|
|
65
|
+
t.string :name
|
66
|
+
end
|
67
|
+
|
68
|
+
create_table :articles_tags, :force => true, :id => false do |t|
|
69
|
+
t.integer :article_id
|
70
|
+
t.integer :tag_id
|
71
|
+
end
|
72
|
+
|
73
|
+
create_table :notes, :force => true do |t|
|
74
|
+
t.integer :notable_id
|
75
|
+
t.string :notable_type
|
76
|
+
t.string :note
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
10.times do
|
83
|
+
person = Person.make
|
84
|
+
Note.make(:notable => person)
|
85
|
+
3.times do
|
86
|
+
article = Article.make(:person => person)
|
87
|
+
3.times do
|
88
|
+
article.tags = [Tag.make, Tag.make, Tag.make]
|
89
|
+
end
|
90
|
+
Note.make(:notable => article)
|
91
|
+
10.times do
|
92
|
+
Comment.make(:article => article)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
2.times do
|
96
|
+
Comment.make(:person => person)
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
Comment.make(:body => 'First post!', :article => Article.make(:title => 'Hello, world!'))
|
101
|
+
|
102
|
+
end
|
103
|
+
end
|
metadata
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: polyamorous
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.5.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ernie Miller
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-03 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: &70198431647040 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70198431647040
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec
|
27
|
+
requirement: &70198431646120 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 2.6.0
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70198431646120
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: machinist
|
38
|
+
requirement: &70198431645200 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.0.6
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70198431645200
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: faker
|
49
|
+
requirement: &70198431644480 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.9.5
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70198431644480
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: sqlite3
|
60
|
+
requirement: &70198431643680 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ~>
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 1.3.3
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70198431643680
|
69
|
+
description: ! "\n This is just an extraction from Ransack/Squeel. You probably
|
70
|
+
don't want to use this\n directly. It extends ActiveRecord's associations to
|
71
|
+
support polymorphic belongs_to\n associations.\n "
|
72
|
+
email:
|
73
|
+
- ernie@metautonomo.us
|
74
|
+
executables: []
|
75
|
+
extensions: []
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- .gitignore
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- lib/polyamorous.rb
|
84
|
+
- lib/polyamorous/join.rb
|
85
|
+
- lib/polyamorous/join_association.rb
|
86
|
+
- lib/polyamorous/join_dependency.rb
|
87
|
+
- lib/polyamorous/version.rb
|
88
|
+
- polyamorous.gemspec
|
89
|
+
- spec/blueprints/articles.rb
|
90
|
+
- spec/blueprints/comments.rb
|
91
|
+
- spec/blueprints/notes.rb
|
92
|
+
- spec/blueprints/people.rb
|
93
|
+
- spec/blueprints/tags.rb
|
94
|
+
- spec/helpers/polyamorous_helper.rb
|
95
|
+
- spec/polyamorous/join_association_spec.rb
|
96
|
+
- spec/polyamorous/join_dependency_spec.rb
|
97
|
+
- spec/polyamorous/join_spec.rb
|
98
|
+
- spec/spec_helper.rb
|
99
|
+
- spec/support/schema.rb
|
100
|
+
homepage: http://github.com/ernie/polyamorous
|
101
|
+
licenses: []
|
102
|
+
post_install_message:
|
103
|
+
rdoc_options: []
|
104
|
+
require_paths:
|
105
|
+
- lib
|
106
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ! '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project: polyamorous
|
120
|
+
rubygems_version: 1.8.10
|
121
|
+
signing_key:
|
122
|
+
specification_version: 3
|
123
|
+
summary: Loves/is loved by polymorphic belongs_to associations, Ransack, Squeel, MetaSearch...
|
124
|
+
test_files:
|
125
|
+
- spec/blueprints/articles.rb
|
126
|
+
- spec/blueprints/comments.rb
|
127
|
+
- spec/blueprints/notes.rb
|
128
|
+
- spec/blueprints/people.rb
|
129
|
+
- spec/blueprints/tags.rb
|
130
|
+
- spec/helpers/polyamorous_helper.rb
|
131
|
+
- spec/polyamorous/join_association_spec.rb
|
132
|
+
- spec/polyamorous/join_dependency_spec.rb
|
133
|
+
- spec/polyamorous/join_spec.rb
|
134
|
+
- spec/spec_helper.rb
|
135
|
+
- spec/support/schema.rb
|