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
Post a Comment