Class: CamaleonCms::PostDecorator

Inherits:
ApplicationDecorator show all
Includes:
CustomFieldsConcern
Defined in:
app/decorators/camaleon_cms/post_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CustomFieldsConcern

#render_fields, #the_attribute_field, #the_attribute_fields, #the_field, #the_field!, #the_fields, #the_fields_grouped

Methods inherited from ApplicationDecorator

#_calc_locale, #get_locale, #set_decoration_locale, #the_breadcrumb, #the_created_at, #the_id, #the_keywords, #the_slug, #the_updated_at

Methods included from MetasDecoratorMethods

#the_meta, #the_option

Class Method Details

.object_class_nameObject

fix for “Using Draper::Decorator without inferred source class”



239
240
241
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 239

def self.object_class_name
  'CamaleonCms::Post'
end

Instance Method Details

#can_visit?Boolean

check if the post can be visited by current visitor

Returns:

  • (Boolean)


195
196
197
198
199
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 195

def can_visit?
  r = {flag: true, post: object}
  h.hooks_run("post_can_visit", r)
  r[:flag] && object.status == 'published'
end

#generate_breadcrumb(show_categories = true, add_post_type = true) ⇒ Object

add_post_type: true/false to include post type link children: true/false (show/hide last item link) show_categories: true/false, true: add categories tree to the breadcrumb



204
205
206
207
208
209
210
211
212
213
214
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 204

def generate_breadcrumb(show_categories = true, add_post_type = true)
  p_type = object.post_type
  f_cat = object.categories.first
  if f_cat.present? && show_categories
    f_cat.decorate.generate_breadcrumb(add_post_type, true)
  else
    p_type.decorate.generate_breadcrumb(add_post_type, true)
  end
  object.parents.reverse.each{|p| p=p.decorate; h.breadcrumb_add(p.the_title, p.published? ? p.the_url : nil) } if object.post_parent.present? && p_type.manage_hierarchy?
  h.breadcrumb_add(self.the_title)
end

#has_thumb?Boolean

check if this page has registered the thumbnail

Returns:

  • (Boolean)


38
39
40
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 38

def has_thumb?
  object.get_meta("thumb").present?
end

#the_authorObject

return the user object who created this post



175
176
177
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 175

def the_author
  object.author.decorate
end

#the_categoriesObject

return all categories assigned for this post filtered by permissions + hidden posts + roles + etc…



180
181
182
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 180

def the_categories
  object.categories
end

#the_commentsObject

return all comments for this post filtered by permissions + hidden posts + roles + etc…



190
191
192
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 190

def the_comments
  object.comments.main.approveds.eager_load(:user)
end

#the_contentObject

return the content of this post



22
23
24
25
26
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 22

def the_content
  r = {content: object.content.to_s.translate(get_locale), post: object}
  h.hooks_run("post_the_content", r)
  h.do_shortcode(r[:content], self)
end

create the html link with edit link return html link attrs: Hash of link tag attributes, sample: {id: “myid”, class: “sss” }



133
134
135
136
137
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 133

def the_edit_link(title = nil, attrs = { })
  return '' unless h.cama_current_user.present?
  attrs = {target: "_blank", style: "font-size:11px !important;cursor:pointer;"}.merge(attrs)
  h.link_to("→ #{title || h.ct("edit", default: 'Edit')}".html_safe, the_edit_url, attrs)
end

#the_edit_urlObject

return edit url for this post



126
127
128
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 126

def the_edit_url
  h.edit_cama_admin_post_type_post_url(object.post_type.id, object)
end

#the_excerpt(qty_chars = 200) ⇒ Object

return the excerpt of this post



13
14
15
16
17
18
19
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 13

def the_excerpt(qty_chars = 200)
  excerpt = object.get_meta("summary").to_s.translate(get_locale)
  # r = {content: (excerpt.present? ? excerpt : object.content_filtered.to_s.translate(get_locale).strip_tags.gsub(/
|\n/, " ").truncate(qty_chars)), post: object}
  r = {content: (excerpt.present? ? excerpt : h.cama_strip_shortcodes(object.content_filtered.to_s.translate(get_locale).strip_tags.gsub(/
|\n/, " ").truncate(qty_chars))), post: object}
  h.hooks_run("post_the_excerpt", r)
  r[:content]
end

#the_hierarchy_titleObject

return the title with hierarchy prefixed sample: title paren 1 - title parent 2 -.. -… if add_parent_title: true will add parent title like: —— item 1.1.1 | item 1.1



224
225
226
227
228
229
230
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 224

def the_hierarchy_title
  return the_title unless object.post_parent.present?
  res = '—' * object.parents.count
  res << " " + the_title
  res << " | #{object.parent.decorate.the_title}" if object.show_title_with_parent
  res.html_safe
end

show link and thumbnail included as html link_args: html attributes for link img_args: html attributes for image



149
150
151
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 149

def the_link_thumb(link_args = {}, img_args = {})
  h.link_to(the_thumb(img_args), the_url, link_args)
end

#the_path(*args) ⇒ Object

return the path for this page sample: /my-page.html



44
45
46
47
48
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 44

def the_path(*args)
  args = args.extract_options!
  args[:as_path] = true
  the_url(args)
end

