Sha256: fc4e9aeb82d42e1853dbae7a4d84a7be8163d1aef9d990dcb3daa40d4ac8d6d5

Contents?: true

Size: 1.31 KB

Versions: 40

Compression:

Stored size: 1.31 KB

Contents

class UserReserveStat < ActiveRecord::Base
  include CalculateStat
  default_scope :order => 'id DESC'
  scope :not_calculated, where(:state => 'pending')
  has_many :reserve_stat_has_users
  has_many :users, :through => :reserve_stat_has_users

  state_machine :initial => :pending do
    before_transition :pending => :completed, :do => :calculate_count
    event :calculate do
      transition :pending => :completed
    end
  end

  def self.per_page
    10
  end

  def calculate_count
    self.started_at = Time.zone.now
    User.find_each do |user|
      daily_count = user.reserves.created(self.start_date, self.end_date).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, self.id, user.id]
        ActiveRecord::Base.connection.execute(
          self.class.send(:sanitize_sql_array, sql)
        )
      end
    end
    self.completed_at = Time.zone.now
  end
end

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

Version data entries

40 entries across 40 versions & 1 rubygems

Version Path
enju_circulation-0.0.43 app/models/user_reserve_stat.rb
enju_circulation-0.0.42 app/models/user_reserve_stat.rb
enju_circulation-0.0.41 app/models/user_reserve_stat.rb
enju_circulation-0.0.40 app/models/user_reserve_stat.rb
enju_circulation-0.0.39 app/models/user_reserve_stat.rb
enju_circulation-0.0.38 app/models/user_reserve_stat.rb
enju_circulation-0.0.37 app/models/user_reserve_stat.rb
enju_circulation-0.0.36 app/models/user_reserve_stat.rb
enju_circulation-0.0.35 app/models/user_reserve_stat.rb
enju_circulation-0.0.34 app/models/user_reserve_stat.rb
enju_circulation-0.0.33 app/models/user_reserve_stat.rb
enju_circulation-0.0.32 app/models/user_reserve_stat.rb
enju_circulation-0.0.31 app/models/user_reserve_stat.rb
enju_circulation-0.0.30 app/models/user_reserve_stat.rb
enju_circulation-0.0.29 app/models/user_reserve_stat.rb
enju_circulation-0.0.28 app/models/user_reserve_stat.rb
enju_circulation-0.0.27 app/models/user_reserve_stat.rb
enju_circulation-0.0.26 app/models/user_reserve_stat.rb
enju_circulation-0.0.25 app/models/user_reserve_stat.rb
enju_circulation-0.0.24 app/models/user_reserve_stat.rb