Sha256: 2f2f2bbeb11c6976559ff43e5618fbca671fc1481bb822c3ad055fb6f6ee58bc

Contents?: true

Size: 1.79 KB

Versions: 4

Compression:

Stored size: 1.79 KB

Contents

class BookmarkStat < ActiveRecord::Base
  include Statesman::Adapters::ActiveRecordModel
  include CalculateStat
  attr_accessible :start_date, :end_date, :note
  default_scope { order('bookmark_stats.id DESC') }
  scope :not_calculated, -> {in_state(:pending)}
  has_many :bookmark_stat_has_manifestations
  has_many :manifestations, through: :bookmark_stat_has_manifestations

  paginates_per 10

  has_many :bookmark_stat_transitions

  def state_machine
    BookmarkStatStateMachine.new(self, transition_class: BookmarkStatTransition)
  end

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

  def calculate_count!
    self.started_at = Time.zone.now
    Manifestation.find_each do |manifestation|
      daily_count = Bookmark.manifestations_count(start_date, end_date, manifestation)
      #manifestation.update_attributes({:daily_bookmarks_count => daily_count, :total_count => manifestation.total_count + daily_count})
      if daily_count > 0
        self.manifestations << manifestation
        sql = ['UPDATE bookmark_stat_has_manifestations SET bookmarks_count = ? WHERE bookmark_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
    transition_to!(:completed)
  end

  private
  def self.transition_class
    BookmarkStatTransition
  end
end

# == Schema Information
#
# Table name: bookmark_stats
#
#  id           :integer          not null, primary key
#  start_date   :datetime
#  end_date     :datetime
#  started_at   :datetime
#  completed_at :datetime
#  note         :text
#  created_at   :datetime         not null
#  updated_at   :datetime         not null
#

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
enju_bookmark-0.1.2.pre20 app/models/bookmark_stat.rb
enju_bookmark-0.1.2.pre19 app/models/bookmark_stat.rb
enju_bookmark-0.1.2.pre18 app/models/bookmark_stat.rb
enju_bookmark-0.1.2.pre17 app/models/bookmark_stat.rb