Module: ActiveStash::ModelReflection

Defined in:
lib/active_stash/model_reflection.rb

Class Method Summary collapse

Class Method Details

.associated_model(parent_model, association) ⇒ Object



35
36
37
# File 'lib/active_stash/model_reflection.rb', line 35

def self.associated_model(parent_model, association)
  parent_model.reflect_on_association(association).klass
end

.association_names(model) ⇒ Object



29
30
31
32
33
# File 'lib/active_stash/model_reflection.rb', line 29

def self.association_names(model)
  [:has_one, :belongs_to].map do |macro|
    model.reflect_on_all_associations(macro)
  end.flatten.map{|assoc| assoc.name }
end

.associations(model) ⇒ Object



23
24
25
26
27
# File 'lib/active_stash/model_reflection.rb', line 23

def self.associations(model)
  [:has_one, :belongs_to].map do |macro|
    model.reflect_on_all_associations(macro)
  end.flatten
end

.fields(model_class) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/active_stash/model_reflection.rb', line 4

def self.fields(model_class)
  fields = model_class.attribute_types.inject({}) do |attrs, (k,v)|
    type = v.type

    # ActiveRecord encryption is available from Rails 7.
    if Rails::VERSION::MAJOR >= 7
      type = ActiveRecord::Encryption::EncryptedAttributeType === v ? v.cast_type.type : v.type
    end

    attrs[k] = type
    attrs
  end

  without_lockbox_fields(fields, model_class)
  without_id_fields(fields)

  fields
end