[Groonga-commit] ranguba/groonga-client-model at bbe76d4 [master] Support auto timestamp update

Back to archive index

Kouhei Sutou null+****@clear*****
Mon Mar 6 10:14:30 JST 2017


Kouhei Sutou	2017-03-06 10:14:30 +0900 (Mon, 06 Mar 2017)

  New Revision: bbe76d442f77afb04bec03771700f8a2cf2840a9
  https://github.com/ranguba/groonga-client-model/commit/bbe76d442f77afb04bec03771700f8a2cf2840a9

  Message:
    Support auto timestamp update

  Modified files:
    lib/groonga_client_model/record.rb
    test/unit/test_record.rb

  Modified: lib/groonga_client_model/record.rb (+16 -0)
===================================================================
--- lib/groonga_client_model/record.rb    2017-03-06 10:13:27 +0900 (5563c42)
+++ lib/groonga_client_model/record.rb    2017-03-06 10:14:30 +0900 (7f733ed)
@@ -264,6 +264,8 @@ module GroongaClientModel
     def upsert_raw(validate: true)
       return false unless upsert_sub_records(validate: validate)
 
+      update_timestamps
+
       Client.open do |client|
         table = self.class.schema.tables[self.class.table_name]
         load_value_generator = LoadValueGenerator.new(self)
@@ -369,5 +371,19 @@ module GroongaClientModel
         sub_record_class.new(value)
       end
     end
+
+    def update_timestamps
+      now = Time.now
+
+      if new_record?
+        [:created_at, :created_on].each do |attribute|
+          __send__("#{attribute}=", now) if respond_to?("#{attribute}=")
+        end
+      end
+
+      [:updated_at, :updated_on].each do |attribute|
+        __send__("#{attribute}=", now) if respond_to?("#{attribute}=")
+      end
+    end
   end
 end

  Modified: test/unit/test_record.rb (+65 -0)
===================================================================
--- test/unit/test_record.rb    2017-03-06 10:13:27 +0900 (81e2e45)
+++ test/unit/test_record.rb    2017-03-06 10:14:30 +0900 (bb8db9c)
@@ -342,4 +342,69 @@ class TestRecord < Test::Unit::TestCase
       end
     end
   end
+
+  sub_test_case("timestamps") do
+    include GroongaClientModel::TestHelper
+
+    setup do
+      schema = <<-SCHEMA
+table_create timestamps TABLE_NO_KEY
+column_create timestamps created_at COLUMN_SCALAR Time
+column_create timestamps created_on COLUMN_SCALAR Time
+column_create timestamps updated_at COLUMN_SCALAR Time
+column_create timestamps updated_on COLUMN_SCALAR Time
+      SCHEMA
+      schema_loader = GroongaClientModel::SchemaLoader.new(schema)
+      schema_loader.load
+    end
+
+    class Timestamp < GroongaClientModel::Record
+    end
+
+    test("created_at") do
+      now = Time.now
+      timestamp = Timestamp.new
+      timestamp.save
+      saved_timestamp = Timestamp.find(timestamp._id)
+      assert do
+        saved_timestamp.created_at > now
+      end
+    end
+
+    test("created_on") do
+      now = Time.now
+      timestamp = Timestamp.new
+      timestamp.save
+      saved_timestamp = Timestamp.find(timestamp._id)
+      assert do
+        saved_timestamp.created_on > now
+      end
+    end
+
+    test("updated_at") do
+      timestamp = Timestamp.new
+      timestamp.save
+      saved_timestamp = Timestamp.find(timestamp._id)
+      created_at = saved_timestamp.created_at
+      saved_timestamp.save
+      updated_timestamp = Timestamp.find(timestamp._id)
+      updated_timestamp.save
+      assert do
+        saved_timestamp.updated_at > created_at
+      end
+    end
+
+    test("updated_on") do
+      timestamp = Timestamp.new
+      timestamp.save
+      saved_timestamp = Timestamp.find(timestamp._id)
+      created_on = saved_timestamp.created_on
+      saved_timestamp.save
+      updated_timestamp = Timestamp.find(timestamp._id)
+      updated_timestamp.save
+      assert do
+        saved_timestamp.updated_on > created_on
+      end
+    end
+  end
 end
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index