Guest User

Untitled

a guest
Aug 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. class Broadcaster < ActiveRecord::Base
  2. extend MintyScopes
  3. extend MintValidations
  4. include SeoSupport
  5.  
  6. ## Associations ##
  7. has_many :brand_assignments, :dependent => :destroy
  8. has_many :brands, :through => :brand_assignments
  9. with_options(:through => :brand_assignments, :source => :brand) do |o|
  10. o.has_many :tv_shows, :conditions => {:type => 'TvShow'}
  11. o.has_many :movies, :conditions => {:type => 'Movie'}
  12. end
  13. has_many :seo_items, :as => :attachable, :dependent => :destroy
  14.  
  15. with_options(:class_name => 'Image', :as => :attachable, :dependent => :destroy) do |i|
  16. i.has_one :banner, :conditions => {:image_type => 'Banner'}
  17. i.has_one :thumbnail, :conditions => {:image_type => 'Image'}
  18. i.has_one :wp7_bg, :conditions => {:image_type => 'Wp7bg'}
  19. i.has_one :small_icon, :conditions => {:image_type => 'Small'}
  20. end
  21. disable_update_counters_for :images
  22. has_many :content_partners, :through => :content_partner_broadcasters
  23.  
  24. ## Validations ##
  25. validates_presence_of :name
  26. validates_as_hex_color :background_color, :allow_blank => true
  27. validates_numericality_of :content_expiry_hours, :only_integer => true, :allow_nil => true, :greater_than => 0
  28.  
  29. # Class level declarations
  30. attr_accessible :name, :permalink, :thumbnail_attributes, :wp7_bg_attributes, :small_icon_attributes, :description,
  31. :banner_attributes, :background_color, :content_expiry_hours
  32. attr_accessible *SeoItem::KEYS.map{ |key| :"seo_#{key}" }
  33. accepts_nested_attributes_for :banner, :thumbnail, :wp7_bg, :small_icon, :allow_destroy => false
  34.  
  35. ## Thinking Sphinx ##
  36. define_index do
  37. indexes name
  38. indexes seo_items.seo_content, :as => 'seo_content'
  39. end
  40.  
  41. ## Plugins ##
  42. can_has_permalink?
  43. can_has_one_replacement_for :thumbnail, :klass => Image
  44.  
  45. # Named Scopes #
  46. named_scope :with_popularity, {
  47. :select => 'broadcasters.*, COALESCE(SUM(brands.popularity),0) AS popularity',
  48. :joins =>
  49. 'LEFT JOIN brand_assignments ' +
  50. 'ON broadcasters.id = brand_assignments.broadcaster_id ' +
  51. 'LEFT JOIN brands ON brand_assignments.brand_id = brands.id',
  52. :group => 'broadcasters.id'
  53. }
  54.  
  55. # Instance methods
  56.  
  57. # We can't deeply nest has_many associations
  58. # (:broadcaster => :brand_associations => :brands => :content_items)
  59. # but this works pretty well...
  60. def content_items
  61. brand_ids = brand_assignments.map(&:brand_id)
  62. ContentItem.by_brand(brand_ids)
  63. end
  64.  
  65. def default_thumbnail_style; :network ; end
  66.  
  67. def default_wp7_bg_style; :wp7_broadcaster_bg ; end
  68.  
  69. def thumbnail_url(style=nil)
  70. thumbnail.url(style || default_thumbnail_style) if thumbnail
  71. end
  72.  
  73. def wp7_bg_url(style=nil)
  74. wp7_bg.url(style || default_wp7_bg_style) if wp7_bg
  75. end
  76.  
  77. def to_s
  78. name
  79. end
  80.  
  81. def to_xml(options={})
  82. style = ActiveSupport::StringInquirer.new((options[:style]||'default').to_s)
  83. xml = options[:builder] || Builder::XmlMarkup.new(options)
  84.  
  85. xml.instruct! unless options[:skip_instruct]
  86. xml.tag! 'broadcaster' do
  87. xml.tag! 'id', id
  88. xml.tag! 'name', name
  89. xml.tag! 'type', type
  90. unless style.minimal?
  91. xml.tag! 'popularity', popularity
  92. xml.tag! 'items-count', visible_brands_count
  93. xml.tag! 'thumbnail', thumbnail.try(:url, :small_wide)
  94. xml.tag! 'wp7-bg', wp7_bg_url
  95. end
  96. if style.full?
  97. xml.tag! 'description', seo_text
  98. brands.to_xml(:root => 'brands', :skip_instruct => true, :builder => xml)
  99. end
  100. end
  101. end
  102.  
  103. def popularity
  104. self[:popularity] || brands.sum(:popularity)
  105. end
  106.  
  107. #TODO this is for the demo and may go if broadcasters get their own merch
  108. def merchandise
  109. BrandSellable.find(:all, :include => { :brand => :broadcasters }, :conditions => ["broadcasters.id = ? AND brand_sellables.kind = ?", self.id, 'Merchandise'])
  110. end
  111. end
Add Comment
Please, Sign In to add comment