Class: Panda::CMS::PostsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/panda/cms/posts_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_breadcrumb, #authenticate_admin_user!, #authenticate_user!, #breadcrumbs, #current_user, #set_current_request_details, #user_signed_in?

Methods included from ApplicationHelper

#active_link?, #block_link_to, #component, #level_indent, #menu_indent, #nav_class, #nav_highlight_colour_classes, #panda_cms_collection, #panda_cms_collection_items, #panda_cms_editor, #panda_cms_feature_enabled?, #panda_cms_form_with, #selected_nav_highlight_colour_classes, #table_indent, #title_tag

Instance Method Details

#by_monthObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/panda/cms/posts_controller.rb', line 36

def by_month
  @month = Date.new(params[:year].to_i, params[:month].to_i, 1)
  @posts = Panda::CMS::Post
    .where(status: :active)
    .where("DATE_TRUNC('month', published_at) = ?", @month)
    .includes(:author)
    .ordered

  # HTTP caching: Use the most recent post in this month for conditional requests
  # Returns 304 Not Modified if no posts in this month have changed
  latest_month_timestamp = @posts.maximum(:updated_at) || @month
  fresh_when(etag: [@posts.to_a, @month], last_modified: latest_month_timestamp, public: true)

  render inline: "", layout: Panda::CMS.config.posts[:layouts][:by_month]
end

#indexObject

TODO: Change from layout rendering to standard template rendering inside a /panda/cms/posts/… structure in the application



8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/panda/cms/posts_controller.rb', line 8

def index
  @posts = Panda::CMS::Post.includes(:author).order(published_at: :desc)

  # HTTP caching: Use the most recent post's updated_at for conditional requests
  # Returns 304 Not Modified if no posts have changed since client's last request
   = @posts.maximum(:updated_at) || Time.current
  fresh_when(etag: [@posts.to_a, ], last_modified: , public: true)

  render inline: "", layout: Panda::CMS.config.posts[:layouts][:index]
end

#showObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/panda/cms/posts_controller.rb', line 19

def show
  @post = if params[:year] && params[:month]
    # For date-based URLs
    slug = "/#{params[:year]}/#{params[:month]}/#{params[:slug]}"
    Panda::CMS::Post.find_by!(slug: slug)
  else
    # For non-date URLs
    Panda::CMS::Post.find_by!(slug: "/#{params[:slug]}")
  end

  # HTTP caching: Send ETag and Last-Modified headers for individual posts
  # Returns 304 Not Modified if client's cached version is still valid
  fresh_when(@post, last_modified: @post.updated_at, public: true)

  render inline: "", layout: Panda::CMS.config.posts[:layouts][:show]
end