Class: CamaleonCms::SiteDecorator

Inherits:
TermTaxonomyDecorator show all
Defined in:
app/decorators/camaleon_cms/site_decorator.rb

Overview

Camaleon CMS is a content management system

Copyright (C) 2015 by Owen Peredo Diaz
Email: [email protected]
This program is free software: you can redistribute it and/or modify   it under the terms of the GNU Affero General Public License as  published by the Free Software Foundation, either version 3 of the  License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the  GNU Affero General Public License (GPLv3) for more details.

Instance Method Summary collapse

Methods inherited from TermTaxonomyDecorator

#the_content, #the_edit_link, #the_edit_url, #the_excerpt, #the_owner, #the_parent, #the_slug, #the_status, #the_title

Methods included from CustomFieldsConcern

#render_fields, #the_field, #the_field!, #the_fields

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

Instance Method Details

#draw_languages(list_class = "language_list list-inline pull-right", current_page = false, current_class = "current_l") ⇒ 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


123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 123

def draw_languages(list_class = "language_list list-inline pull-right", current_page = false, current_class = "current_l")
  lan = object.get_languages
  return  if  lan.size < 2
  res = ["<ul class='#{list_class}'>"]
  lan.each do |lang|
    path = lang.to_s+'.png'
    img = "<img src='#{h.asset_path("camaleon_cms/language/#{path}")}'/>"
    res << "<li class='#{ current_class if I18n.locale.to_s == lang.to_s}'> <a href='#{h.cama_url_to_fixed(current_page ? "url_for" : "cama_root_url", {locale: lang})}'>#{img}</a> </li>"
  end
  res << "</ul>"
  res.join("")
end

#generate_breadcrumb(add_post_type = true) ⇒ Object

draw bread crumb for current site


166
167
168
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 166

def generate_breadcrumb(add_post_type = true)
  h.breadcrumb_add(self.the_title)
end

#manage_sites?Boolean

check if current user can manage sites

Returns:

  • (Boolean)

179
180
181
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 179

def manage_sites?
  self.main_site? && h.current_user.admin?
end

#plugin_installed?(plugin_key) ⇒ Boolean

check if plugin_key is already installed for this site

Returns:

  • (Boolean)

148
149
150
151
152
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 148

def plugin_installed?(plugin_key)
  res = false
  PluginRoutes.enabled_plugins(object).each{|plugin| res = true if plugin["key"] == plugin_key }
  res
end

#the_admin_urlObject

ADMIN =======================

admin root url for this site


172
173
174
175
176
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 172

def the_admin_url
  host = object.main_site? ? PluginRoutes.system_info["base_domain"] : (object.slug.include?(".") ? object.slug : "#{object.slug}.#{PluginRoutes.system_info["base_domain"]}" )
  port = (host.split(":")[1] rescue nil)
  h.cama_url_to_fixed("cama_admin_dashboard_url", host: host, port: port, locale: false)
end

#the_categories(slug_or_id = nil) ⇒ Object

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


67
68
69
70
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 67

def the_categories(slug_or_id = nil)
  return the_post_type(slug_or_id).the_categories if slug_or_id.present?
  return object.categories unless slug_or_id.present?
end

#the_category(slug_or_id) ⇒ Object

return the category object with id or slug = slug_or_id from this site


73
74
75
76
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 73

def the_category(slug_or_id)
  return the_full_categories.where(id: slug_or_id).first.decorate rescue nil if slug_or_id.is_a?(Integer)
  return the_full_categories.find_by_slug(slug_or_id).decorate rescue nil if slug_or_id.is_a?(String)
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'


28
29
30
31
32
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 28

