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

Using Virtus with Modules (multiple layer include) - calling attributes does not return all declared attributes #260

Closed
spoptchev opened this issue Apr 13, 2014 · 3 comments · Fixed by #276
Labels

Comments

@spoptchev
Copy link

I have the following structure:

module BaseAddress
  include Virtus.module
  attribute address1, String
  attribute ...
end

module ExtendedAddress
  include BaseAddress

  attribute ...
  attribute ...
end

module SomethingElse
  include Virtus.module
  attribute ...
end

class Entity
  include Virtus.model

  attribute ...
  attribute ...

end

class ExtendedEntity < Entity
  include ExtendedAddress, SomethingElse
end

extended_entity = ExtendedEntity.new
extended_entity.attributes

The last call to extended_entity.attributes does not return all declared attributes. The attributes from BaseAddress are missing. But I can still access the address1 attribute.

extended_entity.attributes # -> address1 is not in hash
extended_entity.address1 = "address1" # -> still works

You always get the attributes of the last included module and those that are declared on the class.

@spoptchev spoptchev changed the title Using Virtus with Modules (multipy layer include) - calling attributes does not return all declared attributes Using Virtus with Modules (multiple layer include) - calling attributes does not return all declared attributes Apr 13, 2014
@tjstankus
Copy link

require 'virtus'

module BaseAddress
  include Virtus.module
  attribute :address1, String
end

module ExtendedAddress
  include BaseAddress
end

module SomethingElse
  include Virtus.module
end

class Entity
  include Virtus.model
end

class ExtendedEntity < Entity
  include ExtendedAddress, SomethingElse
end

extended_entity = ExtendedEntity.new(address1: '666 Pleasant Lane')
puts extended_entity.attributes # => {:address1=>"666 Pleasant Lane"}

Is it because you did not initialize ExtendedEntity with a hash as above?

@spoptchev
Copy link
Author

No, try this:

# just copy paste it into a file
require 'virtus'

module BaseAddress
  include Virtus.module
  attribute :address1, String
  attribute :address2, String
end

module ExtendedAddress
  include BaseAddress

  attribute :base_address1, String
  attribute :base_address2, String
end

module SomethingElse
  include Virtus.module
  attribute :something_else1, String
  attribute :something_else2, String
end

class Entity
  include Virtus.model

  attribute :entity1, String
  attribute :entity2, String

end

class ExtendedEntity < Entity
  include ExtendedAddress, SomethingElse
end

extended_entity = ExtendedEntity.new(entity1: 1, something_else1: 'test', something_else2: 'testme')
puts extended_entity.attributes

In this case something_else1 and something_else2 are not in the attributes hash.

@solnic
Copy link
Owner

solnic commented Jul 3, 2014

It's the same issue (probably) as described in #274 so I'll fix it there and it should fix this one too

@solnic solnic added the bug label Jul 3, 2014
trptcolin added a commit to trptcolin/virtus that referenced this issue Jul 11, 2014
Known caveat: if one module wants constructors/coercions/mass-assignment
but the other one doesn't, the one that *does* want those features will
win since it mixes in the module.

fixes solnic#260
fixes solnic#274
solnic added a commit that referenced this issue Jul 11, 2014
Allow mixing in multiple Virtus.modules

fixes #260
fixes #274
trptcolin added a commit to trptcolin/virtus that referenced this issue Jul 14, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants