amatsuda/active_decorator

Access undecorated methods

Closed this issue · 4 comments

g8d3 commented

How is this possible? I know super call the original method in decorator, but take a look at my problem.

I am trying to create a form in Rails with:

f.date_field :start_date

But since I decorated that field I do not get the edit form with right values:

module MyDecorator
  def start_date
    l super
  end
end

Same problem, decorated then didn't get the original value in edit form.

Technically this sort of behavior is possible in Ruby. You can get the unbound method of the original class using .instance_method and bind it to the instance of your decorated class. However, this sort of programming doesn't lead to clean and reasonable code. The easiest way is to just to use different name for your decorated method, so both version will be available. If you insist on using the original name of the method as the decorated version (I do not recommend this) use should use alias_method / alias_attribute in the decorator to preserve the original method.

In the end I believe that this issue has very little to do with the Gem, because it covers the general patterns and Ruby programming. Therefore I think places like StackOverflow are more suitable for such questions. :-)

like what mdrozdziel said, you can use alias_method_chain

module MyDecorator
  def start_date_with_decorator
    l(start_date_without_decorator)
  end
  alias_method_chain :start_date, :decorator
end

Thanks for answers, forgive my ignorance with Ruby, imprudent question, sorry.