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



65
66
67
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 65

def autoValidate(autoValidate)
  @autoValidate = autoValidate
end

#contextObject



105
106
107
108
109
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 105

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

#createdObject

Override



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 70

def created
  if @prop && (form = page.current_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
  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



15
16
17
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 15

def default_url_options
  { only_path: true }
end

#determine_value(context, prop) ⇒ Object

To be overridden



101
102
103
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 101

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

#hint(hint) ⇒ Object



37
38
39
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 37

def hint(hint)
  @hint = hint
end

#hint_args(hint_args) ⇒ Object



41
42
43
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 41

def hint_args(hint_args)
  @hint_args = hint_args
end

#label(label) ⇒ Object



29
30
31
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 29

def label(label)
  @label = label
end

#label_args(label_args) ⇒ Object



33
34
35
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 33

def label_args(label_args)
  @label_args = label_args
end

#name(name) ⇒ Object



53
54
55
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 53

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

#placeholder(placeholder) ⇒ Object



45
46
47
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 45

def placeholder(placeholder)
  @placeholder = placeholder
end

#placeholder_args(placeholder_args) ⇒ Object



49
50
51
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 49

def placeholder_args(placeholder_args)
  @placeholder_args = placeholder_args
end

#prop(prop) ⇒ Object



61
62
63
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 61

def prop(prop)
  @prop = prop
end

#validation(validation) ⇒ Object



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

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



57
58
59
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 57

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