<commit_before>module Carnival
  class BaseAdminController < InheritedResources::Base
    respond_to :html, :json
    before_filter :authenticate_admin_user!


    def generate_datatable
      model_presenter = instantiate_presenter
      Carnival::GenericDatatable.new(view_context, instantiate_model(model_presenter), self, model_presenter, table_items)
    end

    def table_items
      nil
    end

    def index
      @datatable = generate_datatable
      @advanced_search = params["advanced_search"] if params["advanced_search"].present?

      if params[:special_scope].present?
        presenter = @datatable.presenter
        presenter.parse_special_scope params[:special_scope]
      end

      respond_to do |format|
        format.html do |render|
          render 'index' and return
        end
        format.json do |render|
          if params[:list_scope]
            render(json: @datatable.as_list) and return
          else
            render(json: @datatable) and return
          end
        end
        format.csv do
          render text: @datatable.as_csv.encode("utf-16le") and return
        end
        format.pdf do
          render :pdf => t("#{@datatable.model.to_s.underscore}.lista") , :template => 'carnival/index.pdf.haml',  :show_as_html => params[:debug].present? and return
        end
      end
    end

    def show
      @model_presenter = instantiate_presenter
      show! do |format|
        format.html do |render|
          render 'show' and return
        end
      end
    end

    def new
      @model_presenter = instantiate_presenter
      new! do |format|
        @model = instance_variable_get("@#{controller_name.classify.underscore}")
        format.html do |render|
          render 'new' and return
        end
      end
    end

    def edit
      @model_presenter = instantiate_presenter
      edit! do |format|
        @model = instance_variable_get("@#{controller_name.classify.underscore}")
        format.html do |render|
          render 'edit' and return
        end
      end
    end

    def create
      @model_presenter = instantiate_presenter
      create! do |success, failure|
        success.html{ redirect_to @model_presenter.model_path(:index), :notice => I18n.t("messages.created") and return}
        failure.html do |render|
          @model = instance_variable_get("@#{controller_name.classify.underscore}")
          render 'new' and return
        end
      end
    end

    def update
      @model_presenter = instantiate_presenter
      update! do |success, failure|
        success.html{ redirect_to @model_presenter.model_path(:index), :notice => I18n.t("messages.updated") and return}
        failure.html do |render|
          @model = instance_variable_get("@#{controller_name.classify.underscore}")
          render 'edit' and return
        end
      end
    end

    def render_popup partial
      @application_popup = partial
      render 'layouts/shared/render_popup' and return
    end

    def load_dependent_select_options
      presenter = params[:presenter].constantize.send(:new, :controller => self)
      model = presenter.relation_model(params[:field].gsub("_id", "").to_sym)
      @options = model.list_for_select(add_empty_option: true, query: ["#{params[:dependency_field]} = ?", params[:dependency_value]])
      render layout: nil
    end

    private

    def instantiate_model(presenter)
      presenter.full_model_name.classify.constantize
    end

    def instantiate_presenter<commit_msg>Fix render_popup<commit_after>module Carnival
  class BaseAdminController < InheritedResources::Base
    respond_to :html, :json
    before_filter :authenticate_admin_user!


    def generate_datatable
      model_presenter = instantiate_presenter
      Carnival::GenericDatatable.new(view_context, instantiate_model(model_presenter), self, model_presenter, table_items)
    end

    def table_items
      nil
    end

    def index
      @datatable = generate_datatable
      @advanced_search = params["advanced_search"] if params["advanced_search"].present?

      if params[:special_scope].present?
        presenter = @datatable.presenter
        presenter.parse_special_scope params[:special_scope]
      end

      respond_to do |format|
        format.html do |render|
          render 'index' and return
        end
        format.json do |render|
          if params[:list_scope]
            render(json: @datatable.as_list) and return
          else
            render(json: @datatable) and return
          end
        end
        format.csv do
          render text: @datatable.as_csv.encode("utf-16le") and return
        end
        format.pdf do
          render :pdf => t("#{@datatable.model.to_s.underscore}.lista") , :template => 'carnival/index.pdf.haml',  :show_as_html => params[:debug].present? and return
        end
      end
    end

    def show
      @model_presenter = instantiate_presenter
      show! do |format|
        format.html do |render|
          render 'show' and return
        end
      end
    end

    def new
      @model_presenter = instantiate_presenter
      new! do |format|
        @model = instance_variable_get("@#{controller_name.classify.underscore}")
        format.html do |render|
          render 'new' and return
        end
      end
    end

    def edit
      @model_presenter = instantiate_presenter
      edit! do |format|
        @model = instance_variable_get("@#{controller_name.classify.underscore}")
        format.html do |render|
          render 'edit' and return
        end
      end
    end

    def create
      @model_presenter = instantiate_presenter
      create! do |success, failure|
        success.html{ redirect_to @model_presenter.model_path(:index), :notice => I18n.t("messages.created") and return}
        failure.html do |render|
          @model = instance_variable_get("@#{controller_name.classify.underscore}")
          render 'new' and return
        end
      end
    end

    def update
      @model_presenter = instantiate_presenter
      update! do |success, failure|
        success.html{ redirect_to @model_presenter.model_path(:index), :notice => I18n.t("messages.updated") and return}
        failure.html do |render|
          @model = instance_variable_get("@#{controller_name.classify.underscore}")
          render 'edit' and return
        end
      end
    end

    def render_popup partial
      @application_popup = partial
      render '/carnival/shared/render_popup' and return
    end

    def load_dependent_select_options
      presenter = params[:presenter].constantize.send(:new, :controller => self)
      model = presenter.relation_model(params[:field].gsub("_id", "").to_sym)
      @options = model.list_for_select(add_empty_option: true, query: ["#{params[:dependency_field]} = ?", params[:dependency_value]])
      render layout: nil
    end

    private

    def instantiate_model(presenter)
      presenter.full_model_name.classify.constantize
    end

    def instantiate_presenter
{'fraction_non_alphanumeric': 0.09036402569593148, 'fraction_numerical': 0.0017130620985010706, 'mean_word_length': 2.6169334021683013, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, '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': '3624973', 'n_tokens_mistral': 2057, 'n_tokens_neox': 2002, 'n_words': 466}