active_modularity 1.0.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 +7 -0
- data/.gitignore +21 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +31 -0
- data/README.md +133 -0
- data/Rakefile +24 -0
- data/active_modularity.gemspec +29 -0
- data/lib/active_modularity/modulize.rb +65 -0
- data/lib/active_modularity/version.rb +3 -0
- data/lib/active_modularity.rb +13 -0
- data/spec/active_modularity_spec.rb +133 -0
- data/spec/spec_helper.rb +16 -0
- data/spec/support/database_cleaner.rb +16 -0
- data/spec/support/models.rb +56 -0
- metadata +174 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e14b31ee831e9a582ac868ad6f6197fc3c0d6bff
|
4
|
+
data.tar.gz: 80c01c7311c87a7f95faa9ac8ec1ff22477b4704
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 204b5c3de34bc895263d142cf750fbc0769eb032e0a3aa8232acdaf09c7f23f6ba7ff00bd64497bad8768cabdfc278cb0c3c9f345900857066f8b587e6de2163
|
7
|
+
data.tar.gz: 0182d3828ee3b8a1ebf14e8c1af1fcc873e16085ca81d21b575fca83b36de9c696c016162a8e041b60271140298e9a1af9f9faee543d842155b2c1743e1919d3
|
data/.gitignore
ADDED
@@ -0,0 +1,21 @@
|
|
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
|
18
|
+
|
19
|
+
.bundle
|
20
|
+
vendor/bundle
|
21
|
+
Gemfile.lock
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
Copyright (c) 2013, Synergy Marketing, Inc.
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions
|
6
|
+
are met:
|
7
|
+
|
8
|
+
Redistributions of source code must retain the above copyright notice,
|
9
|
+
this list of conditions and the following disclaimer.
|
10
|
+
|
11
|
+
Redistributions in binary form must reproduce the above copyright
|
12
|
+
notice, this list of conditions and the following disclaimer in the
|
13
|
+
documentation and/or other materials provided with the distribution.
|
14
|
+
|
15
|
+
Neither the name of the Synergy Marketing, Inc. nor the names of its
|
16
|
+
contributors may be used to endorse or promote products derived from
|
17
|
+
this software without specific prior written permission.
|
18
|
+
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
20
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
21
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
22
|
+
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
23
|
+
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
24
|
+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
25
|
+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
26
|
+
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
27
|
+
AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
29
|
+
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
30
|
+
DAMAGE.
|
31
|
+
|
data/README.md
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
# ActiveModularity
|
2
|
+
|
3
|
+
ActiveRecord model inheritance support by module.
|
4
|
+
Fix inner module association and single table inheritance.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'active_modularity'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install active_modularity
|
19
|
+
|
20
|
+
## Examples ##
|
21
|
+
|
22
|
+
# config/initializers/acts_as_modurarity.rb
|
23
|
+
# enable active modurality
|
24
|
+
ActiveRecord::Base.acts_as_modurality
|
25
|
+
|
26
|
+
#
|
27
|
+
# has many association
|
28
|
+
#
|
29
|
+
|
30
|
+
# app/models/user.rb
|
31
|
+
class Person < ActiveRecord::Base
|
32
|
+
has_many :entries
|
33
|
+
end
|
34
|
+
|
35
|
+
# app/models/entry.rb
|
36
|
+
class Entry < ActiveRecord::Base
|
37
|
+
belongs_to :person
|
38
|
+
end
|
39
|
+
|
40
|
+
# app/models/admin/user.rb
|
41
|
+
module Admin
|
42
|
+
class Person < ::Person
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# app/models/admin/user.rb
|
47
|
+
module Admin
|
48
|
+
class Entry < ::Entry
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# standard association
|
53
|
+
person = Person.create
|
54
|
+
person.entries.build.class.name # Person
|
55
|
+
|
56
|
+
# inner module association
|
57
|
+
admin_person = Admin::Person.create
|
58
|
+
admin_person.entries.build.class.name # Admin::Person
|
59
|
+
|
60
|
+
|
61
|
+
#
|
62
|
+
# single table inheritance
|
63
|
+
#
|
64
|
+
|
65
|
+
# app/models/person.rb
|
66
|
+
class Person < ActiveRecord::Base
|
67
|
+
end
|
68
|
+
|
69
|
+
# app/models/customer.rb
|
70
|
+
class Customer < Person
|
71
|
+
end
|
72
|
+
|
73
|
+
# app/models/employee.rb
|
74
|
+
class Employee < Person
|
75
|
+
end
|
76
|
+
|
77
|
+
# app/models/admin/person.rb
|
78
|
+
module Admin
|
79
|
+
class Person < ::Person
|
80
|
+
module Common
|
81
|
+
extend ActiveSupport::Concern
|
82
|
+
included do
|
83
|
+
# Admin::Person common class macro
|
84
|
+
validates :name, :presence => true
|
85
|
+
end
|
86
|
+
|
87
|
+
def common_method
|
88
|
+
# do something...
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
# app/models/admin/customer.rb
|
95
|
+
module Admin
|
96
|
+
class Customer < ::Customer
|
97
|
+
# include Admin::Person common code
|
98
|
+
include Person::Common
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# app/models/admin/employee.rb
|
103
|
+
module Admin
|
104
|
+
class Employee < ::Employee
|
105
|
+
# include Admin::Person common code
|
106
|
+
include Person::Common
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
|
111
|
+
# create
|
112
|
+
alice = Customer.create(:name => "alice")
|
113
|
+
alice.class.name # Customer
|
114
|
+
alice.type # Customer
|
115
|
+
|
116
|
+
bob = Admin::Customer.create(:name => "bob")
|
117
|
+
bob.class.name # Admin::Customer
|
118
|
+
bob.type # Customer (not Admin::Customer)
|
119
|
+
|
120
|
+
# find
|
121
|
+
Person.find(alice.id).class.name # Customer
|
122
|
+
Person.find(bob.id ).class.name # Customer
|
123
|
+
Admin::Person.find(alice.id).class.name # Admin::Customer
|
124
|
+
Admin::Person.find(bob.id ).class.name # Admin::Customer
|
125
|
+
|
126
|
+
|
127
|
+
## Contributing
|
128
|
+
|
129
|
+
1. Fork it
|
130
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
131
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
132
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
133
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
|
5
|
+
require 'rspec/core'
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
8
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
9
|
+
end
|
10
|
+
namespace :spec do
|
11
|
+
desc "Create rspec coverage"
|
12
|
+
task :coverage do
|
13
|
+
ENV['COVERAGE'] = 'true'
|
14
|
+
Rake::Task["spec"].execute
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "boot console with spec helpers"
|
18
|
+
task :console do
|
19
|
+
require File.expand_path('../spec/spec_helper.rb', __FILE__)
|
20
|
+
require 'irb'
|
21
|
+
ARGV.clear
|
22
|
+
IRB.start
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'active_modularity/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "active_modularity"
|
8
|
+
spec.version = ActiveModularity::VERSION
|
9
|
+
spec.authors = ["yuki teraoka"]
|
10
|
+
spec.email = ["info@techscore.com"]
|
11
|
+
spec.description = %q{ActiveRecord model inheritance support by module.}
|
12
|
+
spec.summary = %q{ActiveRecord model inheritance support by module. Fix inner module association and single table inheritance.}
|
13
|
+
spec.homepage = "https://github.com/techscore/active_modularity"
|
14
|
+
spec.license = "BSD"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "simplecov"
|
25
|
+
spec.add_development_dependency "activerecord", ">= 3.0.0"
|
26
|
+
spec.add_development_dependency "sqlite3-ruby"
|
27
|
+
spec.add_development_dependency "database_cleaner"
|
28
|
+
spec.add_runtime_dependency "activerecord", ">= 3.0.0"
|
29
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'active_support/concern'
|
2
|
+
|
3
|
+
module ActiveModularity
|
4
|
+
module Modulize
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
included do
|
7
|
+
modulize!
|
8
|
+
singleton_class.alias_method_chain :inherited, :modularity
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
def inherited_with_modularity(subclass)
|
13
|
+
inherited_without_modularity(subclass).tap do
|
14
|
+
subclass.modulize!
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def modulize!
|
19
|
+
return if self == ActiveRecord::Base
|
20
|
+
modulize_reflections!
|
21
|
+
self.store_full_sti_class = false
|
22
|
+
force_sti_base!
|
23
|
+
end
|
24
|
+
|
25
|
+
# fix inner module single table inheritance
|
26
|
+
def find_sti_class(type_name)
|
27
|
+
module_name = self.name.deconstantize
|
28
|
+
if type_name && module_name.present?
|
29
|
+
type_name = "#{module_name}::#{type_name}"
|
30
|
+
end
|
31
|
+
super
|
32
|
+
end
|
33
|
+
|
34
|
+
# fix inner module sti base class
|
35
|
+
def force_sti_base!
|
36
|
+
if ActiveRecord::Base != superclass &&
|
37
|
+
!self.abstract_class? &&
|
38
|
+
superclass.descends_from_active_record? &&
|
39
|
+
superclass.name == name.demodulize &&
|
40
|
+
columns_hash.include?(inheritance_column)
|
41
|
+
singleton_class.send(:define_method, :descends_from_active_record?) { true }
|
42
|
+
true
|
43
|
+
else
|
44
|
+
false
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# fix inner module reflections
|
49
|
+
def modulize_reflections!
|
50
|
+
self.reflections = Hash[superclass.reflections.map{|k,v| [k, modulize_reflection(v)]}]
|
51
|
+
end
|
52
|
+
|
53
|
+
def modulize_reflection(reflection)
|
54
|
+
return reflection if reflection.active_record != superclass
|
55
|
+
return reflection unless reflection.kind_of?(ActiveRecord::Reflection::AssociationReflection)
|
56
|
+
modulized_reflection = reflection.dup
|
57
|
+
modulized_reflection.instance_variable_set(:@active_record, self)
|
58
|
+
modulized_reflection.instance_variable_set(:@class_name, nil)
|
59
|
+
modulized_reflection.instance_variable_set(:@klass, nil)
|
60
|
+
modulized_reflection.instance_variable_set(:@foreign_key, reflection.foreign_key)
|
61
|
+
modulized_reflection
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require "active_modularity/version"
|
2
|
+
require 'active_modularity/modulize'
|
3
|
+
require 'active_support'
|
4
|
+
|
5
|
+
module ActiveModularity
|
6
|
+
def acts_as_modularity
|
7
|
+
include ActiveModularity::Modulize
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
ActiveSupport.on_load(:active_record) do
|
12
|
+
ActiveRecord::Base.send(:extend, ActiveModularity)
|
13
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe ActiveModularity do
|
4
|
+
context 'Has One Association' do
|
5
|
+
before(:each) do
|
6
|
+
@person = Person.create!
|
7
|
+
@profile = @person.create_profile!
|
8
|
+
@admin_person = Admin::Person.create!
|
9
|
+
@admin_profile = @admin_person.create_profile!
|
10
|
+
end
|
11
|
+
|
12
|
+
it { expect(@profile ).to be_an_instance_of(Profile ) }
|
13
|
+
it { expect(@admin_profile ).to be_an_instance_of(Admin::Profile ) }
|
14
|
+
it { expect(Person.first.profile ).to be_an_instance_of(Profile ) }
|
15
|
+
it { expect(Admin::Person.first.profile ).to be_an_instance_of(Admin::Profile ) }
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'Belongs To Association' do
|
19
|
+
before(:each) do
|
20
|
+
@person = Person.create!
|
21
|
+
@profile = @person.create_profile!
|
22
|
+
end
|
23
|
+
|
24
|
+
it { expect(Profile.find(@profile).person ).to be_an_instance_of(Person ) }
|
25
|
+
it { expect(Admin::Profile.find(@profile).person ).to be_an_instance_of(Admin::Person ) }
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'Has Many Association' do
|
29
|
+
before(:each) do
|
30
|
+
@customer = Customer.create!
|
31
|
+
@admin_customer = Admin::Customer.create!
|
32
|
+
@entry = @customer.entries.create!
|
33
|
+
@admin_entry = @admin_customer.entries.create!
|
34
|
+
end
|
35
|
+
|
36
|
+
it { expect(@entry ).to be_an_instance_of(Entry ) }
|
37
|
+
it { expect(@admin_entry ).to be_an_instance_of(Admin::Entry ) }
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'Has Many Through Association' do
|
41
|
+
before(:each) do
|
42
|
+
@entry = Entry.create!
|
43
|
+
@admin_entry = Admin::Entry.create!
|
44
|
+
@tag = @entry.tags.create!
|
45
|
+
@admin_tag = @admin_entry.tags.create!
|
46
|
+
end
|
47
|
+
|
48
|
+
it { expect(@tag ).to be_an_instance_of(Tag ) }
|
49
|
+
it { expect(@admin_tag ).to be_an_instance_of(Admin::Tag ) }
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'Has And Belongs To Many Association' do
|
53
|
+
before(:each) do
|
54
|
+
@tag = Tag.create!
|
55
|
+
@admin_tag = Admin::Tag.create!
|
56
|
+
@entry = @tag.entries.create!
|
57
|
+
@admin_entry = @admin_tag.entries.create!
|
58
|
+
end
|
59
|
+
|
60
|
+
it { expect(@entry ).to be_an_instance_of(Entry ) }
|
61
|
+
it { expect(@admin_entry ).to be_an_instance_of(Admin::Entry ) }
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'Polymorphic Association' do
|
65
|
+
before(:each) do
|
66
|
+
@person = Person.create!
|
67
|
+
@entry = Entry.create!
|
68
|
+
@person_resource = @person.resources.create!
|
69
|
+
@entry_resource = @entry.resources.create!
|
70
|
+
@admin_person = Admin::Person.create!
|
71
|
+
@admin_entry = Admin::Entry.create!
|
72
|
+
@admin_person_resource = @admin_person.resources.create!
|
73
|
+
@admin_entry_resource = @admin_entry.resources.create!
|
74
|
+
end
|
75
|
+
|
76
|
+
it { expect(@person_resource).to be_an_instance_of(Resource ) }
|
77
|
+
it { expect(@entry_resource ).to be_an_instance_of(Resource ) }
|
78
|
+
it { expect(Person.find(@admin_person.id).resources.first.id).to eq(@admin_person_resource.id) }
|
79
|
+
it { expect(Entry.find(@admin_entry.id).resources.first.id ).to eq(@admin_entry_resource.id ) }
|
80
|
+
|
81
|
+
it { expect(@admin_person_resource).to be_an_instance_of(Admin::Resource ) }
|
82
|
+
it { expect(@admin_entry_resource ).to be_an_instance_of(Admin::Resource ) }
|
83
|
+
it { expect(Admin::Person.find(@person.id).resources.first.id).to eq(@person_resource.id) }
|
84
|
+
it { expect(Admin::Entry.find(@entry.id).resources.first.id ).to eq(@entry_resource.id ) }
|
85
|
+
end
|
86
|
+
|
87
|
+
context 'Single table inheritance' do
|
88
|
+
before(:each) do
|
89
|
+
@person = Person.create!
|
90
|
+
@customer = Customer.create!
|
91
|
+
@employee = Employee.create!
|
92
|
+
@admin_person = Admin::Person.create!
|
93
|
+
@admin_customer = Admin::Customer.create!
|
94
|
+
@admin_employee = Admin::Employee.create!
|
95
|
+
end
|
96
|
+
|
97
|
+
it { expect(Person.all.map(&:class)).to be_all{|klass| [Person, Customer, Employee].include?(klass) } }
|
98
|
+
|
99
|
+
it { expect(@person ).to be_an_instance_of(Person ) }
|
100
|
+
it { expect(@customer).to be_an_instance_of(Customer) }
|
101
|
+
it { expect(@employee).to be_an_instance_of(Employee) }
|
102
|
+
|
103
|
+
it { expect(Person.find(@admin_person.id )).to be_an_instance_of(Person ) }
|
104
|
+
it { expect(Person.find(@admin_customer.id)).to be_an_instance_of(Customer) }
|
105
|
+
it { expect(Person.find(@admin_employee.id)).to be_an_instance_of(Employee) }
|
106
|
+
|
107
|
+
it { expect{Customer.find(@admin_person.id )}.to raise_error }
|
108
|
+
it { expect(Customer.find(@admin_customer.id)).to be_an_instance_of(Customer) }
|
109
|
+
it { expect{Customer.find(@admin_employee.id)}.to raise_error }
|
110
|
+
|
111
|
+
it { expect{Employee.find(@admin_person.id )}.to raise_error }
|
112
|
+
it { expect{Employee.find(@admin_customer.id)}.to raise_error }
|
113
|
+
it { expect(Employee.find(@admin_employee.id)).to be_an_instance_of(Employee) }
|
114
|
+
|
115
|
+
it { expect(Admin::Person.all.map(&:class)).to be_all{|klass| [Admin::Person, Admin::Customer, Admin::Employee].include?(klass) } }
|
116
|
+
|
117
|
+
it { expect(@admin_person ).to be_an_instance_of(Admin::Person ) }
|
118
|
+
it { expect(@admin_customer).to be_an_instance_of(Admin::Customer) }
|
119
|
+
it { expect(@admin_employee).to be_an_instance_of(Admin::Employee) }
|
120
|
+
|
121
|
+
it { expect(Admin::Person.find(@person.id )).to be_an_instance_of(Admin::Person ) }
|
122
|
+
it { expect(Admin::Person.find(@customer.id)).to be_an_instance_of(Admin::Customer) }
|
123
|
+
it { expect(Admin::Person.find(@employee.id)).to be_an_instance_of(Admin::Employee) }
|
124
|
+
|
125
|
+
it { expect{Admin::Customer.find(@person.id )}.to raise_error }
|
126
|
+
it { expect(Admin::Customer.find(@customer.id)).to be_an_instance_of(Admin::Customer) }
|
127
|
+
it { expect{Admin::Customer.find(@employee.id)}.to raise_error }
|
128
|
+
|
129
|
+
it { expect{Admin::Employee.find(@person.id )}.to raise_error }
|
130
|
+
it { expect{Admin::Employee.find(@customer.id)}.to raise_error }
|
131
|
+
it { expect(Admin::Employee.find(@employee.id)).to be_an_instance_of(Admin::Employee) }
|
132
|
+
end
|
133
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
if ENV['COVERAGE']
|
2
|
+
require 'simplecov'
|
3
|
+
SimpleCov.start do
|
4
|
+
add_filter '/spec/'
|
5
|
+
add_filter '/vendor/'
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
10
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
11
|
+
require 'rspec'
|
12
|
+
require 'active_modularity'
|
13
|
+
|
14
|
+
# Requires supporting files with custom matchers and macros, etc,
|
15
|
+
# in ./support/ and its subdirectories.
|
16
|
+
Dir["#{File.dirname(__FILE__)}/support/*.rb"].each {|f| require f}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'database_cleaner'
|
3
|
+
|
4
|
+
DatabaseCleaner[:active_record].strategy = :transaction if defined? ActiveRecord
|
5
|
+
|
6
|
+
RSpec.configure do |config|
|
7
|
+
config.before :suite do
|
8
|
+
DatabaseCleaner.clean_with :truncation
|
9
|
+
end
|
10
|
+
config.before :each do
|
11
|
+
DatabaseCleaner.start
|
12
|
+
end
|
13
|
+
config.after :each do
|
14
|
+
DatabaseCleaner.clean
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ':memory:')
|
3
|
+
|
4
|
+
#migrations
|
5
|
+
class CreateAllTables < ActiveRecord::Migration
|
6
|
+
def self.up
|
7
|
+
create_table(:people ) {|t| t.string :name; t.string :type}
|
8
|
+
create_table(:resources ) {|t| t.string :owner_type; t.integer :owner_id; t.text :content }
|
9
|
+
create_table(:profiles ) {|t| t.integer :person_id }
|
10
|
+
create_table(:entries ) {|t| t.integer :person_id; t.string :name }
|
11
|
+
create_table(:tags ) {|t| t.string :name }
|
12
|
+
create_table(:entries_tags) {|t| t.integer :entry_id; t.integer :tag_id}
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
ActiveRecord::Migration.verbose = false
|
17
|
+
CreateAllTables.up
|
18
|
+
|
19
|
+
ActiveRecord::Base.acts_as_modularity
|
20
|
+
|
21
|
+
class Person < ActiveRecord::Base
|
22
|
+
has_one :profile
|
23
|
+
has_many :entries
|
24
|
+
has_many :resources, :as => :owner
|
25
|
+
end
|
26
|
+
class Customer < Person;end;
|
27
|
+
class Employee < Person;end;
|
28
|
+
class Profile < ActiveRecord::Base
|
29
|
+
belongs_to :person
|
30
|
+
end
|
31
|
+
class Resource < ActiveRecord::Base
|
32
|
+
belongs_to :owner, :polymorphic => true
|
33
|
+
end
|
34
|
+
class Entry < ActiveRecord::Base
|
35
|
+
has_many :resources, :as => :owner
|
36
|
+
has_many :entries_tags
|
37
|
+
has_many :tags, :through => :entries_tags
|
38
|
+
end
|
39
|
+
class Tag < ActiveRecord::Base
|
40
|
+
has_and_belongs_to_many :entries
|
41
|
+
end
|
42
|
+
class EntriesTag < ActiveRecord::Base
|
43
|
+
belongs_to :entry
|
44
|
+
belongs_to :tag
|
45
|
+
end
|
46
|
+
|
47
|
+
module Admin
|
48
|
+
class Person < ::Person ; end;
|
49
|
+
class Customer < ::Customer ; end;
|
50
|
+
class Employee < ::Employee ; end;
|
51
|
+
class Profile < ::Profile ; end;
|
52
|
+
class Resource < ::Resource ; end;
|
53
|
+
class Entry < ::Entry ; end;
|
54
|
+
class Tag < ::Tag ; end;
|
55
|
+
class EntriesTag < ::EntriesTag ; end;
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: active_modularity
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- yuki teraoka
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-05-02 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: simplecov
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: activerecord
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.0.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.0.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sqlite3-ruby
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: database_cleaner
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: activerecord
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 3.0.0
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 3.0.0
|
125
|
+
description: ActiveRecord model inheritance support by module.
|
126
|
+
email:
|
127
|
+
- info@techscore.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- .gitignore
|
133
|
+
- Gemfile
|
134
|
+
- LICENSE.txt
|
135
|
+
- README.md
|
136
|
+
- Rakefile
|
137
|
+
- active_modularity.gemspec
|
138
|
+
- lib/active_modularity.rb
|
139
|
+
- lib/active_modularity/modulize.rb
|
140
|
+
- lib/active_modularity/version.rb
|
141
|
+
- spec/active_modularity_spec.rb
|
142
|
+
- spec/spec_helper.rb
|
143
|
+
- spec/support/database_cleaner.rb
|
144
|
+
- spec/support/models.rb
|
145
|
+
homepage: https://github.com/techscore/active_modularity
|
146
|
+
licenses:
|
147
|
+
- BSD
|
148
|
+
metadata: {}
|
149
|
+
post_install_message:
|
150
|
+
rdoc_options: []
|
151
|
+
require_paths:
|
152
|
+
- lib
|
153
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
154
|
+
requirements:
|
155
|
+
- - '>='
|
156
|
+
- !ruby/object:Gem::Version
|
157
|
+
version: '0'
|
158
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
159
|
+
requirements:
|
160
|
+
- - '>='
|
161
|
+
- !ruby/object:Gem::Version
|
162
|
+
version: '0'
|
163
|
+
requirements: []
|
164
|
+
rubyforge_project:
|
165
|
+
rubygems_version: 2.0.3
|
166
|
+
signing_key:
|
167
|
+
specification_version: 4
|
168
|
+
summary: ActiveRecord model inheritance support by module. Fix inner module association
|
169
|
+
and single table inheritance.
|
170
|
+
test_files:
|
171
|
+
- spec/active_modularity_spec.rb
|
172
|
+
- spec/spec_helper.rb
|
173
|
+
- spec/support/database_cleaner.rb
|
174
|
+
- spec/support/models.rb
|