diff --git a/Dockerfile b/Dockerfile index cf7c3ded9..0a085732e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,40 +1,34 @@ -## BUILDING -## (from project root directory) -## $ docker build -t owen2345-camaleon-cms . -## -## RUNNING -## $ docker run -p 3000:3000 owen2345-camaleon-cms -## -## CONNECTING -## Lookup the IP of your active docker host using: -## $ docker-machine ip $(docker-machine active) -## Connect to the container at DOCKER_IP:3000 -## replacing DOCKER_IP for the IP of your active docker host - -FROM gcr.io/stacksmith-images/ubuntu-buildpack:14.04-r07 - -MAINTAINER Bitnami - -ENV STACKSMITH_STACK_ID="ujruqvm" \ - STACKSMITH_STACK_NAME="owen2345/camaleon-cms" \ - STACKSMITH_STACK_PRIVATE="1" - -RUN bitnami-pkg install ruby-2.3.1-1 --checksum a81395976c85e8b7c8da3c1db6385d0e909bd05d9a3c1527f8fa36b8eb093d84 - -ENV PATH=/opt/bitnami/ruby/bin:$PATH - -## STACKSMITH-END: Modifications below this line will be unchanged when regenerating - -# Ruby on Rails template -ENV RAILS_ENV=development - -COPY Gemfile* /app/ +FROM ruby:3.1-slim as builder +RUN apt-get update && apt-get install curl gnupg build-essential libpq-dev -y +RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - +RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list +RUN apt-get update -qq +RUN apt install yarn -y +RUN apt-get install imagemagick -y + +RUN mkdir /app WORKDIR /app -RUN bundle install --without production - -COPY . /app - +# Default port and web server command EXPOSE 3000 - -CMD ["bundle", "exec", "rails", "server", "-b", "0.0.0.0", "-p", "3000"] +CMD ["bundle exec rspec"] + +##################################### (Development) +# install google chrome +RUN apt-get install wget -y +RUN wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - +RUN sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list' +RUN apt-get -y update +RUN apt-get install -y google-chrome-stable + +# install chromedriver +RUN apt-get install -yqq unzip +RUN wget -O /tmp/chromedriver.zip http://chromedriver.storage.googleapis.com/`curl -sS chromedriver.storage.googleapis.com/LATEST_RELEASE`/chromedriver_linux64.zip +RUN unzip /tmp/chromedriver.zip chromedriver -d /usr/local/bin/ + +#COPY package.json yarn.lock /app/ +#RUN yarn install +COPY Gemfile* camaleon_cms.gemspec /app/ +COPY lib/camaleon_cms/version.rb /app/lib/camaleon_cms/ +RUN bundle install --jobs 20 --retry 5 +COPY . /app diff --git a/app/decorators/camaleon_cms/category_decorator.rb b/app/decorators/camaleon_cms/category_decorator.rb index 9f95ae3f1..23b7e8765 100644 --- a/app/decorators/camaleon_cms/category_decorator.rb +++ b/app/decorators/camaleon_cms/category_decorator.rb @@ -39,7 +39,7 @@ def generate_breadcrumb(add_post_type = true, is_parent = false) if _parent.present? _parent.decorate.generate_breadcrumb(add_post_type, true) else - object.post_type_parent.decorate.generate_breadcrumb(add_post_type, true) + object.post_type.decorate.generate_breadcrumb(add_post_type, true) end h.breadcrumb_add(self.the_title, is_parent ? self.the_url : nil) end diff --git a/app/helpers/camaleon_cms/admin/custom_fields_helper.rb b/app/helpers/camaleon_cms/admin/custom_fields_helper.rb index d0c931d0b..43f219b3a 100644 --- a/app/helpers/camaleon_cms/admin/custom_fields_helper.rb +++ b/app/helpers/camaleon_cms/admin/custom_fields_helper.rb @@ -1,11 +1,11 @@ #encoding: utf-8 module CamaleonCms::Admin::CustomFieldsHelper - def cama_custom_field_elements - return @_cama_custom_field_elements if @_cama_custom_field_elements.present? + + def cama_custom_field_settings items = {} items[:text_box] = { key: 'text_box', - label: t('camaleon_cms.admin.custom_field.fields.text_box'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.text_box'), options: { required: true, multiple: true, @@ -17,7 +17,7 @@ def cama_custom_field_elements items[:text_area] = { key: 'text_area', - label: t('camaleon_cms.admin.custom_field.fields.text_area'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.text_area'), options: { required: true, multiple: true, @@ -28,13 +28,13 @@ def cama_custom_field_elements } items[:select] = { key: 'select', - label: t('camaleon_cms.admin.custom_field.fields.select'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.select'), options: { required: true, multiple: true, translate: true, multiple_options: { - label: t('camaleon_cms.admin.settings.options_select'), + label: I18n.t('camaleon_cms.admin.settings.options_select'), default: 'radio' }, show_frontend: true @@ -42,12 +42,12 @@ def cama_custom_field_elements } items[:radio] = { key: 'radio', - label: t('camaleon_cms.admin.custom_field.fields.radio', default: 'Radio'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.radio', default: 'Radio'), options: { required: true, multiple: false, multiple_options: { - label: t('camaleon_cms.admin.settings.options_select'), + label: I18n.t('camaleon_cms.admin.settings.options_select'), default: 'radio', use_not_default: true }, @@ -57,7 +57,7 @@ def cama_custom_field_elements items[:checkbox] = { key: 'checkbox', - label: t('camaleon_cms.admin.custom_field.fields.checkbox', default: 'Checkbox'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.checkbox', default: 'Checkbox'), options: { required: true, multiple: false, @@ -67,7 +67,7 @@ def cama_custom_field_elements } items[:checkboxes] = { key: 'checkboxes', - label: t('camaleon_cms.admin.custom_field.fields.checkboxes', default: 'Checkboxes'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.checkboxes', default: 'Checkboxes'), options: { required: false, multiple: false, @@ -80,7 +80,7 @@ def cama_custom_field_elements } items[:audio] = { key: 'audio', - label: t('camaleon_cms.admin.custom_field.fields.audio', default: 'Audio'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.audio', default: 'Audio'), options: { required: true, multiple: true, @@ -90,7 +90,7 @@ def cama_custom_field_elements } items[:colorpicker] = { key: 'colorpicker', - label: t('camaleon_cms.admin.custom_field.fields.colorpicker'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.colorpicker'), extra_fields:[ { type: 'select', @@ -121,7 +121,7 @@ def cama_custom_field_elements } items[:date] = { key: 'date', - label: t('camaleon_cms.admin.custom_field.fields.date'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.date'), extra_fields:[ { type: 'radio', @@ -129,11 +129,11 @@ def cama_custom_field_elements values: [ { value: '0', - label: t('camaleon_cms.admin.settings.input_only_date') + label: I18n.t('camaleon_cms.admin.settings.input_only_date') }, { value: '1', - label: t('camaleon_cms.admin.settings.input_date_time') + label: I18n.t('camaleon_cms.admin.settings.input_date_time') } ] } @@ -147,7 +147,7 @@ def cama_custom_field_elements } items[:editor] = { key: 'editor', - label: t('camaleon_cms.admin.custom_field.fields.editor', default: 'Editor'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.editor', default: 'Editor'), options: { required: false, multiple: true, @@ -158,7 +158,7 @@ def cama_custom_field_elements } items[:email] = { key: 'email', - label: t('camaleon_cms.admin.custom_field.fields.email'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.email'), options: { required: true, multiple: true, @@ -168,7 +168,7 @@ def cama_custom_field_elements } items[:file] = { key: 'file', - label: t('camaleon_cms.admin.custom_field.fields.file'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.file'), options: { required: true, multiple: true, @@ -185,7 +185,7 @@ def cama_custom_field_elements } items[:image] = { key: 'image', - label: t('camaleon_cms.admin.custom_field.fields.image'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.image'), options: { required: true, multiple: true, @@ -196,20 +196,20 @@ def cama_custom_field_elements { type: 'text_box', key: 'dimension', - label: cama_t('camaleon_cms.admin.custom_field.fields.image_dimension'), - description: cama_t('camaleon_cms.admin.custom_field.fields.image_dimension_descr'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.image_dimension'), + description: I18n.t('camaleon_cms.admin.custom_field.fields.image_dimension_descr'), }, { type: 'text_box', key: 'versions', - label: cama_t('camaleon_cms.admin.custom_field.fields.image_versions'), - description: cama_t('camaleon_cms.admin.custom_field.fields.image_versions_descr') + label: I18n.t('camaleon_cms.admin.custom_field.fields.image_versions'), + description: I18n.t('camaleon_cms.admin.custom_field.fields.image_versions_descr') } ] } items[:numeric] = { key: 'numeric', - label: t('camaleon_cms.admin.custom_field.fields.numeric'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.numeric'), options: { required: true, multiple: true, @@ -219,7 +219,7 @@ def cama_custom_field_elements } items[:phone] = { key: 'phone', - label: t('camaleon_cms.admin.custom_field.fields.phone'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.phone'), options: { required: true, multiple: true, @@ -229,7 +229,7 @@ def cama_custom_field_elements } items[:url] = { key: 'url', - label: t('camaleon_cms.admin.custom_field.fields.url', default: 'URL'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.url', default: 'URL'), options: { required: true, multiple: true, @@ -240,7 +240,7 @@ def cama_custom_field_elements } items[:video] = { key: 'video', - label: t('camaleon_cms.admin.custom_field.fields.video', default: 'Video'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.video', default: 'Video'), options: { required: true, multiple: true, @@ -250,7 +250,7 @@ def cama_custom_field_elements } items[:users] = { key: 'users', - label: t('camaleon_cms.admin.custom_field.fields.users'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.users'), options: { required: true, multiple: true, @@ -259,7 +259,7 @@ def cama_custom_field_elements } items[:posts] = { key: 'posts', - label: t('camaleon_cms.admin.custom_field.fields.posts'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.posts'), options: { required: true, multiple: true, @@ -277,7 +277,7 @@ def cama_custom_field_elements items[:post_types] = { key: 'post_types', - label: t('camaleon_cms.admin.post_type.post_types'), + label: I18n.t('camaleon_cms.admin.post_type.post_types'), options: { required: true, multiple: true @@ -286,7 +286,7 @@ def cama_custom_field_elements items[:categories] = { key: 'categories', - label: t('camaleon_cms.admin.table.categories'), + label: I18n.t('camaleon_cms.admin.table.categories'), options: { required: true, multiple: true @@ -297,7 +297,7 @@ def cama_custom_field_elements # sample command: options_from_collection_for_select(current_site.the_posts("commerce").decorate, :id, :the_title) items[:select_eval] = { key: 'select_eval', - label: t('camaleon_cms.admin.custom_field.fields.select_eval'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.select_eval'), options: { required: true, multiple: false, @@ -314,7 +314,7 @@ def cama_custom_field_elements } items[:field_attrs] = { key: 'field_attrs', - label: t('camaleon_cms.admin.custom_field.fields.field_attrs'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.field_attrs'), options: { required: false, multiple: true, @@ -324,7 +324,7 @@ def cama_custom_field_elements } items[:private_file] = { key: 'private_file', - label: t('camaleon_cms.admin.custom_field.fields.private_file', default: 'Private File'), + label: I18n.t('camaleon_cms.admin.custom_field.fields.private_file', default: 'Private File'), options: { required: true, multiple: true, @@ -338,7 +338,14 @@ def cama_custom_field_elements } ] } - r = {fields: items}; hooks_run("extra_custom_fields", r) + items + end + module_function :cama_custom_field_settings + + def cama_custom_field_elements + return @_cama_custom_field_elements if @_cama_custom_field_elements.present? + + r = {fields: cama_custom_field_settings}; hooks_run("extra_custom_fields", r) @_cama_custom_field_elements = r[:fields] end diff --git a/app/helpers/camaleon_cms/admin/post_type_helper.rb b/app/helpers/camaleon_cms/admin/post_type_helper.rb index 6541dd4d3..c30d73f04 100644 --- a/app/helpers/camaleon_cms/admin/post_type_helper.rb +++ b/app/helpers/camaleon_cms/admin/post_type_helper.rb @@ -4,7 +4,7 @@ module CamaleonCms::Admin::PostTypeHelper #taxonomy -> (categories || post_tags) def post_type_html_inputs(post_type, taxonomy="categories", name ="categories", type="checkbox", values=[], class_cat="categorychecklist" , required = false) categories = post_type.send(taxonomy) - categories = categories.eager_load(:children, :post_type_parent, :parent) if taxonomy == "categories" || taxonomy == "children" + categories = categories.eager_load(:children, :post_type, :parent) if taxonomy == "categories" || taxonomy == "children" post_type_taxonomy_html_(categories,taxonomy, name, type, values, class_cat, required) end diff --git a/app/models/camaleon_cms/category.rb b/app/models/camaleon_cms/category.rb index d377d7e8e..142b9a8f2 100644 --- a/app/models/camaleon_cms/category.rb +++ b/app/models/camaleon_cms/category.rb @@ -6,14 +6,13 @@ class Category < CamaleonCms::TermTaxonomy default_scope { where(taxonomy: :category) } scope :no_empty, -> { where('count > 0') } # return all categories that contains at least one post scope :empty, -> { where(count: [0, nil]) } # return all categories that does not contain any post - # scope :parents, -> { where("term_taxonomy.parent_id IS NULL") } cama_define_common_relationships('Category') has_many :posts, foreign_key: :objectid, through: :term_relationships, source: :object has_many :children, class_name: 'CamaleonCms::Category', foreign_key: :parent_id, dependent: :destroy belongs_to :parent, class_name: 'CamaleonCms::Category', foreign_key: :parent_id, required: false - belongs_to :post_type_parent, class_name: 'CamaleonCms::PostType', foreign_key: :parent_id, inverse_of: :categories, required: false + belongs_to :post_type, class_name: 'CamaleonCms::PostType', foreign_key: :parent_id, inverse_of: :categories, required: false belongs_to :site, required: false before_save :set_site @@ -21,13 +20,12 @@ class Category < CamaleonCms::TermTaxonomy # return the post type of this category def post_type - cama_fetch_cache('post_type') do - ctg = self - begin - pt = ctg.post_type_parent - ctg = ctg.parent - end while ctg - pt + parent ? path.first.post_type : super + end + + def path + cama_fetch_cache('path') do + (parent&.path || []) + [self] end end diff --git a/app/models/camaleon_cms/custom_field.rb b/app/models/camaleon_cms/custom_field.rb index a7062f4a8..123cfd4de 100644 --- a/app/models/camaleon_cms/custom_field.rb +++ b/app/models/camaleon_cms/custom_field.rb @@ -5,6 +5,7 @@ class CustomField < ActiveRecord::Base self.primary_key = :id self.table_name = "#{PluginRoutes.static_system_info['db_prefix']}custom_fields" + attr_accessor :settings alias_attribute :label, :name default_scope { order("#{CamaleonCms::CustomField.table_name}.field_order ASC") } @@ -17,7 +18,7 @@ class CustomField < ActiveRecord::Base has_many :metas, -> { where(object_class: 'CustomField') }, foreign_key: :objectid, dependent: :destroy has_many :values, class_name: 'CamaleonCms::CustomFieldsRelationship', foreign_key: :custom_field_id, dependent: :destroy - belongs_to :custom_field_group, required: false + belongs_to :custom_field_group, required: false, foreign_key: :parent_id belongs_to :parent, class_name: 'CamaleonCms::CustomField', foreign_key: :parent_id, required: false validates :name, :object_class, presence: true diff --git a/app/models/camaleon_cms/custom_field_group.rb b/app/models/camaleon_cms/custom_field_group.rb index 1659f2da7..9147d1f38 100644 --- a/app/models/camaleon_cms/custom_field_group.rb +++ b/app/models/camaleon_cms/custom_field_group.rb @@ -1,8 +1,10 @@ +# TODO: refactor to use polymorphism, improve associations and refactor methods module CamaleonCms class CustomFieldGroup < CamaleonCms::CustomField self.primary_key = :id # attrs required: name, slug, description alias_attribute :site_id, :parent_id + attr_accessor :record default_scope { where.not(object_class: '_fields') .reorder("#{CamaleonCms::CustomField.table_name}.field_order ASC") } @@ -15,6 +17,7 @@ class CustomFieldGroup < CamaleonCms::CustomField validates_uniqueness_of :slug, scope: [:object_class, :objectid, :parent_id] before_validation :before_validating + before_validation :retrieve_site_id, unless: :site_id # add fields to group # item: @@ -133,5 +136,10 @@ def auto_save_default_values(field, options) end end end + + def retrieve_site_id + site_owner = record.class.to_s.parseCamaClass == 'Site' ? record : record.site + self.site_id ||= site_owner.try(:id) + end end end diff --git a/app/models/camaleon_cms/post.rb b/app/models/camaleon_cms/post.rb index 0da1cd487..ce3dfa050 100644 --- a/app/models/camaleon_cms/post.rb +++ b/app/models/camaleon_cms/post.rb @@ -3,8 +3,10 @@ class Post < CamaleonCms::PostDefault include CamaleonCms::CategoriesTagsForPosts alias_attribute :post_type_id, :taxonomy_id + alias_attribute :parent_id, :post_parent default_scope ->{ where(post_class: 'Post').order(post_order: :asc, created_at: :desc) } cama_define_common_relationships('Post') + delegate :site, to: :post_type # DEPRECATED has_many :post_relationships, class_name: "CamaleonCms::PostRelationship", foreign_key: :objectid, dependent: :destroy, inverse_of: :post @@ -234,6 +236,16 @@ def decorator_class (post_type.get_option('cama_post_decorator_class', 'CamaleonCms::PostDecorator') rescue 'CamaleonCms::PostDecorator').constantize end + def get_field_groups + CamaleonCms::CustomFieldGroup.where("(objectid = ? AND object_class = 'Post') OR + (objectid = ? AND object_class = 'PostType_Post')", + self.id || -1, self.post_type_id) + end + + def add_custom_field_group(values) + custom_field_groups.create!(values.merge(record: self)) + end + private # calculate a post order when it is empty def fix_post_order diff --git a/app/models/camaleon_cms/post_type.rb b/app/models/camaleon_cms/post_type.rb index de5c46db7..008eb493b 100644 --- a/app/models/camaleon_cms/post_type.rb +++ b/app/models/camaleon_cms/post_type.rb @@ -3,7 +3,7 @@ class PostType < CamaleonCms::TermTaxonomy alias_attribute :site_id, :parent_id default_scope { where(taxonomy: :post_type) } cama_define_common_relationships('PostType') - has_many :categories, foreign_key: :parent_id, dependent: :destroy, inverse_of: :post_type_parent + has_many :categories, foreign_key: :parent_id, dependent: :destroy, inverse_of: :post_type has_many :post_tags, foreign_key: :parent_id, dependent: :destroy, inverse_of: :post_type has_many :posts, foreign_key: :taxonomy_id, dependent: :destroy, inverse_of: :post_type has_many :comments, through: :posts @@ -154,6 +154,38 @@ def manage_hierarchy? get_option('has_parent_structure', false) end + # @param kind (Post|Category|PostTag|self|all) + # Sample: mypost_type.get_field_groups('Post') => return custom field groups for posts + # Sample: mypost_type.get_field_groups('Category') => return custom field groups for categories + # Sample: mypost_type.get_field_groups('PostTag') => return custom field groups for post-tags + def get_field_groups(kind = 'Post') + kind = kind[:kind] if kind.is_a?(Hash) + if kind == 'all' + field_types = %w[PostType_Post PostType_Category PostType_PostTag PostType] + CamaleonCms::CustomFieldGroup.where(object_class: field_types, objectid: self.id ) + elsif %w[post_type self].include?(kind) + self.custom_field_groups + else + CamaleonCms::CustomFieldGroup.where(object_class: "PostType_#{kind}", objectid: self.id) + end + end + + # @param kind (see #get_field_groups) + def add_custom_field_group(data, kind = "Post") + get_field_groups(kind).create!(data.merge(record: self)) + end + + # @param kind (see #get_field_groups) + def default_custom_field_group(kind = 'Post') + data = { name: 'Default Field Group', slug: '_default' } + get_field_groups(kind).where(slug: data[:slug]).first || add_custom_field_group(data, kind) + end + + # @param kind (see #get_field_groups) + def add_field(data, settings, kind = 'Post') + default_custom_field_group(kind).add_manual_field(data, settings) + end + private # skip save_metas_options callback after save changes (inherit from taxonomy) to call from here manually def save_metas_options_skip diff --git a/app/models/camaleon_cms/site.rb b/app/models/camaleon_cms/site.rb index 6ecb567e7..33d3a2052 100644 --- a/app/models/camaleon_cms/site.rb +++ b/app/models/camaleon_cms/site.rb @@ -52,7 +52,7 @@ def post_tags # all main categories for this site def categories - CamaleonCms::Category.includes(:post_type_parent).where(post_type_parent: post_types.pluck(:id)) + CamaleonCms::Category.includes(:post_type).where(post_type: post_types.pluck(:id)) end # return all languages configured by the admin diff --git a/app/models/concerns/camaleon_cms/custom_fields_read.rb b/app/models/concerns/camaleon_cms/custom_fields_read.rb index 78aad0fa1..b13b37802 100644 --- a/app/models/concerns/camaleon_cms/custom_fields_read.rb +++ b/app/models/concerns/camaleon_cms/custom_fields_read.rb @@ -9,66 +9,24 @@ module CamaleonCms::CustomFieldsRead extend ActiveSupport::Concern has_many :field_groups, ->(object){where(object_class: object.class.to_s.parseCamaClass)}, :class_name => "CamaleonCms::CustomFieldGroup", foreign_key: :objectid end - # get custom field groups for current object # only: Post_type, Post, Category, PostTag, Widget, Site and a Custom model pre configured - # return collections CustomFieldGroup - # args: (Hash, used only for PostType Objects) - # kind: (Post (Default) | Category | PostTag | PostType). - # If kind = "Post" this will return all groups for all posts from current post type - # If kind = "Category" this will return all groups for all categories from current post type - # If kind = "PostTag" this will return all groups for all posttags from current post type - # If kind = "all" this will return all groups from current post type - # If kind = "post_type" this will return groups for all post_types # Sample: mypost.get_field_groups() ==> return fields for posts from parent posttype # Sample: mycat.get_field_groups() ==> return fields for categories from parent posttype # Sample: myposttag.get_field_groups() ==> return fields for posttags from parent posttype - # Sample: mypost_type.get_field_groups({kind: 'Post'}) => return custom fields for posts - # Sample: mypost_type.get_field_groups({kind: 'Category'}) => return custom fields for posts - # Sample: mypost_type.get_field_groups({kind: 'PostTag'}) => return custom fields for posts - def get_field_groups(args = {}) - args = args.is_a?(String) ? {kind: args, include_parent: false } : {kind: "Post", include_parent: false }.merge(args) + # @return collections CustomFieldGroup + def get_field_groups(_args = {}) class_name = self.class.to_s.parseCamaClass case class_name when 'Category','PostTag' - self.post_type.get_field_groups(class_name) - when 'Post' - if self.term_relationships.size.zero? && args[:cat_ids].nil? - CamaleonCms::CustomFieldGroup.where("(objectid = ? AND object_class = ?) OR (objectid = ? AND object_class = ?)", self.id || -1, class_name, self.post_type.id, "PostType_#{class_name}") - else - cat_ids = self.categories.map(&:id) - cat_ids += args[:cat_ids] unless args[:cat_ids].nil? - cat_ids += CamaleonCms::Category.find(cat_ids).map {|category| _category_parents_ids(category)}.flatten.uniq - CamaleonCms::CustomFieldGroup.where("(objectid = ? AND object_class = ?) OR - (objectid = ? AND object_class = ?) OR - (objectid IN (?) AND object_class = ?)", - self.id || -1, class_name, - self.post_type.id, "PostType_#{class_name}", - cat_ids, "Category_#{class_name}") - end + self.post_type.get_field_groups(kind: class_name) when 'NavMenuItem' self.main_menu.custom_field_groups - when 'PostType' - if args[:kind] == "all" - CamaleonCms::CustomFieldGroup.where(object_class: ["PostType_Post", "PostType_Post", "PostType_PostTag", "PostType"], objectid: self.id ) - elsif args[:kind] == "post_type" - self.custom_field_groups - else - CamaleonCms::CustomFieldGroup.where(object_class: "PostType_#{args[:kind]}", objectid: self.id ) - end else # 'Plugin' or other classes self.field_groups end end - # get custom field groups for current user - # return collections CustomFieldGroup - # site: site object - def get_user_field_groups(site) - site.custom_field_groups.where(object_class: self.class.to_s.parseCamaClass) - end - - # get custom field value # _key: custom field key # if value is not present, then return default @@ -147,53 +105,34 @@ def get_fields_object(f=true) # add a custom field group for current model - # values: + # @param data (Hash) # name: name for the group # slug: key for group (if slug = _default => this will never show title and description) # description: description for the group (optional) # is_repeat: (boolean, optional -> default false) indicate if group support multiple format (repeated values) - # Model supported: PostType, Category, Post, Posttag, Widget, Plugin, Theme, User and Custom models pre configured - # Note 1: If you need add fields for all post's or all categories, then you need to add the fields into the - # post_type.add_custom_field_group(values, kind = "Post") - # post_type.add_custom_field_group(values, kind = "Category") - # Note 2: If you need add fields for only the Post_type, you have to use options or metas - # return: CustomFieldGroup object - # kind: argument only for PostType model: (Post | Category | PostTag), default => Post. If kind = "" this will add group for all post_types - def add_custom_field_group(values, kind = "Post") - values = values.with_indifferent_access - group = get_field_groups(kind).where(slug: values[:slug]).first - unless group.present? - site = _cama_get_field_site - values[:parent_id] = site.id if site.present? - if self.is_a?(CamaleonCms::Post) # harcoded for post to support custom field groups - group = CamaleonCms::CustomFieldGroup.where(object_class: "Post", objectid: self.id).create!(values) - else - group = get_field_groups(kind).create!(values) - end - end - group + def add_custom_field_group(data) + get_field_groups.create!(data.merge(record: self)) end alias_method :add_field_group, :add_custom_field_group + def default_custom_field_group + get_field_groups.where(slug: '_default').first || + add_custom_field_group(name: "Default Field Group", slug: "_default") + end + # Add custom fields for a default group: # This will create a new group with slug=_default if it doesn't exist yet # more details in add_manual_field(item, options) from custom field groups - # kind: argument only for PostType model: (Post | Category | PostTag), default => Post - def add_custom_field_to_default_group(item, options, kind = "Post") - g = get_field_groups(kind).where(slug: "_default").first - g = add_custom_field_group({name: "Default Field Group", slug: "_default"}, kind) unless g.present? - g.add_manual_field(item, options) + def add_field(data, settings) + default_custom_field_group.add_manual_field(data, settings) end - alias_method :add_field, :add_custom_field_to_default_group + alias_method :add_custom_field_to_default_group, :add_field # return field object for current model def get_field_object(slug) CamaleonCms::CustomField.where( slug: slug, parent_id: get_field_groups.pluck(:id), - ).first || CamaleonCms::CustomField.where( - slug: slug, - parent_id: get_field_groups({include_parent: true}) ).first end @@ -278,33 +217,10 @@ def _destroy_custom_field_groups if ['Category','Post','PostTag'].include?(class_name) CamaleonCms::CustomFieldGroup.where(objectid: self.id, object_class: class_name).destroy_all elsif ['PostType'].include?(class_name) - get_field_groups("Post").destroy_all - get_field_groups("Category").destroy_all - get_field_groups("PostTag").destroy_all + get_field_groups(kind: 'all').destroy_all elsif ["NavMenuItem"].include?(class_name) # menu items doesn't include field groups else get_field_groups().destroy_all if get_field_groups.present? end end - # return the Site Model owner of current model - def _cama_get_field_site - case self.class.to_s.parseCamaClass - when 'Category','Post','PostTag' - self.post_type.site - when 'Site' - self - else - self.site - end - end - - def _category_parents_ids(category) - @parents ||= [] - if category.parent.nil? - return @parents - else - @parents << category.parent.id - _category_parents_ids(category.parent) - end - end end diff --git a/app/models/concerns/camaleon_cms/user_methods.rb b/app/models/concerns/camaleon_cms/user_methods.rb index 9988670f1..33d1d8033 100644 --- a/app/models/concerns/camaleon_cms/user_methods.rb +++ b/app/models/concerns/camaleon_cms/user_methods.rb @@ -97,6 +97,10 @@ def send_confirm_email end # end auth + def get_field_groups(site) + site.custom_field_groups.where(object_class: self.class.to_s.parseCamaClass) + end + private def cama_before_validation self.role = PluginRoutes.system_info["default_user_role"] if self.role.blank? diff --git a/app/views/camaleon_cms/admin/users/form.html.erb b/app/views/camaleon_cms/admin/users/form.html.erb index a12de2076..f714e0478 100644 --- a/app/views/camaleon_cms/admin/users/form.html.erb +++ b/app/views/camaleon_cms/admin/users/form.html.erb @@ -95,7 +95,7 @@ - <%= render partial: "camaleon_cms/admin/settings/custom_fields/render", locals: {record: @user, field_groups: @user.get_user_field_groups(current_site) } %> + <%= render partial: "camaleon_cms/admin/settings/custom_fields/render", locals: {record: @user, field_groups: @user.get_field_groups(current_site) } %>