delegate_all_for 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
data/delegate_all_for.gemspec
CHANGED
@@ -14,4 +14,8 @@ Gem::Specification.new do |gem|
|
|
14
14
|
gem.name = "delegate_all_for"
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = DelegateAllFor::VERSION
|
17
|
+
gem.add_dependency 'activerecord', '~> 3.2.3'
|
18
|
+
gem.add_development_dependency 'rake', '~> 0.9.2.2'
|
19
|
+
gem.add_development_dependency 'rspec', '~> 2.9.0'
|
20
|
+
gem.add_development_dependency 'sqlite3', '~> 1.3.6'
|
17
21
|
end
|
data/lib/delegate_all_for.rb
CHANGED
@@ -9,7 +9,7 @@ $LOAD_PATH.shift
|
|
9
9
|
|
10
10
|
module DelegateAllFor
|
11
11
|
# For all columns of the specified association to the current object,
|
12
|
-
# the
|
12
|
+
# the reader, writer, and predicate methods are delegated.
|
13
13
|
#
|
14
14
|
# Supported options:
|
15
15
|
# [:except]
|
@@ -28,6 +28,7 @@ module DelegateAllFor
|
|
28
28
|
class_eval(%{delegate :#{m}, :to => :#{association_name}})
|
29
29
|
end
|
30
30
|
(reflection.klass.column_names - exclude_columns).each do |column_name|
|
31
|
+
next if column_name =~ /_id$/
|
31
32
|
class_eval <<-eoruby, __FILE__, __LINE__ + 1
|
32
33
|
delegate :#{column_name}, :to => :#{association_name}
|
33
34
|
delegate :#{column_name}=, :to => :#{association_name}
|
@@ -43,4 +44,4 @@ end
|
|
43
44
|
|
44
45
|
if defined?(ActiveRecord::Base)
|
45
46
|
ActiveRecord::Base.extend DelegateAllFor
|
46
|
-
end
|
47
|
+
end
|
@@ -1,34 +1,59 @@
|
|
1
1
|
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
require File.expand_path('../../support/active_record', __FILE__)
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
def
|
4
|
+
class Child < ActiveRecord::Base
|
5
|
+
belongs_to :parent
|
6
|
+
def two; 'child' end
|
7
|
+
def extra; true end
|
8
|
+
end
|
7
9
|
|
10
|
+
class Parent < ActiveRecord::Base
|
8
11
|
has_one :child
|
9
12
|
delegate_all_for :child, except: [:three], also_include: [:extra]
|
10
|
-
|
11
|
-
def initialize; end
|
12
13
|
def two; 'parent' end
|
13
14
|
end
|
14
15
|
|
15
|
-
class Child < ActiveRecord::Base
|
16
|
-
belongs_to :parent
|
17
|
-
def self.column_names; %w(two three four) end
|
18
|
-
def two; 'child' end
|
19
|
-
def extra; true end
|
20
|
-
end
|
21
16
|
|
22
17
|
describe DelegateAllFor do
|
23
18
|
describe 'delegate_all_for' do
|
24
|
-
subject
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
19
|
+
subject do
|
20
|
+
Parent.create(one: 1) do |p|
|
21
|
+
p.two = 2
|
22
|
+
p.child = Child.create(two: 'two', three: 'three', four: 'four')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'does not respond to three' do
|
27
|
+
it 'reader' do
|
28
|
+
lambda { subject.three }.should raise_error NoMethodError
|
29
|
+
end
|
30
|
+
it 'predicate' do
|
31
|
+
lambda { subject.three? }.should raise_error NoMethodError
|
32
|
+
end
|
33
|
+
it 'writer' do
|
34
|
+
lambda { subject.three = 'THREE' }.should raise_error NoMethodError
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'delegates to child attributes' do
|
39
|
+
its(:four) { should == 'four' }
|
40
|
+
its(:extra) { should be_true }
|
41
|
+
its(:two) { should == 'parent' }
|
42
|
+
|
43
|
+
it 'does not delegate to association attributes' do
|
44
|
+
lambda { subject.parent_id }.should raise_error NoMethodError
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'guards against user error' do
|
49
|
+
it 'notices when an association is wrong' do
|
50
|
+
lambda do
|
51
|
+
class Parent < ActiveRecord::Base
|
52
|
+
has_one :child
|
53
|
+
delegate_all_for :foo
|
54
|
+
end
|
55
|
+
end.should raise_error ArgumentError
|
56
|
+
end
|
57
|
+
end
|
32
58
|
end
|
33
59
|
end
|
34
|
-
=end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
|
2
|
+
ActiveRecord::Migration.create_table :parents do |t|
|
3
|
+
t.string :one
|
4
|
+
t.string :two
|
5
|
+
t.timestamps
|
6
|
+
end
|
7
|
+
ActiveRecord::Migration.create_table :children do |t|
|
8
|
+
t.string :two
|
9
|
+
t.string :three
|
10
|
+
t.string :four
|
11
|
+
t.integer :parent_id
|
12
|
+
t.timestamps
|
13
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: delegate_all_for
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,8 +9,52 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-05-
|
13
|
-
dependencies:
|
12
|
+
date: 2012-05-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: &70156840498460 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.2.3
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70156840498460
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &70156840495600 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.9.2.2
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70156840495600
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rspec
|
38
|
+
requirement: &70156840494300 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 2.9.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70156840494300
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: sqlite3
|
49
|
+
requirement: &70156840493440 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.3.6
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70156840493440
|
14
58
|
description: Easy delegation of all columns of an ActiveRecord association
|
15
59
|
email:
|
16
60
|
- jason@lessonplanet.com
|
@@ -29,6 +73,7 @@ files:
|
|
29
73
|
- lib/delegate_all_for/version.rb
|
30
74
|
- spec/delegate_all_for/delegate_all_for_spec.rb
|
31
75
|
- spec/spec_helper.rb
|
76
|
+
- spec/support/active_record.rb
|
32
77
|
homepage: https://github.com/LessonPlanet/delegate_all_for
|
33
78
|
licenses: []
|
34
79
|
post_install_message:
|
@@ -56,3 +101,4 @@ summary: Easy delegation of all columns of an ActiveRecord association
|
|
56
101
|
test_files:
|
57
102
|
- spec/delegate_all_for/delegate_all_for_spec.rb
|
58
103
|
- spec/spec_helper.rb
|
104
|
+
- spec/support/active_record.rb
|