multiple_table_inheritance 0.1.10 → 0.2.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/CHANGELOG.md CHANGED
@@ -1,14 +1,15 @@
1
- 0.1.11
1
+ 0.2.0
2
2
 
3
3
  * Child model records can now inherit parent model methods via the :methods
4
4
  option when calling :inherits_from.
5
- * Added several unit tests.
5
+ * Adjusted implementation of find_by_id.
6
+ * Added unit tests pertaining to the above issues.
6
7
 
7
8
  0.1.10
8
9
 
9
10
  * Fix issue with sanitizer not allowing relationships to be set when mass
10
11
  assignment security is not specified.
11
- * Added unit tests specific to the above issue.
12
+ * Added unit tests pertaining to the above issue.
12
13
 
13
14
  0.1.9
14
15
 
data/README.md CHANGED
@@ -25,7 +25,7 @@ From the command line:
25
25
 
26
26
  From your Gemfile:
27
27
 
28
- gem 'multiple_table_inheritance', '~> 0.1.10'
28
+ gem 'multiple_table_inheritance', '~> 0.2.0'
29
29
 
30
30
  Usage
31
31
  =====
@@ -236,9 +236,6 @@ When inheriting from another parent model, methods can optionally be called on
236
236
  the parent model automatically as well. To do so, specify the `:methods`
237
237
  option when calling `inherits_from`.
238
238
 
239
- NOTE: This is not fully implemented yet as of version 0.1.10. Please wait until
240
- a future release to prior to using this feature.
241
-
242
239
  class Employee < ActiveRecord::Base
243
240
  acts_as_superclass
244
241
  belongs_to :team
@@ -18,11 +18,10 @@ module MultipleTableInheritance
18
18
 
19
19
  @inherited_attribute_methods_mutex = Mutex.new
20
20
 
21
- extend AttributeMethods, FinderMethods, SharedMethods
22
- include InstanceMethods, SharedMethods
21
+ extend AttributeMethods, FinderMethods
22
+ include InstanceMethods
23
23
  include DelegateMethods if inherit_methods
24
24
 
25
- # Set association references.
26
25
  self.parent_association_name = association_name.to_sym
27
26
  self.primary_key = "#{parent_association_name}_id"
28
27
 
@@ -37,9 +36,6 @@ module MultipleTableInheritance
37
36
  before_validation :set_association_subtype
38
37
  validate :parent_association_must_be_valid
39
38
  before_save :parent_association_must_be_saved
40
-
41
- # denote that the association methods have not been built
42
- @loaded = false
43
39
  end
44
40
 
45
41
  def parent_association_class
@@ -110,9 +106,7 @@ module MultipleTableInheritance
110
106
  child.send(:parent_association=, parent) if parent
111
107
  end
112
108
  end
113
- end
114
-
115
- module SharedMethods
109
+
116
110
  def find_by_id(*args)
117
111
  send("find_by_#{parent_association_name}_id", *args)
118
112
  end
@@ -128,11 +122,11 @@ module MultipleTableInheritance
128
122
  private
129
123
 
130
124
  def parent_association
131
- send(self.class.parent_association_name)
125
+ send(parent_association_name)
132
126
  end
133
127
 
134
128
  def parent_association=(record)
135
- send("#{self.class.parent_association_name}=", record)
129
+ send("#{parent_association_name}=", record)
136
130
  end
137
131
 
138
132
  def set_association_subtype
@@ -163,16 +157,25 @@ module MultipleTableInheritance
163
157
 
164
158
  module DelegateMethods
165
159
  def method_missing(name, *args, &block)
166
- if parent_association.respond_to?(name)
160
+ if parent_association_respond_to?(name)
167
161
  parent_association.send(name, *args, &block)
168
162
  else
169
- super(name, *args, &block)
163
+ super
170
164
  end
171
165
  end
172
166
 
173
167
  def respond_to?(name, *args)
174
- return true if name.to_sym == :parent_association
175
- super(name, *args) || parent_association.respond_to?(name)
168
+ super || parent_association_respond_to?(name)
169
+ end
170
+
171
+ private
172
+
173
+ def parent_association_respond_to?(name)
174
+ parent_association_loaded? && parent_association.respond_to?(name)
175
+ end
176
+
177
+ def parent_association_loaded?
178
+ !!association_instance_get(parent_association_name)
176
179
  end
177
180
  end
178
181
  end
