Class: CamaleonCms::Site

Inherits:
TermTaxonomy
  • Object
show all
Includes:
SiteDefaultSettings
Defined in:
app/models/camaleon_cms/site.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SiteDefaultSettings

#default_settings, #set_default_user_roles

Instance Attribute Details

#generated_admin_passwordObject

Transient (never persisted): when default_settings auto-provisions the administrator, it stashes the randomly generated plaintext password here so the provisioning controller can surface it once. nil when no admin was minted (e.g. a shared admin already existed). See harden-installer-default-admin.



14
15
16
# File 'app/models/camaleon_cms/site.rb', line 14

def generated_admin_password
  @generated_admin_password
end

#site_domainObject

attrs: [name, description, slug]



8
9
10
# File 'app/models/camaleon_cms/site.rb', line 8

def site_domain
  @site_domain
end

Class Method Details

.main_siteObject

return main site



152
153
154
# File 'app/models/camaleon_cms/site.rb', line 152

def self.main_site
  @main_site ||= CamaleonCms::Site.reorder(id: :asc).first
end

Instance Method Details

#admin_per_pageObject

items per page to be listed on admin panel



128
129
130
# File 'app/models/camaleon_cms/site.rb', line 128

def admin_per_page
  get_option('admin_per_page', 10)
end

#assign_user(user) ⇒ Object

assign user to this site



118
119
120
# File 'app/models/camaleon_cms/site.rb', line 118

def assign_user(user)
  user.assign_site(self)
end

#categoriesObject

all main categories for this site



73
74
75
# File 'app/models/camaleon_cms/site.rb', line 73

def categories
  CamaleonCms::Category.includes(:post_type_parent).where(post_type_parent: post_types.pluck(:id))
end

#front_comment_statusObject

frontend comments status for new comments on frontend



133
134
135
# File 'app/models/camaleon_cms/site.rb', line 133

def front_comment_status
  get_option('comment_status', 'pending')
end

#front_per_pageObject

items per page to be listed on frontend



123
124
125
# File 'app/models/camaleon_cms/site.rb', line 123

def front_per_page
  get_option('front_per_page', 10)
end

#full_categoriesObject

select full_categories for the site, include all children categories



63
64
65
# File 'app/models/camaleon_cms/site.rb', line 63

def full_categories
  CamaleonCms::Category.where(site_id: id)
end

#get_admin_languageObject

return current admin language configured for this site



91
92
93
# File 'app/models/camaleon_cms/site.rb', line 91

def get_admin_language
  options[:_admin_theme] || 'en'
end

#get_anonymous_userObject

return the anonymous user if the anonymous user not exist, will create one



221
222
223
224
225
226
227
228
229
# File 'app/models/camaleon_cms/site.rb', line 221

def get_anonymous_user
  user = users.where(username: 'anonymous').first
  if user.blank?
    pass = "anonymous#{rand(9999)}"
    user = users.create({ email: 'anonymous_user@local.com', username: 'anonymous', password: pass,
                          password_confirmation: pass, first_name: 'Anonymous' })
  end
  user
end

#get_domainObject

return the domain for current site sample: mysite.com | sample.mysite.com also, you can define custom domain for this site by: my_site.site_domain = 'my_site.com' used for sites with different domains to call from console or task



235
236
237
238
239
240
241
# File 'app/models/camaleon_cms/site.rb', line 235

def get_domain
  @site_domain || (if main_site?
                     slug
                   else
                     (slug.include?('.') ? slug : "#{slug}.#{Cama::Site.main_site.slug}")
                   end)
end

#get_languagesObject

return all languages configured by the admin if it is empty, then return default locale



79
80
81
82
83
84
85
86
87
88
# File 'app/models/camaleon_cms/site.rb', line 79

def get_languages
  return @_languages if defined?(@_languages)

  l = get_meta('languages_site', [I18n.default_locale])
  @_languages = begin
    l.map(&:to_sym)
  rescue StandardError
    [I18n.default_locale.to_sym]
  end
end

#get_plugin(plugin_slug) ⇒ Object

return plugin model with slug plugin_slug



113
114
115
# File 'app/models/camaleon_cms/site.rb', line 113

def get_plugin(plugin_slug)
  plugins.where(slug: plugin_slug).first_or_create!
end

#get_theme(theme_slug = nil) ⇒ Object

return theme model with slug theme_slug for this site theme_slug: (optional) if it is null, this will return current theme for this site



108
109
110
# File 'app/models/camaleon_cms/site.rb', line 108

