Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with process_input function with Rails 5 Parameters #159

Open
fishbein opened this issue Nov 29, 2018 · 0 comments
Open

Issue with process_input function with Rails 5 Parameters #159

fishbein opened this issue Nov 29, 2018 · 0 comments

Comments

@fishbein
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant