Sha256: 1b2e4ee5828727d7eae9336b90889d2a1da3a8bf8f7a0fd7d29372b49b662606

Contents?: true

Size: 1.33 KB

Versions: 7

Compression:

Stored size: 1.33 KB

Contents

module Locomotive
  module API
    module Helpers
      module ParamsHelper

        def permitted_params
          @permitted_params ||= declared(params, include_missing: false)
        end

        # Much safer than permitted_params because it also uses the current policy
        # (Pundit) to filter the parameters.
        #
        # Examples:
        #
        # permitted_params_from_policy(current_site, :site)
        #
        # If we want to deal with ActionDispatch::Http::UploadedFile instances
        # instead of hashes (which would break the permitted attributes policy).
        #
        # permitted_params_from_policy(current_site, :site, [:picture])
        #
        def permitted_params_from_policy(object_or_class, key, file_inputs = nil)
          _params = permitted_params[key]

          build_uploaded_files_from_params!(_params, file_inputs) if file_inputs

          _attributes = policy(object_or_class).permitted_attributes
          ::ActionController::Parameters.new(_params).permit(*_attributes)
        end

        def build_uploaded_files_from_params!(hash, list)
          list.each do |name|
            file_hash = hash[name]

            next unless file_hash.try(:has_key?, :tempfile)

            hash[name] = ActionDispatch::Http::UploadedFile.new(file_hash)
          end
        end

      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
locomotivecms-3.0.1 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-3.0.0 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-3.0.0.rc7 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-3.0.0.rc6 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-3.0.0.rc5 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-3.0.0.rc4 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-3.0.0.rc3 app/api/locomotive/api/helpers/params_helper.rb