Skip to content

Instantly share code, notes, and snippets.

@yoichiro-manabe
Created April 27, 2014 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoichiro-manabe/11344687 to your computer and use it in GitHub Desktop.
Save yoichiro-manabe/11344687 to your computer and use it in GitHub Desktop.
ActiveRecordの共通処理をmoduleで行う
# ==
# モデルのベースクラス
# モデルに共通の処理はこのクラスに定義する。
module ApplicationBase
extend ActiveSupport::Concern
included do
acts_as_paranoid
application_base_common
end
private
# ユーザー情報を設定する
def set_user_before_create
self.reg_user_id = $current_user.id unless $current_user.blank?
self.upd_user_id = $current_user.id unless $current_user.blank?
end
# ユーザー情報を設定する
def set_user_before_update
self.upd_user_id = $current_user.id
end
end
# 共通処理を行うためのオープンクラス
class ActiveRecord::Base
def self.application_base_common(options={})
include ApplicationBase
class_attribute :group_id_column
self.group_id_column = options[:column] || :group_id
default_scope {
where(group_id_column => $current_user.group_id) if !$current_user.blank? && !$current_user.group_id.blank?
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment