Sha256: 7cf0910a6fae9ddeacbabc0b347c148665321052ea8ec2911428103888f1f33c

Contents?: true

Size: 1.68 KB

Versions: 29

Compression:

Stored size: 1.68 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_keys = nil, json_keys = nil)
          _params = permitted_params[key]

          build_uploaded_files_from_params!(_params, file_keys) if file_keys

          json_params = json_params!(_params, json_keys || [])

          _attributes = policy(object_or_class).permitted_attributes

          ::ActionController::Parameters.new(_params).permit(*_attributes).tap do |strong_params|
            (json_keys || []).each { |k| strong_params[k] = json_params[k] }
          end
        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

        def json_params!(hash, keys)
          {}.tap do |json_params|
            keys.each { |key| json_params[key] = hash.delete(key) }
          end
        end

      end

    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
locomotivecms-4.2.0.alpha2 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-4.2.0.alpha1 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-4.1.1 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-4.1.0 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-4.1.0.rc1 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-4.0.3 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-4.0.2 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-4.0.1 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-4.0.0 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-4.0.0.rc0 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-4.0.0.alpha3 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-3.4.1 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-4.0.0.alpha2 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-4.0.0.alpha1 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-3.4.0 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-3.3.0 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-3.3.0.rc3 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-3.3.0.rc2 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-3.1.2 app/api/locomotive/api/helpers/params_helper.rb
locomotivecms-3.2.1 app/api/locomotive/api/helpers/params_helper.rb