Sha256: 7b1902c483abbae25cac0a64cf02260471f34e1ebe68ba9f22fb6e9e40c76881
Contents?: true
Size: 1.67 KB
Versions: 20
Compression:
Stored size: 1.67 KB
Contents
class UserCheckoutStat < ActiveRecord::Base include Statesman::Adapters::ActiveRecordQueries include CalculateStat 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 belongs_to :user 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) send_message end private def self.transition_class UserCheckoutStatTransition end def self.initial_state :pending end end # == Schema Information # # Table name: user_checkout_stats # # id :integer not null, primary key # start_date :datetime # end_date :datetime # note :text # created_at :datetime # updated_at :datetime # started_at :datetime # completed_at :datetime # user_id :integer #
Version data entries
20 entries across 20 versions & 1 rubygems