public
Fork of halorgium/mephisto
Description: A mirror of the mephisto code-base
Homepage: http://mephistoblog.com/
Clone URL: git://github.com/technoweenie/mephisto.git
Click here to lend your support to: mephisto and make a donation at www.pledgie.com !
technoweenie (author)
Mon Sep 04 00:28:36 -0700 2006
commit  697616519d9e38ac1ae7a367340217eb39d73a0d
tree    aa8d796e0796043ca98fa817d2ec04fe6b05546c
parent  26e849d40a5b40cb207afb0809c97062831f6840
mephisto / app / controllers / admin / articles_controller.rb
100644 170 lines (146 sloc) 6.413 kb
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
class Admin::ArticlesController < Admin::BaseController
  with_options :only => [:create, :update, :destroy, :upload] do |c|
    c.before_filter :set_default_section_ids
    c.cache_sweeper :article_sweeper, :section_sweeper, :assigned_section_sweeper
    cache_sweeper :comment_sweeper, :only => [:approve, :unapprove, :destroy_comment]
  end
 
  observer :article_observer, :comment_observer
  before_filter :convert_times_to_utc, :only => [:create, :update, :upload]
  before_filter :check_for_new_draft, :only => [:create, :update, :upload]
  
  before_filter :find_site_article, :only => [:edit, :update, :comments, :approve, :unapprove, :destroy]
  before_filter :load_sections, :only => [:new, :edit]
 
  def index
    @article_pages = Paginator.new self, site.articles.count(:all, article_options), 30, params[:page]
    @articles = site.articles.find(:all, article_options(:order => 'contents.published_at DESC', :select => 'contents.*',
                       :limit => @article_pages.items_per_page,
                       :offset => @article_pages.current.offset))
    @comments = @site.unapproved_comments.count :all, :group => :article, :order => '1 desc'
    @sections = site.sections.find(:all)
  end
 
  def show
    @article = site.articles.find(params[:id])
    @comments = @article.comments.collect &:to_liquid
    Mephisto::Liquid::CommentForm.article = @article
    @article = @article.to_liquid(:mode => :single)
    
    render :text => site.render_liquid_for(site.sections.home, :single, 'articles' => [@article], 'article' => @article, 'comments' => @comments, 'site' => site.to_liquid)
  end
 
  def new
    @article = site.articles.build(:comment_age => site.comment_age, :filter => current_user.filter, :published_at => utc_to_local(Time.now.utc))
  end
 
  def edit
    @version = params[:version] ? @article.find_version(params[:version]) : @article or raise(ActiveRecord::RecordNotFound)
    @published = @version.published?
    @version.published_at = utc_to_local(@version.published_at || Time.now.utc)
  end
 
  def create
    @article = current_user.articles.create params[:article].merge(:updater => current_user, :site => site)
    
    @article.save!
    redirect_to :action => 'index'
  rescue ActiveRecord::RecordInvalid
    load_sections
    render :action => 'new'
  end
  
  def update
    @article.attributes = params[:article].merge(:updater => current_user)
    save_with_revision? ? @article.save! : @article.save_without_revision!
    redirect_to :action => 'index'
  rescue ActiveRecord::RecordInvalid
    load_sections
    render :action => 'edit'
  end
 
  def destroy
    @article.destroy
    render :update do |page|
      page.redirect_to :action => 'index'
    end
  end
 
  def comments
    @comments =
      case params[:filter]
        when 'approved' then :comments
        when 'unapproved' then :unapproved_comments
        else :all_comments
      end
    @comments = @article.send @comments
  end
 
  # xhr baby
  # needs some restful lovin'
  def approve
    @comment = @article.unapproved_comments.approve(params[:comment])
  end
 
  def unapprove
    @comment = @article.comments.unapprove(params[:comment])
    render :action => 'approve'
  end
  
  def destroy_comment
    @comments = site.all_comments.find :all, :conditions => ['id in (?)', [params[:comment]].flatten] rescue []
    Comment.transaction { @comments.each(&:destroy) } if @comments.any?
  end
 
  def upload
    @asset = site.assets.build(params[:asset])
    @asset.save!
  rescue ActiveRecord::RecordInvalid
  ensure
    load_sections # do this after the asset has been created
    if params[:id]
      @article = site.articles.find(params[:id])
      @article.attributes = params[:article].merge(:updater => current_user)
      render :action => 'edit'
    else
      @article = current_user.articles.build params[:article].merge(:updater => current_user, :site => site)
      render :action => 'new'
    end
  end
 
  protected
    def load_sections
      @assets = site.assets.find(:all, :order => 'created_at desc', :limit => 6)
      @sections = site.sections.find :all, :order => 'name'
      home = @sections.find &:home?
      @sections.delete home
      @sections.unshift home if home
    end
 
    def find_site_article
      @article = site.articles.find(params[:id])
    end
 
    def set_default_section_ids
      params[:article] ||= {}
      params[:article][:section_ids] ||= []
    end
    
    def check_for_new_draft
      params[:article] ||= {}
      params[:article][:published_at] = nil if params[:draft] == '1'
      true
    end
    
    def convert_times_to_utc
      with_site_timezone do
        date = Time.parse_from_attributes(params[:article], :published_at, :local)
        next unless date
        params[:article].delete_if { |k, v| k.to_s =~ /^#{:published_at}/ }
        params[:article][:published_at] = local_to_utc(date)
      end
    end
    
    def save_with_revision?
      params[:commit].to_s !~ /save without revision/i
    end
    
    def article_options(options = {})
      if @article_options.nil?
        @article_options = {}
        section_id = params[:section].to_i
        case params[:filter]
          when 'title'
            @article_options[:conditions] = Article.send(:sanitize_sql, ["LOWER(contents.title) LIKE ?", "%#{params[:q].downcase}%"])
          when 'body'
            @article_options[:conditions] = Article.send(:sanitize_sql, ["LOWER(contents.excerpt) LIKE :q OR LOWER(contents.body) LIKE :q", {:q => "%#{params[:q].downcase}%"}])
          when 'tags'
            @article_options[:joins] = "INNER JOIN taggings ON taggings.taggable_id = contents.id and taggings.taggable_type = 'Content' INNER JOIN tags on taggings.tag_id = tags.id"
            @article_options[:conditions] = Article.send(:sanitize_sql, ["tags.name IN (?)", Tag.parse(params[:q])])
        end unless params[:q].blank?
        if section_id > 0
          @article_options[:joins] = "#{@article_options[:joins]} INNER JOIN assigned_sections ON contents.id = assigned_sections.article_id"
          cond = Article.send(:sanitize_sql, ['assigned_sections.section_id = ?', params[:section]])
          @article_options[:conditions] = @article_options[:conditions] ? "(#{@article_options[:conditions]}) AND (#{cond})" : cond
        end
      end
      @article_options.merge options
    end
end