Skip to content

Instantly share code, notes, and snippets.

@Epigene
Last active April 18, 2024 12:52
Show Gist options
  • Save Epigene/d6097ad737c262d9f320ded92bb4a818 to your computer and use it in GitHub Desktop.
Save Epigene/d6097ad737c262d9f320ded92bb4a818 to your computer and use it in GitHub Desktop.
# hook approach
class InstructionsController < ApplicationController
before_action :load_instruction, only: %i[show edit]
def show
authorize @instruction
end
def edit
authorize @instruction
end
private
def load_instruction
@instruction = Instruction.find(params[:id])
end
# non-hook approach (simple memoisation, no verbs in getters)
class InstructionsController < ApplicationController
def show
authorize instruction
end
def edit
authorize instruction
end
private
def instruction
@instruction ||= Instruction.find(params[:id])
end
## discussion
1. Hook parsing and management needed, good luck understanding what hooks a NEW action needs.
2. Risk of double auth (as in `app/controllers/trmb/orders_controller.rb`)
3. Less Rails™, more Ruby :heart:.
4. A weird antipattern. Hooks are like guard clauses, intended to step in before getting to action itself (auth, redirects)
5. Yes, hooks are DRY, but where does `@instruction` come from? Let's KISS with LoC https://grugbrain.dev/#grug-on-soc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment