Permalink
Browse files

Do not call save on the model when only a few attributes are being ch…

…anged.
  • Loading branch information...
1 parent a1b365b commit 2c4e573976ea1970cacdbb1755d1777b0df7cbae @pupeno pupeno committed Jun 13, 2012
@@ -29,8 +29,7 @@ def update_failed_logins_count!(credentials)
# Resets the failed logins counter.
# Runs as a hook after a successful login.
def reset_failed_logins_count!(user, credentials)
- user.send(:"#{user_class.sorcery_config.failed_logins_count_attribute_name}=", 0)
- user.save!(:validate => false)
+ user.update_many_attributes(user_class.sorcery_config.failed_logins_count_attribute_name => 0)
end
end
end
@@ -8,11 +8,16 @@ def self.included(klass)
end
module InstanceMethods
- def update_single_attribute(name, value)
- self.send(:"#{name}=", value)
-
+ def update_many_attributes(attrs)
+ attrs.each do |name, value|
+ self.send(:"#{name}=", value)
+ end
primary_key = self.class.primary_key
- self.class.where(:"#{primary_key}" => self.send(:"#{primary_key}")).update_all(name => value)
+ self.class.where(:"#{primary_key}" => self.send(:"#{primary_key}")).update_all(attrs)
+ end
+
+ def update_single_attribute(name, value)
+ update_many_attributes(name => value)
end
end
@@ -16,6 +16,10 @@ def increment(attr)
def save!(options = {})
save(options)
end
+
+ def update_many_attributes(attrs)
+ update_attributes(attrs)
+ end
end
module ClassMethods
@@ -11,11 +11,17 @@ module InstanceMethods
def increment(attr)
self.inc(attr,1)
end
-
+
+ def update_many_attributes(attrs)
+ attrs.each do |name, value|
+ attrs[name] = value.utc if value.is_a?(ActiveSupport::TimeWithZone)
+ self.send(:"#{name}=", value)
+ end
+ self.class.where(:_id => self.id).update_all(attrs)
+ end
+
def update_single_attribute(name, value)
- value = value.utc if value.is_a?(ActiveSupport::TimeWithZone)
- self.send(:"#{name}=", value)
- self.class.where(:_id => self.id).update_all(name => value)
+ update_many_attributes(name => value)
end
end
@@ -72,7 +72,7 @@ def register_failed_login!
config = sorcery_config
return if !unlocked?
self.increment(config.failed_logins_count_attribute_name)
- self.save!(:validate => false)
+ self.update_many_attributes(config.failed_logins_count_attribute_name => self.send(config.failed_logins_count_attribute_name))
self.lock! if self.send(config.failed_logins_count_attribute_name) >= config.consecutive_login_retries_amount_limit
end
@@ -81,23 +81,23 @@ def register_failed_login!
# /!\
def unlock!
config = sorcery_config
- self.send(:"#{config.lock_expires_at_attribute_name}=", nil)
- self.send(:"#{config.failed_logins_count_attribute_name}=", 0)
- self.send(:"#{config.unlock_token_attribute_name}=", nil) unless config.unlock_token_mailer_disabled or config.unlock_token_mailer.nil?
- self.save!(:validate => false)
+ attributes = {config.lock_expires_at_attribute_name => nil,
+ config.failed_logins_count_attribute_name => 0}
+ attributes[config.unlock_token_attribute_name] = nil unless config.unlock_token_mailer_disabled or config.unlock_token_mailer.nil?
+ self.update_many_attributes(attributes)
end
protected
def lock!
config = sorcery_config
- self.send(:"#{config.lock_expires_at_attribute_name}=", Time.now.in_time_zone + config.login_lock_time_period)
+ attributes = {config.lock_expires_at_attribute_name => Time.now.in_time_zone + config.login_lock_time_period}
unless config.unlock_token_mailer_disabled || config.unlock_token_mailer.nil?
- self.send(:"#{config.unlock_token_attribute_name}=", TemporaryToken.generate_random_token)
+ attributes[config.unlock_token_attribute_name] = TemporaryToken.generate_random_token
send_unlock_token_email!
end
- self.save!(:validate => false)
+ self.update_many_attributes(attributes)
end
def unlocked?
@@ -49,17 +49,15 @@ module InstanceMethods
# You shouldn't really use this one yourself - it's called by the controller's 'remember_me!' method.
def remember_me!
config = sorcery_config
- self.send(:"#{config.remember_me_token_attribute_name}=", TemporaryToken.generate_random_token)
- self.send(:"#{config.remember_me_token_expires_at_attribute_name}=", Time.now.in_time_zone + config.remember_me_for)
- self.save!(:validate => false)
+ self.update_many_attributes(config.remember_me_token_attribute_name => TemporaryToken.generate_random_token,
+ config.remember_me_token_expires_at_attribute_name => Time.now.in_time_zone + config.remember_me_for)
end
# You shouldn't really use this one yourself - it's called by the controller's 'forget_me!' method.
def forget_me!
config = sorcery_config
- self.send(:"#{config.remember_me_token_attribute_name}=", nil)
- self.send(:"#{config.remember_me_token_expires_at_attribute_name}=", nil)
- self.save!(:validate => false)
+ self.update_many_attributes(config.remember_me_token_attribute_name => nil,
+ config.remember_me_token_expires_at_attribute_name => nil)
end
end
end
@@ -96,11 +96,11 @@ def deliver_reset_password_instructions!
config = sorcery_config
# hammering protection
return false if config.reset_password_time_between_emails && self.send(config.reset_password_email_sent_at_attribute_name) && self.send(config.reset_password_email_sent_at_attribute_name) > config.reset_password_time_between_emails.ago.utc
- self.send(:"#{config.reset_password_token_attribute_name}=", TemporaryToken.generate_random_token)
- self.send(:"#{config.reset_password_token_expires_at_attribute_name}=", Time.now.in_time_zone + config.reset_password_expiration_period) if config.reset_password_expiration_period
- self.send(:"#{config.reset_password_email_sent_at_attribute_name}=", Time.now.in_time_zone)
+ attributes = {config.reset_password_token_attribute_name => TemporaryToken.generate_random_token,
+ config.reset_password_email_sent_at_attribute_name => Time.now.in_time_zone}
+ attributes[config.reset_password_token_expires_at_attribute_name] = Time.now.in_time_zone + config.reset_password_expiration_period if config.reset_password_expiration_period
self.class.transaction do
- self.save!(:validate => false)
+ self.update_many_attributes(attributes)
generic_send_email(:reset_password_email_method_name, :reset_password_mailer) unless config.reset_password_mailer_disabled
end
end

0 comments on commit 2c4e573

Please sign in to comment.