cameronyule (owner)

Revisions

gist: 215888 Download_button fork
public
Public Clone URL: git://gist.github.com/215888.git
Embed All Files: show embed
Artist.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Artist < ActiveRecord::Base
  
  include Chartable
  
  has_many :albums, :dependent => :destroy
  has_many :tracks, :dependent => :destroy
  has_many :mentions, :as => :mentionable, :dependent => :destroy
  
  # TODO delete similar artists join data when deleting an artist
  has_and_belongs_to_many :similar_artists, :class_name => 'Artist', :join_table => 'similar_artists', :association_foreign_key => 'similar_artist_id'
 
  # TODO figure out what sizes I actually need
  has_attached_file :artwork, :styles => { :medium => "300x300>", :thumb => "100x100>" }
  
  validates_uniqueness_of :name
  
  named_scope :pending_similar_artist_scan, :conditions => {:processed_for_similar_artists => false}
  named_scope :pending_release_history_scan, :conditions => {:processed_for_release_history => false}
  named_scope :not_common_name, :conditions => {:name_too_common => false}
  
  after_create :check_for_common_name
  before_save :delete_mentions_if_setting_common_name
  
  def check_for_common_name
    if !name.blank? && COMMON_WORDS.include?(name.upcase)
      self.update_attribute(:name_too_common, true)
    end
  end
  
  # If we decide an artist has too common a name to be accurately tracked in blog posts
  # then we should delete all instances of that artist having been tracked. This affects
  # the chart data (e.g. if the artist "Yes" had been tracked accidentally)
  def delete_mentions_if_setting_common_name
    self.mentions.clear if name_too_common_changed? && name_too_common
  end
 
end
 
Chartable.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module Chartable
  
  # TODO any class which includes this module must be inheriting from ActiveRecord::Base or
  # it won't work - flawed design? I think my original idea about the Artist, Album
  # and Track classes subclassing Chartable is better, but I don't want Chartable to inherit
  # from AR::Base.
  # http://stackoverflow.com/questions/1542945/testing-modules-in-rspec
  
  def self.included(model)
    model.extend(ClassMethods)
  end
  
  module ClassMethods
    def weekly_chart
      start_date = 7.days.ago
      end_date = Time.now
    
      self.chart(start_date, end_date)
    end
    
    def chart(start_date, end_date, limit = 20)
      all(
        :select => "#{self.table_name}.id, #{self.table_name}.name, count(mentions.mentionable_id) as number_of_mentions",
        :joins => [:mentions],
        :conditions => {
          :mentions => {
            :mentionable_type => self.name,
            :created_at => start_date..end_date
          }
        },
        :group => 'mentions.mentionable_id',
        :order => "number_of_mentions DESC, #{self.table_name}.name ASC",
        :limit => limit
      )
    end
  end
 
end
 
Text only #