Sha256: cd53a2da9f75e79594d92a6efe42992c1de1021d956ef913199f23d6ddb6d8c0

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

class UserCheckoutStat < ActiveRecord::Base
  include Statesman::Adapters::ActiveRecordModel
  include CalculateStat
  attr_accessible :start_date, :end_date, :note, :mode
  default_scope {order('user_checkout_stats.id DESC')}
  scope :not_calculated, -> {in_state(:pending)}
  has_many :checkout_stat_has_users
  has_many :users, :through => :checkout_stat_has_users

  paginates_per 10
  attr_accessor :mode

  has_many :user_checkout_stat_transitions

  def state_machine
    UserCheckoutStatStateMachine.new(self, transition_class: UserCheckoutStatTransition)
  end

  delegate :can_transition_to?, :transition_to!, :transition_to, :current_state,
    to: :state_machine

  def calculate_count!
    self.started_at = Time.zone.now
    User.find_each do |user|
      daily_count = user.checkouts.completed(start_date.beginning_of_day, end_date.tomorrow.beginning_of_day).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, id, user.id]
        UserCheckoutStat.connection.execute(
          self.class.send(:sanitize_sql_array, sql)
        )
      end
    end
    self.completed_at = Time.zone.now
    transition_to!(:completed)
  end

  private
  def self.transition_class
    UserCheckoutStatTransition
  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

1 entries across 1 versions & 1 rubygems

Version Path
enju_circulation-0.1.0.pre36 app/models/user_checkout_stat.rb