Module: AssociateJsonb::Reflection

Defined in:
lib/associate_jsonb/reflection.rb

Instance Method Summary collapse

Instance Method Details

#foreign_store?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/associate_jsonb/reflection.rb', line 24

def foreign_store?
  options.key?(:foreign_store) && options[:foreign_store].present?
end

#foreign_store_attrObject



34
35
36
# File 'lib/associate_jsonb/reflection.rb', line 34

def foreign_store_attr
  options[:foreign_store]
end

#foreign_store_keyObject



38
39
40
# File 'lib/associate_jsonb/reflection.rb', line 38

def foreign_store_key
  options[:foreign_store_key].presence || join_primary_key
end

#foreign_store_key?(fk = nil) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
# File 'lib/associate_jsonb/reflection.rb', line 28

def foreign_store_key?(fk = nil)
  options.key?(:foreign_store_key) \
  && options[:foreign_store_key].present? \
  && (!fk || (foreign_store_key.to_s != fk.to_s))
end

#join_scope(table, foreign_table, foreign_klass) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/associate_jsonb/reflection.rb', line 42

def join_scope(table, foreign_table, foreign_klass)
  return super unless jsonb_store? || foreign_store?

  predicate_builder = predicate_builder(table)
  scope_chain_items = join_scopes(table, predicate_builder)
  klass_scope       = klass_join_scope(table, predicate_builder)

  if type
    klass_scope.where!(type => foreign_klass.polymorphic_name)
  end

  scope_chain_items.inject(klass_scope, &:merge!)

  key         = join_primary_key
  foreign_key = join_foreign_key

  if foreign_store?
    klass_scope.where!(
      Arel::Nodes::NamedFunction.new(
        "CAST",
        [
          Arel::Nodes::Jsonb::DashDoubleArrow.
            new(table, table[foreign_store_attr], foreign_store_key || key).
            as(foreign_klass.columns_hash[foreign_key.to_s].sql_type)
        ]
      ).eq(
        ::Arel::Nodes::SqlLiteral.
          new("#{foreign_table.name}.#{foreign_key}")
      )
    )

    # klass_scope.where!(
    #   Arel::Nodes::Jsonb::DashDoubleArrow.
    #     new(table, table[foreign_store_attr], foreign_store_key || key).
    #     eq(
    #       ::Arel::Nodes::SqlLiteral.
    #         new("#{foreign_table.name}.#{foreign_key}::text")
    #     )
    # )
  elsif jsonb_store?
    klass_scope.where!(
      Arel::Nodes::NamedFunction.new(
        "CAST",
        [
          Arel::Nodes::Jsonb::DashDoubleArrow.
            new(foreign_table, foreign_table[jsonb_store_attr], jsonb_store_key || foreign_key).
            as(klass.columns_hash[key.to_s].sql_type)
        ]
      ).eq(
        ::Arel::Nodes::SqlLiteral.new("#{table.name}.#{key}")
      )
    )
  end

  if klass.finder_needs_type_condition?
    klass_scope.where!(klass.send(:type_condition, table))
  end

  klass_scope
end

#jsonb_store?Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/associate_jsonb/reflection.rb', line 6

def jsonb_store?
  options.key?(:store) && jsonb_store_attr.present?
end

#jsonb_store_attrObject



16
17
18
# File 'lib/associate_jsonb/reflection.rb', line 16

def jsonb_store_attr
  options[:store]
end

#jsonb_store_keyObject



20
21
22
# File 'lib/associate_jsonb/reflection.rb', line 20

def jsonb_store_key
  options[:store_key].presence || join_foreign_key
end

#jsonb_store_key?(fk = nil) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
14
# File 'lib/associate_jsonb/reflection.rb', line 10

def jsonb_store_key?(fk = nil)
  options.key?(:store_key) \
  && options[:store_key].present? \
  && (!fk || (jsonb_store_key.to_s != fk.to_s))
end