Skip to content

Conversation

@aldesantis
Copy link

@aldesantis aldesantis commented Aug 29, 2016

@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
Copy link
Author

@apotonick bump?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant