Browse files

ensuring only associations with frozen column have freeze methods

  • Loading branch information...
1 parent 0b123fe commit 0bee1322916ddc6187ebb1644acc7c28274eaf80 @ryanb committed Jul 11, 2008
Showing with 12 additions and 3 deletions.
  1. +5 −3 lib/association_freezer/model_additions.rb
  2. +5 −0 spec/freezer_spec.rb
  3. +2 −0 spec/lib/order.rb
View
8 lib/association_freezer/model_additions.rb
@@ -2,12 +2,14 @@ module AssociationFreezer
module ModelAdditions
def enable_association_freezer
reflect_on_all_associations(:belongs_to).each do |association|
- define_method "freeze_#{association.name}" do
+ if column_names.include? "frozen_#{association.name}"
+ define_method "freeze_#{association.name}" do
- end
+ end
- define_method "unfreeze_#{association.name}" do
+ define_method "unfreeze_#{association.name}" do
+ end
end
end
end
View
5 spec/freezer_spec.rb
@@ -17,5 +17,10 @@
it "should add an unfreeze association method when enabling freezing" do
Order.new.should respond_to(:unfreeze_ship_method)
end
+
+ it "should not add freeze/unfreeze methods to cart association since it doesn't have a frozen column" do
+ Order.new.should_not respond_to(:freeze_cart)
+ Order.new.should_not respond_to(:unfreeze_cart)
+ end
end
end
View
2 spec/lib/order.rb
@@ -1,12 +1,14 @@
class Order < ActiveRecord::Base
belongs_to :ship_method
+ belongs_to :cart
end
class CreateOrders < ActiveRecord::Migration
def self.up
create_table :orders do |t|
t.integer :ship_method_id
t.binary :frozen_ship_method
+ t.binary :cart_id
end
end

0 comments on commit 0bee132

Please sign in to comment.