Sha256: 57efca2f496939d986a8268adb1909f70010c41a5878df178aea47d80fa24386

Contents?: true

Size: 1.39 KB

Versions: 28

Compression:

Stored size: 1.39 KB

Contents

class UserReserveStat < ActiveRecord::Base
  attr_accessible :start_date, :end_date, :note
  include CalculateStat
  default_scope :order => 'user_reserve_stats.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

  paginates_per 10

  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         not null
#  updated_at   :datetime         not null
#  started_at   :datetime
#  completed_at :datetime
#

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
enju_circulation-0.1.0.pre35 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre34 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre33 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre32 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre31 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre30 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre29 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre28 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre27 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre26 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre25 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre24 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre23 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre22 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre21 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre20 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre19 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre18 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre17 app/models/user_reserve_stat.rb
enju_circulation-0.1.0.pre16 app/models/user_reserve_stat.rb