Sha256: 44e93714e5022b5d9410f79e72aaeab0e218af621eb83befe7fd7091624bc632

Contents?: true

Size: 1.02 KB

Versions: 1

Compression:

Stored size: 1.02 KB

Contents

module ActiveRecord
  module Modal
    extend ActiveSupport::Concern

    included do
      class_attribute :_modal_attributes
    end

    module ClassMethods
      def attr_modal(*attributes)
        self._modal_attributes = Set.new(attrs_map(*attributes)) + (_modal_attributes || [])
      end

      def modal_attributes
        modal_attributes_with_options.map { |attr| attr.is_a?(Hash) ? attr.keys.first : attr }
      end

      def modal_attributes_with_options
        _modal_attributes || allowed_modal_attributes
      end

      private

      def attrs_map(*attributes)
        options    = attributes.extract_options!
        attributes = attributes.map(&:to_s)

        options.stringify_keys!

        attributes.tap { |attr| attr.push options if options.any? }
      end

      def allowed_modal_attributes
        if respond_to?(:accessible_attributes)
          accessible_attributes.to_set.reject do |a|
            a.ends_with?('_attributes') || a.ends_with?('_ids')
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unico-training-7.8.0 lib/active_record/modal.rb