summaryrefslogtreecommitdiff
path: root/lib/activerecord_extensions.rb
diff options
context:
space:
mode:
authorStefan Wintermeyer <stefan.wintermeyer@amooma.de>2012-12-17 12:01:45 +0100
committerStefan Wintermeyer <stefan.wintermeyer@amooma.de>2012-12-17 12:01:45 +0100
commitb80bd744ad873f6fc43018bc4bfb90677de167bd (patch)
tree072c4b0e33d442528555b82c415f5e7a1712b2b0 /lib/activerecord_extensions.rb
parent3e706c2025ecc5523e81ad649639ef2ff75e7bac (diff)
Start of GS5.
Diffstat (limited to 'lib/activerecord_extensions.rb')
-rw-r--r--lib/activerecord_extensions.rb72
1 files changed, 72 insertions, 0 deletions
diff --git a/lib/activerecord_extensions.rb b/lib/activerecord_extensions.rb
new file mode 100644
index 0000000..50c44be
--- /dev/null
+++ b/lib/activerecord_extensions.rb
@@ -0,0 +1,72 @@
+class ActiveRecord::Base
+
+ before_validation :populate_uuid, :on => :create
+ before_validation :populate_gs_node_id, :on => :create
+
+ # Set a UUID.
+ #
+ def populate_uuid
+ if self.attribute_names.include?('uuid') && self.uuid.blank?
+ uuid = UUID.new
+ self.uuid = uuid.generate
+ end
+ end
+
+ # Set the gs_node_id if not already set.
+ #
+ def populate_gs_node_id
+ if self.attribute_names.include?('gs_node_id') && self.gs_node_id.blank?
+ self.gs_node_id = GsNode.where(:ip_address => HOMEBASE_IP_ADDRESS).first.try(:id)
+ end
+ end
+
+ # Create a new GsClusterSyncLogEntry.
+ # This will be populated automatically to GsNode.all.where(...)
+ #
+ def create_on_other_gs_nodes(association_method = nil, association_uuid = nil)
+ action_on_other_gs_nodes('create', self.to_json, nil, association_method, association_uuid)
+ end
+
+ def destroy_on_other_gs_nodes
+ action_on_other_gs_nodes('destroy', self.to_json)
+ end
+
+ def update_on_other_gs_nodes(association_method = nil, association_uuid = nil)
+ action_on_other_gs_nodes('update', self.changes.to_json, 'Changed: ' + self.changed.to_json, association_method, association_uuid)
+ end
+
+ def action_on_other_gs_nodes(action, content, history = nil, association_method = nil, association_uuid = nil)
+ # One doesn't make sense without the other.
+ #
+ if association_method.blank? || association_uuid.blank?
+ association_method = nil
+ association_uuid = nil
+ end
+ history = nil if history.blank?
+ if !self.attribute_names.include?('is_native')
+ logger.error "Couldn't #{action} #{self.class} with the ID #{self.id} on other GsNodes because #{self.class} doesn't have a is_native attribute."
+ else
+ if self.is_native != false
+ if defined? WRITE_GS_CLUSTER_SYNC_LOG && WRITE_GS_CLUSTER_SYNC_LOG == true
+ if !(defined? $gs_cluster_loop_protection) || $gs_cluster_loop_protection != true
+ begin
+ GsClusterSyncLogEntry.create(
+ :class_name => self.class.name,
+ :action => action,
+ :content => content,
+ :history => history,
+ :homebase_ip_address => HOMEBASE_IP_ADDRESS,
+ :waiting_to_be_synced => true,
+ :association_method => association_method,
+ :association_uuid => association_uuid
+ )
+ rescue
+ logger.error "Couldn't add action: #{action} for #{self.class} with the ID #{self.id} to gs_cluster_log_entries."
+ end
+ end
+ end
+ end
+ end
+ end
+
+end \ No newline at end of file