Sha256: 7a4a1ff60963fbc0db4459301f6be3508cc210a1a5d96a5daf7065f203267e14

Contents?: true

Size: 1.64 KB

Versions: 5

Compression:

Stored size: 1.64 KB

Contents

# frozen_string_literal: true

require "dry/schema/params"

module Dry
  module Rails
    module Features
      # SafeParams controller feature
      #
      # @api public
      module SafeParams
        # @api private
        def self.included(klass)
          super
          klass.extend(ClassMethods)

          klass.class_eval do
            before_action(:set_safe_params, prepend: true)
          end
        end

        # ApplicationController methods
        #
        # @api public
        module ClassMethods
          # Define a schema for controller action(s)
          #
          # @param actions [Array<Symbol>]
          #
          # @return [self]
          #
          # @api public
          def schema(*actions, &block)
            schema = Dry::Schema.Params(&block)

            actions.each do |name|
              schemas[name] = schema
            end

            self
          end

          # Return registered schemas
          #
          # @api private
          def schemas
            @schemas ||= {}
          end
        end

        # Return schema result
        #
        # @return [Dry::Schema::Result]
        #
        # @api public
        def safe_params
          @safe_params
        end

        # Return registered action schemas
        #
        # @return [Hash<Symbol => Dry::Schema::Params]
        #
        # @api public
        def schemas
          self.class.schemas
        end

        private

        # @api private
        def set_safe_params
          schema = schemas[action_name.to_sym]

          return unless schema

          @safe_params = schema.(request.params)
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
dry-rails-0.5.0 lib/dry/rails/features/safe_params.rb
dry-rails-0.4.0 lib/dry/rails/features/safe_params.rb
dry-rails-0.3.0 lib/dry/rails/features/safe_params.rb
dry-rails-0.2.1 lib/dry/rails/features/safe_params.rb
dry-rails-0.2.0 lib/dry/rails/features/safe_params.rb