Class: Penman::RecordTag
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Penman::RecordTag
- Defined in:
- lib/penman/record_tag.rb
Constant Summary collapse
- @@enabled =
false
- @@taggable_models =
[]
Class Method Summary collapse
- .create_custom(attributes = {}) ⇒ Object
- .disable ⇒ Object
- .enable ⇒ Object
- .enabled? ⇒ Boolean
- .find_tags_for_model(model) ⇒ Object
- .find_tags_for_models(*models) ⇒ Object
- .generate_seed_for_model(model) ⇒ Object
- .generate_seeds ⇒ Object
- .generate_seeds_for_models(models) ⇒ Object
- .register(model) ⇒ Object
- .tag(record, tag) ⇒ Object
- .valid_ordered_models_for_seeds(models) ⇒ Object
Instance Method Summary collapse
Class Method Details
.create_custom(attributes = {}) ⇒ Object
123 124 125 126 127 |
# File 'lib/penman/record_tag.rb', line 123 def create_custom(attributes = {}) attributes = { record_type: 'custom_tag', tag: 'touched', candidate_key: 'n/a' }.merge attributes record_tag = RecordTag.find_or_create_by(attributes) record_tag.update(record_id: record_tag.id) if record_tag.record_id == 0 # notice validation above, this just ensures that we don't violate the table constraint. end |
.disable ⇒ Object
38 39 40 |
# File 'lib/penman/record_tag.rb', line 38 def disable @@enabled = false end |
.enable ⇒ Object
42 43 44 |
# File 'lib/penman/record_tag.rb', line 42 def enable @@enabled = true end |
.enabled? ⇒ Boolean
46 47 48 |
# File 'lib/penman/record_tag.rb', line 46 def enabled? @@enabled end |
.find_tags_for_model(model) ⇒ Object
110 111 112 |
# File 'lib/penman/record_tag.rb', line 110 def (model) (model) end |
.find_tags_for_models(*models) ⇒ Object
114 115 116 |
# File 'lib/penman/record_tag.rb', line 114 def (*models) RecordTag.where(record_type: models.map { |m| (m.is_a? String) ? m : m.name }) end |
.generate_seed_for_model(model) ⇒ Object
154 155 156 157 158 159 160 161 |
# File 'lib/penman/record_tag.rb', line 154 def generate_seed_for_model(model) time = Time.now seed_files = [] seed_files << generate_update_seed(model, time.strftime('%Y%m%d%H%M%S')) seed_files << generate_destroy_seed(model, (time + 1.second).strftime('%Y%m%d%H%M%S')) RecordTag.where(record_type: model.name).delete_all seed_files.compact end |
.generate_seeds ⇒ Object
129 130 131 |
# File 'lib/penman/record_tag.rb', line 129 def generate_seeds generate_seeds_for_models(seed_order) end |
.generate_seeds_for_models(models) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/penman/record_tag.rb', line 133 def generate_seeds_for_models(models) models = valid_ordered_models_for_seeds(models) return [] if models.empty? time = Time.now seed_files = [] models.each do |model| seed_files << generate_update_seed(model, time.strftime('%Y%m%d%H%M%S')) time += 1.second end models.reverse.each do |model| seed_files << generate_destroy_seed(model, time.strftime('%Y%m%d%H%M%S')) time += 1.second end RecordTag.where(record_type: models.map(&:name)).delete_all seed_files.compact end |
.register(model) ⇒ Object
50 51 52 |
# File 'lib/penman/record_tag.rb', line 50 def register(model) @@taggable_models |= [model] end |
.tag(record, tag) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/penman/record_tag.rb', line 54 def tag(record, tag) return unless @@enabled candidate_key = record.class.try(:candidate_key) || Penman.config.default_candidate_key candidate_key = [candidate_key] unless candidate_key.is_a? Array raise RecordTagExceptions::InvalidCandidateKeyForRecord unless record_has_attributes?(record, candidate_key) candidate_key_to_store = if ['created', 'destroyed'].include? tag Hash[candidate_key.map { |k| [k, record.send(k)] }].to_json else # updated Hash[candidate_key.map { |k| [k, record.send("#{k}_was")] }].to_json end created_tag = RecordTag.find_by(record: record, tag: 'created') updated_tag = RecordTag.find_by(record: record, tag: 'updated') destroyed_tag = RecordTag.find_by(record_type: record.class.name, candidate_key: candidate_key_to_store, tag: 'destroyed') raise RecordTagExceptions::TooManyTagsForRecord if [created_tag, updated_tag, destroyed_tag].count { |t| t.present? } > 1 if created_tag.present? case tag when 'created' raise RecordTagExceptions::BadTracking, ('created', 'created', candidate_key_to_store) when 'updated' created_tag.update!(tag: tag) when 'destroyed' created_tag.destroy! end elsif updated_tag.present? case tag when 'created' raise RecordTagExceptions::BadTracking, ('updated', 'created', candidate_key_to_store) when 'updated' updated_tag.update!(tag: tag) when 'destroyed' if updated_tag.created_this_session updated_tag.destroy! else updated_tag.update!(tag: tag) end end elsif destroyed_tag.present? case tag when 'created' # We make an updated tag in case non-candidate key attributes have changed, since we don't tack those. destroyed_tag.update!(tag: 'updated', record_id: record.id) when 'updated' raise RecordTagExceptions::BadTracking, ('destroyed', 'updated', candidate_key_to_store) when 'destroyed' raise RecordTagExceptions::BadTracking, ('destroyed', 'destroyed', candidate_key_to_store) end else # new tag RecordTag.create!(record: record, tag: tag, candidate_key: candidate_key_to_store, created_this_session: tag == 'created') end end |
.valid_ordered_models_for_seeds(models) ⇒ Object
118 119 120 121 |
# File 'lib/penman/record_tag.rb', line 118 def valid_ordered_models_for_seeds(models) valid_models = @@taggable_models & models seed_order(valid_models) end |
Instance Method Details
#candidate_key ⇒ Object
33 34 35 |
# File 'lib/penman/record_tag.rb', line 33 def candidate_key decode_candidate_key(super) end |
#decode_candidate_key(key) ⇒ Object
19 20 21 22 23 24 25 26 27 28 |
# File 'lib/penman/record_tag.rb', line 19 def decode_candidate_key(key) begin ActiveSupport::JSON.decode(key).symbolize_keys rescue JSON::ParserError # This will occur if the candidate key isn't encoded as json. # An example of this is when we tag yaml files as touched when updating lang. # In that case we store the file path in the candidate key column as a regular string. key end end |
#encode_candidate_key ⇒ Object
13 14 15 16 17 |
# File 'lib/penman/record_tag.rb', line 13 def encode_candidate_key if self.candidate_key.is_a? Hash self.candidate_key = self.candidate_key.to_json end end |