Module: SafeAttributes::Base::ClassMethods

Defined in:
lib/safe_attributes/base.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.



16
17
18
19
20
21
22
23
24
25
# File 'lib/safe_attributes/base.rb', line 16

def bad_attribute_names(*attrs)
  @bad_attribute_names ||= lambda {
    methods = Array.new(attrs.collect { |x| x.to_sym })
    methods += ActiveRecord::Base.public_instance_methods.collect { |x| x.to_sym }
    methods += ActiveRecord::Base.protected_instance_methods.collect { |x| x.to_sym }
    methods += ActiveRecord::Base.private_instance_methods.collect { |x| x.to_sym }
    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



32
33
34
35
# File 'lib/safe_attributes/base.rb', line 32

def define_method_attribute(attr_name)
  return if (bad_attribute_names.include?(attr_name.to_sym))
  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



42
43
44
45
46
# File 'lib/safe_attributes/base.rb', line 42

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

#instance_method_already_implemented?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
53
54
55
# File 'lib/safe_attributes/base.rb', line 48

def instance_method_already_implemented?(method_name)
  begin 
    return super(method_name)
  rescue ActiveRecord::DangerousAttributeError
    return true
  end
  return false
end