<commit_before># frozen_string_literal: true

class Ability
  include CanCan::Ability

  #
  # alias_action :index, :show, :to => :read
  # alias_action :new, :to => :create
  # alias_action :edit, :to => :update

  include Report

  attr_reader :user

  def initialize(user)
    if user
      @user = user
      alias_action :review_collection, :review_collection_users, :modify_collection, :review_collection, to: :manage_collection # Manage related object in the context of a collection, but not outside collection
      alias_action :show, :index, to: :read
      alias_action :read_location, :edit_location, to: :manage_location

      # default role settings
      send("initialize_#{user.role}")

      # specific cross role functionality
      message_rules

      # special additional roles
      role_manager

      cannot :manage, Collection, root: true
    end
  end

  def accessible_collection_ids
    user.accessible_collection_ids
  end

  # centralize store of fields editable per user; this array is used for sanctioning input parameters and filtering forms
  def editable_work_fields
    permitted_fields = []
    permitted_fields += [:location_detail, :location, :location_floor, :work_status_id] if can?(:edit_location, Work)
    permitted_fields += [:internal_comments] if can?(:write_internal_comments, Work)
    if can?(:edit_photos, Work)
      permitted_fields += [
        :photo_front, :photo_back, :photo_detail_1, :photo_detail_2,
        :remove_photo_front, :remove_photo_back, :remove_photo_detail_1, :remove_photo_detail_2
      ]
    end
    if can?(:edit, Work)
      permitted_fields += [
        :inventoried, :refound, :new_found,
        :locality_geoname_id, :imported_at, :import_collection_id, :stock_number, :alt_number_1, :alt_number_2, :alt_number_3,
        :artist_unknown, :title, :title_unknown, :description, :object_creation_year, :object_creation_year_unknown, :medium_id, :frame_type_id,
        :signature_comments, :no_signature_present, :print, :print_unknown, :frame_height, :frame_width, :frame_depth, :frame_diameter,
        :height, :width, :depth, :diameter, :condition_work_id, :condition_work_comments, :condition_frame_id, :condition_frame_comments,
        :information_back, :other_comments, :source_comments, :subset_id, :public_description,
        :grade_within_collection, :entry_status, :entry_status_description, :abstract_or_figurative, :medium_comments,
        :main_collection, :image_rights, :publish, :cluster_name, :collection_id, :cluster_id, :owner_id,
        :placeability_id, artist_ids: [], source_ids: [], damage_type_ids: [], frame_damage_type_ids: [], tag_list: [],
                          theme_ids: [], object_category_ids: [], technique_ids: [], artists_attributes: [
                            :_destroy, :first_name, :last_name, :prefix, :place_of_birth, :place_of_death, :year_of_birth, :year_of_death, :description
                          ]
      ]
    end
    if can?(:create, Appraisal)
      permitted_fields += [
        :selling_price, :minimum_bid, :purchase_price, :purchased_on, :purchase_year, :selling_price_minimum_bid_comments,
        appraisals_attributes: [
          :appraised_on, :market_value, :replacement_value, :market_value_range, :replacement_value_range, :appraised_by, :reference
        ]
      ]
    end
    permitted_fields
  end

  def editable_work_fields_grouped
    return @fields if @fields
    fields = {
      works_attributes: [],
      artists_attributes: [],
      appraisals_attributes: []
    }<commit_msg>allow for registering purchase price unit<commit_after># frozen_string_literal: true

class Ability
  include CanCan::Ability

  #
  # alias_action :index, :show, :to => :read
  # alias_action :new, :to => :create
  # alias_action :edit, :to => :update

  include Report

  attr_reader :user

  def initialize(user)
    if user
      @user = user
      alias_action :review_collection, :review_collection_users, :modify_collection, :review_collection, to: :manage_collection # Manage related object in the context of a collection, but not outside collection
      alias_action :show, :index, to: :read
      alias_action :read_location, :edit_location, to: :manage_location

      # default role settings
      send("initialize_#{user.role}")

      # specific cross role functionality
      message_rules

      # special additional roles
      role_manager

      cannot :manage, Collection, root: true
    end
  end

  def accessible_collection_ids
    user.accessible_collection_ids
  end

  # centralize store of fields editable per user; this array is used for sanctioning input parameters and filtering forms
  def editable_work_fields
    permitted_fields = []
    permitted_fields += [:location_detail, :location, :location_floor, :work_status_id] if can?(:edit_location, Work)
    permitted_fields += [:internal_comments] if can?(:write_internal_comments, Work)
    if can?(:edit_photos, Work)
      permitted_fields += [
        :photo_front, :photo_back, :photo_detail_1, :photo_detail_2,
        :remove_photo_front, :remove_photo_back, :remove_photo_detail_1, :remove_photo_detail_2
      ]
    end
    if can?(:edit, Work)
      permitted_fields += [
        :inventoried, :refound, :new_found,
        :locality_geoname_id, :imported_at, :import_collection_id, :stock_number, :alt_number_1, :alt_number_2, :alt_number_3,
        :artist_unknown, :title, :title_unknown, :description, :object_creation_year, :object_creation_year_unknown, :medium_id, :frame_type_id,
        :signature_comments, :no_signature_present, :print, :print_unknown, :frame_height, :frame_width, :frame_depth, :frame_diameter,
        :height, :width, :depth, :diameter, :condition_work_id, :condition_work_comments, :condition_frame_id, :condition_frame_comments,
        :information_back, :other_comments, :source_comments, :subset_id, :public_description,
        :grade_within_collection, :entry_status, :entry_status_description, :abstract_or_figurative, :medium_comments,
        :main_collection, :image_rights, :publish, :cluster_name, :collection_id, :cluster_id, :owner_id,
        :placeability_id, artist_ids: [], source_ids: [], damage_type_ids: [], frame_damage_type_ids: [], tag_list: [],
                          theme_ids: [], object_category_ids: [], technique_ids: [], artists_attributes: [
                            :_destroy, :first_name, :last_name, :prefix, :place_of_birth, :place_of_death, :year_of_birth, :year_of_death, :description
                          ]
      ]
    end
    if can?(:create, Appraisal)
      permitted_fields += [
        :selling_price, :minimum_bid, :purchase_price, :purchased_on, :purchase_year, :selling_price_minimum_bid_comments, :purchase_price_currency_id,
        appraisals_attributes: [
          :appraised_on, :market_value, :replacement_value, :market_value_range, :replacement_value_range, :appraised_by, :reference
        ]
      ]
    end
    permitted_fields
  end

  def editable_work_fields_grouped
    return @fields if @fields
    fields = {
      works_attributes: [],
      artists_attributes: [],
      appraisals_attributes: []
    }
{'fraction_non_alphanumeric': 0.09783368273934312, 'fraction_numerical': 0.0019566736547868622, 'mean_word_length': 4.014716187806587, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '3787616', 'n_tokens_mistral': 2227, 'n_tokens_neox': 2176, 'n_words': 482}