Class: TypedEAV::Config

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Configurable
Defined in:
lib/typed_eav/config.rb

Overview

Gem-level configuration for field type registration.

TypedEAV.configure do |c|
  c.register_field_type :phone, "MyApp::Fields::Phone"
end

Accessible from anywhere via ‘TypedEAV.config` (which returns this class; class-level `field_types` / `register_field_type` / `field_class_for` / `type_names` methods are defined below).

Constant Summary collapse

DEFAULT_SCOPE_RESOLVER =

Default ambient-scope resolver. Auto-detects ‘acts_as_tenant` when loaded so AAT users get zero-config behavior. Apps using any other multi-tenancy primitive (Rails `Current` attributes, a subdomain lookup, a thread-local, etc.) override via `TypedEAV.configure`.

lambda {
  ::ActsAsTenant.current_tenant if defined?(::ActsAsTenant)
}
BUILTIN_FIELD_TYPES =

Map of type names to their STI class names. Add custom types via TypedEAV.configure.

{
  text: "TypedEAV::Field::Text",
  long_text: "TypedEAV::Field::LongText",
  integer: "TypedEAV::Field::Integer",
  decimal: "TypedEAV::Field::Decimal",
  boolean: "TypedEAV::Field::Boolean",
  date: "TypedEAV::Field::Date",
  date_time: "TypedEAV::Field::DateTime",
  select: "TypedEAV::Field::Select",
  multi_select: "TypedEAV::Field::MultiSelect",
  integer_array: "TypedEAV::Field::IntegerArray",
  decimal_array: "TypedEAV::Field::DecimalArray",
  text_array: "TypedEAV::Field::TextArray",
  date_array: "TypedEAV::Field::DateArray",
  email: "TypedEAV::Field::Email",
  url: "TypedEAV::Field::Url",
  color: "TypedEAV::Field::Color",
  json: "TypedEAV::Field::Json",
}.freeze

Class Method Summary collapse

Class Method Details

.field_class_for(type_name) ⇒ Object

Resolve a type name to its STI class.

Raises:

  • (ArgumentError)


71
72
73
74
75
76
# File 'lib/typed_eav/config.rb', line 71

def field_class_for(type_name)
  class_name = field_types[type_name.to_sym]
  raise ArgumentError, "Unknown field type: #{type_name}" unless class_name

  class_name.constantize
end

.register_field_type(name, class_name) ⇒ Object

Register a custom field type.



66
67
68
# File 'lib/typed_eav/config.rb', line 66

def register_field_type(name, class_name)
  field_types[name.to_sym] = class_name
end

.reset!Object

Restore defaults (test isolation).



84
85
86
87
88
# File 'lib/typed_eav/config.rb', line 84

def reset!
  self.field_types = BUILTIN_FIELD_TYPES.dup
  self.scope_resolver = DEFAULT_SCOPE_RESOLVER
  self.require_scope = true
end

.type_namesObject

All registered type names.



79
80
81
# File 'lib/typed_eav/config.rb', line 79

def type_names
  field_types.keys
end