Module: Praxis::Mapper::ActiveModelCompat::ClassMethods

Defined in:
lib/praxis/mapper/active_model_compat.rb

Instance Method Summary collapse

Instance Method Details

#_add_includes(base, includes) ⇒ Object



94
95
96
# File 'lib/praxis/mapper/active_model_compat.rb', line 94

def _add_includes(base, includes)
  base.includes(includes) # includes(nil) seems to have no effect
end

#_all(conditions = {}) ⇒ Object



90
91
92
# File 'lib/praxis/mapper/active_model_compat.rb', line 90

def _all(conditions = {})
  where(conditions)
end

#_field_selector_query_builder_classObject



22
23
24
# File 'lib/praxis/mapper/active_model_compat.rb', line 22

def _field_selector_query_builder_class
  Praxis::Extensions::FieldSelection::ActiveRecordQuerySelector
end

#_filter_query_builder_classObject



18
19
20
# File 'lib/praxis/mapper/active_model_compat.rb', line 18

def _filter_query_builder_class
  Praxis::Extensions::AttributeFiltering::ActiveRecordFilterQueryBuilder
end

#_firstObject



98
99
100
# File 'lib/praxis/mapper/active_model_compat.rb', line 98

def _first
  first
end

#_get(condition) ⇒ Object

Compatible reader accessors



86
87
88
# File 'lib/praxis/mapper/active_model_compat.rb', line 86

def _get(condition)
  find_by(condition)
end

#_join_foreign_key_for(assoc_reflection) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/praxis/mapper/active_model_compat.rb', line 69

def _join_foreign_key_for(assoc_reflection)
  if ActiveRecord.gem_version >= Gem::Version.new('6.1')
    assoc_reflection.join_foreign_key.to_sym
  else # below 6.1
    assoc_reflection.join_keys.foreign_key.to_sym
  end
end

#_join_primary_key_for(assoc_reflection) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/praxis/mapper/active_model_compat.rb', line 77

def _join_primary_key_for(assoc_reflection)
  if ActiveRecord.gem_version >= Gem::Version.new('6.1')
    assoc_reflection.join_primary_key.to_sym
  else # below 6.1
    assoc_reflection.join_keys.key.to_sym
  end
end

#_lastObject



102
103
104
# File 'lib/praxis/mapper/active_model_compat.rb', line 102

def _last
  last
end

#_pagination_query_builder_classObject



26
27
28
# File 'lib/praxis/mapper/active_model_compat.rb', line 26

def _pagination_query_builder_class
  Praxis::Extensions::Pagination::ActiveRecordPaginationHandler
end

#_praxis_associationsObject



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
58
59
60
61
62
63
64
65
66
67
# File 'lib/praxis/mapper/active_model_compat.rb', line 30

def _praxis_associations
  # Memoize the hash in the model, to avoid recomputing expensive AR reflection lookups
  # NOTE: should this be finalized with the resources? or do we know if all associations and such that are needed here will never change?
  return @_praxis_associations if @_praxis_associations

  orig = reflections.clone

  @_praxis_associations = orig.each_with_object({}) do |(k, v), hash|
    # Assume an 'id' primary key if the system is initializing without AR connected
    # (or without the tables created). This probably means that it's a rake task initializing or so...
    pkey = \
      if v.klass.connected? && v.klass.table_exists?
        v.klass.primary_key
      else
        'id'
      end
    info = { model: v.klass, primary_key: pkey }
    info[:type] = \
      case v
      when ActiveRecord::Reflection::BelongsToReflection
        :many_to_one
      when ActiveRecord::Reflection::HasManyReflection, ActiveRecord::Reflection::HasOneReflection
        :one_to_many
      when ActiveRecord::Reflection::ThroughReflection
        :many_to_many
      else
        raise "Unknown association type: #{v.class.name} on #{v.klass.name} for #{v.name}"
      end
    # Call out any local (i.e., of this model) columns that participate in the association
    info[:local_key_columns] = local_columns_used_for_the_association(info[:type], v)
    info[:remote_key_columns] = remote_columns_used_for_the_association(info[:type], v)

    if v.is_a?(ActiveRecord::Reflection::ThroughReflection)
      info[:through] = v.through_reflection.name # TODO: is this correct?
    end
    hash[k.to_sym] = info
  end
end