Skip to content

Issue with process_input function with Rails 5 Parameters #159

@fishbein

Description

@fishbein

In Rails 4, ActionController::Parameters subclassed HashWithIndifferentAccess so lib/attachinary/utils.rb process_input called the process_hash function successfully by first hitting the Array case which then would hit the Hash case to call process_hash.

Before process_input run:

[
    [0] {
            "public_id" => "1",
        "resource_type" => "raw",
              "version" => "123",
             "filename" => "file.xlsx"
    }
]

After:

[
    [0] #<Attachinary::File:0x00007fa5b51ca360> {
                          :id => nil,
          :attachinariable_id => nil,
        :attachinariable_type => nil,
                       :scope => "documents",
                   :public_id => "1",
                     :version => "123",
                       :width => nil,
                      :height => nil,
                      :format => nil,
               :resource_type => "raw",
                  :created_at => nil,
                  :updated_at => nil,
                    :filename => "file.xlsx",
                     :user_id => nil,
           :publicly_viewable => nil
    }
]

In Rails 5, ActionController::Parameters no longer subclasses HashWithIndifferentAccess so the switch statement in lib/attachinary/utils.rb process_input hits the else case and returns the same input data.
This is not the expected behavior as it should call to_h on the input in that scenario and re-call process_input.
Before process_input run:

[
    [0] {
            "public_id" => "1",
        "resource_type" => "raw",
              "version" => "123",
             "filename" => "file.xlsx"
    }
]

After:

[
    [0] {
            "public_id" => "1",
        "resource_type" => "raw",
              "version" => "123",
             "filename" => "file.xlsx"
    }
]

self.process_input:

def self.process_input(input, upload_options, scope=nil)
      case input
      when :blank?.to_proc
        nil
      when lambda { |e| e.respond_to?(:read) }
        upload_options.merge! resource_type: 'auto'
        upload_info = Cloudinary::Uploader.upload(input, upload_options)
        process_hash upload_info, scope
      when String
        process_json(input, scope)
      when Hash
        process_hash(input, scope) # hits here in Rails 4 after hitting Array
      when Array
        input = input.map{ |el| process_input(el, upload_options, scope) }.flatten.compact
        input = nil if input.empty?
        input
      else
        input # hits here in Rails 5 because ActionController::Parameters is not a Hash
      end
    end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions