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

#contextObject



89
90
91
92
93
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 89

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

#createdObject

Override



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 55

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 || {})
    @validation ||= context.field_validation(@prop)

    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

  # TODO: Add a flag so the framework's user can turn it on and off.
  # json.validation @validation if @validation
end

#default_url_optionsObject



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

def default_url_options
  { only_path: true }
end

#determine_value(context, prop) ⇒ Object

To be overridden



85
86
87
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 85

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

#hint(hint) ⇒ Object



26
27
28
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 26

def hint(hint)
  @hint = hint
end

#hint_args(hint_args) ⇒ Object



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

def hint_args(hint_args)
  @hint_args = hint_args
end

#label(label) ⇒ Object



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

def label(label)
  @label = label
end

#label_args(label_args) ⇒ Object



22
23
24
# File 'app/helpers/glib/json_ui/view_builder/fields.rb', line 22

def label_args(label_args)
  @label_args = label_args
end

#name(name) ⇒ Object



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

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

#placeholder(placeholder) ⇒ Object



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

def placeholder(placeholder)
  @placeholder = placeholder
end

#placeholder_args(placeholder_args) ⇒ Object



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

def placeholder_args(placeholder_args)
  @placeholder_args = placeholder_args
end

#prop(prop) ⇒ Object



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

def prop(prop)
  @prop = prop
end

#value(value) ⇒ Object



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

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