def get_theme(theme_slug = nil)
  themes.where(slug: theme_slug || get_theme_slug, status: nil).first_or_create!
end

#get_theme_slugObject

return current theme slug configured for this site if theme was not configured, then return system.json defined



102
103
104
# File 'app/models/camaleon_cms/site.rb', line 102

def get_theme_slug
  options[:_theme] || PluginRoutes.system_info['default_template']
end

#get_valid_post_slug(slug, post_id = nil) ⇒ Object

return an available slug for a new post slug: (String) possible slug value post_id: (integer, optional) current post id sample: ("features-1caract-1") | ("features") return: (String) available slugs



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'app/models/camaleon_cms/site.rb', line 187

def get_valid_post_slug(slug, post_id = nil)
  slugs = slug.translations
  if slugs.present?
    slugs.each { |k, v| slugs[k] = get_valid_post_slug(v) }
    slugs.to_translate
  else
    res = slug
    (1..9999).each do |i|
      p = posts.find_by_slug(res) # rubocop:disable Rails/DynamicFindBy
      break if p.blank? || (p.present? && p.id == post_id)

      res = "#{slug}-#{i}"
    end
    res
  end
end

#is_active?Boolean

check if current site is active or not

Returns:

  • (Boolean)


205
206
207
# File 'app/models/camaleon_cms/site.rb', line 205

def is_active?
  status.blank? || status == 'active'
end

#is_enable_captcha_for_comments?Boolean

check if current site permit capctha for anonymous comments

Returns:

  • (Boolean)


143
144
145
# File 'app/models/camaleon_cms/site.rb', line 143

def is_enable_captcha_for_comments?
  get_option('enable_captcha_for_comments', false)
end

#is_inactive?Boolean

check if current site is active or not

Returns:

  • (Boolean)


210
211
212
# File 'app/models/camaleon_cms/site.rb', line 210

def is_inactive?
  status == 'inactive'
end

#is_maintenance?Boolean

check if current site is in maintenance or not

Returns:

  • (Boolean)


215
216
217
# File 'app/models/camaleon_cms/site.rb', line 215

def is_maintenance?
  status == 'maintenance'
end

#main_site?Boolean Also known as: is_default?

check if this site is the main site main site is a site that doesn't have slug

Returns:

  • (Boolean)


158
159
160
# File 'app/models/camaleon_cms/site.rb', line 158

def main_site?
  self.class.main_site == self
end

#need_validate_email?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'app/models/camaleon_cms/site.rb', line 147

def need_validate_email?
  get_option('need_validate_email', false) == true
end

#post_tagsObject

all post_tags for this site



68
69
70
# File 'app/models/camaleon_cms/site.rb', line 68

def 
  CamaleonCms::PostTag.includes(:post_type).where(post_type: post_types.pluck(:id))
end

#security_user_register_captcha_enabled?Boolean

security: user register form show captcha?

Returns:

  • (Boolean)


138
139
140
# File 'app/models/camaleon_cms/site.rb', line 138

def security_user_register_captcha_enabled?
  get_option('security_captcha_user_register', false) == true
end

#set_admin_language(language) ⇒ Object

Set the current admin language for this site



96
97
98
# File 'app/models/camaleon_cms/site.rb', line 96

def set_admin_language(language)
  set_option('_admin_theme', language)
end

#upload_directory(inner_directory = nil) ⇒ Object

return upload directory for this site (deprecated for cloud support)



172
173
174
175
# File 'app/models/camaleon_cms/site.rb', line 172

def upload_directory(inner_directory = nil)
  File.join(Rails.public_path, "/media/#{PluginRoutes.static_system_info['media_slug_folder'] ? slug : id}",
            inner_directory.to_s)
end

#upload_directory_nameObject

return the directory name where to upload file for this site



178
179
180
# File 'app/models/camaleon_cms/site.rb', line 178

def upload_directory_name
  (PluginRoutes.static_system_info['media_slug_folder'] ? slug : id).to_s
end

#user_rolesObject

all user roles for this site



54
55
56
57
58
59
60
# File 'app/models/camaleon_cms/site.rb', line 54

def user_roles
  if PluginRoutes.system_info['users_share_sites']
    CamaleonCms::Site.main_site.user_roles_rel
  else
    user_roles_rel
  end
end

#usersObject Also known as: users_include_admins

list all users of current site



164
165
166
167
168
# File 'app/models/camaleon_cms/site.rb', line 164

def users
  return CamaleonCms::User.all if PluginRoutes.system_info['users_share_sites']

  CamaleonCms::User.where(site_id: id)
end