summaryrefslogtreecommitdiff
path: root/lib/activerecord_extensions.rb
blob: 102ddd8788bb674e9487d30c4fcb27f4cea5e581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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 => GsParameter.get('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? GsParameter.get('WRITE_GS_CLUSTER_SYNC_LOG') && GsParameter.get('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 => GsParameter.get('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