How I almost lost the C in MVC

Model-View-Control is one of a list of cornerstones in Ruby on Rails and a pattern in general implementation of man-machine interfaces. The C handles the request from the 'man', collects data from the the M (model) that interfaces with data whether it's persisted in some (r)DBMS or elsewhere, and finally handing it back to the 'man' with the help of the V (view) that assists by generating some sort of output whether HTML, JSON, or something entirely different.

How did I (almost) lose the C?

Back in the day there was this thing called inherited_resources that I grew very fond of and as time passed it morphed into this:

class MortimerController < BaseController
  #
  # defined in the resourceable concern
  before_action :set_resource, only: %i[ new show edit update destroy ]
  before_action :set_filter, only: %i[ index destroy ]
  before_action :set_resources, only: %i[ index destroy ]
  before_action :set_resources_stream

  include Resourceable
  include DefaultActions

  --8<--
  
end

I know – I could have put the before_action statements in the include but I like to have just a tad overview of what all the magic is about :wink:

The Resourceable is responsible for setting up required resource(s) for the actions (index, show, new, edit, create, update, delete).

The DefaultActions is responsible for handling a "default" action to any of these actions (and a few more like `lookup` which, BTW, to me is a specialization to the index action).

The result?

My standard "post" controller looks like this:

class PostController < MortimerController
  private
    # Only allow a list of trusted parameters through.
    def resource_params
      params.expect(post: [ :id, :title, :body ])
    end
end

The DefaultActions is a concern looking  – in part – like this:

module DefaultActions
  extend ActiveSupport::Concern

  included do
    def index
      @pagy, @records = pagy(@resources)

      respond_to do |format|
        format.html { }
        format.turbo_stream { }
        format.pdf { send_pdf }
        format.csv { send_csv }
      end

    rescue => e
      HandleError.new(request: request, params: params, error: e)
    end
    
    --8<--
    
  end
end

Sweet, isn't it?