-
-
Save sergii/40af5b6fcb0d23a083c294d4c7e5d455 to your computer and use it in GitHub Desktop.
story.rb refactor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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