<commit_before>module HalActions::Actions
  def index
    respond_with index_collection, index_options
  end

  def show
    respond_with root_resource(show_resource), show_options
  end

  def create
    create_resource.tap do |res|
      consume_with_content_type! res
      authorize res
      res.save!
      respond_with root_resource(res), show_options
    end
  end

  def update
    update_resource.tap do |res|
      consume_with_content_type! res
      authorize res
      res.save!
      respond_with root_resource(res), show_options
    end
  end

  def destroy
    destroy_resource.tap do |res|
      authorize res
      res.destroy
      head :no_content
    end
  end

  private

  def destroy_redirect
    { action: 'index' }
  end

  def index_options
    valid_params_for_action(:index).tap do |options|
      options[:_keys] = options.keys
    end
  end

  def show_options
    valid_params_for_action(:show).tap do |options|
      options[:_keys] = options.keys
      if self.class.resource_representer
        options[:represent_with] = self.class.resource_representer
      end
    end
  end

  def valid_params_for_action(action)
    (params.slice(*self.class.valid_params_list(action)) || {}).tap do |p|
      p[:zoom] = zoom_param if zoom_param
    end
  end

  def zoom_param
    @zoom_param ||= begin
      if (zp = params[:zoom]) && zp.present?
        zp.split(',').map(&:strip).compact.sort
      end
    end
  end

  def root_resource(resource)
    resource.tap { |res| res.is_root_resource = true }
  end

  # TODO: Remove this method when we upgrade roar-rails
  # Background: https://github.com/apotonick/roar-rails/blob/a109b40/lib/roar/rails/controller_additions.rb#L27
  def consume_with_content_type!(model, options = {})
    type = request.content_type
    format = Mime::Type.lookup(type).try(:symbol)

    if format.blank?
      raise HalActions::Errors::UnsupportedMediaType.new(type)
    end

    parse_method = compute_parsing_method(format)
    representer = prepare_model_for(format, model, options)

    representer.send(parse_method, incoming_string, options)
    model
  end

  module ClassMethods
    attr_accessor :valid_params

    def allow_params(action, *params)
      self.valid_params ||= {}
      valid_params[action.to_sym] = Array(params).flatten
    end

    def valid_params_list(action)
      (valid_params || {})[action.to_sym]
    end
  end
end
<commit_msg>Fix namespace issue with paged collections in nested representers<commit_after>module HalActions::Actions
  def index
    respond_with index_collection, index_options
  end

  def show
    respond_with root_resource(show_resource), show_options
  end

  def create
    create_resource.tap do |res|
      consume_with_content_type! res
      authorize res
      res.save!
      respond_with root_resource(res), show_options
    end
  end

  def update
    update_resource.tap do |res|
      consume_with_content_type! res
      authorize res
      res.save!
      respond_with root_resource(res), show_options
    end
  end

  def destroy
    destroy_resource.tap do |res|
      authorize res
      res.destroy
      head :no_content
    end
  end

  private

  def destroy_redirect
    { action: 'index' }
  end

  def index_options
    valid_params_for_action(:index).tap do |options|
      options[:_keys] = options.keys
      # TODO: this isn't great
      options[:represent_with] = Api::PagedCollectionRepresenter
    end
  end

  def show_options
    valid_params_for_action(:show).tap do |options|
      options[:_keys] = options.keys
      if self.class.resource_representer
        options[:represent_with] = self.class.resource_representer
      end
    end
  end

  def valid_params_for_action(action)
    (params.slice(*self.class.valid_params_list(action)) || {}).tap do |p|
      p[:zoom] = zoom_param if zoom_param
    end
  end

  def zoom_param
    @zoom_param ||= begin
      if (zp = params[:zoom]) && zp.present?
        zp.split(',').map(&:strip).compact.sort
      end
    end
  end

  def root_resource(resource)
    resource.tap { |res| res.is_root_resource = true }
  end

  # TODO: Remove this method when we upgrade roar-rails
  # Background: https://github.com/apotonick/roar-rails/blob/a109b40/lib/roar/rails/controller_additions.rb#L27
  def consume_with_content_type!(model, options = {})
    type = request.content_type
    format = Mime::Type.lookup(type).try(:symbol)

    if format.blank?
      raise HalActions::Errors::UnsupportedMediaType.new(type)
    end

    parse_method = compute_parsing_method(format)
    representer = prepare_model_for(format, model, options)

    representer.send(parse_method, incoming_string, options)
    model
  end

  module ClassMethods
    attr_accessor :valid_params

    def allow_params(action, *params)
      self.valid_params ||= {}
      valid_params[action.to_sym] = Array(params).flatten
    end

    def valid_params_list(action)
      (valid_params || {})[action.to_sym]
    end
  end
end

{'fraction_non_alphanumeric': 0.08834134615384616, 'fraction_numerical': 0.0028044871794871795, 'mean_word_length': 3.1367025683512844, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '4547141', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1572, 'n_words': 348}