class Upgrow::ActiveRecordSchema

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.

Public Class Methods

new(base_name, default_schema) click to toggle source

Set the Schema's initial state.

@param base_name [String] the name of the Active Record Base class that

should be used to fetch attribute names.

@param default_schema [ModelSchema] the original Model Schema to be used

to fetch and define custom attributes.
# File lib/upgrow/active_record_schema.rb, line 16
def initialize(base_name, default_schema)
  @base_name = base_name
  @default_schema = default_schema
end

Public Instance Methods

association(name) click to toggle source

Define a custom association in the default Schema.

@param name [Symbol] the name of the association.

# File lib/upgrow/active_record_schema.rb, line 31
def association(name)
  @default_schema.association(name)
end
association_names() click to toggle source

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.

@return [Array<Symbol>] the list of attribute names.

# 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) click to toggle source

Define a custom attribute in the default Schema.

@param name [Symbol] the name of the new attribute.

# File lib/upgrow/active_record_schema.rb, line 24
def attribute(name)
  @default_schema.attribute(name)
end
attribute_names() click to toggle source

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.

@return [Array<Symbol>] the list of attribute names.

# File lib/upgrow/active_record_schema.rb, line 40
def attribute_names
  base.attribute_names.map(&:to_sym) | @default_schema.attribute_names
end

Private Instance Methods

base() click to toggle source
# File lib/upgrow/active_record_schema.rb, line 57
def base
  Object.const_get(@base_name)
end