polymorpheus 1.1.2 → 1.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -65,15 +65,18 @@ module Polymorpheus
65
65
 
66
66
  # Setter method
67
67
  define_method "#{polymorphic_api}=" do |polymorphic_obj|
68
- possible_associations = [ polymorphic_obj.class.name.underscore,
69
- polymorphic_obj.class.superclass.name.underscore ]
70
- match = associations & possible_associations
68
+
69
+ klass_ancestors = polymorphic_obj.class
70
+ .ancestors.map(&:name).compact.map(&:underscore)
71
+ match = associations & klass_ancestors
72
+
71
73
  if match.blank?
72
- raise Polymorpheus::Interface::PolymorphicError, associations
74
+ raise Polymorpheus::Interface::InvalidTypeError, associations
73
75
  elsif match.length > 1
74
76
  raise Polymorpheus::Interface::AmbiguousTypeError
75
77
  else
76
- self.send("#{match[0]}_id=", polymorphic_obj.id)
78
+ accessor = "#{polymorphic_obj.class.base_class.name.underscore}_id="
79
+ self.send(accessor, polymorphic_obj.id)
77
80
  (associations - match).each do |association_to_reset|
78
81
  self.send("#{association_to_reset}_id=", nil)
79
82
  end
@@ -1,3 +1,3 @@
1
1
  module Polymorpheus
2
- VERSION = '1.1.2'
2
+ VERSION = '1.1.3'
3
3
  end
@@ -16,6 +16,20 @@ end
16
16
  class Woman < ActiveRecord::Base
17
17
  end
18
18
 
19
+ class Dog < ActiveRecord::Base
20
+ end
21
+
22
+ class Glove < ActiveRecord::Base
23
+ belongs_to_polymorphic :gentleman, :as => :wearer
24
+ validates_polymorph :wearer
25
+ end
26
+
27
+ class Gentleman < Man
28
+ end
29
+
30
+ class Knight < Gentleman
31
+ end
32
+
19
33
  describe Shoe do
20
34
 
21
35
  let(:shoe) { Shoe.new(attributes) }
@@ -35,6 +49,59 @@ describe Shoe do
35
49
  include?(:polymorphic_wearer_relationship_is_valid).should be_true
36
50
  end
37
51
 
52
+ describe "dynamic generated setter" do
53
+ let(:attributes) { {} }
54
+
55
+ it "sets the correct attribute value for the setter" do
56
+ shoe.wearer = man
57
+ shoe.man_id.should == man.id
58
+ shoe.woman_id.should == nil
59
+ end
60
+
61
+ it "sets competing associations to nil" do
62
+ shoe.wearer = man
63
+ shoe.man_id.should == man.id
64
+ shoe.wearer = woman
65
+ shoe.woman_id.should == woman.id
66
+ shoe.man_id.should == nil
67
+ end
68
+
69
+ it "throws an error if the assigned object isn't a valid type" do
70
+ dog = Dog.create!
71
+ expect { shoe.wearer = dog }
72
+ .to raise_error(Polymorpheus::Interface::InvalidTypeError,
73
+ "Invalid type. Must be one of {man, woman}")
74
+ end
75
+
76
+ it "does not throw an error if the assigned object is a subclass of a valid type" do
77
+ gentleman = Gentleman.create!
78
+ expect { shoe.wearer = gentleman }.not_to raise_error
79
+ shoe.man_id.should == gentleman.id
80
+ end
81
+
82
+ it "does not throw an error if the assigned object is a descendant of a valid type" do
83
+ knight = Knight.create!
84
+ expect { shoe.wearer = knight }.not_to raise_error
85
+ shoe.man_id.should == knight.id
86
+ end
87
+
88
+ it "does not throw an error if the association is to a parent class of
89
+ assigned object that is not the base" do
90
+ glove = Glove.new({})
91
+ knight = Knight.create!
92
+ expect { glove.wearer = knight }.not_to raise_error
93
+ glove.man_id.should == knight.id
94
+ end
95
+
96
+ it "throws an error if assigned object is a less specific instance of the association" do
97
+ glove = Glove.new({})
98
+ man = Man.create!
99
+ expect { glove.wearer = man }
100
+ .to raise_error(Polymorpheus::Interface::InvalidTypeError,
101
+ "Invalid type. Must be one of {gentleman}")
102
+ end
103
+ end
104
+
38
105
  shared_examples_for "invalid polymorphic relationship" do
39
106
  specify { shoe.wearer.should == nil }
40
107
  specify { shoe.wearer_active_key.should == nil }
data/spec/spec_helper.rb CHANGED
@@ -15,7 +15,14 @@ ActiveRecord::Schema.define do
15
15
  t.integer :man_id
16
16
  t.integer :woman_id
17
17
  end
18
- create_table :men
18
+ create_table :gloves do |t|
19
+ t.integer :man_id
20
+ t.integer :woman_id
21
+ end
22
+ create_table :men do |t|
23
+ t.string :type
24
+ end
19
25
  create_table :women
26
+ create_table :dogs
20
27
  end
21
28
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polymorpheus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.1.3
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-12-23 00:00:00.000000000Z
12
+ date: 2013-06-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: foreigner
16
- requirement: &70126037079580 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,15 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70126037079580
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
25
30
  - !ruby/object:Gem::Dependency
26
31
  name: activerecord
27
- requirement: &70126037079040 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
28
33
  none: false
29
34
  requirements:
30
35
  - - ! '>='
@@ -32,10 +37,15 @@ dependencies:
32
37
  version: '3.0'
33
38
  type: :runtime
34
39
  prerelease: false
35
- version_requirements: *70126037079040
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '3.0'
36
46
  - !ruby/object:Gem::Dependency
37
47
  name: rspec-rails
38
- requirement: &70126037078620 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
39
49
  none: false
40
50
  requirements:
41
51
  - - ! '>='
@@ -43,10 +53,15 @@ dependencies:
43
53
  version: '0'
44
54
  type: :development
45
55
  prerelease: false
46
- version_requirements: *70126037078620
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
47
62
  - !ruby/object:Gem::Dependency
48
63
  name: mysql2
49
- requirement: &70126037078160 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
50
65
  none: false
51
66
  requirements:
52
67
  - - ! '>='
@@ -54,7 +69,12 @@ dependencies:
54
69
  version: '0'
55
70
  type: :development
56
71
  prerelease: false
57
- version_requirements: *70126037078160
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
58
78
  description: Provides a database-friendly method for polymorphic relationships
59
79
  email: bsingh@wegowise.com
60
80
  executables: []
@@ -104,7 +124,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
124
  version: 1.3.6
105
125
  requirements: []
106
126
  rubyforge_project:
107
- rubygems_version: 1.8.6
127
+ rubygems_version: 1.8.23
108
128
  signing_key:
109
129
  specification_version: 3
110
130
  summary: Provides a database-friendly method for polymorphic relationships