Class: ArticlesController

Inherits:
ContentController show all
Includes:
ActionView::Helpers::SanitizeHelper
Defined in:
app/controllers/articles_controller.rb

Instance Method Summary collapse

Methods included from BlogHelper

#blog_base_url, #this_blog

Instance Method Details

#archivesObject



104
105
106
107
108
109
110
# File 'app/controllers/articles_controller.rb', line 104

def archives
  limit = this_blog.limit_archives_display
  @articles = this_blog.published_articles.includes(:tags).page(params[:page]).per(limit)
  @page_title = this_blog.archives_title_template.to_title(@articles, this_blog, params)
  @keywords = this_blog.meta_keywords
  @description = this_blog.archives_desc_template.to_title(@articles, this_blog, params)
end

#check_passwordObject



70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/articles_controller.rb', line 70

def check_password
  return unless request.xhr?

  @article = Article.find(params[:article][:id])
  if @article.password == params[:article][:password]
    render partial: "articles/full_article_content", locals: { article: @article }
  else
    render partial: "articles/password_form", locals: { article: @article }
  end
end

#indexObject



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
# File 'app/controllers/articles_controller.rb', line 14

def index
  wanted_types = this_blog.statuses_in_timeline ? ["Article", "Note"] : ["Article"]

  limit = this_blog.per_page(params[:format])
  articles_base = if params[:year].blank?
                    this_blog.contents.published
                  else
                    this_blog.contents.published_at(params.values_at(:year, :month, :day))
                  end
  @articles = articles_base.includes(:user, :resources, :tags)
    .where(type: wanted_types).page(params[:page]).per(limit)

  respond_to do |format|
    format.html do
      set_index_title_and_description(this_blog, params)
      @keywords = this_blog.meta_keywords

      render_paginated_index
    end
    format.atom do
      render_articles_feed("atom")
    end
    format.rss do
      auto_discovery_feed(only_path: false)
      render_articles_feed("rss")
    end
  end
end

#live_searchObject



58
59
60
61
62
# File 'app/controllers/articles_controller.rb', line 58

def live_search
  @search = params[:q]
  @articles = Article.search(@search)
  render :live_search, layout: false
end

#markup_helpObject

TODO: Move to TextfilterController



129
130
131
132
133
134
135
136
# File 'app/controllers/articles_controller.rb', line 129

def markup_help
  filter = TextFilter.make_filter(params[:id])
  if filter
    render html: sanitize(filter.commenthelp)
  else
    render plain: "Unknown filter"
  end
end

#previewObject



64
65
66
67
68
# File 'app/controllers/articles_controller.rb', line 64

def preview
  @article = Article.last_draft(params[:id])
  @page_title = this_blog.article_title_template.to_title(@article, this_blog, params)
  render "read"
end

#preview_pageObject



116
117
118
119
# File 'app/controllers/articles_controller.rb', line 116

def preview_page
  @page = Page.find(params[:id])
  render "view_page"
end

#redirectObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/controllers/articles_controller.rb', line 81

def redirect
  from = extract_feed_format(params[:from])
  factory = Article::Factory.new(this_blog, current_user)

  @article = factory.match_permalink_format(from, this_blog.permalink_format)
  return show_article if @article

  # Redirect old version with /:year/:month/:day/:title to new format,
  # because it's changed
  ["%year%/%month%/%day%/%title%", "articles/%year%/%month%/%day%/%title%"].each do |part|
    @article = factory.match_permalink_format(from, part)
    if @article
      return redirect_to URI.parse(@article.permalink_url).path,
                         status: :moved_permanently
    end
  end

  r = Redirect.find_by!(from_path: from)
  # TODO: If linked to article, directly redirect to the article.
  # Let redirection made outside of the blog on purpose (deal with it, Brakeman!)
  redirect_to r.full_to_path, status: :moved_permanently if r
end

#searchObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/articles_controller.rb', line 43

def search
  @articles = this_blog.articles_matching(params[:q],
                                          page: params[:page],
                                          per_page: this_blog.per_page(params[:format]))
  return error! if @articles.empty?

  @page_title = this_blog.search_title_template.to_title(@articles, this_blog, params)
  @description = this_blog.search_desc_template.to_title(@articles, this_blog, params)
  respond_to do |format|
    format.html { render "search" }
    format.rss { render_articles_feed "rss" }
    format.atom { render_articles_feed "atom" }
  end
end

#tagObject



112
113
114
# File 'app/controllers/articles_controller.rb', line 112

def tag
  redirect_to tags_path, status: :moved_permanently
end

#view_pageObject



121
122
123
124
125
126
# File 'app/controllers/articles_controller.rb', line 121

def view_page
  @page = Page.published.find_by!(name: Array(params[:name]).join("/"))
  @page_title = @page.title
  @description = this_blog.meta_description
  @keywords = this_blog.meta_keywords
end