lib/couch_surfer/model.rb in addywaddy-couch_surfer-0.0.2 vs lib/couch_surfer/model.rb in addywaddy-couch_surfer-0.0.4

- old
+ new

@@ -13,10 +13,19 @@ def self.default_database= db @database = db end + # Adapted from ActiveSupport Time#formatted_offset + def self.format_utc_offset(time) + seconds_offset_from_utc = time.utc_offset + sign = (seconds_offset_from_utc < 0 ? -1 : 1) + hours = seconds_offset_from_utc.abs / 3600 + minutes = (seconds_offset_from_utc.abs % 3600) / 60 + "%+03d%02d" % [ hours * sign, minutes ] + end + module ClassMethods # override the CouchSurfer::Model-wide default_database def use_database db self.class_database = db end @@ -152,12 +161,12 @@ Time.parse(@attributes[method]) end end before(:save) do time = Time.now - usec = time.usec - self['updated_at'] = time.strftime("%Y/%m/%d %H:%M:%S.#{time.usec} %z") + utc_offset = CouchSurfer::Model.format_utc_offset(time) + self['updated_at'] = time.strftime("%Y/%m/%d %H:%M:%S.#{time.usec} #{utc_offset}") self['created_at'] = self['updated_at'] if new_document? end end # Name a method that will be called before the document is first saved, @@ -288,11 +297,11 @@ end # Deletes any non-current design docs that were created by this class. # Running this when you're deployed version of your application is steadily # and consistently using the latest code, is the way to clear out old design - # docs. Running it to early could mean that live code has to regenerate + # docs. Running it too early could mean that live code has to regenerate # potentially large indexes. def cleanup_design_docs! ddocs = all_design_doc_versions ddocs["rows"].each do |row| if (row['id'] != design_doc_id) @@ -357,11 +366,11 @@ "language" => "javascript", "views" => { 'all' => { 'map' => "function(doc) { if (doc['couchrest-type'] == '#{self.to_s}') { - emit(null,null); + emit(doc['_id'],null); } }" } } } @@ -383,9 +392,10 @@ design_doc.database = database design_doc.save end self.design_doc_fresh = true end + end module InstanceMethods # returns the database used by this model's class \ No newline at end of file