Sha256: d65849493b242536909c15723d639d02aac06fc08b04256255da46aaaf2deb3e

Contents?: true

Size: 1.53 KB

Versions: 7

Compression:

Stored size: 1.53 KB

Contents

class BookmarkStat < ActiveRecord::Base
  include CalculateStat
  default_scope :order => 'id DESC'
  scope :not_calculated, where(:state => 'pending')
  has_many :bookmark_stat_has_manifestations
  has_many :manifestations, :through => :bookmark_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 = 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
  end
end

# == Schema Information
#
# Table name: bookmark_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

7 entries across 7 versions & 1 rubygems

Version Path
enju_bookmark-0.0.12 app/models/bookmark_stat.rb
enju_bookmark-0.0.11 app/models/bookmark_stat.rb
enju_bookmark-0.0.10 app/models/bookmark_stat.rb
enju_bookmark-0.0.9 app/models/bookmark_stat.rb
enju_bookmark-0.0.8 app/models/bookmark_stat.rb
enju_bookmark-0.0.7 app/models/bookmark_stat.rb
enju_bookmark-0.0.6 app/models/bookmark_stat.rb