Class: Workarea::Configuration::Administrable::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/workarea/configuration/administrable/field.rb

Defined Under Namespace

Classes: Invalid

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type:, fieldset: nil, **options) ⇒ Field

Returns a new instance of Field.



10
11
12
13
14
15
# File 'lib/workarea/configuration/administrable/field.rb', line 10

def initialize(name, type:, fieldset: nil,  **options)
  @name = name
  @type = type.to_s.underscore.to_sym
  @fieldset = fieldset
  @options = OpenStruct.new(options)
end

Instance Attribute Details

#fieldsetObject (readonly)

Returns the value of attribute fieldset.



7
8
9
# File 'lib/workarea/configuration/administrable/field.rb', line 7

def fieldset
  @fieldset
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/workarea/configuration/administrable/field.rb', line 7

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/workarea/configuration/administrable/field.rb', line 7

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/workarea/configuration/administrable/field.rb', line 7

def type
  @type
end

Instance Method Details

#allow_blank?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/workarea/configuration/administrable/field.rb', line 55

def allow_blank?
  !!@options.allow_blank
end

#encrypted?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/workarea/configuration/administrable/field.rb', line 45

def encrypted?
  !!encrypted
end

#idObject



17
18
19
# File 'lib/workarea/configuration/administrable/field.rb', line 17

def id
  @id ||= (options.id || name.to_s.systemize).to_sym
end

#keyObject



21
22
23
# File 'lib/workarea/configuration/administrable/field.rb', line 21

def key
  @key ||= fieldset&.namespaced? ? :"#{fieldset.id}_#{id}" : id
end

#merge!(options = {}) ⇒ Object



59
60
61
# File 'lib/workarea/configuration/administrable/field.rb', line 59

def merge!(options = {})
  @options = OpenStruct.new(options.to_h.merge(options))
end

#overridden?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/workarea/configuration/administrable/field.rb', line 41

def overridden?
  Workarea.config.key?(key)
end

#type_classObject



25
26
27
# File 'lib/workarea/configuration/administrable/field.rb', line 25

def type_class
  Workarea.config.configurable_field_types[type]&.constantize
end

#validate!Object



49
50
51
52
53
# File 'lib/workarea/configuration/administrable/field.rb', line 49

def validate!
  validate_id
  validate_type
  self
end

#valuesObject



29
30
31
32
# File 'lib/workarea/configuration/administrable/field.rb', line 29

def values
  return options.values unless options.values.respond_to?(:call)
  options.values.call
end

#values_type_classObject



34
35
36
37
38
39
# File 'lib/workarea/configuration/administrable/field.rb', line 34

def values_type_class
  return unless type == :hash

  values_type = options.values_type || :string
  Workarea.config.configurable_field_types[values_type]&.constantize
end