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
Instance Method Summary collapse
- #content_operation? ⇒ Boolean
- #control_operation? ⇒ Boolean
- #delete_operation? ⇒ Boolean
- #end_operation? ⇒ Boolean
- #insert_operation? ⇒ Boolean
- #key ⇒ Object
- #matches?(others) ⇒ Boolean
- #record_class ⇒ Object
- #save_operation? ⇒ Boolean
- #start_operation? ⇒ Boolean
- #update_operation? ⇒ Boolean
Instance Attribute Details
#record ⇒ Object
72 73 74 |
# File 'lib/stagehand/staging/commit_entry.rb', line 72 def record @record ||= delete_operation? ? build_deleted_record : record_class.find_by_id(record_id) if record_id? end |
Class Method Details
.infer_class(table_name, record_id = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/stagehand/staging/commit_entry.rb', line 58 def self.infer_class(table_name, record_id = nil) classes = ActiveRecord::Base.descendants.select {|klass| klass.table_name == table_name } classes.delete(Stagehand::Production::Record) root_class = classes.first || table_name.classify.constantize # Try loading the class if it isn't loaded yet if record_id && record = root_class.find_by_id(record_id) klass = record.class end return klass || root_class rescue NameError raise(IndeterminateRecordClass, "Can't determine class from table name: #{table_name}") end |
.matching(object) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/stagehand/staging/commit_entry.rb', line 37 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
80 81 82 |
# File 'lib/stagehand/staging/commit_entry.rb', line 80 def content_operation? operation.in?(CONTENT_OPERATIONS) end |
#control_operation? ⇒ Boolean
76 77 78 |
# File 'lib/stagehand/staging/commit_entry.rb', line 76 def control_operation? operation.in?(CONTROL_OPERATIONS) end |
#delete_operation? ⇒ Boolean
96 97 98 |
# File 'lib/stagehand/staging/commit_entry.rb', line 96 def delete_operation? operation == DELETE_OPERATION end |
#end_operation? ⇒ Boolean
104 105 106 |
# File 'lib/stagehand/staging/commit_entry.rb', line 104 def end_operation? operation == END_OPERATION end |
#insert_operation? ⇒ Boolean
88 89 90 |
# File 'lib/stagehand/staging/commit_entry.rb', line 88 def insert_operation? operation == INSERT_OPERATION end |
#key ⇒ Object
112 113 114 |
# File 'lib/stagehand/staging/commit_entry.rb', line 112 def key @key ||= Stagehand::Key.generate(self) end |
#matches?(others) ⇒ Boolean
108 109 110 |
# File 'lib/stagehand/staging/commit_entry.rb', line 108 def matches?(others) Array.wrap(others).any? {|other| key == Stagehand::Key.generate(other) } end |
#record_class ⇒ Object
116 117 118 119 120 |
# File 'lib/stagehand/staging/commit_entry.rb', line 116 def record_class @record_class ||= self.class.infer_class(table_name, record_id) rescue IndeterminateRecordClass @record_class ||= self.class.build_missing_model(table_name) end |
#save_operation? ⇒ Boolean
84 85 86 |
# File 'lib/stagehand/staging/commit_entry.rb', line 84 def save_operation? operation.in?(SAVE_OPERATIONS) end |
#start_operation? ⇒ Boolean
100 101 102 |
# File 'lib/stagehand/staging/commit_entry.rb', line 100 def start_operation? operation == START_OPERATION end |
#update_operation? ⇒ Boolean
92 93 94 |
# File 'lib/stagehand/staging/commit_entry.rb', line 92 def update_operation? operation == UPDATE_OPERATION end |