How to subclass and add functionality to Bridgetown::Resource::Base
?
#676
-
I'd like to add custom methods to my post resources. The docs on resources explain that I can subclass This doesn't seem to work. I'm assuming I'm missing something – can anyone help? # plugins/post.rb
class Post < Bridgetown::Resource::Base
def foo
"Hi!"
end
end
FWIW, the same thing occurs if I instead subclass |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
So there's not really a concept of a Resource subclass. If you were using a Model subclass, you could get at that via https://edge.bridgetownrb.com/docs/plugins/resource-extensions (1.2 docs) That applies to any type of resource, so if you want different behavior depending on collection you'd handle that within the method. Otherwise, at the model level, you could do: class Post < Bridgetown::Model::Base
def foo
"Hi!"
end
end
# later:
post_resource.model.foo |
Beta Was this translation helpful? Give feedback.
So there's not really a concept of a Resource subclass. If you were using a Model subclass, you could get at that via
resource.model.some_method
. But there's a way to define resource extensions:https://edge.bridgetownrb.com/docs/plugins/resource-extensions (1.2 docs)
That applies to any type of resource, so if you want different behavior depending on collection you'd handle that within the method.
Otherwise, at the model level, you could do: