Module: Cell::Meta

Defined in:
lib/cell/meta.rb

Constant Summary collapse

GLOBAL_KEY =
'Cell.global'

Class Method Summary collapse

Class Method Details

.add_global_table(name) ⇒ Object



72
73
74
# File 'lib/cell/meta.rb', line 72

def self.add_global_table(name)
  set_global_tables(global_tables + [name.to_s])
end

.connectionObject



8
9
10
# File 'lib/cell/meta.rb', line 8

def self.connection
  ::ActiveRecord::Base.connection
end

.global_model?(m) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
# File 'lib/cell/meta.rb', line 80

def self.global_model?(m)
  @model_map ||= global_tables.map do |k|
    [k, true]
  end.to_h

  @model_map[m.table_name] || false
end

.global_schemaObject



34
35
36
37
# File 'lib/cell/meta.rb', line 34

def self.global_schema
  # This could be racy?
  @global_schema ||= ::ActiveRecord::Base.connection.schema_search_path
end

.global_tablesObject



59
60
61
62
63
# File 'lib/cell/meta.rb', line 59

def self.global_tables
  with_global_schema do
    (::ActiveRecord::InternalMetadata[GLOBAL_KEY] || '').split(',')
  end
end

.prototype_schemaObject



39
40
41
# File 'lib/cell/meta.rb', line 39

def self.prototype_schema
  'cell_prototype'
end

.remove_global_table(name) ⇒ Object



76
77
78
# File 'lib/cell/meta.rb', line 76

def self.remove_global_table(name)
  set_global_tables(global_tables - [name.to_s])
end

.set_global_tables(tables) ⇒ Object



65
66
67
68
69
70
# File 'lib/cell/meta.rb', line 65

def self.set_global_tables(tables)
  with_global_schema do
    table_string = tables.map(&:to_s).sort.uniq.join(',')
    ::ActiveRecord::InternalMetadata[GLOBAL_KEY] = table_string
  end
end

.set_schema_search_path(path) ⇒ Object



27
28
29
30
31
32
# File 'lib/cell/meta.rb', line 27

def self.set_schema_search_path(path)
  if path != connection.schema_search_path
    connection.schema_search_path = path
    connection.clear_query_cache
  end
end

.structural_schemaObject



43
44
45
# File 'lib/cell/meta.rb', line 43

def self.structural_schema
  "#{prototype_schema},#{global_schema}"
end

.with_global_schema(&block) ⇒ Object



47
48
49
# File 'lib/cell/meta.rb', line 47

def self.with_global_schema(&block)
  with_schema(global_schema, exclusive: true, &block)
end

.with_prototype_schema(&block) ⇒ Object



51
52
53
# File 'lib/cell/meta.rb', line 51

def self.with_prototype_schema(&block)
  with_schema(prototype_schema, exclusive: true, &block)
end

.with_schema(new_path, exclusive: false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cell/meta.rb', line 12

def self.with_schema(new_path, exclusive: false)
  saved_path = connection.schema_search_path
  active_path = exclusive ? new_path : "#{new_path},#{saved_path}"

  set_schema_search_path(active_path)

  if block_given?
    begin
      yield
    ensure
      set_schema_search_path(saved_path)
    end
  end
end

.with_structural_schema(&block) ⇒ Object



55
56
57
# File 'lib/cell/meta.rb', line 55

def self.with_structural_schema(&block)
  with_schema(structural_schema, exclusive: true, &block)
end