Module: Foobara::ActiveRecordType

Defined in:
foobara-active-record-type-0.0.14/lib/foobara/active_record_type.rb,
foobara-active-record-type-0.0.14/src/casters/hash.rb,
foobara-active-record-type-0.0.14/src/active_record_type.rb,
foobara-active-record-type-0.0.14/src/casters/primary_key.rb,
foobara-active-record-type-0.0.14/src/active_record_foobara_methods.rb,
foobara-active-record-type-0.0.14/src/extend_active_record_type_declaration.rb,
foobara-active-record-type-0.0.14/src/extend_active_record_type_declaration/hash_desugarizer.rb,
foobara-active-record-type-0.0.14/src/extend_active_record_type_declaration/to_type_transformer.rb,
foobara-active-record-type-0.0.14/src/extend_active_record_type_declaration/model_class_desugarizer.rb,
foobara-active-record-type-0.0.14/src/extend_active_record_type_declaration/primary_key_desugarizer.rb,
foobara-active-record-type-0.0.14/src/extend_active_record_type_declaration/validate_primary_key_present.rb,
foobara-active-record-type-0.0.14/src/extend_active_record_type_declaration/attributes_handler_desugarizer.rb,
foobara-active-record-type-0.0.14/src/extend_active_record_type_declaration/validate_primary_key_is_symbol.rb,
foobara-active-record-type-0.0.14/src/extend_active_record_type_declaration/active_record_base_class_desugarizer.rb,
foobara-active-record-type-0.0.14/src/extend_active_record_type_declaration/validate_primary_key_references_attribute.rb,
foobara-active-record-type-0.0.14/src/extend_active_record_type_declaration/type_declaration_extension/registered_type_declaration/desugarizers/registered_active_record_base_class_desugarizer.rb,
foobara-active-record-type-0.0.14/src/extend_active_record_type_declaration/type_declaration_extension/registered_type_declaration/desugarizers/unregistered_active_record_base_class_desugarizer.rb

Overview

TODO: I think we should have a configuration that indicates if created records can have primary keys past to them or not. That is, do primary keys get issued by the database upon insertion? Or are they generated externally and passed in? Would be nice to have programmatic clarification via explicit configuration.

Defined Under Namespace

Modules: ActiveRecordFoobaraMethods, Casters Classes: ExtendActiveRecordTypeDeclaration

Class Method Summary collapse

Class Method Details

.column_from_name(active_record_class, name) ⇒ Object



29
30
31
32
33
# File 'foobara-active-record-type-0.0.14/src/active_record_type.rb', line 29

def column_from_name(active_record_class, name)
  active_record_class.columns.find do |column|
    column.name == name
  end
end

.column_name_to_foobara_type_declaration(active_record_class, name) ⇒ Object



4
5
6
7
# File 'foobara-active-record-type-0.0.14/src/active_record_type.rb', line 4

def column_name_to_foobara_type_declaration(active_record_class, name)
  column = column_from_name(active_record_class, name)
  column_to_foobara_type_declaration(column)
end

.column_to_foobara_type_declaration(column) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'foobara-active-record-type-0.0.14/src/active_record_type.rb', line 9

def column_to_foobara_type_declaration(column)
  column_type = column..type

  type_declaration = case column_type
                     when :integer, :string, :datetime, :boolean
                       column_type
                     else
                       # :nocov:
                       raise ArgumentError, "Not sure how to convert #{column_type} to a foobara type symbol"
                       # :nocov:
                     end

  # defaults and required will be handled further up
  if column.null
    { type: type_declaration, allow_nil: true }
  else
    type_declaration
  end
end

.install!Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'foobara-active-record-type-0.0.14/lib/foobara/active_record_type.rb', line 9

def install!
  TypeDeclarations.register_type_declaration(ExtendActiveRecordTypeDeclaration.new)

  detached_entity = Namespace.global.foobara_lookup_type!(:detached_entity)
  type = BuiltinTypes.build_and_register!(
    :active_record,
    detached_entity,
    nil,
    type_module: Foobara::ActiveRecordType
  )
  type.remove_processor_by_symbol(:attributes_declaration)

  BuiltinTypes.install_type_declaration_extensions_for(ExtendActiveRecordTypeDeclaration)

  ActiveRecord::Base.include ModelAttributeHelpers::Concerns::AttributeHelpers
  ActiveRecord::Base.include ActiveRecordFoobaraMethods
  ActiveRecord::Base.include Foobara::Model::Concerns::Reflection
  ActiveRecord::Base.include Foobara::DetachedEntity::Concerns::Reflection

  if defined?(Foobara::CommandConnectors::RailsCommandConnector)
    Foobara::CommandConnectors::RailsCommandConnector.default_serializers = [
      Foobara::CommandConnectors::Serializers::ErrorsSerializer,
      Foobara::CommandConnectors::Serializers::ActiveRecordAtomicSerializer,
      Foobara::CommandConnectors::Serializers::JsonSerializer
    ]
  end

  DetachedEntityType.types_requiring_conversion << :active_record
  DetachedEntityType.model_base_classes_requiring_conversion << "ActiveRecord::Base"
end

.reset_allObject



40
41
42
43
44
# File 'foobara-active-record-type-0.0.14/lib/foobara/active_record_type.rb', line 40

def reset_all
  Entity::Concerns::Callbacks.reset_all

  install!
end