Module: BridgeCache::Data::BridgeModel

Constant Summary collapse

EXCLUDED_COLUMNS =
Set['id', 'created_at', 'updated_at']
OVERRIDDEN_COLUMNS =
{ 'bridge_created_at' => 'created_at'.freeze, 'bridge_updated_at' => 'updated_at'.freeze }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



14
15
16
17
18
19
20
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 14

def self.extended(base)
  base.class_eval do
    scope :in_domain, ->(domain_id) { for_domain(domain_id) }
    scope :active, -> { is_active? }
    self.table_name = adjusted_table_name
  end
end

Instance Method Details

#adjusted_table_nameObject



6
7
8
9
10
11
12
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 6

def adjusted_table_name
  if BridgeCache.use_internal_database
    table_name
  else
    to_s.demodulize.underscore.pluralize
  end
end

#cleanup(row_ids) ⇒ Object



34
35
36
37
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 34

def cleanup(row_ids)
  # Implement this method in your model if you want to do any sort of post creation cleanup.
  # See tagging.rb for an example.
end

#create_from_csv_row(row) ⇒ Object



30
31
32
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 30

def create_from_csv_row(row)
  BridgeCache::Plugins::CSVDump.dump_row(self, row)
end

#csv_mappingObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 71

def csv_mapping
  columns = column_names.as_json
  mapping = {}
  columns.each do |column|
    next if EXCLUDED_COLUMNS.include? column
    if OVERRIDDEN_COLUMNS.key?(column)
      mapping[OVERRIDDEN_COLUMNS[column]] = column
    else
      mapping[column] = column
    end
  end
  mapping['bridge_id'] = 'bridge_id'
  mapping
end

#for_domain(domain_id) ⇒ Object



55
56
57
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 55

def for_domain(domain_id)
  where(domain_id: domain_id)
end

#format_import_row(row) ⇒ Object



22
23
24
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 22

def format_import_row(row)
  row
end

#import_from_csv(file_path) ⇒ Object



26
27
28
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 26

def import_from_csv(file_path)
  BridgeCache::Plugins::CSVDump.dump_to_table(self, file_path)
end

#is_active?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 67

def is_active?
  where(deleted_at: nil) if column_names.include? 'deleted_at'
end

#uniq_constraint_nameObject



59
60
61
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 59

def uniq_constraint_name
  "#{table_name.downcase}_bridge_uniq"
end

#unique_column_namesObject



63
64
65
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 63

def unique_column_names
  %w[bridge_id]
end

#webhook_completed(_message) ⇒ Object



51
52
53
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 51

def webhook_completed(_message)
  raise 'Method not implemented'
end

#webhook_created(message) ⇒ Object



43
44
45
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 43

def webhook_created(message)
  create_or_update_from_webhook(message)
end

#webhook_deleted(message) ⇒ Object



47
48
49
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 47

def webhook_deleted(message)
  message.payload_class.where(bridge_id: message.resource_object['id']).destroy_all
end

#webhook_updated(message) ⇒ Object



39
40
41
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 39

def webhook_updated(message)
  create_or_update_from_webhook(message)
end