Sha256: e29f04af35d86d68dda82b330727298901c24d2222ac5a938dc63c7c8fb9d2ed

Contents?: true

Size: 1.41 KB

Versions: 26

Compression:

Stored size: 1.41 KB

Contents

class UserCheckoutStat < ActiveRecord::Base
  attr_accessible :start_date, :end_date, :note
  include CalculateStat
  default_scope :order => 'user_checkout_stats.id DESC'
  scope :not_calculated, where(:state => 'pending')
  has_many :checkout_stat_has_users
  has_many :users, :through => :checkout_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.checkouts.completed(self.start_date, self.end_date).size
      if daily_count > 0
        self.users << user
        sql = ['UPDATE checkout_stat_has_users SET checkouts_count = ? WHERE user_checkout_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_checkout_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

26 entries across 26 versions & 1 rubygems

Version Path
enju_circulation-0.0.71 app/models/user_checkout_stat.rb
enju_circulation-0.0.70 app/models/user_checkout_stat.rb
enju_circulation-0.0.69 app/models/user_checkout_stat.rb
enju_circulation-0.0.68 app/models/user_checkout_stat.rb
enju_circulation-0.0.67 app/models/user_checkout_stat.rb
enju_circulation-0.0.66 app/models/user_checkout_stat.rb
enju_circulation-0.0.65 app/models/user_checkout_stat.rb
enju_circulation-0.0.64 app/models/user_checkout_stat.rb
enju_circulation-0.0.63 app/models/user_checkout_stat.rb
enju_circulation-0.0.62 app/models/user_checkout_stat.rb
enju_circulation-0.0.61 app/models/user_checkout_stat.rb
enju_circulation-0.0.60 app/models/user_checkout_stat.rb
enju_circulation-0.0.59 app/models/user_checkout_stat.rb
enju_circulation-0.0.58 app/models/user_checkout_stat.rb
enju_circulation-0.0.57 app/models/user_checkout_stat.rb
enju_circulation-0.0.56 app/models/user_checkout_stat.rb
enju_circulation-0.0.55 app/models/user_checkout_stat.rb
enju_circulation-0.0.54 app/models/user_checkout_stat.rb
enju_circulation-0.0.53 app/models/user_checkout_stat.rb
enju_circulation-0.0.52 app/models/user_checkout_stat.rb