Class: Stagehand::Staging::CommitEntry
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Stagehand::Staging::CommitEntry
- 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
- .infer_base_class(table_name) ⇒ Object
- .joins_active_commits(type = "INNER") ⇒ Object
- .joins_contained(type = "INNER") ⇒ Object
- .matching(object) ⇒ Object
Instance Method Summary collapse
- #content_operation? ⇒ Boolean
- #control_operation? ⇒ Boolean
- #delete_operation? ⇒ Boolean
- #end_operation? ⇒ Boolean
- #insert_operation? ⇒ Boolean
- #key ⇒ Object
- #matches?(others) ⇒ Boolean
- #production_record ⇒ Object
- #record_class ⇒ Object
- #save_operation? ⇒ Boolean
- #start_operation? ⇒ Boolean
- #update_operation? ⇒ Boolean
Instance Attribute Details
#record ⇒ Object
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
82 83 84 |
# File 'lib/stagehand/staging/commit_entry.rb', line 82 def content_operation? operation.in?(CONTENT_OPERATIONS) end |
#control_operation? ⇒ Boolean
78 79 80 |
# File 'lib/stagehand/staging/commit_entry.rb', line 78 def control_operation? operation.in?(CONTROL_OPERATIONS) end |
#delete_operation? ⇒ Boolean
98 99 100 |
# File 'lib/stagehand/staging/commit_entry.rb', line 98 def delete_operation? operation == DELETE_OPERATION end |
#end_operation? ⇒ Boolean
106 107 108 |
# File 'lib/stagehand/staging/commit_entry.rb', line 106 def end_operation? operation == END_OPERATION end |
#insert_operation? ⇒ Boolean
90 91 92 |
# File 'lib/stagehand/staging/commit_entry.rb', line 90 def insert_operation? operation == INSERT_OPERATION end |
#key ⇒ Object
114 115 116 |
# File 'lib/stagehand/staging/commit_entry.rb', line 114 def key @key ||= Stagehand::Key.generate(self) end |
#matches?(others) ⇒ 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_record ⇒ Object
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_class ⇒ Object
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
86 87 88 |
# File 'lib/stagehand/staging/commit_entry.rb', line 86 def save_operation? operation.in?(SAVE_OPERATIONS) end |
#start_operation? ⇒ Boolean
102 103 104 |
# File 'lib/stagehand/staging/commit_entry.rb', line 102 def start_operation? operation == START_OPERATION end |
#update_operation? ⇒ Boolean
94 95 96 |
# File 'lib/stagehand/staging/commit_entry.rb', line 94 def update_operation? operation == UPDATE_OPERATION end |