ruby - Rails before_filter different arguments -


i have controller actions use 1 method different arguments. can refactor somehow use before_filter?

  def usd_cash     transaction_currency("usd cash")   end    def usd_bank     transaction_currency("usd bank")   end    def rub_bank     transaction_currency("rub bank")   end    private    def transaction_currency(currency)     @transactions = transaction.where(location: "#{currency}")   end 

you use metaprogramming, not readable have:

%i(   usd_cash   usd_bank   rub_bank ).each |method|   define_method method     transaction_currency(method.to_s.split('_').instance_eval {first.upcase + ' ' + last})   end end 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -