Class: CamaleonCms::PostDecorator
- Inherits:
-
ApplicationDecorator
- Object
- ApplicationDecorator
- CamaleonCms::PostDecorator
- Includes:
- CustomFieldsConcern
- Defined in:
- app/decorators/camaleon_cms/post_decorator.rb
Class Method Summary collapse
-
.object_class_name ⇒ Object
fix for "Using Draper::Decorator without inferred source class".
Instance Method Summary collapse
-
#can_visit? ⇒ Boolean
check if the post can be visited by the current user.
-
#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.
-
#has_thumb? ⇒ Boolean
check if this page has registered the thumbnail.
-
#the_author ⇒ Object
return the user object who created this post.
-
#the_categories ⇒ Object
return all categories assigned for this post filtered by permissions + hidden posts + roles + etc...
-
#the_comments ⇒ Object
return all comments for this post filtered by permissions + hidden posts + roles + etc...
-
#the_content ⇒ Object
return the content of this post.
-
#the_edit_link(title = nil, attrs = {}) ⇒ Object
create the html link with edit link return html link attrs: Hash of link tag attributes, sample: {id: "myid", class: "sss" }.
-
#the_edit_url ⇒ Object
return edit url for this post.
-
#the_excerpt(qty_chars = 200) ⇒ Object
return the excerpt of this post.
-
#the_hierarchy_title ⇒ Object
return the title with hierarchy prefixed sample: title paren 1 - title parent 2 -...
-
#the_link_thumb(link_args = {}, img_args = {}) ⇒ Object
show link and thumbnail included as html link_args: html attributes for the link img_args: html attributes for image.
-
#the_next_post(parent = nil) ⇒ Object
It looks for the next post item related to the parent element based on the post_order attribute @samples: my_post.the_next_post(), my_post.the_next_post(@category), my_post.the_next_post(current_site).
-
#the_path(*args) ⇒ Object
return the path for this page sample: /my-page.html.
-
#the_post_type ⇒ Object
return the post type of this post.
-
#the_prev_post(parent = nil) ⇒ Object
looks for the next post item related to the parent element based on post_order attribute @samples: my_post.the_prev_post(), my_post.the_prev_post(@category), my_post.the_prev_post(current_site).
-
#the_related_posts ⇒ Object
return all related posts of the current post.
- #the_status ⇒ Object
-
#the_tags ⇒ Object
return all post_tags assigned for this post.
-
#the_thumb(img_args = {}) ⇒ Object
show thumbnail image as html.
-
#the_thumb_url(default = nil) ⇒ Object
(also: #the_image_url)
Return the thumbnail image for this post default: default image if thumbails not exist if default is empty, post_type default thumb will be returned.
- #the_title(locale = nil) ⇒ Object
-
#the_url(*args) ⇒ Object
return front url for this post sample: https://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") => https://localhost.com/my-page.html?my_param=value&other=asd Return String URL.
-
#the_urls(*args) ⇒ Object
return a hash of frontend urls for this post sample: 'https://mydomain.com/es/articulo-3.html', en: 'https://mydomain.com/en/post-3.html'.
Methods included from CustomFieldsConcern
#render_fields, #the_field, #the_field_grouped, #the_fields, #the_fields_grouped, #the_json_field, #the_json_fields
Class Method Details
.object_class_name ⇒ Object
fix for "Using Draper::Decorator without inferred source class"
287 288 289 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 287 def self.object_class_name 'CamaleonCms::Post' end |
Instance Method Details
#can_visit? ⇒ Boolean
check if the post can be visited by the current user
218 219 220 221 222 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 218 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
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 227 def (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.(add_post_type, true) else p_type.decorate.(add_post_type, true) end if object.post_parent.present? && p_type.manage_hierarchy? object.parents.reverse_each do |p| p = p.decorate h.(p.the_title, p.published? ? p.the_url : nil) end end h.(the_title) end |
#has_thumb? ⇒ Boolean
check if this page has registered the thumbnail
50 51 52 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 50 def has_thumb? object.('thumb').present? end |
#the_author ⇒ Object
return the user object who created this post
198 199 200 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 198 def object..decorate end |
#the_categories ⇒ Object
return all categories assigned for this post filtered by permissions + hidden posts + roles + etc...
203 204 205 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 203 def the_categories object.categories end |
#the_comments ⇒ Object
return all comments for this post filtered by permissions + hidden posts + roles + etc...
213 214 215 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 213 def the_comments object.comments.main.approveds.eager_load(:user) end |
#the_content ⇒ Object
return the content of this post
33 34 35 36 37 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 33 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 |
#the_edit_link(title = nil, attrs = {}) ⇒ Object
create the html link with edit link return html link attrs: Hash of link tag attributes, sample: {id: "myid", class: "sss" }
147 148 149 150 151 152 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 147 def the_edit_link(title = nil, attrs = {}) return '' if h.cama_current_user.blank? attrs = { target: '_blank', style: 'font-size:11px !important;cursor:pointer;' }.merge!(attrs) h.link_to(h.safe_join(['→ ', title || h.ct('edit', default: 'Edit')]), the_edit_url, attrs) end |
#the_edit_url ⇒ Object
return edit url for this post
139 140 141 142 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 139 def the_edit_url args = h.cama_current_site_host_port({}) h.edit_cama_admin_post_type_post_url(object.post_type.id, object, args) end |
#the_excerpt(qty_chars = 200) ⇒ Object
return the excerpt of this post
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 13 def the_excerpt(qty_chars = 200) excerpt = object.('summary').to_s.translate(get_locale) # r = { # content: ( # (excerpt if excerpt.present?) || # object.content_filtered.to_s.translate(get_locale).strip_tags.gsub(/ |\n/, " ").truncate(qty_chars) # ), # post: object # } r = { content: excerpt.presence || h.cama_strip_shortcodes(object.content_filtered.to_s.translate(get_locale) ..gsub(/ |\n/, ' ').truncate(qty_chars)), post: object } h.hooks_run('post_the_excerpt', r) r[:content] end |
#the_hierarchy_title ⇒ Object
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
270 271 272 273 274 275 276 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 270 def the_hierarchy_title return the_title if object.post_parent.blank? parts = [h.raw('—' * object.parents.count), h.sanitize(" #{the_title}")] parts << h.sanitize(" | #{object.parent.decorate.the_title}") if object.show_title_with_parent h.safe_join(parts) end |
#the_link_thumb(link_args = {}, img_args = {}) ⇒ Object
show link and thumbnail included as html link_args: html attributes for the link img_args: html attributes for image
164 165 166 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 164 def the_link_thumb(link_args = {}, img_args = {}) h.link_to(the_thumb(img_args), the_url, link_args) end |
#the_next_post(parent = nil) ⇒ Object
It looks for the next post item related to the parent element based on the post_order attribute @samples: my_post.the_next_post(), my_post.the_next_post(@category), my_post.the_next_post(current_site)
252 253 254 255 256 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 252 def the_next_post(parent = nil) (parent.presence || the_post_type).the_posts.where( post_order_predicate_gt(object.post_order, object.created_at) ).where.not(id: object.id).take.try(:decorate) end |
#the_path(*args) ⇒ Object
return the path for this page sample: /my-page.html
56 57 58 59 60 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 56 def the_path(*args) args = args. args[:as_path] = true the_url(args) end |
#the_post_type ⇒ Object
return the post type of this post
245 246 247 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 245 def the_post_type @the_post_type ||= object.post_type.decorate end |
#the_prev_post(parent = nil) ⇒ Object
looks for the next post item related to the parent element based on post_order attribute @samples: my_post.the_prev_post(), my_post.the_prev_post(@category), my_post.the_prev_post(current_site)
261 262 263 264 265 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 261 def the_prev_post(parent = nil) (parent.presence || the_post_type).the_posts .where(post_order_predicate_lt(object.post_order, object.created_at)) .where.not(id: object.id).reorder(post_order: :asc, created_at: :asc).last.try(:decorate) end |
#the_related_posts ⇒ Object
return all related posts of the current post
279 280 281 282 283 284 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 279 def ptype = the_post_type ptype.the_posts.joins(:categories).where( CamaleonCms::TermRelationship.table_name.to_s => { term_taxonomy_id: the_categories.pluck(:id) } ).distinct end |
#the_status ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 168 def the_status case status when 'published' color = 'info' status = I18n.t('camaleon_cms.admin.post_type.published', default: 'Published') when 'draft', 'draft_child' 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 # The `else` arm above returns the raw column value, which is not validated and is writable at # contributor privilege, and three admin views render this label through `raw`. Escaping the # interpolated value here -- at the source, as `the_title` does -- is what makes those sinks # safe. `titleize` is not a mitigation: HTML tag names and hostnames are case-insensitive. # `content_tag` is deliberately not used: it emits double-quoted attributes and would change # the byte output for every legitimate status, which downstream themes may match on. # rubocop:disable Rails/OutputSafety -- only the escaped status is interpolated into fixed markup "<span class='label label-#{color} label-form'>#{h.h(status.titleize)}</span>".html_safe # rubocop:enable Rails/OutputSafety end |
#the_tags ⇒ Object
return all post_tags assigned for this post
208 209 210 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 208 def object. end |
#the_thumb(img_args = {}) ⇒ Object
show thumbnail image as html
155 156 157 158 159 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 155 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 the thumbnail image for this post default: default image if thumbails not exist if default is empty, post_type default thumb will be returned
42 43 44 45 46 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 42 def the_thumb_url(default = nil) th = object.('thumb') th.presence || 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: h.h(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: https://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")
=> https://localhost.com/my-page.html?my_param=value&other=asd
Return String URL
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 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 70 def the_url(*args) args = args. args[:locale] = get_locale unless args.include?(:locale) args[:format] = 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[:label] = I18n.t('routes.group', default: 'group') args[:post_type_id] = ptype.id args[:title] = ptype.the_title(args[:locale]).parameterize.presence || ptype.the_slug when 'post_of_category' if ptype.manage_categories? cat = begin object.categories.first.decorate rescue StandardError ptype.default_category.decorate end args[:label_cat] = I18n.t('routes.category', default: 'category') args[:category_id] = cat.id args[:title] = cat.the_title(args[:locale]).parameterize args[:title] = cat.the_slug if args[:title].blank? else p_url_format = 'post' end when 'post_of_posttype' args[:post_type_title] = ptype.the_title(args[:locale]).parameterize.presence || ptype.the_slug when 'post_of_category_post_type' if ptype.manage_categories? cat = begin object.categories.first.decorate rescue StandardError ptype.default_category.decorate end args[:label_cat] = I18n.t('routes.category', default: 'category') args[:post_type_title] = ptype.the_title(args[:locale]).parameterize.presence || ptype.the_slug args[:category_id] = cat.id args[:title] = cat.the_title(args[:locale]).parameterize args[:title] = cat.the_slug if args[:title].blank? else p_url_format = 'post' end when 'hierarchy_post' if object.post_parent.present? slugs = ([args[:slug]] + object.parents.map { |parent| parent.decorate.the_slug(args[:locale]) }).reverse args[:slug] = slugs.slice(1..-1).join('/') args[:parent_title] = slugs.first else p_url_format = 'post' end end h.cama_url_to_fixed("cama_#{p_url_format}_#{p}", args) end |
#the_urls(*args) ⇒ Object
return a hash of frontend urls for this post sample: 'https://mydomain.com/es/articulo-3.html', en: 'https://mydomain.com/en/post-3.html'
130 131 132 133 134 135 136 |
# File 'app/decorators/camaleon_cms/post_decorator.rb', line 130 def the_urls(*args) args = args. h.current_site.the_languages.each_with_object({}) do |l, hsh| args[:locale] = l hsh[l] = the_url(args.clone) end end |