trailblazer / representable

Maps representation documents from and to Ruby objects. Includes JSON, XML and YAML support, plain properties and compositions.
http://trailblazer.to/2.1/docs/representable.html
MIT License
689 stars 108 forks source link

Inheriting a base representer #223

Closed kalynrobinson closed 6 years ago

kalynrobinson commented 6 years ago
module Shinka::Representer
  include Representable::JSON

  def self.included(base)
    base.include Representable::JSON
  end

  property :id
  property :name
end
module Shinka::ChildRepresenter
  include Shinka::Representer

  property :test
end

I have a base representer class with properties that are common across all of my other representers. However, the properties in the base representer are not getting populated. If I copy the base properties over to the child, they work correctly.

# input: { id: 1, name: 'Test', test: 'Another test' }
# Output
child.id   # => nil
child.name # => nil
child.test # => 'Another test'

# Expected
child.id   # => 1
child.name # => 'Test'
child.test # => 'Another test'
apotonick commented 6 years ago

The included hook kills the inherited representer, that's why! :confetti_ball:

I advise you not to use module representers are we're fading out modules. They suck, are slow and mutate objects. Switch to class decorators! :rocket: