I have controller actions which all use one method but with different arguments.
Can I refactor this somehow to 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 could use some metaprogramming, but this would not be as readable as what you currently have:
%i(
usd_cash
usd_bank
rub_bank
).each do |method|
define_method method do
transaction_currency(method.to_s.split('_').instance_eval {first.upcase + ' ' + last})
end
end