Sha256: 9b84de1dee3a28d13e4ac814f8d36a4b961f21f11bd0bd01c1f1df0c5afa5917

Contents?: true

Size: 1.14 KB

Versions: 7

Compression:

Stored size: 1.14 KB

Contents

module CalculateStat

  def self.included(base)
    base.extend ClassMethods
    base.send :include, InstanceMethods
    base.class_eval do
      validates_presence_of :start_date, :end_date
      validate :check_date
    end
  end

  module ClassMethods
    # 利用統計の集計を開始します。
    def calculate_stat
      self.not_calculated.each do |stat|
        stat.transition_to!(:started)
      end
    end
  end

  module InstanceMethods
    # 利用統計の日付をチェックします。
    def check_date
      if self.start_date and self.end_date
        if self.start_date >= self.end_date
          errors.add(:start_date)
          errors.add(:end_date)
        end
      end
    end

    # 利用統計の集計完了メッセージを送信します。
    def send_message
      sender = User.find(1) #system
      message_template = MessageTemplate.localized_template('counting_completed', user.profile.locale)
      request = MessageRequest.new
      request.assign_attributes({sender: sender, receiver: user, message_template: message_template})
      request.save_message_body
      request.transition_to!(:sent)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
enju_leaf-1.1.4 lib/enju_leaf/calculate_stat.rb
enju_leaf-1.1.3 lib/enju_leaf/calculate_stat.rb
enju_leaf-1.1.2 lib/enju_leaf/calculate_stat.rb
enju_leaf-1.1.1 lib/enju_leaf/calculate_stat.rb
enju_leaf-1.1.0 lib/enju_leaf/calculate_stat.rb
enju_leaf-1.1.0.rc22 lib/enju_leaf/calculate_stat.rb
enju_leaf-1.1.0.rc21 lib/enju_leaf/calculate_stat.rb