representable
representable copied to clipboard
Pass options to getters and setters
@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.
@apotonick bump?