<commit_before># encoding: utf-8
module Cms
  class ContentNodesController < ApplicationController
    include Cms::ControllerHelpers::ContentNodes
    include Cms::ControllerHelpers::Paginate

    helper_method :template_options
    helper_method :content_node_options

    before_action :load_object, only: [:show, :edit, :update, :destroy, :sort, :toggle_access]
    before_action :load_children, only: [:show]
    before_action :load_parent, only: [:new, :create, :update]
    before_action :load_assets, only: [:new, :edit, :create, :update]

    def index
      @content_nodes = unscoped.where(parent_id: nil)
    end

    def new
      type = params.delete(:type)
      @content_node = safe_new(type, content_node_types(@parent), parent_id: params[:parent_id])
      @content_node.load_attributes
      load_components
    end

    def create
      create_params = content_node_params
      type = create_params.delete('type')
      @content_node = safe_new(type, content_node_types(@parent))
      @content_node.load_attributes
      if @content_node.update_attributes(create_params)
        touch_parent_node
        redirect_to_parent_or_index
      else
        load_components
        render action: :new
      end
    end

    def edit
      @content_node.load_attributes
      @content_node.content_components.map(&:load_attributes)
      load_components
    end

    def update
      @content_node.load_attributes
      if destroy_params = params[:destroy]
        @content_node.destroy_content_attributes_including_components(destroy_params[:content_node])
      end
      if @content_node.update_attributes(content_node_params)
        touch_parent_node
        @content_node.touch
        @content_node.save
        redirect_to_parent_or_index
      else
        load_components
        render action: :edit
      end
    end

    def toggle_access
      touch_parent_node
      @content_node.update_attribute(:access, @content_node.public? ? 'private' : 'public')
    end

    def sort
      touch_parent_node
      @content_node.set_list_position(params[:position])
      render json: { status: :success }
    end

    def index_search
      @content_nodes = find_objects
      respond_to do |format|
        format.js { render :index_search }
      end
    end

    def destroy
      touch_parent_node
      @content_node.destroy
      flash.notice = I18n.t('cms.node_delete_success')
      redirect_to content_nodes_path
    end

    protected

    def touch_parent_node
      parent = Cms::ContentNode.unscoped.find_by(id: @content_node.parent_id)
      if parent.present?
        parent.touch
      end
    end

    def redirect_to_parent_or_index
      if @parent.present?
        redirect_to content_node_path(@parent)
      else
        redirect_to content_nodes_path
      end
    end

    def base_attrs
      [:title, :type, :parent_id, :name, :template, :meta_title, :meta_keywords, :meta_description, :url, :redirect, :access, content_category_ids: []]
    end

    def component_attrs
      return [] unless params[:content_node][:content_components_attributes].present?
      params[:content_node][:content_components_attributes].clone.map do |key, comp_attrs|
        type = comp_attrs[:type]
        node_type = params[:content_node][:type]
        klass = safe_type(type, content_component_types(node_type)).classify.constantize
        klass.sanitize_content_attributes + [:id, '_destroy', 'template']
      end.flatten + [:type]
    end

    def node_attrs
      params.require(:content_node).permit(base_attrs)
      type = params[:content_node][:type]
      safe_type(type, content_node_types(@parent)).classify.constantize.sanitize_content_attributes
    end

    def content_node_params
      permit = base_attrs + node_attrs + [ content_components_attributes: component_attrs ]
      params.require(:content_node).permit(permit)
    end

    def load_assets
      @images = ContentImage.page(params[:page]).per(assets_per_page)
      @documents = ContentDocument.page(params[:page]).per(assets_per_page)
    end

    def unscoped
      ContentNode.unscoped.order(position: :asc)
    end

    def load_components
      @components = content_components(@content_node)
      @components.map(&:load_attributes)
    end

    def load_parent
      parent_id = params[:parent_id] || (params[:content_node] || {})[:parent_id]
      if parent_id.present?
        @parent = unscoped.find(parent_id)
      end
    end

    def load_object
      @content_node = unscoped.with_relations.find(params[:id])
    end

    def load_children
      @children ||= unscoped.where(parent_id: load_object.id)
    end

    def find_objects
      query = unscoped.where(parent_id: params[:parent_id])
      if params[:q]
        queries = params[:q].gsub(' ', ',').split(',').map(&:strip).reject(&:blank?).map{|p| "%#{p}%" }
        queries.each do |q|
          query = query.where('title LIKE ? OR access LIKE ? OR name LIKE ?', q, q, q)
        end
      end
      query
    end
  end
