Sha256: 2568e2a5e2a61b412b3f50fb4b86ad54a335e0563e6acbda2b2ff19cea02d4f1

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

module AttributeExt
  module SafeAttributes
    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods
      def safe_attributes(*attrs)
        @safe_attributes ||= []
        if attrs.empty?
          @safe_attributes
        else
          options = attrs.last.is_a?(Hash) ? attrs.pop : {}
          @safe_attributes << [attrs, options]
        end
      end
    end

    def safe_attribute_names(role = :default)
      names = []
      self.class.safe_attributes.collect do |attrs, options|
        if (options[:if].nil? || safe_attrs_call_block(options[:if], role)) &&        # if
            (options[:unless].nil? || !safe_attrs_call_block(options[:unless], role)) # unless
          names += attrs.collect(&:to_s)
        end
      end
      names.uniq
    end
    
    def safe_attrs_call_block(block, role)
      case block.arity
      when 0
        return block.call
      when 1
        return block.call(self)
      else
        return block.call(self, role)
      end
    end

    def mass_assignment_authorizer(role = nil)
      if role.nil?
        super + safe_attribute_names(:default)
      else
        super(role) + safe_attribute_names(role)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
attribute_ext-1.0.1 lib/attribute_ext/safe_attributes.rb
attribute_ext-1.0 lib/attribute_ext/safe_attributes.rb