Radio, Web and Free Software

Changeset cb93cb8c2391bcfdff4eefed378aa47f6a8ef266

Show
Ignore:
Timestamp:
07/27/10 09:01:10 (6 weeks ago)
Author:
Alban Peignier <alban@…>
Parents:
4c2e420eff672f881710178e0488b6a07aebec4c
Children:
c5037a4ce54f151978b2a29e61f78dde36d3cbea
git-committer:
Alban Peignier <alban@tryphon.eu> / 2010-07-27T09:01:10Z+0200
Message:

Optimise db usage for PublicController#feed?

Location:
app
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • app/controllers/public_controller.rb

    r439b576 rcb93cb8  
    77  append_view_path "#{Rails.root}/templates" 
    88 
    9   before_filter :assigns_show, :assigns_now, :create_user_google_analytics_account 
     9  before_filter :assigns_show, :create_user_google_analytics_account, :except => :feed 
     10  before_filter :assigns_now 
    1011 
    1112  rescue_from ActiveRecord::RecordNotFound, :with => :show_home_page_when_not_found 
     
    3536 
    3637  def feed 
     38    current_show [{:episodes => [ :show, { :contents => { :episode => [ :contents, :tags, { :show => :host } ] } }, :tags ]}, :host]  
    3739    render :content_type => "application/rss+xml", :layout => false 
    3840  end 
     
    4850 
    4951  def direct 
    50     render_template @show, :stream, @show 
     52    render_template current_show, :stream, current_show 
    5153  end 
    5254 
     
    5860 
    5961  def vote 
    60     @episode = @show.episodes.find(params[:episode_id]) 
     62    @episode = current_show.episodes.find(params[:episode_id]) 
    6163 
    6264    if request.post? and user_session.can_rate_episode?(@episode) 
     
    7375  def tags 
    7476    @search = params[:search] 
    75     @episodes = Episode.sort(@show.episodes.find_tagged_with(@search)) 
     77    @episodes = Episode.sort(current_show.episodes.find_tagged_with(@search)) 
    7678 
    7779    respond_to do |format| 
    7880      format.html { 
    79         render_template @show, :search, @search 
     81        render_template current_show, :search, @search 
    8082      } 
    8183      format.m3u { 
     
    9496 
    9597  def render_show 
    96     create_visit @show 
    97     render_template @show, :show, @show 
     98    create_visit current_show 
     99    render_template current_show, :show, current_show 
    98100  end 
    99101 
     
    105107  end 
    106108 
    107   def assigns_show 
    108     host = Host.find_by_name(request.host) 
    109     unless host.blank? 
    110       @show = host.show 
    111     end 
    112  
    113     show_slug = "" 
    114     if request.host =~ /^(.*).bonnes-ondes.(fr|local)$/ 
    115       show_slug = $1 
    116     end 
    117  
    118     @show = find_show(show_slug) 
    119   end 
    120  
    121109  def assigns_now 
    122110    @now = Time.now 
     
    124112 
    125113  def create_user_google_analytics_account 
    126     user_tracker_id = (@show and @show.host and @show.host.google_analytics_tracker_id) 
     114    user_tracker_id = (current_show and current_show.host and current_show.host.google_analytics_tracker_id) 
    127115 
    128116    unless user_tracker_id.blank? 
     
    131119  end 
    132120 
    133   def find_show(slug = nil) 
    134     @show ||= Show.find_by_slug(slug) 
     121  def current_show(include = []) 
     122    unless @show 
     123      if host = Host.find_by_name(request.host) 
     124        @show = Show.find(host.show, :include => include) 
     125      else 
     126        if request.host =~ /^(.*).bonnes-ondes.(fr|local)$/ 
     127          @show = Show.find_by_slug($1, :include => include) 
     128        end 
     129      end 
     130 
     131      raise ActiveRecord::RecordNotFound unless @show 
     132    end 
     133 
     134    @show 
    135135  end 
     136  alias_method :load_show, :current_show 
     137  alias_method :assigns_show, :current_show 
    136138 
    137139  def find_episode 
    138     find_show.episodes.find_by_slug(params[:episode_slug]) or raise ActiveRecord::RecordNotFound 
     140    current_show.episodes.find_by_slug(params[:episode_slug]) or raise ActiveRecord::RecordNotFound 
    139141  end 
    140142 
  • app/helpers/public_helper.rb

    r027b9a4 rcb93cb8  
    33 
    44  def contents_for_feed(show) 
    5     Episode.sort(show.episodes.broadcasted).collect { |e| e.contents.principal }.flatten 
     5    Episode.sort(show.episodes).select(&:broadcasted?).collect do |episode| 
     6      episode.contents.select(&:principal?)  
     7    end.flatten 
    68  end 
    79 
  • app/models/content.rb

    r2bbe6cc rcb93cb8  
    55 
    66  named_scope :principal, :conditions => { :principal => true } 
     7  named_scope :for_feed, lambda { |show| {  
     8      :conditions => [ "shows.id = ? and episodes.broadcasted_at < ?", show.id, Time.now ],  
     9      :include => { :episode => [:tags, :show, :contents] }, 
     10      :order => "episodes.broadcasted_at desc" 
     11    } 
     12  } 
    713 
    814  liquid_methods :name, :episode, :duration, :has_duration?, :id, :available? 
  • app/models/episode.rb

    r309fce1 rcb93cb8  
    2828  belongs_to :image 
    2929 
     30  def broadcasted? 
     31    self.broadcasted_at < Time.now 
     32  end 
     33 
    3034  def self.sort(episodes) 
    3135    if episodes.all?(&:broadcasted_at) 
  • app/views/public/_feed_content.rxml

    r112123f rcb93cb8  
    1313  end 
    1414 
    15   xml.description textilize_in_text(content.episode.description) 
    16   xml.itunes :summary, textilize_in_text(content.episode.description) 
     15  text = textilize_in_text(content.episode.description) 
     16  xml.description text 
     17  xml.itunes :summary, text 
    1718 
    1819  xml.itunes :duration, "#{content.duration}:00" if content.has_duration?