Class: Panda::CMS::Post

Inherits:
ApplicationRecord show all
Includes:
Editor::Content
Defined in:
app/models/panda/cms/post.rb

Instance Method Summary collapse

Instance Method Details

#admin_paramObject



70
71
72
# File 'app/models/panda/cms/post.rb', line 70

def admin_param
  id
end

#excerpt(length = 100, squish: true) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/panda/cms/post.rb', line 74

def excerpt(length = 100, squish: true)
  return "" if content.blank?

  text = if content.is_a?(Hash) && content["blocks"]
    content["blocks"]
      .select { |block| block["type"] == "paragraph" }
      .map { |block| block["data"]["text"] }
      .join(" ")
  else
    content.to_s
  end

  text = text.squish if squish
  text.truncate(length).html_safe
end

#monthObject



56
57
58
59
60
# File 'app/models/panda/cms/post.rb', line 56

def month
  return nil unless slug.match?(%r{\A/\d{4}/\d{2}/})

  slug.split("/")[2]
end

#route_paramsObject



62
63
64
65
66
67
68
# File 'app/models/panda/cms/post.rb', line 62

def route_params
  if year && month
    {year: year, month: month, slug: to_param}
  else
    {slug: to_param}
  end
end

#to_paramObject



40
41
42
43
44
45
46
47
48
# File 'app/models/panda/cms/post.rb', line 40

def to_param
  # For date-based URLs, return just the slug portion
  parts = CGI.unescape(slug).delete_prefix("/").split("/")
  if parts.length == 3 # year/month/slug format
    parts.last
  else
    parts.first
  end
end

#yearObject



50
51
52
53
54
# File 'app/models/panda/cms/post.rb', line 50

def year
  return nil unless slug.match?(%r{\A/\d{4}/})

  slug.split("/")[1]
end