Class: Airview::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/airview/field.rb,
sig/airview.rbs

Constant Summary collapse

TYPES =
%i[
  string text integer float decimal boolean date datetime select belongs_to has_many attachment json
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type:, label: nil, readonly: false, options: nil, model: nil, label_method: nil, default_visible: true) ⇒ Field

Returns a new instance of Field.

Parameters:

  • name (Symbol, String)
  • type: (Symbol, String)
  • label: (String, nil) (defaults to: nil)
  • readonly: (Boolean) (defaults to: false)
  • options: (Object) (defaults to: nil)
  • model: (Object) (defaults to: nil)
  • label_method: (Symbol, nil) (defaults to: nil)


11
12
13
14
15
16
17
18
19
20
21
# File 'lib/airview/field.rb', line 11

def initialize(name, type:, label: nil, readonly: false, options: nil, model: nil, label_method: nil,
               default_visible: true)
  @name = name.to_sym
  @label = label.presence || name.to_s.humanize
  @type = type.to_sym
  @readonly = readonly
  @options = options
  @model = model
  @label_method = label_method || :to_s
  @default_visible = default_visible
end

Instance Attribute Details

#labelString (readonly)

Returns the value of attribute label.

Returns:

  • (String)


9
10
11
# File 'lib/airview/field.rb', line 9

def label
  @label
end

#label_methodSymbol (readonly)

Returns the value of attribute label_method.

Returns:

  • (Symbol)


9
10
11
# File 'lib/airview/field.rb', line 9

def label_method
  @label_method
end

#modelObject (readonly)

Returns the value of attribute model.

Returns:

  • (Object)


9
10
11
# File 'lib/airview/field.rb', line 9

def model
  @model
end

#nameSymbol (readonly)

Returns the value of attribute name.

Returns:

  • (Symbol)


9
10
11
# File 'lib/airview/field.rb', line 9

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.

Returns:

  • (Object)


9
10
11
# File 'lib/airview/field.rb', line 9

def options
  @options
end

#typeSymbol (readonly)

Returns the value of attribute type.

Returns:

  • (Symbol)


9
10
11
# File 'lib/airview/field.rb', line 9

def type
  @type
end

Instance Method Details

#association?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/airview/field.rb', line 35

def association?
  type.in?(%i[belongs_to has_many])
end

#attachment?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/airview/field.rb', line 43

def attachment?
  type == :attachment
end

#attribute_nameSymbol

Returns:

  • (Symbol)


59
60
61
# File 'lib/airview/field.rb', line 59

def attribute_name
  type == :belongs_to ? :"#{name}_id" : name
end

#collection_association?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/airview/field.rb', line 39

def collection_association?
  type == :has_many
end

#default_visible?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/airview/field.rb', line 31

def default_visible?
  @default_visible
end

#display_only?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/airview/field.rb', line 47

def display_only?
  collection_association? || attachment?
end

#editable?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/airview/field.rb', line 27

def editable?
  !readonly? && !display_only?
end

#filterable?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/airview/field.rb', line 51

def filterable?
  !display_only?
end

#model_classObject

Returns:

  • (Object)


69
70
71
72
73
# File 'lib/airview/field.rb', line 69

def model_class
  return nil unless model

  model.to_s.constantize
end

#option_valuesArray[untyped]

Returns:

  • (Array[untyped])


75
76
77
78
79
# File 'lib/airview/field.rb', line 75

def option_values
  return [] unless options

  options.respond_to?(:call) ? options.call : options
end

#readonly?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/airview/field.rb', line 23

def readonly?
  @readonly
end

#sortable?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/airview/field.rb', line 55

def sortable?
  !display_only?
end

#validate!void

This method returns an undefined value.

Raises:



63
64
65
66
67
# File 'lib/airview/field.rb', line 63

def validate!
  return if TYPES.include?(type)

  raise ConfigurationError, "Unsupported Airview field type #{type.inspect} for #{name}"
end