Class: Upgrow::ActiveRecordSchema

Inherits:
Object
  • Object
show all
Defined in:
lib/upgrow/active_record_schema.rb

Overview

A Schema that dynamically infers attribute names from an Active Record Base, plus the attributes from an original Schema.

This object is used to power Models so they can include Active Record attributes from a matching class based on the Model’s name.

Instance Method Summary collapse

Constructor Details

#initialize(base_name, default_schema) ⇒ ActiveRecordSchema

Set the Schema’s initial state.

Parameters:

  • base_name (String)

    the name of the Active Record Base class that should be used to fetch attribute names.

  • default_schema (ModelSchema)

    the original Model Schema to be used to fetch and define custom attributes.



16
17
18
19
# File 'lib/upgrow/active_record_schema.rb', line 16

def initialize(base_name, default_schema)
  @base_name = base_name
  @default_schema = default_schema
end

Instance Method Details

#association(name) ⇒ Object

Define a custom association in the default Schema.

Parameters:

  • name (Symbol)

    the name of the association.



31
32
33
# File 'lib/upgrow/active_record_schema.rb', line 31

def association(name)
  @default_schema.association(name)
end

#association_namesArray<Symbol>

The list of association names. This is an aggregate of both the associations from the Active Record Base as well as any custom associations from the default Schema.

Returns:

  • (Array<Symbol>)

    the list of attribute names.



49
50
51
52
53
# File 'lib/upgrow/active_record_schema.rb', line 49

def association_names
  association_names = base.reflections.keys.map(&:to_sym)

  association_names | @default_schema.association_names
end

#attribute(name) ⇒ Object

Define a custom attribute in the default Schema.

Parameters:

  • name (Symbol)

    the name of the new attribute.



24
25
26
# File 'lib/upgrow/active_record_schema.rb', line 24

def attribute(name)
  @default_schema.attribute(name)
end

#attribute_namesArray<Symbol>

The list of attribute names. This is an aggregate of both the attributes from the Active Record Base as well as any custom attributes from the default Schema.

Returns:

  • (Array<Symbol>)

    the list of attribute names.



40
41
42
# File 'lib/upgrow/active_record_schema.rb', line 40

def attribute_names
  base.attribute_names.map(&:to_sym) | @default_schema.attribute_names
end