Sha256: 0001c38b2a53a9555c00ba55af84703c0e88e6396f3483d35bf493b32a668d97

Contents?: true

Size: 1.58 KB

Versions: 10

Compression:

Stored size: 1.58 KB

Contents

class BookmarkStat < ActiveRecord::Base
  attr_accessible :start_date, :end_date, :note
  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

10 entries across 10 versions & 1 rubygems

Version Path
enju_bookmark-0.0.22 app/models/bookmark_stat.rb
enju_bookmark-0.0.21 app/models/bookmark_stat.rb
enju_bookmark-0.0.20 app/models/bookmark_stat.rb
enju_bookmark-0.0.19 app/models/bookmark_stat.rb
enju_bookmark-0.0.18 app/models/bookmark_stat.rb
enju_bookmark-0.0.17 app/models/bookmark_stat.rb
enju_bookmark-0.0.16 app/models/bookmark_stat.rb
enju_bookmark-0.0.15 app/models/bookmark_stat.rb
enju_bookmark-0.0.14 app/models/bookmark_stat.rb
enju_bookmark-0.0.13 app/models/bookmark_stat.rb