koujou 0.1.2 → 0.1.3

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.
@@ -2,3 +2,9 @@
2
2
 
3
3
  * 1 major enhancement:
4
4
  * Initial release
5
+
6
+ === 0.1.3 2010-05-27
7
+
8
+ * 2 small fixes:
9
+ * fix for how Koujou deals with polymorphic associations, ensuring that it leaves polymorphic fields up to the user to populate
10
+ * fix to ignore the inheritance column and not generate data for it when working with STI models
@@ -16,6 +16,7 @@ script/generate
16
16
  test/lib/active_record_test_connector.rb
17
17
  test/lib/models/car.rb
18
18
  test/lib/models/comment.rb
19
+ test/lib/models/invite.rb
19
20
  test/lib/models/message.rb
20
21
  test/lib/models/photo.rb
21
22
  test/lib/models/post.rb
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{koujou}
5
- s.version = "0.1.2"
5
+ s.version = "0.1.3"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Michael Leung"]
@@ -11,7 +11,7 @@ require 'koujou/validation_reflection'
11
11
  require 'koujou/custom_validation'
12
12
 
13
13
  module Koujou
14
- VERSION = '0.1.2'
14
+ VERSION = '0.1.3'
15
15
  end
16
16
 
17
17
  if ENV["RAILS_ENV"] == "test"
@@ -119,6 +119,8 @@ module Koujou #:nodoc:
119
119
  # We only want to create the association if the user has required the id field.
120
120
  # This will build the minimum valid requirements.
121
121
  next unless has_required_id_validation?(instance, a)
122
+ # Skip polymorphic associations as we don't know what to build for them
123
+ next if a.options.keys.include?(:polymorphic) && a.options[:polymorphic] == true
122
124
 
123
125
  if a.macro == :has_one || a.macro == :belongs_to
124
126
  # If there's a two way association here (user has_one profile, profile belongs_to user)
@@ -144,6 +146,10 @@ module Koujou #:nodoc:
144
146
  end
145
147
 
146
148
  def generate_and_set_data(instance, validation, sequenced)
149
+ # Don't set if it's the inheritance column
150
+ return if validation.name.to_s == instance.class.inheritance_column.to_s
151
+ # Don't set values for polymorphic association fields - if no values are supplied we'll want that to just fall through to validation errors
152
+ return if instance.class.reflect_on_all_associations.select { |a| a.options.keys.include?(:polymorphic) && a.options[:polymorphic] == true }.collect { |a| [:"#{a.name}_id", :"#{a.name}_type"] }.flatten.include?(validation.name)
147
153
  data_generator = DataGenerator.new(sequenced, validation)
148
154
  data_generator.required_length = get_required_length(instance, validation)
149
155
  if has_inclusion_validation?(instance, validation)
@@ -49,6 +49,8 @@ class ActiveRecordTestConnector
49
49
  create_table "comments", :force => true do |t|
50
50
  t.text "bod"
51
51
  t.integer "post_id"
52
+ t.integer "commentable_id"
53
+ t.string "commentable_type"
52
54
  end
53
55
  create_table "profiles", :force => true do |t|
54
56
  t.integer "user_id"
@@ -70,6 +72,9 @@ class ActiveRecordTestConnector
70
72
  t.integer "user_id", "year"
71
73
  t.string "make", "model"
72
74
  end
75
+ create_table "invites", :force => true do |t|
76
+ t.string "type"
77
+ end
73
78
  end
74
79
  end
75
80
 
@@ -1,7 +1,8 @@
1
1
  class Comment < ActiveRecord::Base
2
2
  validates_presence_of :bod
3
3
  validates_length_of :bod, :maximum => 200
4
+ validates_presence_of :commentable_id, :commentable_type
4
5
 
5
6
  belongs_to :post
6
-
7
- end
7
+ belongs_to :commentable, :polymorphic => true
8
+ end
@@ -0,0 +1,9 @@
1
+ class Invite < ActiveRecord::Base
2
+ validates_presence_of :type
3
+ end
4
+
5
+ class EventInvite < Invite
6
+ end
7
+
8
+ class GroupInvite < Invite
9
+ end
@@ -71,7 +71,8 @@ class TestBuilder < Test::Unit::TestCase
71
71
 
72
72
  should 'allow me to override the model attributes' do
73
73
  comment = 'your post is epic fail'
74
- c = Comment.koujou_create(:bod => comment)
74
+ p = Post.koujou
75
+ c = Comment.koujou_create(:bod => comment, :commentable_id => p.id, :commentable_type => p.class.name)
75
76
  assert_equal comment, c.bod
76
77
  end
77
78
 
@@ -80,7 +81,6 @@ class TestBuilder < Test::Unit::TestCase
80
81
  # The first digit should not be an integer.
81
82
  assert_equal 0, u.first_name[0,1].to_i
82
83
  end
83
-
84
84
  end
85
85
 
86
86
  context 'on sending the koujou_build message' do
@@ -147,7 +147,33 @@ class TestBuilder < Test::Unit::TestCase
147
147
  end
148
148
 
149
149
  end
150
-
150
+
151
+ context "using polymorphic associations" do
152
+ should "leave the association alone and not validate if no values supplied" do
153
+ ex = assert_raise ActiveRecord::RecordInvalid do
154
+ c = Comment.koujou(true, :bod => "test body")
155
+ end
156
+ assert_equal "Validation failed: Commentable can't be blank, Commentable type can't be blank", ex.message
157
+ end
158
+
159
+ should "work when supplying the polymorphic fields" do
160
+ p = Post.koujou
161
+ c = Comment.koujou(true, :bod => "test body", :commentable_type => p.class.name, :commentable_id => p.id)
162
+ assert_equal p.id, c.commentable.id
163
+ end
164
+ end
165
+
166
+ context "using STI" do
167
+ should "not be setting the type field to something random" do
168
+ @event_invite = EventInvite.koujou
169
+ assert_equal 1, EventInvite.all.length
170
+ assert_equal "EventInvite", @event_invite.reload.type
171
+ @group_invite = GroupInvite.koujou
172
+ assert_equal 1, GroupInvite.all.length
173
+ assert_equal "GroupInvite", @group_invite.reload.type
174
+ assert_equal 2, Invite.all.length
175
+ end
176
+ end
151
177
 
152
178
  end
153
179
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 3
9
+ version: 0.1.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Michael Leung
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-05-19 00:00:00 +01:00
17
+ date: 2010-05-27 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -83,6 +83,7 @@ files:
83
83
  - test/lib/active_record_test_connector.rb
84
84
  - test/lib/models/car.rb
85
85
  - test/lib/models/comment.rb
86
+ - test/lib/models/invite.rb
86
87
  - test/lib/models/message.rb
87
88
  - test/lib/models/photo.rb
88
89
  - test/lib/models/post.rb