Skip to content

Instantly share code, notes, and snippets.

@robzolkos
Last active September 8, 2023 21:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save robzolkos/88591301a6360be857ca408c8c755152 to your computer and use it in GitHub Desktop.
Save robzolkos/88591301a6360be857ca408c8c755152 to your computer and use it in GitHub Desktop.
story.rb refactor
# Original code https://twitter.com/robinbortlik/status/1699524286568964440?s=20
# app/controllers/stories.rb
class StoriesController < ApplicationController
before_action :check_authorizationa # this should check and return a 401
def create
story = Story.new(story_params)
if story.save
render jsonapi: story, include: include_param
else
render jsonapi_errors: story.errors, status: :bad_request
end
end
private
def story_params
params.require(:story).permit(:title, :content)
end
end
# app/models/story.rb
class Story < ApplicationRecord
include Trackable
after_create_commit -> { track_event("track_story_created") }
end
# app/concerns/trackable.rb
module Trackable
extend ActiveSupport::Concern
included do
def track_event(event_name)
EventTrackerJob.perform_later(user: Current.user, event_name: event_name, ...)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment