<commit_before>module Blacksand
  class Dashboard::PagesController < Dashboard::BaseController
    before_action :set_page, only: [:show, :edit, :update, :destroy, :onchange_edit, :get_prototype_id]
    before_action :set_select, only: [:new, :edit, :onchange_new, :onchange_edit, :create, :update]

    def default
      redirect_to pages_url
    end

    def index
      @page = Page.where(id: params[:parent_id]).first
      @parent_id = params[:parent_id] || 0
      @all_children = Page.where(parent_id: nil).order(:position)
      @pages = @all_children.page(params[:page])

      # 编辑只能看到自己负责的板块
      tree_nodes = if false && current_user.role == 'editor'
                     Page.tree_nodes(current_user.page_ids)
                   else
                     Page.tree_nodes
                   end

      gon.pages = [{
                   text: '网站',
                   href: children_partial_pages_path(parent_id: nil),
                   page_id: 0,
                   nodes: tree_nodes,
                   }]

      # 选中父页面
      Page.selectNode(gon.pages, @parent_id.to_i)
    end

    def search
      # TODO: 查询排除自己
      @q = Page.ransack(params[:q])
      @pages = @q.result.page(params[:page])
    end

    def children_partial
      @page = Page.where(id: params[:parent_id]).first
      @parent_id = params[:parent_id]
      @parent_page = Page.find(params[:parent_id]) if params[:parent_id]
      @q = Page.where(parent_id: params[:parent_id]).order(:position, created_at: :desc).ransack(params[:q])
      @all_children = @q.result
      @pages = @all_children.page(params[:page])
    end

    def show
    end

    def new
      @page = Page.new
      if params["parent_id"].present?
        @parent_id = params[:parent_id]
        parent_page = Page.find @parent_id
        @page.parent_id = params["parent_id"]

        # Set default template
        if parent_page.preferred_child_template_name
          @page.template = Template.find_by(name: parent_page.preferred_child_template_name)
        end

        # Set default prototype
        if parent_page.preferred_child_prototype_name
          prototype = Prototype.find_by(name: parent_page.preferred_child_prototype_name)
          @page.prototype = prototype
          prototype.fields.each do |field|
            Property.build_property(@page, field)
          end
        end
      end
    end<commit_msg>Fix n+1 query<commit_after>module Blacksand
  class Dashboard::PagesController < Dashboard::BaseController
    before_action :set_page, only: [:show, :edit, :update, :destroy, :onchange_edit, :get_prototype_id]
    before_action :set_select, only: [:new, :edit, :onchange_new, :onchange_edit, :create, :update]

    def default
      redirect_to pages_url
    end

    def index
      @page = Page.where(id: params[:parent_id]).first
      @parent_id = params[:parent_id] || 0
      @all_children = Page.where(parent_id: nil).order(:position)
      @pages = @all_children.page(params[:page])

      # 编辑只能看到自己负责的板块
      tree_nodes = if false && current_user.role == 'editor'
                     Page.tree_nodes(current_user.page_ids)
                   else
                     Page.tree_nodes
                   end

      gon.pages = [{
                   text: '网站',
                   href: children_partial_pages_path(parent_id: nil),
                   page_id: 0,
                   nodes: tree_nodes,
                   }]

      # 选中父页面
      Page.selectNode(gon.pages, @parent_id.to_i)
    end

    def search
      # TODO: 查询排除自己
      @q = Page.ransack(params[:q])
      @pages = @q.result.page(params[:page])
    end

    def children_partial
      @page = Page.where(id: params[:parent_id]).first
      @parent_id = params[:parent_id]
      @parent_page = Page.find(params[:parent_id]) if params[:parent_id]
      @q = Page.includes(:template, :prototype).where(parent_id: params[:parent_id]).order(:position, created_at: :desc).ransack(params[:q])
      @all_children = @q.result
      @pages = @all_children.page(params[:page])
    end

    def show
    end

    def new
      @page = Page.new
      if params["parent_id"].present?
        @parent_id = params[:parent_id]
        parent_page = Page.find @parent_id
        @page.parent_id = params["parent_id"]

        # Set default template
        if parent_page.preferred_child_template_name
          @page.template = Template.find_by(name: parent_page.preferred_child_template_name)
        end

        # Set default prototype
        if parent_page.preferred_child_prototype_name
          prototype = Prototype.find_by(name: parent_page.preferred_child_prototype_name)
          @page.prototype = prototype
          prototype.fields.each do |field|
            Property.build_property(@page, field)
          end
        end
      end
    end
{'fraction_non_alphanumeric': 0.10623700623700624, 'fraction_numerical': 0.0010395010395010396, 'mean_word_length': 2.334026334026334, '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': '5136475', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1493, 'n_words': 280}