Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Broadcaster < ActiveRecord::Base
- extend MintyScopes
- extend MintValidations
- include SeoSupport
- ## Associations ##
- has_many :brand_assignments, :dependent => :destroy
- has_many :brands, :through => :brand_assignments
- with_options(:through => :brand_assignments, :source => :brand) do |o|
- o.has_many :tv_shows, :conditions => {:type => 'TvShow'}
- o.has_many :movies, :conditions => {:type => 'Movie'}
- end
- has_many :seo_items, :as => :attachable, :dependent => :destroy
- with_options(:class_name => 'Image', :as => :attachable, :dependent => :destroy) do |i|
- i.has_one :banner, :conditions => {:image_type => 'Banner'}
- i.has_one :thumbnail, :conditions => {:image_type => 'Image'}
- i.has_one :wp7_bg, :conditions => {:image_type => 'Wp7bg'}
- i.has_one :small_icon, :conditions => {:image_type => 'Small'}
- end
- disable_update_counters_for :images
- has_many :content_partners, :through => :content_partner_broadcasters
- ## Validations ##
- validates_presence_of :name
- validates_as_hex_color :background_color, :allow_blank => true
- validates_numericality_of :content_expiry_hours, :only_integer => true, :allow_nil => true, :greater_than => 0
- # Class level declarations
- attr_accessible :name, :permalink, :thumbnail_attributes, :wp7_bg_attributes, :small_icon_attributes, :description,
- :banner_attributes, :background_color, :content_expiry_hours
- attr_accessible *SeoItem::KEYS.map{ |key| :"seo_#{key}" }
- accepts_nested_attributes_for :banner, :thumbnail, :wp7_bg, :small_icon, :allow_destroy => false
- ## Thinking Sphinx ##
- define_index do
- indexes name
- indexes seo_items.seo_content, :as => 'seo_content'
- end
- ## Plugins ##
- can_has_permalink?
- can_has_one_replacement_for :thumbnail, :klass => Image
- # Named Scopes #
- named_scope :with_popularity, {
- :select => 'broadcasters.*, COALESCE(SUM(brands.popularity),0) AS popularity',
- :joins =>
- 'LEFT JOIN brand_assignments ' +
- 'ON broadcasters.id = brand_assignments.broadcaster_id ' +
- 'LEFT JOIN brands ON brand_assignments.brand_id = brands.id',
- :group => 'broadcasters.id'
- }
- # Instance methods
- # We can't deeply nest has_many associations
- # (:broadcaster => :brand_associations => :brands => :content_items)
- # but this works pretty well...
- def content_items
- brand_ids = brand_assignments.map(&:brand_id)
- ContentItem.by_brand(brand_ids)
- end
- def default_thumbnail_style; :network ; end
- def default_wp7_bg_style; :wp7_broadcaster_bg ; end
- def thumbnail_url(style=nil)
- thumbnail.url(style || default_thumbnail_style) if thumbnail
- end
- def wp7_bg_url(style=nil)
- wp7_bg.url(style || default_wp7_bg_style) if wp7_bg
- end
- def to_s
- name
- end
- def to_xml(options={})
- style = ActiveSupport::StringInquirer.new((options[:style]||'default').to_s)
- xml = options[:builder] || Builder::XmlMarkup.new(options)
- xml.instruct! unless options[:skip_instruct]
- xml.tag! 'broadcaster' do
- xml.tag! 'id', id
- xml.tag! 'name', name
- xml.tag! 'type', type
- unless style.minimal?
- xml.tag! 'popularity', popularity
- xml.tag! 'items-count', visible_brands_count
- xml.tag! 'thumbnail', thumbnail.try(:url, :small_wide)
- xml.tag! 'wp7-bg', wp7_bg_url
- end
- if style.full?
- xml.tag! 'description', seo_text
- brands.to_xml(:root => 'brands', :skip_instruct => true, :builder => xml)
- end
- end
- end
- def popularity
- self[:popularity] || brands.sum(:popularity)
- end
- #TODO this is for the demo and may go if broadcasters get their own merch
- def merchandise
- BrandSellable.find(:all, :include => { :brand => :broadcasters }, :conditions => ["broadcasters.id = ? AND brand_sellables.kind = ?", self.id, 'Merchandise'])
- end
- end
Add Comment
Please, Sign In to add comment