Class: Glib::JsonUi::ViewBuilder::Fields::AbstractField

Inherits:
View show all
Defined in:
app/helpers/glib/json_ui/view_builder/fields.rb

Instance Attribute Summary

Attributes inherited from JsonUiElement

#json, #page

Instance Method Summary collapse

Methods inherited from View

component_name

Methods inherited from JsonUiElement

#initialize, #props

Constructor Details

This class inherits a constructor from Glib::JsonUi::JsonUiElement

Instance Method Details

#autoValidate(autoValidate) ⇒ Object



67
68
69
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 67

def autoValidate(autoValidate)
  @autoValidate = autoValidate
end

#contextObject



121
122
123
124
125
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 121

def context
  form = page.current_form
  association = form.nested_associations.last
  @context ||= association || form
end

#createdObject

Override



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 76

def created
  form = page.current_form
  if @prop && form
    context.field_assert_respond_to(@prop)

    @name ||= context.field_name(@prop, @multiple || false)
    @label ||= context.field_label(@prop, @label_args || {})
    @hint ||= context.hint_label(@prop, @hint_args || {})
    @placeholder ||= context.placeholder_label(@prop, @placeholder_args || {})
    if form._autoValidate && @autoValidate.nil? || !@autoValidate.nil? && @autoValidate
      @validation ||= context.field_validation(@prop)
    end

    if form.current_dynamic_group.nil?
      # This is not relevant inside a dynamic group
      @value ||= determine_value(context, @prop)
    end
  end

  if form.present? && !form.disable_dirty_check.nil?
    json.set! 'disableDirtyCheck', form.disable_dirty_check
  end

  if !@disable_dirty_check.nil?
    json.set! 'disableDirtyCheck', @disable_dirty_check
  end

  json.name @name
  json.value @value if @value
  json.label @label if @label
  json.hint @hint if @hint

  # In general, use hint over placeholder for the following reasons:
  # - Placeholder gets hidden when there is input which may be confusing
  # - Placeholder competes with label for space
  json.placeholder @placeholder if @placeholder

  json.validation @validation if @validation.present?
end

#default_url_optionsObject



17
18
19
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 17

def default_url_options
  { only_path: true }
end

#determine_value(context, prop) ⇒ Object

To be overridden



117
118
119
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 117

def determine_value(context, prop)
  context.field_value(prop)
end

#disableDirtyCheck(value) ⇒ Object



71
72
73
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 71

def disableDirtyCheck(value)
  @disable_dirty_check = value
end

#hint(hint) ⇒ Object



39
40
41
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 39

def hint(hint)
  @hint = hint
end

#hint_args(hint_args) ⇒ Object



43
44
45
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 43

def hint_args(hint_args)
  @hint_args = hint_args
end

#label(label) ⇒ Object



31
32
33
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 31

def label(label)
  @label = label
end

#label_args(label_args) ⇒ Object



35
36
37
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 35

def label_args(label_args)
  @label_args = label_args
end

#name(name) ⇒ Object



55
56
57
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 55

def name(name)
  @name = name if name != Glib::Value::DEFAULT
end

#placeholder(placeholder) ⇒ Object



47
48
49
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 47

def placeholder(placeholder)
  @placeholder = placeholder
end

#placeholder_args(placeholder_args) ⇒ Object



51
52
53
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 51

def placeholder_args(placeholder_args)
  @placeholder_args = placeholder_args
end

#prop(prop) ⇒ Object



63
64
65
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 63

def prop(prop)
  @prop = prop
end

#validation(validation) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 21

def validation(validation)
  return if validation.blank?

  if validation[:format].present?
    context.cast_to_js_regex(validation[:format])
  end

  json.validation validation
end

#value(value) ⇒ Object



59
60
61
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 59

def value(value)
  @value = value if value != Glib::Value::DEFAULT
end