Class: Stagehand::Staging::CommitEntry

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/stagehand/staging/commit_entry.rb

Constant Summary collapse

START_OPERATION =
'commit_start'
END_OPERATION =
'commit_end'
INSERT_OPERATION =
'insert'
UPDATE_OPERATION =
'update'
DELETE_OPERATION =
'delete'
CONTROL_OPERATIONS =
[START_OPERATION, END_OPERATION]
CONTENT_OPERATIONS =
[INSERT_OPERATION, UPDATE_OPERATION, DELETE_OPERATION]
SAVE_OPERATIONS =
[INSERT_OPERATION, UPDATE_OPERATION]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#recordObject



75
76
77
# File 'lib/stagehand/staging/commit_entry.rb', line 75

def record
  @record ||= delete_operation? ? build_deleted_record : record_class.find_by_id(record_id) if record_id?
end

Class Method Details

.infer_base_class(table_name) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/stagehand/staging/commit_entry.rb', line 63

def self.infer_base_class(table_name)
  classes = ActiveRecord::Base.descendants
  classes.select! {|klass| klass.table_name == table_name }
  classes.reject! {|klass| klass < Stagehand::Database::Probe }
  return classes.first || table_name.classify.constantize.base_class # Try loading the class if it isn't loaded yet
rescue NameError
  raise(IndeterminateRecordClass, "Can't determine class from table name: #{table_name}")
end

.joins_active_commits(type = "INNER") ⇒ Object



32
33
34
35
# File 'lib/stagehand/staging/commit_entry.rb', line 32

def self.joins_active_commits(type = "INNER")
  joins("#{type} JOIN (#{ unscoped.select('session, MAX(id) AS commit_id').uncontained.start_operations.group('session').to_sql }) AS active_commits
         ON active_commits.session = #{table_name}.session AND active_commits.commit_id <= #{table_name}.id")
end

.joins_contained(type = "INNER") ⇒ Object



37
38
39
40
# File 'lib/stagehand/staging/commit_entry.rb', line 37

def self.joins_contained(type = "INNER")
  joins("#{type} JOIN (#{ unscoped.contained.select('record_id, table_name').distinct.to_sql}) AS contained
         ON contained.record_id = #{table_name}.record_id AND contained.table_name = #{table_name}.table_name")
end

.matching(object) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/stagehand/staging/commit_entry.rb', line 42

def self.matching(object)
  keys = Array.wrap(object).collect {|entry| Stagehand::Key.generate(entry) }.compact
  sql = []
  interpolates = []
  groups = keys.group_by(&:first)

  # If passed control operation commit entries, ensure they are returned since their keys match the CommitEntry's primary key
  if commit_entry_group = groups.delete(CommitEntry.table_name)
    sql << 'id IN (?)'
    interpolates << commit_entry_group.collect(&:last)
  end

  groups.each do |table_name, keys|
    sql << "(table_name = ? AND record_id IN (?))"
    interpolates << table_name
    interpolates << keys.collect(&:last)
  end

  return keys.present? ? where(sql.join(' OR '), *interpolates) : none
end

Instance Method Details

#content_operation?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/stagehand/staging/commit_entry.rb', line 83

def content_operation?
  operation.in?(CONTENT_OPERATIONS)
end

#control_operation?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/stagehand/staging/commit_entry.rb', line 79

def control_operation?
  operation.in?(CONTROL_OPERATIONS)
end

#delete_operation?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/stagehand/staging/commit_entry.rb', line 99

def delete_operation?
  operation == DELETE_OPERATION
end

#end_operation?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/stagehand/staging/commit_entry.rb', line 107

def end_operation?
  operation == END_OPERATION
end

#insert_operation?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/stagehand/staging/commit_entry.rb', line 91

def insert_operation?
  operation == INSERT_OPERATION
end

#keyObject



115
116
117
# File 'lib/stagehand/staging/commit_entry.rb', line 115

def key
  @key ||= Stagehand::Key.generate(self)
end

#matches?(others) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/stagehand/staging/commit_entry.rb', line 111

def matches?(others)
  Array.wrap(others).any? {|other| key == Stagehand::Key.generate(other) }
end

#production_recordObject



125
126
127
128
# File 'lib/stagehand/staging/commit_entry.rb', line 125

def production_record
  @production_record = Stagehand::Production.find(record_id, table_name) unless defined?(@production_record)
  return @production_record
end

#record_classObject



119
120
121
122
123
# File 'lib/stagehand/staging/commit_entry.rb', line 119

def record_class
  @record_class ||= infer_class
rescue IndeterminateRecordClass
  @record_class ||= self.class.build_missing_model(table_name)
end

#save_operation?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/stagehand/staging/commit_entry.rb', line 87

def save_operation?
  operation.in?(SAVE_OPERATIONS)
end

#start_operation?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/stagehand/staging/commit_entry.rb', line 103

def start_operation?
  operation == START_OPERATION
end

#update_operation?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/stagehand/staging/commit_entry.rb', line 95

def update_operation?
  operation == UPDATE_OPERATION
end