Module: AssociateJsonb::ConnectionAdapters::ReferenceDefinition

Defined in:
lib/associate_jsonb/connection_adapters/reference_definition.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#add_to(table) ⇒ Object

rubocop:enable Metrics/ParameterLists



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/associate_jsonb/connection_adapters/reference_definition.rb', line 18

def add_to(table)
  return super unless store

  should_add_col = false
  if table.respond_to? :column_exists?
    should_add_col = !table.column_exists?(store)
  elsif table.respond_to? :columns
    should_add_col = table.columns.none? {|col| col.name.to_sym == store}
  end

  table.column(store, :jsonb, null: false, default: {}) if should_add_col

  return unless index

  # should_add_idx = false
  # if table.respond_to? :index_exists?
  #   should_add_idx = !table.index_exists?([ store ], using: :gin)
  # elsif table.respond_to? :indexes
  #   should_add_idx = table.indexes.none? do |idx, opts|
  #     (idx == [ store ]) \
  #     && (opts == { using: :gin })
  #   end
  # end
  #
  # table.index([ store ], using: :gin) if should_add_idx

  column_names.each do |column_name|
    table.index(
      "CAST (\"#{store}\"->'#{column_name}' AS #{@type || :bigint})",
      using: :btree,
      name: "index_#{table.name}_on_#{store}_#{column_name}"
    )

    table.index(
      "(#{store}->>'#{column_name}')",
      using: :btree,
      name: "index_#{table.name}_on_#{store}_#{column_name}_text"
    )
  end
end

#initialize(name, store: false, **options) ⇒ Object

rubocop:disable Metrics/ParameterLists



7
8
9
10
11
12
13
14
15
# File 'lib/associate_jsonb/connection_adapters/reference_definition.rb', line 7

def initialize(
  name,
  store: false,
  **options
)
  @store = store && store.to_sym

  super(name, **options)
end