我的rails控制器中有这个动作,
def step_submit
validate_user()
@owning = @user.create_user_car_transaction(Variant.find(params[:variant]), params[:details], params[:address], params[:somethin1])
Contact.user_contact(current_user, params[:contact]) if @user.contact.nil?
redirect_to "/next_step"
end
我使用 codeClimate 来检查代码的质量..
它显示了这个动作的复杂性〜30 ..我实际上打破了一个非常巨大的方法......我怎样才能降低这种复杂性?
这些是动作调用的不同方法
def self.user_contact(user, contact_hash = nil)
contact = user.contact || user.create_contact()
contact.update_attributes(contact_hash) if contact_hash.present?
contact
end
def validate_user
if params[:user] && current_user.nil?
user = User.create(params[:user])
sign_in user
end
end
def create_user_car_transaction(car, details_hash, address_hash, coupon_hash = nil)
transaction = self.transactions.create()
car.transaction_item = transaction.transaction_items.create()
car.save
payment_hash = details_hash
payment_hash.merge!(address_hash)
payment = transaction.create_payment(payment_hash)
transaction.update_attributes(:status=>"1") if transaction.status.nil?
transaction
end