<commit_before># frozen_string_literal: true
# Prepares a non-ActiveRecord export object.
# Expects params like {model_fields: [:foo], users: [:name, :bar], comments: []}
class Export::Create < Trailblazer::Operation
  step :instantiate_model
  step Policy::Pundit(ExportPolicy, :create?)
  step :validate_and_sanitize_params
  step :set_requested_fields

  def instantiate_model(options, params:, **)
    object = params[:object_name].classify.constantize
    options['model'] =
      Export.new(object, GenericSortFilter.transform(object, params[:export]))
  end

  def validate_and_sanitize_params(options, params:, model:, **)
    validate_model_fields(options, params, model) &&
      clean_empty_field_sets(options, params)
  end

  def validate_model_fields(options, params, model)
    # TODO: doesn't validate association keys
    forbidden_fields = params[:export][:model_fields] - model.allowed_fields
    if forbidden_fields.empty?
      true
    else
      options['result.message'] =
        "Forbidden fields provided: #{forbidden_fields}"
      false
    end
  end

  def clean_empty_field_sets(options, params)
    # clean params of empty array entries
    params[:export].reject! { |_key, value| value.empty? }
    options['params'] = params
    true
  end

  def set_requested_fields(options, params:, **)
    options['model'].requested_fields = params[:export]
  end
end<commit_msg>clarat-org/clarat#1154 - export now uses all given params (e.g. filters) except :controller<commit_after># frozen_string_literal: true
# Prepares a non-ActiveRecord export object.
# Expects params like {model_fields: [:foo], users: [:name, :bar], comments: []}
class Export::Create < Trailblazer::Operation
  step :instantiate_model
  step Policy::Pundit(ExportPolicy, :create?)
  step :validate_and_sanitize_params
  step :set_requested_fields

  def instantiate_model(options, params:, **)
    object = params[:object_name].classify.constantize
    options['model'] =
      Export.new(
        object,
        GenericSortFilter.transform(object, params.except(:controller))
      )
  end

  def validate_and_sanitize_params(options, params:, model:, **)
    validate_model_fields(options, params, model) &&
      clean_empty_field_sets(options, params)
  end

  def validate_model_fields(options, params, model)
    # TODO: doesn't validate association keys
    forbidden_fields = params[:export][:model_fields] - model.allowed_fields
    if forbidden_fields.empty?
      true
    else
      options['result.message'] =
        "Forbidden fields provided: #{forbidden_fields}"
      false
    end
  end

  def clean_empty_field_sets(options, params)
    # clean params of empty array entries
    params[:export].reject! { |_key, value| value.empty? }
    options['params'] = params
    true
  end

  def set_requested_fields(options, params:, **)
    options['model'].requested_fields = params[:export]
  end
end
{'fraction_non_alphanumeric': 0.1097770154373928, 'fraction_numerical': 0.0013722126929674098, 'mean_word_length': 4.360294117647059, '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': '6692368', 'n_tokens_mistral': 896, 'n_tokens_neox': 875, 'n_words': 204}