Declare callbacks explicitly
authorTom Hughes <tom@compton.nu>
Fri, 3 Sep 2010 16:17:27 +0000 (17:17 +0100)
committerTom Hughes <tom@compton.nu>
Mon, 14 Nov 2011 09:42:43 +0000 (09:42 +0000)
app/models/changeset.rb
app/models/relation_member.rb
app/models/user.rb
app/models/user_token.rb
lib/geo_record.rb

index 8f9a1e6..3f6df27 100644 (file)
@@ -20,6 +20,8 @@ class Changeset < ActiveRecord::Base
   validates_numericality_of :user_id,  :integer_only => true
   validates_numericality_of :num_changes, :integer_only => true, :greater_than_or_equal_to => 0
 
+  before_save :update_closed_at
+
   # over-expansion factor to use when updating the bounding box
   EXPAND = 0.1
 
@@ -191,7 +193,7 @@ class Changeset < ActiveRecord::Base
   # set the auto-close time to be one hour in the future unless
   # that would make it more than 24h long, in which case clip to
   # 24h, as this has been decided is a reasonable time limit.
-  def before_save
+  def update_closed_at
     if self.is_open?
       if (closed_at - created_at) > (MAX_TIME_OPEN - IDLE_TIMEOUT)
         self.closed_at = created_at + MAX_TIME_OPEN
index fbaf89d..82cb4cc 100644 (file)
@@ -2,23 +2,26 @@ class RelationMember < ActiveRecord::Base
   set_table_name 'current_relation_members'
   
   set_primary_keys :id, :sequence_id
+
   belongs_to :member, :polymorphic => true
   belongs_to :relation, :foreign_key => :id
 
-  def after_find
-    self[:member_class] = self.member_type.classify
+  after_find :set_class_from_type
+  after_initialize :set_class_from_type
+  before_save :set_type_from_class
+
+  def member_type=(type)
+    self[:member_type] = type
+    self[:member_class] = type.capitalize
   end
 
-  def after_initialize
+private
+
+  def set_class_from_type
     self[:member_class] = self.member_type.classify unless self.member_type.nil?
   end
 
-  def before_save
+  def set_type_from_class
     self.member_type = self[:member_class].classify
   end
-
-  def member_type=(type)
-    self[:member_type] = type
-    self[:member_class] = type.capitalize
-  end
 end
index 0b2a902..80dad87 100644 (file)
@@ -36,21 +36,11 @@ class User < ActiveRecord::Base
   validates_numericality_of :home_zoom, :only_integer => true, :allow_nil => true
   validates_inclusion_of :preferred_editor, :in => Editors::ALL_EDITORS, :allow_nil => true
 
+  after_initialize :set_creation_time
   before_save :encrypt_password
 
   file_column :image, :magick => { :geometry => "100x100>" }
 
-  def after_initialize
-    self.creation_time = Time.now.getutc unless self.attribute_present?(:creation_time)
-  end
-
-  def encrypt_password
-    if pass_crypt_confirmation
-      self.pass_salt = OSM::make_token(8)
-      self.pass_crypt = OSM::encrypt_password(pass_crypt, pass_salt)
-    end
-  end
-
   def self.authenticate(options)
     if options[:username] and options[:password]
       user = find(:first, :conditions => ["email = ? OR display_name = ?", options[:username], options[:username]])
@@ -210,4 +200,17 @@ class User < ActiveRecord::Base
   def access_token(application_key)
     return ClientApplication.find_by_key(application_key).access_token_for_user(self)
   end
+
+private
+
+  def set_creation_time
+    self.creation_time = Time.now.getutc unless self.attribute_present?(:creation_time)
+  end
+
+  def encrypt_password
+    if pass_crypt_confirmation
+      self.pass_salt = OSM::make_token(8)
+      self.pass_crypt = OSM::encrypt_password(pass_crypt, pass_salt)
+    end
+  end
 end
index a795de0..dda1911 100644 (file)
@@ -1,7 +1,11 @@
 class UserToken < ActiveRecord::Base
   belongs_to :user
 
-  def after_initialize
+  after_initialize :set_defaults
+
+private
+
+  def set_defaults
     self.token = OSM::make_token() if self.token.blank?
     self.expiry = 1.week.from_now if self.expiry.blank?
   end
index 2740eab..0728a6a 100644 (file)
@@ -6,10 +6,7 @@ module GeoRecord
   
   def self.included(base)
     base.extend(ClassMethods)
-  end
-
-  def before_save
-    self.update_tile
+    base.before_save :update_tile
   end
 
   # Is this node within -90 >= latitude >= 90 and -180 >= longitude >= 180