Created
April 27, 2014 12:44
-
-
Save yoichiro-manabe/11344687 to your computer and use it in GitHub Desktop.
ActiveRecordの共通処理をmoduleで行う
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
# == | |
# モデルのベースクラス | |
# モデルに共通の処理はこのクラスに定義する。 | |
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