Sha256: 530002ee5cf2dacce0c352cd6a908bc070472dfa5359dabf3dde32036a621e2d

Contents?: true

Size: 1.08 KB

Versions: 6

Compression:

Stored size: 1.08 KB

Contents

# encoding: UTF-8
require 'set'

module MarkMapper
  module Plugins
    module Protected
      extend ActiveSupport::Concern

      included do
        class_attribute :protected_attributes
      end

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

        def key(*args)
          key = super
          attr_protected key.name.to_sym if key.options[:protected]
          key
        end
      end

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

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

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

      protected
        def filter_protected_attrs(attrs)
          return attrs if protected_attributes.blank? || attrs.blank?
          attrs.dup.delete_if { |key, val| protected_attributes.include?(key.to_sym) }
        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/protected.rb
mark_mapper-0.0.5 lib/mark_mapper/plugins/protected.rb
mark_mapper-0.0.4 lib/mark_mapper/plugins/protected.rb
mark_mapper-0.0.3 lib/mark_mapper/plugins/protected.rb
mark_mapper-0.0.2 lib/mark_mapper/plugins/protected.rb
mark_mapper-0.0.1 lib/mark_mapper/plugins/protected.rb