Class: CamaleonCms::SiteDecorator
- Inherits:
-
TermTaxonomyDecorator
- Object
- TermTaxonomyDecorator
- CamaleonCms::SiteDecorator
- Defined in:
- app/decorators/camaleon_cms/site_decorator.rb
Instance Method Summary collapse
-
#draw_languages(list_class = 'language_list list-inline pull-right', current_page = false, current_class = 'current_l', &block) ⇒ Object
draw languages configured for this site list_class: (String) Custom css classes for ul list current_page: (boolean) true: link with translation to current url, false: link with translation to root url block permit to render custom link label, default: flag icon.
-
#generate_breadcrumb(_add_post_type = true) ⇒ Object
draw bread crumb for current site.
-
#manage_sites? ⇒ Boolean
check if current user can manage sites.
-
#plugin_installed?(plugin_key) ⇒ Boolean
check if plugin_key is already installed for this site.
-
#the_admin_url ⇒ Object
ADMIN ======================= admin root url for this site.
-
#the_categories(slug_or_id = nil) ⇒ Object
return a collection of categories Arguments: slug_or_id: string or integer return: slug_or_id: nil => return all main_categories for this site slug_or_id: integer => return all main categories of the post_type with id = slug_or_id slug_or_id: string => return all main categories of the post_type with slug = slug_or_id.
-
#the_category(slug_or_id) ⇒ Object
return the category object with id or slug = slug_or_id from this site.
-
#the_contents(slug_or_id = 'post') ⇒ Object
return all contents from this site registered for post_type = slug (filter visibility, hidden, expired, ...) slug_or_id: slug or id of the post_type or array of slugs of post_types, default 'post'.
- #the_description ⇒ Object
-
#the_full_categories ⇒ Object
return all categories for ths site (include all children categories).
- #the_icon ⇒ Object
-
#the_languages ⇒ Object
return Array of frontend languages configured for this site.
-
#the_logo(default = nil) ⇒ Object
return logo url for this site default: this url will be returned if logo is not present.
-
#the_path(*args) ⇒ Object
return the path for this site.
-
#the_post(slug_or_id) ⇒ Object
return the post with id or slug equal to slug_or_id slug_or_id: (String) for post slug slug_or_id: (Integer) for post id slug_or_id: (Array) array of post ids, return multiple posts return post model or nil.
-
#the_post_type(slug_or_id) ⇒ Object
return a post_type object with id or slug = slug_or_id Arguments: slug_or_id: string or integer return: slug_or_id: integer => return the post type with id = slug_or_id slug_or_id: string => return the post type with slug = slug_or_id slug_or_id: array => return all post types with slugs in the array of this site.
-
#the_post_types ⇒ Object
return all post types for this site.
-
#the_posts(slug_or_id = nil) ⇒ Object
return all contents for this site filteredby (visibility, hidden, expired, ...) slug_or_id: (slug of the post_type) possible values: empty: return all posts of the current site string: return all posts of post_type with slug = slug_or_id integer: return all posts of post_type with id = slug_or_id array: return all posts of post_types with slug in slug_or_id.
-
#the_status ⇒ Object
return the text status of current site.
-
#the_status_options ⇒ Object
return an array for select options of all status for this site.
-
#the_tag(slug_or_id) ⇒ Object
return the post_tag object with id or slug = slug_or_id from this site sample: current_site.the_tag('test').the_url sample2: current_site.the_tag('test').the_posts.
-
#the_tags ⇒ Object
return all post tags for ths site.
-
#the_url(*args) ⇒ Object
return root url for this site args = true/false(default), as_path: true/false(default).
-
#the_user(id_or_username) ⇒ Object
return the user object with id or username = id_or_username from this site.
-
#visitor_role ⇒ Object
return the role_id of current visitor for this site if the visitor was not logged in, then return -1.
Instance Method Details
#draw_languages(list_class = 'language_list list-inline pull-right', current_page = false, current_class = 'current_l', &block) ⇒ Object
draw languages configured for this site list_class: (String) Custom css classes for ul list current_page: (boolean) true: link with translation to current url, false: link with translation to root url block permit to render custom link label, default: flag icon
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 197 def draw_languages(list_class = 'language_list list-inline pull-right', current_page = false, current_class = 'current_l', &block) lan = object.get_languages return if lan.size < 2 h.content_tag(:ul, class: list_class) do h.safe_join(lan.map do |lang| label = if block h.capture(lang, I18n.locale.to_s == lang.to_s, &block) else h.tag.img(src: h.asset_path("camaleon_cms/language/#{lang}.png")) end h.content_tag(:li, class: (current_class if I18n.locale.to_s == lang.to_s)) do h.link_to( label, h.cama_url_to_fixed( current_page ? 'url_for' : 'cama_root_url', { locale: lang, cama_set_language: lang } ) ) end end) end end |
#generate_breadcrumb(_add_post_type = true) ⇒ Object
draw bread crumb for current site
273 274 275 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 273 def (_add_post_type = true) h.(the_title) end |
#manage_sites? ⇒ Boolean
check if current user can manage sites
294 295 296 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 294 def manage_sites? main_site? && h.cama_current_user.admin? end |
#plugin_installed?(plugin_key) ⇒ Boolean
check if plugin_key is already installed for this site
235 236 237 238 239 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 235 def plugin_installed?(plugin_key) res = false PluginRoutes.enabled_plugins(object).each { |plugin| res = true if plugin['key'] == plugin_key } res end |
#the_admin_url ⇒ Object
ADMIN =======================
admin root url for this site
279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 279 def the_admin_url host = if object.main_site? object.slug else (object.slug.include?('.') ? object.slug : "#{object.slug}.#{Cama::Site.main_site.slug}") end port = begin host.split(':')[1] rescue StandardError nil end h.cama_url_to_fixed('cama_admin_dashboard_url', host: host, port: port, locale: nil) end |
#the_categories(slug_or_id = nil) ⇒ Object
return a collection of categories Arguments:
slug_or_id: string or integer
return: slug_or_id: nil => return all main_categories for this site slug_or_id: integer => return all main categories of the post_type with id = slug_or_id slug_or_id: string => return all main categories of the post_type with slug = slug_or_id
85 86 87 88 89 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 85 def the_categories(slug_or_id = nil) return the_post_type(slug_or_id).the_categories if slug_or_id.present? object.categories if slug_or_id.blank? end |
#the_category(slug_or_id) ⇒ Object
return the category object with id or slug = slug_or_id from this site
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 92 def the_category(slug_or_id) if slug_or_id.is_a?(Integer) begin return the_full_categories.where(id: slug_or_id).first.decorate rescue StandardError nil end end return unless slug_or_id.is_a?(String) begin the_full_categories.find_by_slug(slug_or_id).decorate # rubocop:disable Rails/DynamicFindBy rescue StandardError nil end end |
#the_contents(slug_or_id = 'post') ⇒ Object
return all contents from this site registered for post_type = slug (filter visibility, hidden, expired, ...) slug_or_id: slug or id of the post_type or array of slugs of post_types, default 'post'
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 21 def the_contents(slug_or_id = 'post') if slug_or_id.is_a?(Integer) return h.verify_front_visibility(object.posts.where("#{CamaleonCms::TermTaxonomy.table_name}.id = ?", slug_or_id)) end if slug_or_id.is_a?(String) return h.verify_front_visibility(object.posts.where("#{CamaleonCms::TermTaxonomy.table_name}.slug = ?", slug_or_id)) end return unless slug_or_id.is_a?(Array) h.verify_front_visibility(object.posts.where("#{CamaleonCms::TermTaxonomy.table_name}.slug in (?)", slug_or_id)) end |
#the_description ⇒ Object
5 6 7 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 5 def the_description the_content end |
#the_full_categories ⇒ Object
return all categories for ths site (include all children categories)
110 111 112 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 110 def the_full_categories object.full_categories end |
#the_icon ⇒ Object
15 16 17 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 15 def the_icon object.get_option('icon') || h.asset_url('camaleon_cms/favicon.ico') end |
#the_languages ⇒ Object
return Array of frontend languages configured for this site
224 225 226 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 224 def the_languages object.get_languages end |
#the_logo(default = nil) ⇒ Object
return logo url for this site default: this url will be returned if logo is not present.
11 12 13 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 11 def the_logo(default = nil) object.get_option('logo') || (default || h.asset_url('camaleon_cms/camaleon.png').to_s) end |
#the_path(*args) ⇒ Object
return the path for this site
266 267 268 269 270 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 266 def the_path(*args) args = args. args[:as_path] = true the_url(args) end |
#the_post(slug_or_id) ⇒ Object
return the post with id or slug equal to slug_or_id slug_or_id: (String) for post slug slug_or_id: (Integer) for post id slug_or_id: (Array) array of post ids, return multiple posts return post model or nil
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 55 def the_post(slug_or_id) # eager: false -- a single-record lookup must not pay the listing preloads (with_eager) rel = h.verify_front_visibility(object.posts, eager: false) if slug_or_id.is_a?(Integer) post = begin rel.find(slug_or_id) rescue StandardError nil end end if slug_or_id.is_a?(Array) post = begin rel.find(slug_or_id) rescue StandardError nil end end # use find_by_slug (multi-language aware) so posts with localized slugs # (e.g. "<!--:en-->sample-post<!--:-->...") are matched instead of returning nil post = rel.find_by_slug(slug_or_id) if slug_or_id.is_a?(String) # rubocop:disable Rails/DynamicFindBy post&.decorate end |
#the_post_type(slug_or_id) ⇒ Object
return a post_type object with id or slug = slug_or_id Arguments:
slug_or_id: string or integer
return: slug_or_id: integer => return the post type with id = slug_or_id slug_or_id: string => return the post type with slug = slug_or_id slug_or_id: array => return all post types with slugs in the array of this site
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 169 def the_post_type(slug_or_id) if slug_or_id.is_a?(String) begin return object.post_types.find_by_slug(slug_or_id).decorate # rubocop:disable Rails/DynamicFindBy rescue StandardError nil end end if slug_or_id.is_a?(Array) begin return object.post_types.find_by_slug(slug_or_id).decorate # rubocop:disable Rails/DynamicFindBy rescue StandardError nil end end return unless slug_or_id.is_a?(Integer) begin object.post_types.find(slug_or_id).decorate rescue StandardError nil end end |
#the_post_types ⇒ Object
return all post types for this site
158 159 160 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 158 def the_post_types object.post_types end |
#the_posts(slug_or_id = nil) ⇒ Object
return all contents for this site filteredby (visibility, hidden, expired, ...) slug_or_id: (slug of the post_type) possible values:
empty: return all posts of the current site
string: return all posts of post_type with slug = slug_or_id
integer: return all posts of post_type with id = slug_or_id
array: return all posts of post_types with slug in slug_or_id
42 43 44 45 46 47 48 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 42 def the_posts(slug_or_id = nil) if slug_or_id.present? the_contents(slug_or_id) else h.verify_front_visibility(object.posts) end end |
#the_status ⇒ Object
return the text status of current site
299 300 301 302 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 299 def the_status I18n.t("camaleon_cms.models.site.status_options.#{object.status || 'active'}", default: (object.status || 'active').titleize) end |
#the_status_options ⇒ Object
return an array for select options of all status for this site
305 306 307 308 309 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 305 def %w[active inactive maintenance].map do |o| [I18n.t("camaleon_cms.models.site.status_options.#{o}", default: o.titleize), o == 'active' ? '' : o] end end |
#the_tag(slug_or_id) ⇒ Object
return the post_tag object with id or slug = slug_or_id from this site sample: current_site.the_tag('test').the_url sample2: current_site.the_tag('test').the_posts
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 122 def the_tag(slug_or_id) if slug_or_id.is_a?(Integer) begin return object..where(id: slug_or_id).first.decorate rescue StandardError nil end end return unless slug_or_id.is_a?(String) begin object..find_by_slug(slug_or_id).decorate # rubocop:disable Rails/DynamicFindBy rescue StandardError nil end end |
#the_tags ⇒ Object
return all post tags for ths site
115 116 117 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 115 def object. end |
#the_url(*args) ⇒ Object
return root url for this site args = true/false(default), as_path: true/false(default)
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 243 def the_url(*args) args = args. args[:site] = self args[:host], args[:port] = object.get_domain.to_s.split(':') if !args[:as_path] && begin h.current_site rescue StandardError false end != self h.cama_current_site_host_port(args) unless args[:as_path] args[:locale] = @_deco_locale unless args.include?(:locale) postfix = 'url' postfix = 'path' if args.delete(:as_path) skip_relative_url_root = args.delete(:skip_relative_url_root) h.cama_current_site_host_port(args) unless args.key?(:host) res = h.cama_url_to_fixed("cama_root_#{postfix}", args) if skip_relative_url_root && PluginRoutes.static_system_info['relative_url_root'].present? res = res.sub("/#{PluginRoutes.static_system_info['relative_url_root']}", '') end res end |
#the_user(id_or_username) ⇒ Object
return the user object with id or username = id_or_username from this site
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 140 def the_user(id_or_username) if id_or_username.is_a?(Integer) begin return object.users.where(id: id_or_username).first.decorate rescue StandardError nil end end return unless id_or_username.is_a?(String) begin object.users.find_by_username(id_or_username).decorate # rubocop:disable Rails/DynamicFindBy rescue StandardError nil end end |
#visitor_role ⇒ Object
return the role_id of current visitor for this site if the visitor was not logged in, then return -1
230 231 232 |
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 230 def visitor_role h.signin? ? h.cama_current_user.role : '-1' end |