<commit_before># frozen_string_literal: true

module GobiertoAdmin
  class AdminGroupForm < BaseForm
    PERMISSION_TYPES = {
      site_options: { scope: :site_options_permissions, attribute: :resource_name },
      modules_actions: { scope: :modules_permissions, attribute: :resource_name, action_names: [:manage, :edit, :moderate] },
      people: { scope: :people_permissions, attribute: :resource_id, parent_type: :modules_actions, parent: :gobierto_people, allow_all: true }
    }.freeze

    attr_accessor(
      :id,
      :name,
      :site_id,
      :parsed_attributes,
      :permissions_configuration
    )

    attr_reader :permissions

    delegate :persisted?, to: :admin_group

    validates :name, :site, presence: true

    def initialize(attributes = {})
      @parsed_attributes = attributes.to_h.with_indifferent_access
      PERMISSION_TYPES.each do |permission_type, options|
        next unless options[:attribute_parse_method] && @parsed_attributes[permission_type].present?

        @parsed_attributes[permission_type] = @parsed_attributes[permission_type].map { |attribute| attribute.send(options[:attribute_parse_method]) }
      end

      super(parsed_attributes.except(*permissions_keys))
      set_permissions
      set_all_permissions
    end

    def save
      @new_record = admin_group.new_record?

      return false unless valid?

      save_admin_group
    end

    def admin_group
      @admin_group ||= AdminGroup.find_by(id: id).presence || build_admin_group
    end

    def action_names(permission_type)
      return unless PERMISSION_TYPES.has_key? permission_type

      PERMISSION_TYPES[permission_type][:action_names] || [:manage]
    end

    private

    def site
      Site.find_by(id: site_id)
    end

    def permissions_keys
      PERMISSION_TYPES.keys + PERMISSION_TYPES.select { |_, options| options[:allow_all] }.keys.map { |permission_type| :"all_#{permission_type}" }
    end

    def build_admin_group
      AdminGroup.new(site_id: site_id)
    end

    def new_record?
      @new_record
    end

    def action_names_from_attributes(options)
      if options[:parent] && parsed_attributes[options[:parent_type]].is_a?(Hash) && parsed_attributes[options[:parent_type]][options[:parent]].present?<commit_msg>Allow config actions names per module. ['manage'] by default<commit_after># frozen_string_literal: true

module GobiertoAdmin
  class AdminGroupForm < BaseForm
    PERMISSION_TYPES = {
      site_options: { scope: :site_options_permissions, attribute: :resource_name },
      modules_actions: { scope: :modules_permissions, attribute: :resource_name, action_names: { gobierto_plans: [:manage, :edit, :moderate] } },
      people: { scope: :people_permissions, attribute: :resource_id, parent_type: :modules_actions, parent: :gobierto_people, allow_all: true }
    }.freeze

    attr_accessor(
      :id,
      :name,
      :site_id,
      :parsed_attributes,
      :permissions_configuration
    )

    attr_reader :permissions

    delegate :persisted?, to: :admin_group

    validates :name, :site, presence: true

    def initialize(attributes = {})
      @parsed_attributes = attributes.to_h.with_indifferent_access
      PERMISSION_TYPES.each do |permission_type, options|
        next unless options[:attribute_parse_method] && @parsed_attributes[permission_type].present?

        @parsed_attributes[permission_type] = @parsed_attributes[permission_type].map { |attribute| attribute.send(options[:attribute_parse_method]) }
      end

      super(parsed_attributes.except(*permissions_keys))
      set_permissions
      set_all_permissions
    end

    def save
      @new_record = admin_group.new_record?

      return false unless valid?

      save_admin_group
    end

    def admin_group
      @admin_group ||= AdminGroup.find_by(id: id).presence || build_admin_group
    end

    def action_names(permission_type, resource_name = nil)
      return unless PERMISSION_TYPES.has_key? permission_type

      PERMISSION_TYPES.dig(permission_type, :action_names, resource_name.to_sym) || [:manage]
    end

    private

    def site
      Site.find_by(id: site_id)
    end

    def permissions_keys
      PERMISSION_TYPES.keys + PERMISSION_TYPES.select { |_, options| options[:allow_all] }.keys.map { |permission_type| :"all_#{permission_type}" }
    end

    def build_admin_group
      AdminGroup.new(site_id: site_id)
    end

    def new_record?
      @new_record
    end

    def action_names_from_attributes(options)
      if options[:parent] && parsed_attributes[options[:parent_type]].is_a?(Hash) && parsed_attributes[options[:parent_type]][options[:parent]].present?
{'fraction_non_alphanumeric': 0.09505376344086021, 'fraction_numerical': 0.0, 'mean_word_length': 3.8957894736842107, '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': '5516369', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1426, 'n_words': 272}