Module: BridgeCache::Data::BridgeModel

Constant Summary collapse

EXCLUDED_COLUMNS =
%w[created_at updated_at].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



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

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



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

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

#cleanup(row_ids) ⇒ Object



32
33
34
35
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 32

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



28
29
30
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 28

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

#csv_mappingObject



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

def csv_mapping
  columns = column_names.as_json
  mapping = {}
  columns.each { |column| mapping[column] = column unless EXCLUDED_COLUMNS.include?(column) }
  mapping['bridge_id'] = 'bridge_id'
  mapping
end

#for_domain(domain_id) ⇒ Object



53
54
55
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 53

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

#format_import_row(row) ⇒ Object



20
21
22
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 20

def format_import_row(row)
  row
end

#import_from_csv(file_path) ⇒ Object



24
25
26
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 24

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

#is_active?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 61

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

#uniq_constraint_nameObject



57
58
59
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 57

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

#webhook_completed(_message) ⇒ Object



49
50
51
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 49

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

#webhook_created(message) ⇒ Object



41
42
43
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 41

def webhook_created(message)
  create_or_update_from_webhook(message)
end

#webhook_deleted(message) ⇒ Object



45
46
47
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 45

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

#webhook_updated(message) ⇒ Object



37
38
39
# File 'app/lib/bridge_cache/data/bridge_model.rb', line 37

def webhook_updated(message)
  create_or_update_from_webhook(message)
end