Sha256: 430b17958aad247e78c839d58f768ea71d2f166dab97a105f0b4c36a270b48ef

Contents?: true

Size: 1.57 KB

Versions: 6

Compression:

Stored size: 1.57 KB

Contents

module MarkMapper
  module Plugins
    module Accessible
      extend ActiveSupport::Concern

      included do
        class_attribute :_accessible_attributes
      end

      module ClassMethods
        def attr_accessible(*attrs)
          raise AccessibleOrProtected.new(name) if try(:protected_attributes?)
          self._accessible_attributes = Set.new(attrs) + (_accessible_attributes || [])
        end

        def accessible_attributes(*)
          _accessible_attributes
        end

        def accessible_attributes?
          _accessible_attributes?
        end
      end

      def attributes=(attrs={})
        super(filter_inaccessible_attrs(attrs))
      end

      def update_attributes(attrs={})
        super(filter_inaccessible_attrs(attrs))
      end

      def update_attributes!(attrs={})
        super(filter_inaccessible_attrs(attrs))
      end

      def accessible_attributes(*args)
        self.class.accessible_attributes(*args)
      end

      def accessible_attributes?
        self.class.accessible_attributes?
      end

      protected
        def filter_inaccessible_attrs(attrs)
          return attrs if !accessible_attributes? || attrs.blank?
          attrs.dup.delete_if { |key, val| attribute_inaccessible?(key.to_sym) }
        end

        def attribute_inaccessible?(attribute)
          unless accessible_attributes.include?(attribute)
            message = "Can't mass-assign protected attribute: #{attribute}"
            MarkMapper.logger ? MarkMapper.logger.warn(message) : puts(message)

            return true
          end
        end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
mark_mapper-0.0.6 lib/mark_mapper/plugins/accessible.rb
mark_mapper-0.0.5 lib/mark_mapper/plugins/accessible.rb
mark_mapper-0.0.4 lib/mark_mapper/plugins/accessible.rb
mark_mapper-0.0.3 lib/mark_mapper/plugins/accessible.rb
mark_mapper-0.0.2 lib/mark_mapper/plugins/accessible.rb
mark_mapper-0.0.1 lib/mark_mapper/plugins/accessible.rb