Sha256: 3ec434582e62fd555dfabf6629e91924016f39bc90f056ebd9036d09aeaa3f4d

Contents?: true

Size: 1.54 KB

Versions: 40

Compression:

Stored size: 1.54 KB

Contents

class ManifestationCheckoutStat < ActiveRecord::Base
  include CalculateStat
  scope :not_calculated, where(:state => 'pending')
  has_many :checkout_stat_has_manifestations
  has_many :manifestations, :through => :checkout_stat_has_manifestations

  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
    Manifestation.find_each do |manifestation|
      daily_count = Checkout.manifestations_count(self.start_date, self.end_date, manifestation)
      #manifestation.update_attributes({:daily_checkouts_count => daily_count, :total_count => manifestation.total_count + daily_count})
      if daily_count > 0
        self.manifestations << manifestation
        sql = ['UPDATE checkout_stat_has_manifestations SET checkouts_count = ? WHERE manifestation_checkout_stat_id = ? AND manifestation_id = ?', daily_count, self.id, manifestation.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: manifestation_checkout_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/manifestation_checkout_stat.rb
enju_circulation-0.0.42 app/models/manifestation_checkout_stat.rb
enju_circulation-0.0.41 app/models/manifestation_checkout_stat.rb
enju_circulation-0.0.40 app/models/manifestation_checkout_stat.rb
enju_circulation-0.0.39 app/models/manifestation_checkout_stat.rb
enju_circulation-0.0.38 app/models/manifestation_checkout_stat.rb
enju_circulation-0.0.37 app/models/manifestation_checkout_stat.rb
enju_circulation-0.0.36 app/models/manifestation_checkout_stat.rb
enju_circulation-0.0.35 app/models/manifestation_checkout_stat.rb
enju_circulation-0.0.34 app/models/manifestation_checkout_stat.rb
enju_circulation-0.0.33 app/models/manifestation_checkout_stat.rb
enju_circulation-0.0.32 app/models/manifestation_checkout_stat.rb
enju_circulation-0.0.31 app/models/manifestation_checkout_stat.rb
enju_circulation-0.0.30 app/models/manifestation_checkout_stat.rb
enju_circulation-0.0.29 app/models/manifestation_checkout_stat.rb
enju_circulation-0.0.28 app/models/manifestation_checkout_stat.rb
enju_circulation-0.0.27 app/models/manifestation_checkout_stat.rb
enju_circulation-0.0.26 app/models/manifestation_checkout_stat.rb
enju_circulation-0.0.25 app/models/manifestation_checkout_stat.rb
enju_circulation-0.0.24 app/models/manifestation_checkout_stat.rb