Module: SSHTunnel::UI::Helpers::Common::FormHelper::InstanceMethods

Included in:
HostWindowHelper, TunnelWindowHelper
Defined in:
lib/ssh-hull/ui/helpers/common/form_helper.rb

Instance Method Summary collapse

Instance Method Details

#bind_submit_buttonObject



49
50
51
52
53
54
# File 'lib/ssh-hull/ui/helpers/common/form_helper.rb', line 49

def bind_submit_button
  button_submit.label = t('button.submit')
  button_submit.signal_connect :clicked do
    submit_form(params)
  end
end

#form_objectObject

Raises:

  • (NotImplementedError)


69
70
71
# File 'lib/ssh-hull/ui/helpers/common/form_helper.rb', line 69

def form_object
  raise NotImplementedError
end

#getter_for(input_type) ⇒ Object



150
151
152
# File 'lib/ssh-hull/ui/helpers/common/form_helper.rb', line 150

def getter_for(input_type)
  INPUT_TYPES.dig(input_type, :getter)
end

#paramsObject



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ssh-hull/ui/helpers/common/form_helper.rb', line 35

def params
  params = {}
  form_fields.each do |field_name, opts|
    # get input value
    input  = "input_#{field_name}"
    field  = __send__(input)
    getter = getter_for(opts[:type])
    value = field.__send__(getter)
    params[field_name] = value
  end
  params
end

#redObject



90
91
92
# File 'lib/ssh-hull/ui/helpers/common/form_helper.rb', line 90

def red
  Gdk::RGBA.new(255, 0, 0)
end

#render_form_errors(errors) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/ssh-hull/ui/helpers/common/form_helper.rb', line 79

def render_form_errors(errors)
  form_fields.each do |field_name, _method|
    has_errors = errors.of_kind?(field_name, :blank) || errors.of_kind?(field_name, :inclusion)
    input = "input_#{field_name}"
    field = __send__(input)
    color = has_errors ? red : white
    field.override_background_color(:normal, color)
  end
end

#restore_form_value(model, field_name, opts) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ssh-hull/ui/helpers/common/form_helper.rb', line 107

def restore_form_value(model, field_name, opts)
  # get model value
  value = model.__send__(field_name)

  # set input form
  input  = "input_#{field_name}"
  field  = __send__(input)
  setter = setter_for(opts[:type])

  if opts[:type] == :file && value.nil?
    field.unselect_all
  else
    field.__send__(setter, value)
  end
end

#restore_form_values(model) ⇒ Object



100
101
102
103
104
# File 'lib/ssh-hull/ui/helpers/common/form_helper.rb', line 100

def restore_form_values(model)
  form_fields.each do |field_name, opts|
    restore_form_value(model, field_name, opts)
  end
end

#save_and_reload_viewObject

Raises:

  • (NotImplementedError)


74
75
76
# File 'lib/ssh-hull/ui/helpers/common/form_helper.rb', line 74

def save_and_reload_view
  raise NotImplementedError
end

#set_input_labels(scope:) ⇒ Object



155
156
157
158
159
160
161
162
# File 'lib/ssh-hull/ui/helpers/common/form_helper.rb', line 155

def set_input_labels(scope:)
  form_fields.each do |field_name, _method|
    label  = "label_#{field_name}"
    field  = __send__(label)
    value  = t("form.#{scope}.#{field_name}")
    field.text = value
  end
end

#setter_for(input_type) ⇒ Object



145
146
147
# File 'lib/ssh-hull/ui/helpers/common/form_helper.rb', line 145

def setter_for(input_type)
  INPUT_TYPES.dig(input_type, :setter)
end

#submit_form(params) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/ssh-hull/ui/helpers/common/form_helper.rb', line 57

def submit_form(params)
  form = form_object
  form.submit(params)
  if form.valid?
    form.save
    save_and_reload_view
  else
    render_form_errors(form.errors)
  end
end

#whiteObject



95
96
97
# File 'lib/ssh-hull/ui/helpers/common/form_helper.rb', line 95

def white
  Gdk::RGBA.new(255, 255, 255)
end