solnic / virtus

[DISCONTINUED ] Attributes on Steroids for Plain Old Ruby Objects
MIT License
3.77k stars 229 forks source link

When including multiple modules only one works properly #274

Closed sbrauer closed 10 years ago

sbrauer commented 10 years ago

I'm using version 1.0.2 and found that including multiple Virtus modules in one model doesn't seem to work. It appears to be the case that only the attributes from one module work. Consider the following demo:

require 'virtus'

module Foo
  include Virtus.module
  attribute :foo, String, default: 'foo'
end

module Bar
  include Virtus.module
  attribute :bar, String, default: 'bar'
end

class Demo
  include Virtus.model
  include Foo
  include Bar
end

d = Demo.new
puts "d.foo=#{d.foo}"
puts "d.bar=#{d.bar}"
puts "d.attributes=#{d.attributes}"

Here's the output:

d.foo=
d.bar=bar
d.attributes={:bar=>"bar"}

I expected the instance of Demo to have attributes :foo and :bar with default values, but it only has the :bar attribute. It seems to be the case that only the last module included works.

Am I doing something wrong, or is this a bug?

sbrauer commented 10 years ago

I think this may be a simpler example of the the same issue as https://github.com/solnic/virtus/issues/260

Btw: if the multiple includes are done on one line (include Foo, Bar) only the attributes in the first module work.

solnic commented 10 years ago

Seems like a bug. I'll work on fixing it which should close both this and #260