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



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

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)


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

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
# File 'lib/cell/meta.rb', line 34

def self.global_schema
  @global_schema ||= ActiveRecord::Base.connection.schema_search_path
end

.global_tablesObject



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

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

.prototype_schemaObject



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

def self.prototype_schema
  'cell_prototype'
end

.remove_global_table(name) ⇒ Object



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

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

.set_global_tables(tables) ⇒ Object



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

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



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

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

.with_global_schema(&block) ⇒ Object



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

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

.with_prototype_schema(&block) ⇒ Object



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

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



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

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