Class: Upgrow::ActiveRecordSchema
- Inherits:
-
Object
- Object
- Upgrow::ActiveRecordSchema
- 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
-
#association(name) ⇒ Object
Define a custom association in the default Schema.
-
#association_names ⇒ Array<Symbol>
The list of association names.
-
#attribute(name) ⇒ Object
Define a custom attribute in the default Schema.
-
#attribute_names ⇒ Array<Symbol>
The list of attribute names.
-
#initialize(base_name, default_schema) ⇒ ActiveRecordSchema
constructor
Set the Schema’s initial state.
Constructor Details
#initialize(base_name, default_schema) ⇒ ActiveRecordSchema
Set the Schema’s initial state.
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.
31 32 33 |
# File 'lib/upgrow/active_record_schema.rb', line 31 def association(name) @default_schema.association(name) end |
#association_names ⇒ Array<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.
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.
24 25 26 |
# File 'lib/upgrow/active_record_schema.rb', line 24 def attribute(name) @default_schema.attribute(name) end |
#attribute_names ⇒ Array<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.
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 |