end
<commit_msg>rails 5.1 use unsafe params for content_component_attributes<commit_after># encoding: utf-8
module Cms
  class ContentNodesController < ApplicationController
    include Cms::ControllerHelpers::ContentNodes
    include Cms::ControllerHelpers::Paginate

    helper_method :template_options
    helper_method :content_node_options

    before_action :load_object, only: [:show, :edit, :update, :destroy, :sort, :toggle_access]
    before_action :load_children, only: [:show]
    before_action :load_parent, only: [:new, :create, :update]
    before_action :load_assets, only: [:new, :edit, :create, :update]

    def index
      @content_nodes = unscoped.where(parent_id: nil)
    end

    def new
      type = params.delete(:type)
      @content_node = safe_new(type, content_node_types(@parent), parent_id: params[:parent_id])
      @content_node.load_attributes
      load_components
    end

    def create
      create_params = content_node_params
      type = create_params.delete('type')
      @content_node = safe_new(type, content_node_types(@parent))
      @content_node.load_attributes
      if @content_node.update_attributes(create_params)
        touch_parent_node
        redirect_to_parent_or_index
      else
        load_components
        render action: :new
      end
    end

    def edit
      @content_node.load_attributes
      @content_node.content_components.map(&:load_attributes)
      load_components
    end

    def update
      @content_node.load_attributes
      if destroy_params = params[:destroy]
        @content_node.destroy_content_attributes_including_components(destroy_params[:content_node])
      end
      if @content_node.update_attributes(content_node_params)
        touch_parent_node
        @content_node.touch
        @content_node.save
        redirect_to_parent_or_index
      else
        load_components
        render action: :edit
      end
    end

    def toggle_access
      touch_parent_node
      @content_node.update_attribute(:access, @content_node.public? ? 'private' : 'public')
    end

    def sort
      touch_parent_node
      @content_node.set_list_position(params[:position])
      render json: { status: :success }
    end

    def index_search
      @content_nodes = find_objects
      respond_to do |format|
        format.js { render :index_search }
      end
    end

    def destroy
      touch_parent_node
      @content_node.destroy
      flash.notice = I18n.t('cms.node_delete_success')
      redirect_to content_nodes_path
    end

    protected

    def touch_parent_node
      parent = Cms::ContentNode.unscoped.find_by(id: @content_node.parent_id)
      if parent.present?
        parent.touch
      end
    end

    def redirect_to_parent_or_index
      if @parent.present?
        redirect_to content_node_path(@parent)
      else
        redirect_to content_nodes_path
      end
    end

    def base_attrs
      [:title, :type, :parent_id, :name, :template, :meta_title, :meta_keywords, :meta_description, :url, :redirect, :access, content_category_ids: []]
    end

    def component_attrs
      return [] unless params[:content_node][:content_components_attributes].present?
      params[:content_node][:content_components_attributes].to_unsafe_h.clone.map do |key, comp_attrs|
        type = comp_attrs[:type]
        node_type = params[:content_node][:type]
        klass = safe_type(type, content_component_types(node_type)).classify.constantize
        klass.sanitize_content_attributes + [:id, '_destroy', 'template']
      end.flatten + [:type]
    end

    def node_attrs
      params.require(:content_node).permit(base_attrs)
      type = params[:content_node][:type]
      safe_type(type, content_node_types(@parent)).classify.constantize.sanitize_content_attributes
    end

    def content_node_params
      permit = base_attrs + node_attrs + [ content_components_attributes: component_attrs ]
      params.require(:content_node).permit(permit)
    end

    def load_assets
      @images = ContentImage.page(params[:page]).per(assets_per_page)
      @documents = ContentDocument.page(params[:page]).per(assets_per_page)
    end

    def unscoped
      ContentNode.unscoped.order(position: :asc)
    end

    def load_components
      @components = content_components(@content_node)
      @components.map(&:load_attributes)
    end

    def load_parent
      parent_id = params[:parent_id] || (params[:content_node] || {})[:parent_id]
      if parent_id.present?
        @parent = unscoped.find(parent_id)
      end
    end

    def load_object
      @content_node = unscoped.with_relations.find(params[:id])
    end

    def load_children
      @children ||= unscoped.where(parent_id: load_object.id)
    end

    def find_objects
      query = unscoped.where(parent_id: params[:parent_id])
      if params[:q]
        queries = params[:q].gsub(' ', ',').split(',').map(&:strip).reject(&:blank?).map{|p| "%#{p}%" }
        queries.each do |q|
          query = query.where('title LIKE ? OR access LIKE ? OR name LIKE ?', q, q, q)
        end
      end
      query
    end
  end
end

{'fraction_non_alphanumeric': 0.09422943680095021, 'fraction_numerical': 0.0007918440067306741, 'mean_word_length': 3.1460812474353714, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5443100', 'n_tokens_mistral': 3094, 'n_tokens_neox': 3040, 'n_words': 598}