:index
General Setup & Layout
Example Setup:
class ItemsController < ApplicationController
include QueryParamsProcessor
def index
# LOCAL HANDLING
##################
# Basic scoping
@items = Item.all
# Filter by item's company
@items = @items.where(company: get_current_company)
# Filter by project if project_id is present
if params[:project_id].present?
@items = @items.joins(:projects).where(projects: { id: params[:project_id] })
end
# Role-based filtering
if @current_user_role == "manager"
@items = @items.joins(:projects).where(projects: { id: @current_user.project_ids })
end
# ABSTRACTED/REUSABLE HANDLING (e.g. Concerns, etc.)
##################
# CONCERNS
# Process query params
@items = process_query_params(@items)
# OTHER (?)
# ...
# EXECUTE & RESPOND
##################
render json: @items
end
# ... Other actions ...
end
Last updated