Sha256: 2b73dd55f78bea75e2f2f5d83837ad1602e68296e8151b9ddb76f0c97125d1ce

Contents?: true

Size: 1.64 KB

Versions: 27

Compression:

Stored size: 1.64 KB

Contents

class ManifestationReserveStat < ActiveRecord::Base
  attr_accessible :start_date, :end_date, :note
  include CalculateStat
  default_scope :order => 'manifestation_reserve_stats.id DESC'
  scope :not_calculated, where(:state => 'pending')
  has_many :reserve_stat_has_manifestations
  has_many :manifestations, :through => :reserve_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 = manifestation.reserves.created(self.start_date, self.end_date).size
      #manifestation.update_attributes({:daily_reserves_count => daily_count, :total_count => manifestation.total_count + daily_count})
      if daily_count > 0
        self.manifestations << manifestation
        sql = ['UPDATE reserve_stat_has_manifestations SET reserves_count = ? WHERE manifestation_reserve_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_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

27 entries across 27 versions & 1 rubygems

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