Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skip_parse does not work if property is nil #192

Open
denisovlev opened this issue Jul 28, 2016 · 0 comments
Open

skip_parse does not work if property is nil #192

denisovlev opened this issue Jul 28, 2016 · 0 comments

Comments

@denisovlev
Copy link

denisovlev commented Jul 28, 2016

Option skip_parse on property is ignored when when corresponding value on input hash is nil.
Expected: when skip_parse: true representer does not try to set any value in represented model
Actual: representer tries to set value if value is nil

it 'representable skip_parse nil' do
  class TestRepresenter < Representable::Decorator
    include ::Representable::Hash
    property :property1
    property :property2, skip_parse: true
  end

  class TestModel
    attr_accessor :property1
    attr_reader :property2

    def initialize
      @property1 = '123'
      @property2 = '456'
    end
  end

  m = TestModel.new
  r = TestRepresenter.new(m)

  expected_hash = {property1: '123', property2: '456'}.stringify_keys
  expect(r.to_hash).to eq(expected_hash)

  new_hash = {property1: '789', property2: nil}.stringify_keys
  r.from_hash(new_hash) # << fails here when trying to set readonly property2
  expect(m.property1).to eq('789')
  expect(m.property2).to eq('456')
end
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

No branches or pull requests

1 participant