@@ -1,3 +1,3 @@
1
1
  module MultipleTableInheritance
2
- VERSION = "0.1.10"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -18,6 +18,28 @@ describe MultipleTableInheritance::Child do
18
18
  @programmer.employee.should be_instance_of(Employee)
19
19
  @programmer.employee.id.should be(@programmer_id)
20
20
  end
21
+
22
+ context 'via find_by_id on class' do
23
+ before do
24
+ @programmer = Programmer.find_by_id(@programmer_id)
25
+ end
26
+
27
+ it 'should retrieve child records' do
28
+ @programmer.should be_instance_of(Programmer)
29
+ @programmer.id.should be(@programmer_id)
30
+ end
31
+ end
32
+
33
+ context 'via find_by_id on relation' do
34
+ before do
35
+ @programmer = Programmer.where(Programmer.primary_key => @programmer_id).find_by_id(@programmer_id)
36
+ end
37
+
38
+ it 'should retrieve child records' do
39
+ @programmer.should be_instance_of(Programmer)
40
+ @programmer.id.should be(@programmer_id)
41
+ end
42
+ end
21
43
  end
22
44
 
23
45
  context 'creating records' do
@@ -409,8 +431,13 @@ describe MultipleTableInheritance::Child do
409
431
  end
410
432
 
411
433
  it 'should allow parent methods to be called through child' do
412
- pending "infinite recursion must be addressed"
413
- # @programmer.should respond_to(:give_raise!)
434
+ @programmer.should respond_to(:give_raise!)
435
+ end
436
+
437
+ it 'should call parent method through child' do
438
+ salary = @programmer.salary
439
+ @programmer.give_raise!(50)
440
+ @programmer.salary.should == salary + 50
414
441
  end
415
442
  end
416
443
 
@@ -12,25 +12,25 @@ class Employee < ActiveRecord::Base
12
12
  validates :salary, :presence => true, :numericality => { :min => 0 }
13
13
 
14
14
  def give_raise!(amount)
15
- Employee.update_counters self.id, :salary => amount
15
+ update_attributes(:salary => salary + amount)
16
16
  end
17
17
  end
18
18
 
19
19
  class Programmer < ActiveRecord::Base
20
- inherits_from :employee #, :methods => true
20
+ inherits_from :employee, :methods => true
21
21
  attr_accessible :languages, :language_ids
22
22
  has_many :known_languages
23
23
  has_many :languages, :through => :known_languages
24
24
  end
25
25
 
26
26
  class Manager < ActiveRecord::Base
27
- inherits_from :employee #, :methods => true
27
+ inherits_from :employee, :methods => true
28
28
  attr_accessible :bonus
29
29
  validates :bonus, :numericality => true
30
30
  end
31
31
 
32
32
  class Janitor < ActiveRecord::Base
33
- inherits_from :employee #, :methods => true
33
+ inherits_from :employee, :methods => true
34
34
  end
35
35
 
36
36
  class Team < ActiveRecord::Base
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multiple_table_inheritance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.10
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-24 00:00:00.000000000Z
12
+ date: 2012-03-31 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &2156165380 !ruby/object:Gem::Requirement
16
+ requirement: &2152938260 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 3.0.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2156165380
24
+ version_requirements: *2152938260
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &2156164800 !ruby/object:Gem::Requirement
27
+ requirement: &2152937760 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 3.0.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2156164800
35
+ version_requirements: *2152937760
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec-rails
38
- requirement: &2156164220 !ruby/object:Gem::Requirement
38
+ requirement: &2152937240 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 2.8.0
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2156164220
46
+ version_requirements: *2152937240
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec_tag_matchers
49
- requirement: &2156163640 !ruby/object:Gem::Requirement
49
+ requirement: &2152936760 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: 1.0.0
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *2156163640
57
+ version_requirements: *2152936760
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: sqlite3-ruby
60
- requirement: &2156163060 !ruby/object:Gem::Requirement
60
+ requirement: &2152936300 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: 1.3.3
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *2156163060
68
+ version_requirements: *2152936300
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: database_cleaner
71
- requirement: &2156141780 !ruby/object:Gem::Requirement
71
+ requirement: &2152935840 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: 0.7.1
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *2156141780
79
+ version_requirements: *2152935840
80
80
  description: ActiveRecord plugin designed to allow simple multiple table inheritance.
81
81
  email:
82
82
  - matt@matthuggins.com