representable icon indicating copy to clipboard operation
representable copied to clipboard

Pass options to getters and setters

Open aldesantis opened this issue 9 years ago • 1 comments

@apotonick as per our email conversation.

This PR allows to do the following:

class SongDecorator < Representable::Decorator
  property :title, exec_context: :decorator

  def title(user_options:, **)
    user_options[:upcase_title] ? represented.title.upcase : represented.title
  end
end

The same can be done with setters:

class SongDecorator < Representable::Decorator
  property :title, exec_context: :decorator

  def title=(input, user_options:, **)
    super(user_options[:upcase_title] ? input.upcase : input)
  end
end

In addition to accepting the user_options keyword argument, the methods can also accept options, if the full options hash is needed.

I am not sure if there's a better approach to it.

Also not sure if a test would be required or if the existing tests are fine. I've run into an issue writing a test for it because the test representer (#representer!) seems to disregard the exec_context option for properties and always call getters and setters on the decorated object instead.

aldesantis avatar Aug 29 '16 17:08 aldesantis

@apotonick bump?

aldesantis avatar Nov 27 '16 16:11 aldesantis