Sha256: 2a492526f4f0668822843b9de64bffb2cc63bc190cf933f42466ffb094107df7

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

require 'active_support/deprecation'
require 'active_support/core_ext/class/attribute'

module ActiveFormModel
  module Permittable
    extend ActiveSupport::Concern

    included do
      class_attribute(:_permitted_args, instance_predicate: false, default: [])
    end

    class_methods do
      def new(attrs = nil, &block)
        attrs = _permit_attrs(attrs) if attrs

        super
      end

      def permit(*args)
        self._permitted_args = _permitted_args | args
      end

      alias_method :fields, :permit
      deprecate fields: :permit, deprecator: ActiveSupport::Deprecation.new('0.6.0', 'ActiveFormModel')

      def _permit_attrs(attrs)
        attrs.respond_to?(:permit) ? attrs.send(:permit, _permitted_args) : attrs
      end
    end

    def update(attrs = {})
      permitted_attrs = permit_attrs(attrs)
      super(permitted_attrs)
    end

    def update!(attrs = {})
      permitted_attrs = permit_attrs(attrs)
      super(permitted_attrs)
    end

    def assign_attributes(attrs = {})
      permitted_attrs = permit_attrs(attrs)
      super(permitted_attrs)
    end

    private

    def permit_attrs(attrs)
      self.class._permit_attrs(attrs)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_form_model-0.5.1 lib/active_form_model/permittable.rb