Class: TinyAdmin::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/tiny_admin/field.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, title:, type:, options: {}) ⇒ Field

Returns a new instance of Field.



7
8
9
10
11
12
# File 'lib/tiny_admin/field.rb', line 7

def initialize(name:, title:, type:, options: {})
  @type = type
  @name = name
  @title = title
  @options = options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/tiny_admin/field.rb', line 5

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/tiny_admin/field.rb', line 5

def options
  @options
end

#titleObject (readonly)

Returns the value of attribute title.



5
6
7
# File 'lib/tiny_admin/field.rb', line 5

def title
  @title
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/tiny_admin/field.rb', line 5

def type
  @type
end

Class Method Details

.create_field(name:, title: nil, type: nil, options: {}) ⇒ Object



33
34
35
36
37
# File 'lib/tiny_admin/field.rb', line 33

def create_field(name:, title: nil, type: nil, options: {})
  field_name = name.to_s
  field_title = field_name.respond_to?(:humanize) ? field_name.humanize : field_name.tr('_', ' ').capitalize
  new(name: field_name, title: title || field_title, type: type || :string, options: options || {})
end

Instance Method Details

#apply_call_option(target) ⇒ Object



14
15
16
17
# File 'lib/tiny_admin/field.rb', line 14

def apply_call_option(target)
  messages = (options[:call] || '').split(',').map(&:strip)
  messages.inject(target) { |result, msg| result&.send(msg) } if messages.any?
end

#translate_value(value) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/tiny_admin/field.rb', line 19

def translate_value(value)
  if options && options[:method]
    method, *args = options[:method].split(',').map(&:strip)
    if options[:converter]
      Object.const_get(options[:converter]).send(method, value, options: args || [])
    else
      TinyAdmin.settings.helper_class.send(method, value, options: args || [])
    end
  else
    value&.to_s
  end
end