# controller
class ActivityController < ApplicationController
# [POST] /activity
def create
activity_params = create_params
ActivityService.delay.log(activity_params[:user_id], activity_params[:module_id], activity_params[:entity_id])
render :nothing => true, :status => 200
end
private
def create_params
params.require(:activity).permit(:user_id, :module_id, :entity_id)
end
end
# service
class ActivityService
def self.log(user_id, module_id, entity_id)
ActivityLog.create({user_id: user_id, module_id: module_id, entity_id: entity_id})
end
end
# model
class ActivityLog < ActiveRecord::Base
extend Common
def self.create(create_params)
user_id = create_params[:user_id]
module_id = create_params[:module_id]
entity_id = create_params[:entity_id]
activity = nil
if entity_id.present?
activity = where({user_id: user_id, module_id: module_id, entity_id: entity_id}).first
else
activity = where({user_id: user_id, module_id: module_id}).first
end
unless activity.present? && activity.is_unread
activity = super.create(create_params)
end
activity
end
end
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.