Closed aar0nr closed 3 years ago
When using ActiveSupport::Concern
, both the overridden services
method as well as the method defined by attribute
are defined in modules that just happen to be included in the "wrong" order.
I think we build the anonymous module for the attribute methods lazily. Maybe a fix could be to build and include it as soon as possible.
As a workaround you might consider using a Modularity trait instead of ActiveSupport::Concern
:
module Serviceable
as_trait do
attribute :services, :string
def services
raise "foo"
end
end
end
class Project < ActiveType::Object
include Serviceable
end
Project.new.services # raises "foo"
Thanks for the reply guys. Here is a more simple example without using a concern.
module Serviceable
def services
raise "foo"
end
end
class Project < ActiveType::Object
include Serviceable
attribute :services, :string
end
Project.new.services
# => nil
Now we try including the module after the attribute, and it works as expected...
class Project < ActiveType::Object
attribute :services, :string
include Serviceable
end
Project.new.services
# => RuntimeError (foo)
When using
ActiveSupport::Concern
, both the overriddenservices
method as well as the method defined byattribute
are defined in modules that just happen to be included in the "wrong" order.I think we build the anonymous module for the attribute methods lazily. Maybe a fix could be to build and include it as soon as possible.
@kratob Hello again. Yes, I think it would be good to mirror the behavior of Rails. I just spent some time slamming my head against my keyboard until I remembered why this wasn't working as expected. Doh! 😄
It turns out to be quite difficult to fix it. It is certainly doable (since ActiveRecord does it), but we did not find a simple consistent way to do it.
We would accept a PR that fixes this in a clean way, but will not try to fix it ourselves.
@kratob I understand, thanks for investigating.
Given the following code...
I would expect calling the
services
method to raise an exception, but it returnsnil
.Rewritten to use
ActiveModel
, it works as expected.However, it works with ActiveType when not using a module:
Any ideas?
Versions: