<commit_before># frozen_string_literal: true

class Folio::Console::Index::ActionsCell < Folio::ConsoleCell
  def safe_url_for(opts)
    controller.url_for(opts)
  rescue StandardError
  end

  def default_actions
    if I18n.available_locales.size > 1
      locale = model.try(:locale) || I18n.default_locale
    else
      locale = nil
    end

    @default_actions ||= {
      destroy: {
        name: :destroy,
        icon: 'trash-alt',
        button: 'danger',
        method: :delete,
        confirm: true,
        url: safe_url_for([:console, model]),
      },
      discard: {
        name: :discard,
        icon: 'trash-alt',
        button: 'secondary',
        method: :delete,
        confirm: true,
        url: safe_url_for([:discard, :console, model]),
      },
      undiscard: {
        name: :undiscard,
        icon: 'redo-alt',
        button: 'secondary',
        method: :post,
        url: safe_url_for([:undiscard, :console, model]),
      },
      edit: {
        name: :edit,
        icon: 'edit',
        button: 'secondary',
        url: safe_url_for([:edit, :console, model]),
      },
      show: {
        name: :show,
        icon: 'eye',
        button: 'light',
        url: safe_url_for([:console, model]),
      },
      preview: {
        name: :preview,
        icon: 'eye',
        button: 'light',
        target: '_blank',
        url: safe_url_for([model, locale: locale]),
      },
      arrange: {
        name: :arrange,
        icon: 'list',
        button: 'light',
        url: nil,
      },
    }
  end

  def actions
    acts = []
    with_default = (options[:actions].presence || %i[edit destroy])

    sort_array_hashes_first(with_default).each do |sym_or_hash|
      if sym_or_hash.is_a?(Symbol)
        next if sym_or_hash == :destroy && model.class.try(:indestructible?)
        acts << default_actions[sym_or_hash]
      elsif sym_or_hash.is_a?(Hash)
        sym_or_hash.each do |name, obj|
          next if name == :destroy && model.class.try(:indestructible?)
          base = default_actions[name].presence || {}
          if obj.is_a?(Hash)
            acts << base.merge(obj)
          else
            acts << base.merge(url: obj)
          end
        end
      end
    end

    acts.map do |action|
      if action[:confirm]
        if action[:confirm].is_a?(String)
          confirmation = action[:confirm]
        else
          confirmation = t('folio.console.confirmation')
        end
      else
        confirmation = nil
      end

      opts = {
        title: t("folio.console.actions.#{action[:name]}"),
        class: "btn btn-#{action[:button]} fa fa-#{action[:icon]}",
        method: action[:method],
        target: action[:target],
        'data-confirm': confirmation,
      }

      begin
        link_to('', action[:url], opts)
      rescue ActionController::UrlGenerationError
      end
    end.compact
  end

  def sort_array_hashes_first(ary)
    ary.sort do |a, b|
      if a.is_a?(Hash) && default_actions.exclude?(a.keys.first)
        -1<commit_msg>optimize actions cell by using procs for urls<commit_after># frozen_string_literal: true

class Folio::Console::Index::ActionsCell < Folio::ConsoleCell
  def safe_url_for(opts)
    controller.url_for(opts)
  rescue StandardError
  end

  def default_actions
    if I18n.available_locales.size > 1
      locale = model.try(:locale) || I18n.default_locale
    else
      locale = nil
    end

    @default_actions ||= {
      destroy: {
        name: :destroy,
        icon: 'trash-alt',
        button: 'danger',
        method: :delete,
        confirm: true,
        url: -> { safe_url_for([:console, model]) },
      },
      discard: {
        name: :discard,
        icon: 'trash-alt',
        button: 'secondary',
        method: :delete,
        confirm: true,
        url: -> { safe_url_for([:discard, :console, model]) },
      },
      undiscard: {
        name: :undiscard,
        icon: 'redo-alt',
        button: 'secondary',
        method: :post,
        url: -> { safe_url_for([:undiscard, :console, model]) },
      },
      edit: {
        name: :edit,
        icon: 'edit',
        button: 'secondary',
        url: -> { safe_url_for([:edit, :console, model]) },
      },
      show: {
        name: :show,
        icon: 'eye',
        button: 'light',
        url: -> { safe_url_for([:console, model]) },
      },
      preview: {
        name: :preview,
        icon: 'eye',
        button: 'light',
        target: '_blank',
        url: -> { safe_url_for([model, locale: locale]) },
      },
      arrange: {
        name: :arrange,
        icon: 'list',
        button: 'light',
        url: nil,
      },
    }
  end

  def actions
    acts = []
    with_default = (options[:actions].presence || %i[edit destroy])

    sort_array_hashes_first(with_default).each do |sym_or_hash|
      if sym_or_hash.is_a?(Symbol)
        next if sym_or_hash == :destroy && model.class.try(:indestructible?)
        acts << default_actions[sym_or_hash]
      elsif sym_or_hash.is_a?(Hash)
        sym_or_hash.each do |name, obj|
          next if name == :destroy && model.class.try(:indestructible?)
          base = default_actions[name].presence || {}
          if obj.is_a?(Hash)
            acts << base.merge(obj)
          else
            acts << base.merge(url: obj)
          end
        end
      end
    end

    acts.map do |action|
      if action[:confirm]
        if action[:confirm].is_a?(String)
          confirmation = action[:confirm]
        else
          confirmation = t('folio.console.confirmation')
        end
      else
        confirmation = nil
      end

      opts = {
        title: t("folio.console.actions.#{action[:name]}"),
        class: "btn btn-#{action[:button]} fa fa-#{action[:icon]}",
        method: action[:method],
        target: action[:target],
        'data-confirm': confirmation,
      }

      begin
        url = action[:url].is_a?(Proc) ? action[:url].call : action[:url]
        link_to('', url, opts)
      rescue ActionController::UrlGenerationError
      end
    end.compact
  end

  def sort_array_hashes_first(ary)
    ary.sort do |a, b|
      if a.is_a?(Hash) && default_actions.exclude?(a.keys.first)
        -1
{'fraction_non_alphanumeric': 0.13645850056188794, 'fraction_numerical': 0.0019264729491090063, 'mean_word_length': 1.9937530033637674, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 11, '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': '3823751', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1968, 'n_words': 420}