Sha256: 0aedcda765d382a075d764d672a170c650257ab36c18871a299f69cb60435753

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

class Share < ActiveRecord::Base
  belongs_to :shareable, :polymorphic => true
  belongs_to :user
  
  before_create :set_url_hash
  
  after_create :create_total
  
  validates :shareable_type, :presence => true, :on => :create
  validates :shareable_id, :presence => true, :on => :create
  
  def set_url_hash
    self.url_hash = Digest::SHA1.hexdigest("#{shareable_type}#{shareable_id}#{user_id}how_are_we_doing#{service_ident}")
  end
  
  def self.json_data_for_chart(start_date,end_date)
    arr = []
    start_date.beginning_of_day.to_date.upto(end_date.end_of_day.to_date) do |day|
      shares = self.where("created_at between ? and ?",day,day+1.day)
      arr << [day.to_time.to_i,shares.count]
    end
    arr
  end
  
  def self.json_labels_for_chart(start_date,end_date)
    shares = self.where("created_at between ? and ?",start_date.beginning_of_day,end_date.end_of_day)
    count = shares.count
    shares = shares.where(:shareable_id.not_eq => nil)
    label = shares.first.nil? ? "" : shares.first.shareable.to_share_chart_label

    [count,label]
  end
  
  def create_total
    shareable.totals.create
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
how_are_we_doing-0.0.4 app/models/share.rb