#the_post_typeObject

return the post type of this post



217
218
219
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 217

def the_post_type
  object.post_type.decorate
end

return all related posts of current post



233
234
235
236
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 233

def the_related_posts
  ptype = self.the_post_type
  ptype.the_posts.joins(:categories).where("#{CamaleonCms::TermRelationship.table_name}" => {term_taxonomy_id: the_categories.pluck(:id)})
end

#the_statusObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 153

def the_status
  case self.status
    when "published"
      color = "info"
      status = I18n.t('camaleon_cms.admin.post_type.published', default: 'Published')
    when "draft"
      color = "warning"
      status = I18n.t('camaleon_cms.admin.table.draft', default: 'Draft')
    when "trash"
      color = "danger"
      status = I18n.t('camaleon_cms.admin.table.trash', default: 'Trash')
    when "pending"
      color = "default"
      status = I18n.t('camaleon_cms.admin.table.pending', default: 'Pending')
    else
      color = "default"
      status = self.status
  end
  "<span class='label label-#{color} label-form'>#{status.titleize}</span>"
end

#the_tagsObject

return all post_tags assigned for this post



185
186
187
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 185

def the_tags
  object.
end

#the_thumb(img_args = {}) ⇒ Object

show thumbnail image as html



140
141
142
143
144
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 140

def the_thumb(img_args = {})
  r = {image: h.image_tag(the_thumb_url, img_args), post: object}
  h.hooks_run("post_the_thumb", r)
  r[:image]
end

#the_thumb_url(default = nil) ⇒ Object Also known as: the_image_url

return thumbnail image for this post default: default image if thumbails not exist if default is empty, post_type default thumb will be returned



31
32
33
34
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 31

def the_thumb_url(default = nil)
  th = object.get_meta("thumb")
  th.present? ? th : (default || object.post_type.get_option('default_thumb', nil) || h.asset_url("camaleon_cms/image-not-found.png"))
end

#the_title(locale = nil) ⇒ Object



6
7
8
9
10
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 6

def the_title(locale = nil)
  r = {title: object.title.to_s.translate(get_locale(locale)), post: object}
  h.hooks_run("post_the_title", r)
  r[:title]
end

#the_url(*args) ⇒ Object

return front url for this post sample: localhost.com/my-page.html args:

locale: language (default current language)
as_path: return the path instead of full url, sample: /my-page.html
Also, you can pass extra attributes as params for the url, sample: page.the_url(my_param: 'value', other: "asd")
  => http://localhost.com/my-page.html?my_param=value&other=asd

Return String URL



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
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 58

def the_url(*args)
  args = args.extract_options!
  args[:locale] = get_locale unless args.include?(:locale)
  args[:format] = "html"
  args[:slug] = the_slug(args[:locale])
  p = args.delete(:as_path).present? ? "path" : "url"
  l = _calc_locale(args[:locale])
  ptype = object.post_type.decorate
  p_url_format = ptype.contents_route_format
  p_url_format = "hierarchy_post" if ptype.manage_hierarchy?
  case p_url_format
    when "post_of_post_type"
      args[:post_type_id] = ptype.id
      args[:title] = ptype.the_title(args[:locale]).parameterize
      args[:title] = ptype.the_slug unless args[:title].present?
    when "post_of_category"
      if ptype.manage_categories?
        cat = object.categories.first.decorate rescue ptype.default_category.decorate
        args[:category_id] = cat.id
        args[:title] = cat.the_title(args[:locale]).parameterize
        args[:title] = cat.the_slug unless args[:title].present?
      else
        p_url_format = "post"
        l = ""
      end
    when "post_of_posttype"
      args[:post_type_title] = ptype.the_title(args[:locale]).parameterize
      args[:post_type_title] = ptype.the_slug unless args[:post_type_title].present?
      l = ""
    when "post_of_category_post_type"
      if ptype.manage_categories?
        cat = object.categories.first.decorate rescue ptype.default_category.decorate
        args[:post_type_title] = ptype.the_title(args[:locale]).parameterize
        args[:post_type_title] = ptype.the_slug unless args[:post_type_title].present?
        args[:category_id] = cat.id
        args[:title] = cat.the_title(args[:locale]).parameterize
        args[:title] = cat.the_slug unless args[:title].present?
      else
        p_url_format = "post"
        l = ""
      end
    when 'hierarchy_post'
      l = ""
      if object.post_parent.present?
        slugs = ([args[:slug]]+object.parents.map{|parent| parent.decorate.the_slug(args[:locale]) }).reverse
        args[:slug], args[:parent_title] = slugs.slice(1..-1).join("/"), slugs.first
      else
        p_url_format = "post"
      end
    else
      l = ""
  end
  h.cama_url_to_fixed("cama_#{p_url_format}#{l}_#{p}", args)
end

#the_urls(*args) ⇒ Object

return a hash of frontend urls for this post sample: ‘mydomain.com/es/articulo-3.html’, en: ‘mydomain.com/en/post-3.html



115
116
117
118
119
120
121
122
123
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 115

def the_urls(*args)
  args = args.extract_options!
  res = {}
  h.current_site.the_languages.each do |l|
    args[:locale] = l
    res[l] = the_url(args.clone)
  end
  res
end