[Groonga-commit] ranguba/groonga-client-model at e2e0236 [master] Support recursive save

Zurück zum Archiv-Index

Kouhei Sutou null+****@clear*****
Mon Dec 19 15:29:49 JST 2016


Kouhei Sutou	2016-12-19 15:29:49 +0900 (Mon, 19 Dec 2016)

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

  Message:
    Support recursive save

  Modified files:
    lib/groonga_client_model/modelize.rb
    lib/groonga_client_model/record.rb

  Modified: lib/groonga_client_model/modelize.rb (+0 -1)
===================================================================
--- lib/groonga_client_model/modelize.rb    2016-12-19 14:17:40 +0900 (3b7b55a)
+++ lib/groonga_client_model/modelize.rb    2016-12-19 15:29:49 +0900 (2576289)
@@ -41,7 +41,6 @@ module GroongaClientModel
       raw_records.collect do |raw_record|
         attributes, dynamic_attributes = build_attributes(columns, raw_record)
         record = @model_class.new(attributes)
-        record.instance_variable_set(:@new_record, false)
         record.assign_dynamic_attributes(dynamic_attributes)
         record
       end

  Modified: lib/groonga_client_model/record.rb (+16 -1)
===================================================================
--- lib/groonga_client_model/record.rb    2016-12-19 14:17:40 +0900 (ee84a92)
+++ lib/groonga_client_model/record.rb    2016-12-19 15:29:49 +0900 (186f860)
@@ -19,6 +19,7 @@ module GroongaClientModel
     include ActiveModel::AttributeAssignment
     include ActiveModel::AttributeMethods
     include ActiveModel::Conversion
+    include ActiveModel::Dirty
     include ActiveModel::Translation
     include ActiveModel::Validations
 
@@ -104,6 +105,9 @@ module GroongaClientModel
           if value.is_a?(Hash)
             value = build_sub_record(name, value)
           end
+          unless @attributes[name] == value
+            attribute_will_change!(name)
+          end
           @attributes[name] = value
         end
       end
@@ -116,7 +120,12 @@ module GroongaClientModel
       self.class.define_attributes
       assign_attributes(attributes) if attributes
 
-      @new_record = true
+      if @attributes["_id"]
+        @new_record = false
+        clear_changes_information
+      else
+        @new_record = true
+      end
       @destroyed = false
     end
 
@@ -198,6 +207,11 @@ module GroongaClientModel
 
     private
     def upsert
+      attributes.each do |name, value|
+        if value.is_a?(Record) and value.changed?
+          value.save
+        end
+      end
       Client.open do |client|
         table = self.class.schema.tables[self.class.table_name]
         load_value_generator = LoadValueGenerator.new(self)
@@ -231,6 +245,7 @@ module GroongaClientModel
           self._id = id
         end
         @new_record = false
+        changes_applied
         true
       end
     end
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Zurück zum Archiv-Index