Sha256: 7b6e471b65712dd168271adb6f08f53251bde697fd3a4d4eaa6395185ec455ff

Contents?: true

Size: 1.69 KB

Versions: 4

Compression:

Stored size: 1.69 KB

Contents

class UserReserveStat < ActiveRecord::Base
  include Statesman::Adapters::ActiveRecordModel
  include CalculateStat
  attr_accessible :start_date, :end_date, :note, :mode
  default_scope {order('user_reserve_stats.id DESC')}
  scope :not_calculated, -> {in_state(:pending)}
  has_many :reserve_stat_has_users
  has_many :users, through: :reserve_stat_has_users
  belongs_to :user

  paginates_per 10
  attr_accessor :mode

  has_many :user_reserve_stat_transitions

  def state_machine
    UserReserveStatStateMachine.new(self, transition_class: UserReserveStatTransition)
  end

  delegate :can_transition_to?, :transition_to!, :transition_to, :current_state,
    to: :state_machine

  def calculate_count!
    self.started_at = Time.zone.now
    User.find_each do |user|
      daily_count = user.reserves.created(start_date.beginning_of_day, end_date.tomorrow.beginning_of_day).size
      if daily_count > 0
        self.users << user
        sql = ['UPDATE reserve_stat_has_users SET reserves_count = ? WHERE user_reserve_stat_id = ? AND user_id = ?', daily_count, id, user.id]
        UserReserveStat.connection.execute(
          self.class.send(:sanitize_sql_array, sql)
        )
      end
    end
    self.completed_at = Time.zone.now
    transition_to!(:completed)
    send_message
  end

  private
  def self.transition_class
    UserReserveStatTransition
  end
end

# == Schema Information
#
# Table name: user_reserve_stats
#
#  id           :integer          not null, primary key
#  start_date   :datetime
#  end_date     :datetime
#  note         :text
#  created_at   :datetime         not null
#  updated_at   :datetime         not null
#  started_at   :datetime
#  completed_at :datetime
#  user_id      :integer
#

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
enju_circulation-0.1.0.pre40 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre39 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre38 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre37 app/models/user_reserve_stat.rb