Module: SudoAttributes::InstanceMethods

Defined in:
lib/sudo_attributes.rb

Instance Method Summary collapse

Instance Method Details

#sudo_assign_attributes(attributes) ⇒ Object

Used by sudo_attributes internally as a common API between Rails 3 and 3.1



94
95
96
97
98
99
100
# File 'lib/sudo_attributes.rb', line 94

def sudo_assign_attributes(attributes)
  if respond_to? :assign_attributes
    assign_attributes(attributes, :without_protection => true)
  else
    self.send(:attributes=, attributes, false)
  end
end

#sudo_update_attributes(new_attributes) ⇒ Object

Updates attributes of a model, including protected ones, from the passed-in hash and saves the record. If the object is invalid, the saving will fail and false will be returned.

Example

# Updated protected attributes on an instance of User
@user = User.find(params[:id])
@user.sudo_update_attributes(params[:user])


76
77
78
79
# File 'lib/sudo_attributes.rb', line 76

def sudo_update_attributes(new_attributes)
  sudo_assign_attributes(new_attributes)
  save
end

#sudo_update_attributes!(new_attributes) ⇒ Object

Updates its receiver just like sudo_update_attributes but calls save! instead of save, so an exception is raised if the record is invalid.

Example

# Updated protected attributes on an instance of User
@user = User.find(params[:id])
@user.sudo_update_attributes!(params[:user])


88
89
90
91
# File 'lib/sudo_attributes.rb', line 88

def sudo_update_attributes!(new_attributes)
  sudo_assign_attributes(new_attributes)
  save!
end