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



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

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
# File 'lib/stagehand/staging/commit_entry.rb', line 63

def self.infer_base_class(table_name)
  classes = ActiveRecord::Base.descendants.select {|klass| klass.table_name == table_name }
  classes.delete(Stagehand::Production::Record)
  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)


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

def content_operation?
  operation.in?(CONTENT_OPERATIONS)
end

#control_operation?Boolean

Returns:

  • (Boolean)


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

def control_operation?
  operation.in?(CONTROL_OPERATIONS)
end

#delete_operation?Boolean

Returns:

  • (Boolean)


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

def delete_operation?
  operation == DELETE_OPERATION
end

#end_operation?Boolean

Returns:

  • (Boolean)


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

def end_operation?
  operation == END_OPERATION
end

#insert_operation?Boolean

Returns:

  • (Boolean)


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

def insert_operation?
  operation == INSERT_OPERATION
end

#keyObject



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

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

#matches?(others) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#production_recordObject



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

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

#record_classObject



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

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

#save_operation?Boolean

Returns:

  • (Boolean)


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

def save_operation?
  operation.in?(SAVE_OPERATIONS)
end

#start_operation?Boolean

Returns:

  • (Boolean)


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

def start_operation?
  operation == START_OPERATION
end

#update_operation?Boolean

Returns:

  • (Boolean)


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

def update_operation?
  operation == UPDATE_OPERATION
end