#
#= self.id更新用モジュール
# Authors:: Sumiyo Yamamoto
# Copyright:: Copyright (C) OrbusNeich Medical K.K.  2010.
#--
# date        name                   note
# 2010.10.22  Sumiyo Yamamoto        新規作成
#-------------------------------------------------------------------------------
#++
module Comm::Module::Model
  #= self.id更新用モジュール
  #------------------------------------------------------------------------#++
  module SelfId
    #== 登録処理実行
    #-----------------------------------------------------------------#++
    def create_exec(params = {})
      super
      result = true
      @self_id_columns.each do |col|
        result = update_self_id(col, params[col])
        unless result
          break
        end
      end
      result
    end

    #== 更新処理実行
    #-----------------------------------------------------------------#++
    def update_exec(params = {})
      @self_id_columns.each do |col|
        if params[col].to_i == Comm::Const::DispNameLists::LIST_SELF
          params[col] = params.include?(:id) ? params[:id] : self.id
        end
      end
      super
    end

    #== 更新処理実行
    #-----------------------------------------------------------------#++
    def update_self_id(target_name, target_id)
      result = true
      if target_id.to_i == Comm::Const::DispNameLists::LIST_SELF
        ret = update_attributes(target_name => self.id)
        unless ret
          errors.add_to_base(EMJ0003 + EMD0009)
          result = false
        end
      end
      return result
    end
  end
end
