Module: SafeAttributes::ClassMethods

Defined in:
lib/safe_attributes.rb

Instance Method Summary collapse

Instance Method Details

#bad_attribute_names(*attrs) ⇒ Object

Within your model call this method once with a list of methods matching either attribute() or attribute=() for attributes (column names) you do not want to create the the normal method for. You should not need to do this but the option is there in case the default list is inadequate



13
14
15
16
17
18
19
20
21
22
# File 'lib/safe_attributes.rb', line 13

def bad_attribute_names(*attrs)
    @bad_attribute_names ||= lambda {
        methods = Array.new(attrs.collect { |x| x.to_s })
        methods += ActiveRecord::Base.public_instance_methods
        methods += ActiveRecord::Base.protected_instance_methods
        methods += ActiveRecord::Base.private_instance_methods
        methods -= ['id']
        return methods
    }.call
end

#define_method_attribute(attr_name) ⇒ Object

Override the default implementation to not create the attribute() method if that method name is in the list of bad names



29
30
31
32
# File 'lib/safe_attributes.rb', line 29

def define_method_attribute(attr_name)
    return if (bad_attribute_names.include?(attr_name))
    super(attr_name)
end

#define_method_attribute=(attr_name) ⇒ Object

Override the default implementation to not create the attribute= method if that method name is in the list of bad names



39
40
41
42
43
# File 'lib/safe_attributes.rb', line 39

def define_method_attribute=(attr_name)
    method = attr_name + '='
    return if (bad_attribute_names.include?(method))
    super(attr_name)
end