Hi everyone How are you doing
# avo-2
b
Hi everyone! How are you doing? I have a question about using inheritance in resources. In my current use case, I have 3 resources that have many common fields. Would be appopriate to use inheritance in those cases? For example I would have:
Copy code
class CommonResource < Avo::BaseResource
  field :common_field, as: :text do
      logic
  end
end
and then 3 other resources that inherit from that one. So returning to the question, I am supposed to use avo in this way? Is there other alternatives for this?
l
this is going to be improved in Avo 3. in Avo 2 it's still a bit clunky
the reason is that they are all class methods
b
ahhhh ok ok
l
I think you could use a concern for that
b
so i guess a concern is better for this
hahahah
yes
great minds think alike
l
yeah, but then you wouldn't have control over the position of the fields
but if you don't care about that, you coudl use a concern
b
the common fields would be where i place the include
l
Avo 3 will have this API much improved as all fields will be declared inside a
def fields
method and the composability will be improved
b
that's cool
I will update you if I find a solution for this
l
please do
not sure if something as ugly as this will work
Copy code
ruby
module SomeModule
  def self.common_fields
    -> {
      field :common_field, as: :text do
        logic
      end
    }
  end
end

class UserResource < Avo::BaseResource
  field :one
  SomeModule.common_fields.call
  field :two
end
class PersonResource < Avo::BaseResource
  field :four
  SomeModule.common_fields.call
  field :five
end
I made this in a few seconds now, so don't judge it please 😛