def the_contents(slug_or_id = "post")
  return h.verify_front_visibility(object.posts.where("#{CamaleonCms::TermTaxonomy.table_name}.id = ?", slug_or_id)).eager_load(:post_type) if slug_or_id.is_a?(Integer)
  return h.verify_front_visibility(object.posts.where("#{CamaleonCms::TermTaxonomy.table_name}.slug = ?", slug_or_id)).eager_load(:post_type) if slug_or_id.is_a?(String)
  return h.verify_front_visibility(object.posts.where("#{CamaleonCms::TermTaxonomy.table_name}.slug in (?)", slug_or_id)).eager_load(:post_type) if slug_or_id.is_a?(Array)
end

#the_descriptionObject


12
13
14
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 12

def the_description
  the_content
end

#the_full_categoriesObject

return all categories for ths site (include all children categories)


79
80
81
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 79

def the_full_categories
  object.full_categories
end

#the_iconObject


22
23
24
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 22

def the_icon
  object.get_option("icon") || h.asset_url('camaleon_cms/favicon.ico')
end

#the_languagesObject

return Array of frontend languages configured for this site


137
138
139
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 137

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.


18
19
20
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 18

def (default = nil)
  object.get_option("logo") || (default || "#{h.asset_url("camaleon_cms/camaleon.png")}")
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


53
54
55
56
57
58
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 53

def the_post(slug_or_id)
  post = self.the_posts.find(slug_or_id) rescue nil if slug_or_id.is_a?(Integer) # id
  post = self.the_posts.find(slug_or_id) rescue nil if slug_or_id.is_a?(Array) # id
  post = self.the_posts.find_by_slug(slug_or_id) if slug_or_id.is_a?(String) # id
  post.present? ? post.decorate : nil
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


114
115
116
117
118
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 114

def the_post_type(slug_or_id)
  return object.post_types.find_by_slug(slug_or_id).decorate rescue nil if slug_or_id.is_a?(String)
  return object.post_types.find_by_slug(slug_or_id).decorate rescue nil if slug_or_id.is_a?(Array)
  return object.post_types.find(slug_or_id).decorate rescue nil if slug_or_id.is_a?(Integer)
end

#the_post_typesObject

return all post types for this site


103
104
105
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 103

def the_post_types
  object.post_types.eager_load(:metas)
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

40
41
42
43
44
45
46
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 40

def the_posts(slug_or_id = nil)
  if slug_or_id.present?
    the_contents(slug_or_id)
  else
    h.verify_front_visibility(object.posts).eager_load(:post_type)
  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


91
92
93
94
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 91

def the_tag(slug_or_id)
  return object..where(id: slug_or_id).first.decorate rescue nil if slug_or_id.is_a?(Integer)
  return object..find_by_slug(slug_or_id).decorate rescue nil if slug_or_id.is_a?(String)
end

#the_tagsObject

return all post tags for ths site


84
85
86
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 84

def the_tags
  object.
end

#the_url(*args) ⇒ Object

return root url for this site


155
156
157
158
159
160
161
162
163
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 155

def the_url(*args)
  args = args.extract_options!
  args[:host] = object.main_site? ? PluginRoutes.system_info["base_domain"] : (object.slug.include?(".") ? object.slug : "#{object.slug}.#{PluginRoutes.system_info["base_domain"]}" )
  args[:port] = (args[:host].split(":")[1] rescue nil)
  args[:locale] = @_deco_locale unless args.include?(:locale)
  args[:host] = args[:host].split(":").first
  args.delete(:as_path)
  h.cama_url_to_fixed("cama_root_url", args)
end

#the_user(id_or_username) ⇒ Object

return the user object with id or username = id_or_username from this site


97
98
99
100
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 97

def the_user(id_or_username)
  return object.users.where(id: id_or_username).first.decorate rescue nil if id_or_username.is_a?(Integer)
  return object.users.find_by_username(id_or_username).decorate rescue nil if id_or_username.is_a?(String)
end

#visitor_roleObject

return the role_id of current visitor for this site if the visitor was not logged in, then return -1


143
144
145
# File 'app/decorators/camaleon_cms/site_decorator.rb', line 143

def visitor_role
  h.signin? ? h.cama_current_user.get_role(object).slug : "-1"
end