A
A
Andrey Venin2014-02-13 12:08:36
Ruby on Rails
Andrey Venin, 2014-02-13 12:08:36

Codeclimate rails Similar code: how to optimize?

I decided to start using codeclimate in one of my projects. In the entire project, he found only 1 "darling"

def delivery_method=(deliver)
    unless product.shop.delivery_methods.include? deliver
      raise ArgumentError.new('This product does not contain delivery method')
    end
    self.delivery_method_id = deliver.id if deliver.kind_of?(DeliveryMethod)
end

def payment_method=(payment)
    unless product.shop.payment_methods.include? payment
      raise ArgumentError.new('This product does not contain payment method')
    end
    self.payment_method_id = payment.id if payment.kind_of?(PaymentMethod)
end

he sees it as redundant. Please tell me how it can be "optimized".

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
himik, 2014-02-13
@shaelf

def method=(type, object)
    unless product.shop.send("#{type}_methods").include?(object)
      raise ArgumentError.new('This product does not contain #{type} method')
    end
    self.send("#{type}_method_id}=", object.id) if object.kind_of?("#{type.capitalize}Method".constantize)
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question