Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Viewable callable object mixin for Ruby components #959

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jaredcwhite
Copy link
Member

This PR builds upon the recent #892 PR by providing a Viewable mixin that Ruby components can use to become fully-fledged views with front matter and layout rendering support. It also fixes a cascade issue, so now you can chain multiple callable objects together and also return things like a Hash from a callable object which will automatically become a JSON response.

This effectively means you can now write clean routes in server/routes, create Controllers in server/controllers, create Views in src/_components/views, and get a full MVC paradigm like you would be familiar with coming from frameworks like Rails, Hanami, etc. And because the underlying patterns are so simple (callable objects via mixins), any aspect of this can be further extended by app developers in their projects, e.g. we don't have a class for a "controller" or "action" or whatever, but that could be easily done! For example:

class Action
  include Bridgetown::RodaCallable

  class << self
    attr_reader :before_method

    def before(method_name)
      @before_method = method_name
    end
  end

  def call(app)
    self.send(self.class.before_method, app.request, app.response) if self.class.before_method
    handle(app.request, app.response)
  rescue StandardError => e
    # do something special
  end
end

I don't want to prematurely optimize for this kind of thing — I personally am not looking for the amount of OOP ceremony as Hanami recommends — but for folks who want that, I like the idea we provide the primitives so you can seamlessly scale up to Hanami levels or down to simple Roda route blocks whenever needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant