Changeset 1676

Show
Ignore:
Timestamp:
03/15/08 09:53:35 (2 months ago)
Author:
pdcawley
Message:

Some article refactorings...

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/app/controllers/content_controller.rb

    r1612 r1676  
    2424  helper :theme 
    2525#  before_filter :auto_discovery_defaults 
    26  
    27  
    28  
    2926 
    3027  def self.caches_action_with_params(*actions) 
  • trunk/app/models/article.rb

    r1670 r1676  
    88 
    99  has_many :pings,      :dependent => :destroy, :order => "created_at ASC" 
     10 
    1011  has_many :comments,   :dependent => :destroy, :order => "created_at ASC" 
    1112  with_options(:conditions => { :published => true }, :order => 'created_at DESC') do |this| 
     
    1415    this.has_many :published_feedback,   :class_name => "Feedback", :order => "created_at ASC" 
    1516  end 
     17 
    1618  has_many :trackbacks, :dependent => :destroy, :order => "created_at ASC" 
     19 
    1720  has_many :feedback,                           :order => "created_at DESC" 
     21 
    1822  has_many :resources, :order => "created_at DESC", 
    1923           :class_name => "Resource", :foreign_key => 'article_id' 
     24  after_destroy :fix_resources 
    2025 
    2126  has_many :categorizations 
     
    2631 
    2732  has_and_belongs_to_many :tags, :foreign_key => 'article_id' 
     33 
    2834  belongs_to :user 
     35 
    2936  has_many :triggers, :as => :pending_item 
    30  
    3137  after_save :post_trigger 
    32   after_destroy :fix_resources 
    3338 
    3439  has_state(:state, 
     
    4348 
    4449 
    45   include States 
     50  include Article::States 
    4651 
    4752  class << self 
     
    5560  end 
    5661 
     62  accents = { ['á','à','â','À','ã','Ã','Ä','Â','À'] => 'a', 
     63    ['é','Ú','ê','ë','Ë','É','È','Ê'] => 'e', 
     64    ['í','ì','î','ï','I','Î','Ì'] => 'i', 
     65    ['ó','ò','ÃŽ','ö','õ','Õ','Ö','Ô','Ò'] => 'o', 
     66    ['œ'] => 'oe', 
     67    ['ß'] => 'ss', 
     68    ['ú','ù','û','ÃŒ','U','Û','Ù'] => 'u', 
     69    ['ç','Ç'] => 'c' 
     70  } 
     71 
     72  FROM, TO = accents.inject(['','']) { |o,(k,v)| 
     73    o[0] << k * ''; 
     74    o[1] << v * k.size 
     75    o 
     76  } 
     77 
    5778  def stripped_title 
    58     str = String.new(self.title) 
    59  
    60     accents = { ['á','à','â','À','ã','Ã','Ä','Â','À'] => 'a', 
    61       ['é','Ú','ê','ë','Ë','É','È','Ê'] => 'e', 
    62       ['í','ì','î','ï','I','Î','Ì'] => 'i', 
    63       ['ó','ò','ÃŽ','ö','õ','Õ','Ö','Ô','Ò'] => 'o', 
    64       ['œ'] => 'oe', 
    65       ['ß'] => 'ss', 
    66       ['ú','ù','û','ÃŒ','U','Û','Ù'] => 'u', 
    67       ['ç','Ç'] => 'c' 
    68       } 
    69     accents.each do |ac,rep| 
    70       ac.each do |s| 
    71         str.gsub!(s, rep) 
    72       end 
    73     end 
    74  
    75     str.gsub(/<[^>]*>/,'').to_url 
     79    self.title.tr(FROM, TO).gsub(/<[^>]*>/, '').to_url 
    7680  end 
    7781 
    7882  def permalink_url_options(nesting = false) 
    79     {:year => published_at.year, 
    80      :month => sprintf("%.2d", published_at.month), 
    81      :day => sprintf("%.2d", published_at.day), 
    82      :controller => 'articles', 
    83      :action => 'show', 
     83    {:year                         => published_at.year, 
     84     :month                        => sprintf("%.2d", published_at.month), 
     85     :day                          => sprintf("%.2d", published_at.day), 
     86     :controller                   => 'articles', 
     87     :action                       => 'show', 
    8488     (nesting ? :article_id : :id) => permalink} 
    8589  end 
     
    8791  def permalink_url(anchor=nil, only_path=true) 
    8892    @cached_permalink_url ||= {} 
     93 
    8994    @cached_permalink_url["#{anchor}#{only_path}"] ||= \ 
    9095      blog.with_options(permalink_url_options) do |b| 
     
    95100  def param_array 
    96101    @param_array ||= 
    97       returning([published_at.year, sprintf('%.2d', published_at.month), 
    98                  sprintf('%.2d', published_at.day), permalink]) do |params| 
    99       this = self 
    100       k = class << params; self; end 
    101       k.send(:define_method, :to_s) { params[-1] } 
    102     end 
     102      returning([published_at.year, 
     103                 sprintf('%.2d', published_at.month), 
     104                 sprintf('%.2d', published_at.day), 
     105                 permalink]) \ 
     106      do |params| 
     107        this = self 
     108        k = class << params; self; end 
     109        k.send(:define_method, :to_s) { params[-1] } 
     110      end 
    103111  end 
    104112 
     
    164172 
    165173  def next 
    166     blog.articles.find(:first, :conditions => ['published_at > ?', published_at], 
    167                        :order => 'published_at asc') 
     174    self.class.find(:first, :conditions => ['published_at > ?', published_at], 
     175                    :order => 'published_at asc') 
    168176  end 
    169177 
    170178  def previous 
    171     blog.articles.find(:first, :conditions => ['published_at < ?', published_at], 
    172                        :order => 'published_at desc') 
     179    self.class.find(:first, :conditions => ['published_at < ?', published_at], 
     180                    :order => 'published_at desc') 
    173181  end 
    174182 
     
    176184  def self.count_by_date(year, month = nil, day = nil, limit = nil) 
    177185    if !year.blank? 
    178       from, to = self.time_delta(year, month, day) 
    179       Article.count(["published_at BETWEEN ? AND ? AND published = ?", 
    180                      from, to, true]) 
     186      count(:conditions => { :published_at => time_delta(year, month, day), 
     187              :published => true }) 
    181188    else 
    182       Article.count(:conditions => { :published => true }) 
     189      count(:conditions => { :published => true }) 
    183190    end 
    184191  end 
     
    187194  def self.find_all_by_date(year, month = nil, day = nil) 
    188195    if !year.blank? 
    189       from, to = self.time_delta(year, month, day) 
    190       Article.find_published(:all, :conditions => ["published_at BETWEEN ? AND ?", 
    191                                                    from, to]
     196      find_published(:all, 
     197                     :conditions => { :published_at => 
     198                       time_delta(year,month,day) }
    192199    else 
    193       Article.find_published(:all) 
     200      find_published(:all) 
    194201    end 
    195202  end 
     
    217224      end 
    218225    end 
    219     from, to = self.time_delta(year, month, day) 
    220     returning(find_published(:first, 
    221                              :conditions => ['permalink = ? AND ' + 
    222                                              'published_at BETWEEN ? AND ?', 
    223                                              title, from, to ])) do |res| 
    224       if res.nil? 
    225         raise ActiveRecord::RecordNotFound 
    226       end 
    227     end 
     226    date_range = self.time_delta(year, month, day) 
     227    find_published(:first, 
     228                   :conditions => { :permalink => title, 
     229                                    :published_at => date_range }) \ 
     230      or raise ActiveRecord::RecordNotFound 
    228231  end 
    229232 
     
    473476    to = from + 1.day unless day.blank? 
    474477    to = to - 1 # pull off 1 second so we don't overlap onto the next day 
    475     return [from, to] 
     478    return from..to 
    476479  end 
    477480