Permalink
Browse files

moved common model instance setup into Conversation

  • Loading branch information...
1 parent 7cf85a6 commit b1d96a4d799d5e3364f9f2ca54c9be4e5d1dce39 @tomcz tomcz committed Mar 11, 2010
@@ -43,15 +43,7 @@ def edit
def form
@conversation_id = params[:id]
conversation = Conversation.get_or_create(@conversation_id)
- if conversation.ref_id
- @product = Product.find(conversation.ref_id)
- else
- @product = Product.new
- end
- if conversation.parameters
- @product.attributes = conversation.parameters
- @product.valid?
- end
+ @product = conversation.setup_instance(Product)
if conversation.ref_id
render :action => "edit"
else
@@ -31,15 +31,9 @@ def new_order
def checkout
@cart = find_cart
- conversation = Conversation.get_or_create(params[:id])
- if conversation.new_record?
- @conversation_id = :new
- @order = Order.new
- else
- @conversation_id = conversation.id
- @order = Order.new(conversation.parameters)
- @order.valid?
- end
+ @conversation_id = params[:id]
+ conversation = Conversation.get_or_create(@conversation_id)
+ @order = conversation.setup_instance(Order)
end
def save_order
@@ -43,15 +43,7 @@ def edit
def form
@conversation_id = params[:id]
conversation = Conversation.get_or_create(@conversation_id)
- if conversation.ref_id
- @user = User.find(conversation.ref_id)
- else
- @user = User.new
- end
- if conversation.parameters
- @user.attributes = conversation.parameters
- @user.valid?
- end
+ @user = conversation.setup_instance(User)
if conversation.ref_id
render :action => "edit"
else
View
@@ -23,4 +23,17 @@ def self.destroy_if_exists(uuid)
end
end
+ def setup_instance(model_class)
+ if self.ref_id
+ item = model_class.find(self.ref_id)
+ else
+ item = model_class.new
+ end
+ if self.parameters
+ item.attributes = self.parameters
+ item.valid?
+ end
+ item
+ end
+
end

0 comments on commit b1d96a4

Please sign in to comment.