acts_as_relation 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +6 -0
- data/Gemfile +4 -0
- data/Rakefile +10 -0
- data/acts_as_relation.gemspec +29 -0
- data/lib/active_record/acts/as_relation.rb +3 -3
- data/lib/version.rb +7 -0
- data/test/acts_as_relation_test.rb +82 -0
- data/test/schema.rb +48 -0
- data/test/test_helper.rb +4 -0
- metadata +50 -9
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
|
7
|
+
# Description Meta...
|
8
|
+
s.name = 'acts_as_relation'
|
9
|
+
s.version = ActiveRecord::Acts::AsRelation::VERSION
|
10
|
+
s.platform = Gem::Platform::RUBY
|
11
|
+
s.author = 'Hassan Zamani'
|
12
|
+
s.email = 'hsn.zamani@gmail.com'
|
13
|
+
s.homepage = 'http://github.com/hzamani/acts_as_relation'
|
14
|
+
s.summary = 'Easy multi-table inheritance for rails'
|
15
|
+
s.description = "This 'acts_as' extension provides multi-table inheritance for rails models."
|
16
|
+
|
17
|
+
|
18
|
+
# Load Paths...
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
s.require_paths = ['lib']
|
23
|
+
|
24
|
+
|
25
|
+
# Dependencies (installed via 'bundle install')...
|
26
|
+
s.add_development_dependency("bundler")
|
27
|
+
s.add_development_dependency("sqlite3")
|
28
|
+
s.add_development_dependency("activerecord")
|
29
|
+
end
|
@@ -39,11 +39,12 @@ module ActiveRecord
|
|
39
39
|
base.has_one :#{name}, :as => :#{name}, :autosave => true, :validate => false
|
40
40
|
base.validate :#{name}_must_be_valid
|
41
41
|
base.alias_method_chain :#{name}, :autobuild
|
42
|
-
|
42
|
+
|
43
43
|
base.extend ActiveRecord::Acts::AsRelation::AccessMethods
|
44
44
|
all_attributes = #{name.camelcase.constantize}.content_columns.map(&:name)
|
45
45
|
ignored_attributes = ["created_at", "updated_at", "#{name}_type"]
|
46
|
-
|
46
|
+
associations = #{name.camelcase.constantize}.reflect_on_all_associations(:belongs_to).map! { |assoc| assoc.name }
|
47
|
+
attributes_to_delegate = all_attributes - ignored_attributes + associations
|
47
48
|
base.define_acts_as_accessors(attributes_to_delegate, "#{name}")
|
48
49
|
end
|
49
50
|
|
@@ -52,7 +53,6 @@ module ActiveRecord
|
|
52
53
|
end
|
53
54
|
|
54
55
|
def method_missing(method, *arg, &block)
|
55
|
-
|
56
56
|
end
|
57
57
|
|
58
58
|
def respond_to?(method, include_private_methods = false)
|
data/lib/version.rb
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ActsAsRelationTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
test "acts as validation" do
|
6
|
+
pen = Pen.new
|
7
|
+
assert !pen.valid?
|
8
|
+
assert_equal pen.errors.keys, [:name, :price, :color]
|
9
|
+
|
10
|
+
pen.name = "TestPen"
|
11
|
+
assert !pen.valid?
|
12
|
+
assert_equal pen.errors.keys, [:price, :color]
|
13
|
+
|
14
|
+
pencil = Pencil.new
|
15
|
+
assert !pencil.valid?
|
16
|
+
assert_equal pencil.errors.keys, [:name, :price, :color]
|
17
|
+
|
18
|
+
pencil.color = "red"
|
19
|
+
assert !pencil.valid?
|
20
|
+
assert_equal pencil.errors.keys, [:name, :price]
|
21
|
+
end
|
22
|
+
|
23
|
+
test "save model" do
|
24
|
+
assert Pen.new(:name=>"FOO", :color=>"black", :price=>0.89).save
|
25
|
+
pen = Pen.new
|
26
|
+
pen.name = "BAR"
|
27
|
+
pen.color = "red"
|
28
|
+
pen.price = 0.99
|
29
|
+
assert pen.save
|
30
|
+
end
|
31
|
+
|
32
|
+
test "access methods" do
|
33
|
+
assert_nothing_raised(ActiveRecord::UnknownAttributeError) do
|
34
|
+
Pen.new(:name=>"RedPen", :price=>0.59, :color=>"red")
|
35
|
+
Pencil.new(:name=>"RedPencil", :price=>0.59, :color=>"red")
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
test "acts as method missing" do
|
40
|
+
assert_nothing_raised(NoMethodError) do
|
41
|
+
pen = Pen.new
|
42
|
+
pen.name_changed?
|
43
|
+
pen.price_changed?
|
44
|
+
pen.name_was
|
45
|
+
|
46
|
+
pencil = Pencil.new
|
47
|
+
pencil.name_changed?
|
48
|
+
pencil.price_changed?
|
49
|
+
pencil.name_was
|
50
|
+
pencil.color_was
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
test "acts as respond to?" do
|
55
|
+
pen = Pen.new
|
56
|
+
assert(pen.respond_to? :name_changed?)
|
57
|
+
assert(pen.respond_to? :name_was)
|
58
|
+
assert(pen.respond_to? :price_will_change!)
|
59
|
+
|
60
|
+
pencil = Pencil.new
|
61
|
+
assert(pencil.respond_to? :name_changed?)
|
62
|
+
assert(pencil.respond_to? :name_was)
|
63
|
+
assert(pencil.respond_to? :price_will_change!)
|
64
|
+
assert(pencil.respond_to? :color_changed?)
|
65
|
+
assert(pencil.respond_to? :color_was)
|
66
|
+
end
|
67
|
+
|
68
|
+
test "association reflections" do
|
69
|
+
store = Store.new
|
70
|
+
pen = Pen.new
|
71
|
+
pen.store = store
|
72
|
+
|
73
|
+
assert_equal store, pen.product.store
|
74
|
+
assert_equal store, pen.store
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
#ActiveRecord::Base.connection.tables.each do |table|
|
80
|
+
# ActiveRecord::Base.connection.drop_table(table)
|
81
|
+
#end
|
82
|
+
|
data/test/schema.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_record'
|
3
|
+
require 'acts_as_relation'
|
4
|
+
|
5
|
+
ActiveRecord::Base.establish_connection(
|
6
|
+
:adapter => "sqlite3",
|
7
|
+
:database => ":memory:"
|
8
|
+
)
|
9
|
+
|
10
|
+
ActiveRecord::Schema.define(:version => 1) do
|
11
|
+
|
12
|
+
create_table :stores do |t|
|
13
|
+
t.string :store_name
|
14
|
+
end
|
15
|
+
|
16
|
+
create_table :products do |t|
|
17
|
+
t.string :name
|
18
|
+
t.float :price
|
19
|
+
t.string :product_type
|
20
|
+
t.integer :product_id
|
21
|
+
end
|
22
|
+
|
23
|
+
create_table :pens do |t|
|
24
|
+
t.string :color
|
25
|
+
t.integer :pen_id
|
26
|
+
t.string :pen_type
|
27
|
+
end
|
28
|
+
|
29
|
+
create_table :pencils
|
30
|
+
end
|
31
|
+
|
32
|
+
class Store < ActiveRecord::Base
|
33
|
+
has_many :products
|
34
|
+
end
|
35
|
+
|
36
|
+
class Product < ActiveRecord::Base
|
37
|
+
belongs_to :store
|
38
|
+
validates_presence_of :name, :price
|
39
|
+
end
|
40
|
+
|
41
|
+
class Pen < ActiveRecord::Base
|
42
|
+
acts_as :product
|
43
|
+
validates_presence_of :color
|
44
|
+
end
|
45
|
+
|
46
|
+
class Pencil < ActiveRecord::Base
|
47
|
+
acts_as :pen
|
48
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: acts_as_relation
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,19 +9,60 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-
|
13
|
-
dependencies:
|
14
|
-
|
12
|
+
date: 2011-09-12 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: &14204820 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *14204820
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: sqlite3
|
27
|
+
requirement: &14203940 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *14203940
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: activerecord
|
38
|
+
requirement: &14178400 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *14178400
|
47
|
+
description: This 'acts_as' extension provides multi-table inheritance for rails models.
|
15
48
|
email: hsn.zamani@gmail.com
|
16
49
|
executables: []
|
17
50
|
extensions: []
|
18
51
|
extra_rdoc_files: []
|
19
52
|
files:
|
20
|
-
-
|
21
|
-
-
|
22
|
-
- init.rb
|
53
|
+
- .gitignore
|
54
|
+
- Gemfile
|
23
55
|
- README.markdown
|
24
|
-
|
56
|
+
- Rakefile
|
57
|
+
- acts_as_relation.gemspec
|
58
|
+
- init.rb
|
59
|
+
- lib/active_record/acts/as_relation.rb
|
60
|
+
- lib/acts_as_relation.rb
|
61
|
+
- lib/version.rb
|
62
|
+
- test/acts_as_relation_test.rb
|
63
|
+
- test/schema.rb
|
64
|
+
- test/test_helper.rb
|
65
|
+
homepage: http://github.com/hzamani/acts_as_relation
|
25
66
|
licenses: []
|
26
67
|
post_install_message:
|
27
68
|
rdoc_options: []
|
@@ -41,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
41
82
|
version: '0'
|
42
83
|
requirements: []
|
43
84
|
rubyforge_project:
|
44
|
-
rubygems_version: 1.8.
|
85
|
+
rubygems_version: 1.8.10
|
45
86
|
signing_key:
|
46
87
|
specification_version: 3
|
47
88
|
summary: Easy multi-table inheritance for rails
|