trestle icon indicating copy to clipboard operation
trestle copied to clipboard

Ability to generate base resource

Open yagudaev opened this issue 5 years ago • 2 comments

It would be great if we could have the ability to generate a base resource somehow.

Even something like:

Trestle.resource(:post) do
  include BaseResource
end

module BaseResource
  def self.included(r)
    r.scope :active, -> { model.kept }, default: true
    r.scope :deleted, -> { model.unscoped.discarded }

    r.delete_instance do |instance|
      instance.discard
    end
  end
end

It will give us the following error:

undefined method `include' for #<Trestle::Resource::Builder:0x00007f9c1ce3c848>

Related to https://github.com/TrestleAdmin/trestle/issues/161

yagudaev avatar Mar 12 '19 20:03 yagudaev

Have you found a solution to your problem ? I have the same problem.

lphm avatar Apr 26 '20 19:04 lphm

I definitely want to implement some nicer support for pluggable/extendable functionality like this. For now though, the best way to do this would be something like this:

BaseResource = -> {
  scope :active, -> { model.kept }, default: true
  scope :deleted, -> { model.unscoped.discarded }

  delete_instance do |instance|
    instance.discard
  end
}

Trestle.resource(:post) do
  instance_exec(&BaseResource)
end

spohlenz avatar Apr 29 '20 11:04 spohlenz