app/models/camaleon_cms/site.rb in camaleon_cms-2.1.1 vs app/models/camaleon_cms/site.rb in camaleon_cms-2.1.1.4

- old
+ new

@@ -31,11 +31,11 @@ validates_uniqueness_of :slug, scope: :taxonomy # all user roles for this site def user_roles if PluginRoutes.system_info["users_share_sites"] - CamaleonCms::Site.first.user_roles_rel + CamaleonCms::Site.main_site.user_roles_rel else user_roles_rel end end @@ -132,37 +132,35 @@ user_role = self.user_roles.where({slug: 'editor'}).first_or_create({name: 'Editor', description: 'Editor Role'}) if user_role.valid? d = {} if post_type.present? - d = user_role.get_meta("_post_type", {}) + d = user_role.get_meta("_post_type_#{self.id}", {}) CamaleonCms::UserRole::ROLES[:post_type].each { |value| - value_old = d[value[:key]] || [] - d[value[:key]] = value_old + [post_type.id] + value_old = d[value[:key].to_sym] || [] + d[value[:key].to_sym] = value_old + [post_type.id] } else pts = self.post_types.all.pluck(:id) CamaleonCms::UserRole::ROLES[:post_type].each { |value| d[value[:key]] = pts } end - user_role.set_meta("_post_type_#{self.id}", d || {}) end user_role = self.user_roles.where({slug: 'contributor'}).first_or_create({name: 'Contributor', description: 'Contributor Role'}) if user_role.valid? d = {} if post_type.present? - d = user_role.get_meta("_post_type", {}) + d = user_role.get_meta("_post_type_#{self.id}", {}) CamaleonCms::UserRole::ROLES[:post_type].each { |value| - value_old = d[value[:key]] || [] - d[value[:key]] = value_old + [post_type.id] if value[:key].to_s == 'edit' + value_old = d[value[:key].to_sym] || [] + d[value[:key].to_sym] = value_old + [post_type.id] if value[:key].to_s == 'edit' } else pts = self.post_types.all.pluck(:id) CamaleonCms::UserRole::ROLES[:post_type].each { |value| d[value[:key]] = pts if value[:key].to_s == 'edit' } end - user_role.set_meta("_post_type_#{self.id}", d || {}) end unless post_type.present? user_role = self.user_roles.where({slug: 'client', term_group: -1}).first_or_create({name: 'Client', description: 'Default roles client'}) @@ -174,17 +172,17 @@ end # return main site def self.main_site - CamaleonCms::Site.first + @main_site ||= CamaleonCms::Site.reorder(id: :ASC).first end # check if this site is the main site # main site is a site that doesn't have slug def main_site? - @_is_default_site ||= (CamaleonCms::Site.first.id == self.id) + self.class.main_site == self end alias_method :is_default?, :main_site? # list all users of current site @@ -215,23 +213,25 @@ "#{PluginRoutes.static_system_info["media_slug_folder"] ? self.slug : self.id}" end # return an available slug for a new post # slug: (String) possible slug value + # post_id: (integer, optional) current post id # sample: ("<!--:es-->features-1<!--:--><!--:en-->caract-1<!--:-->") | ("features") # return: (String) available slugs - def get_valid_post_slug(slug) + def get_valid_post_slug(slug, post_id=nil) slugs = slug.translations if slugs.present? slugs.each do |k, v| slugs[k] = get_valid_post_slug(v) end slugs.to_translate else res = slug (1..9999).each do |i| - break unless self.posts.find_by_slug(res) + p = self.posts.find_by_slug(res) + break if !p.present? || (p.present? && p.id == post_id) res = "#{slug}-#{i}" end res end end @@ -285,15 +285,15 @@ end # update all routes of the system # reload system routes for this site def update_routes - if self.id == CamaleonCms::Site.first.id - set_option("base_domain", self.slug, "main_settings") if self.slug.present? - end - PluginRoutes.reload + PluginRoutes.reload if self.slug_changed? end def before_validating - #overwriting/skipping term_taxonomy validations + slug = self.slug + slug = self.name if slug.blank? + self.name = slug unless self.name.present? + self.slug = slug.to_s.try(:downcase) end -end +end \ No newline at end of file