Module: Shopapp3FormHelper

Defined in:
app/helpers/shopapp3_form_helper.rb

Instance Method Summary collapse

Instance Method Details

#format_js_render_remote_response(destination_selector, partial, locals = {}) ⇒ Object



175
176
177
178
179
180
181
# File 'app/helpers/shopapp3_form_helper.rb', line 175

def format_js_render_remote_response(destination_selector, partial, locals = {})
  respond_to do |format|
    format.js {
      render_remote_response(destination_selector, partial, locals)
    }
  end
end

#help_tag(tag, params) ⇒ Object

private helpers



162
163
164
165
166
# File 'app/helpers/shopapp3_form_helper.rb', line 162

def help_tag(tag, params)
  if params[:help].present?
    %(<small class="form-text text-muted" id="#{tag}_help">#{params[:help]}</small>)
  end
end

#invalid_feedback(params) ⇒ Object



168
169
170
171
172
173
# File 'app/helpers/shopapp3_form_helper.rb', line 168

def invalid_feedback(params)
  if params[:invalid_feedback].present?
    %(<div class="invalid-feedback">#{params[:invalid_feedback]}</div>)
  end

end

#label_flag(tag, params) ⇒ Object



29
30
31
32
33
# File 'app/helpers/shopapp3_form_helper.rb', line 29

def label_flag(tag, params)
  if params[:language].present?
    render partial: 'shopapp/display_flag', locals: { language: params[:language], html_class: ""}
  end
end

#render_remote_response(destination_selector, partial, locals = {}) ⇒ Object



183
184
185
186
187
188
189
190
191
# File 'app/helpers/shopapp3_form_helper.rb', line 183

def render_remote_response(destination_selector, partial, locals = {})
  status = locals[:status]
  locals.delete :status
  render :render_remote_response, status: status, locals: {
    destination_selector: destination_selector,
    partial: partial.to_s,
    locals: locals
  }
end

#sf_action(url, action_name = nil, text = nil, remote: true, method: :post, additional_classes: nil, form_class: nil, onclick_action: onclick) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/helpers/shopapp3_form_helper.rb', line 126

def sf_action(url, action_name = nil, text = nil, remote: true, method: :post, additional_classes: nil, form_class: nil, onclick_action: onclick)
  action_name ||= :submit
  button_to((text || action_name.to_s.humanize),
            url,
            {
              form_class: (form_class || 'float-right mr-2'),
              name: :button_action,
              method: method,
              remote: remote,
              class: ["btn", additional_classes || 'btn-secondary'].join(' '),
              data: { disable_with: "Please wait..." },
              onclick: onclick_action,
            }
  )
end

#sf_bool_value(value) ⇒ Object



155
156
157
158
# File 'app/helpers/shopapp3_form_helper.rb', line 155

def sf_bool_value(value)
    symbol = value ? 'check-outline' : 'close-outline'
    mdi_tag(size: 24, icon: symbol)
end

#sf_cancel(return_path, label = 'Cancel', params = {}) ⇒ Object



122
123
124
# File 'app/helpers/shopapp3_form_helper.rb', line 122

def sf_cancel(return_path, label = 'Cancel', params = {})
  %(<a href="#{return_path}" class="btn btn-warning float-right ml-2 mt-3" role="button">#{label}</a>).html_safe
end

#sf_checkbox(f, tag, params = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/helpers/shopapp3_form_helper.rb', line 58

def sf_checkbox(f, tag, params = {})
  unless params.key?(:label)
    params[:label] = tag.to_s.capitalize
  end
  label_tag = f.label tag,
              value: params[:label],
              class: 'form-check-label'
  input_tag = f.check_box tag,
                           value: params[:value],
                           placeholder: params[:placeholder],
                           class: 'form-check-input',
                           required: params[:required],
                           'aria-describedby' => "#{tag}_help",
                           readonly: params[:readonly]

  "    <div class=\"form-group form-check pt-2\">\n      \#{input_tag}\n      \#{label_tag}\n      \#{help_tag(tag, params)}\n      \#{invalid_feedback(params)}\n    </div>\n  HTML\nend\n".html_safe

#sf_model_validation(model) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/helpers/shopapp3_form_helper.rb', line 142

def sf_model_validation(model)
  if model.errors.any?
    "      <div class=\"alert alert-danger\">\n        <h5>\#{pluralize(@product.errors.count, \"error\")} appeared in the form:</h5>\n        <ul>\n          \#{model.errors.full_messages.map { |msg| \"<li>\#{msg}</li>\" }.join}\n        <ul>\n      </div>\n    HTML\n  end\nend\n".html_safe

#sf_number_field(f, tag, params = {}) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/helpers/shopapp3_form_helper.rb', line 35

def sf_number_field(f, tag, params = {})
  unless params.key?(:label)
    params[:label] = tag.to_s.capitalize
  end
  label_tag = f.label tag, params[:label]
  input_tag = f.number_field tag,
                             value: params[:value],
                             min: 0,
                             placeholder: params[:placeholder],
                             class: 'form-control',
                             required: params[:required],
                             'aria-describedby' => "#{tag}_help"

  "    <div class=\"form-group\">\n      \#{label_tag}\n      \#{input_tag}\n      \#{help_tag(tag, params)}\n      \#{invalid_feedback(params)}\n    </div>\n  HTML\nend\n".html_safe

#sf_select(f, tag, params = {}) ⇒ Object



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
# File 'app/helpers/shopapp3_form_helper.rb', line 83

def sf_select(f, tag, params = {})
  unless params.key?(:label)
    params[:label] = tag.to_s.capitalize
  end
  label_tag = f.label tag, params[:label]
  options_list = if params.key? :enum
                   options_for_select(params[:enum].keys, params[:selected_value])
                 elsif params.key? :list
                   options_for_select(params[:list], params[:selected_value])
                 else
                   # we will impolement them as we go
                   fail NotImplementedError
                 end

  select_tag = f.select tag,
                        options_list,
                        { include_blank: (params[:blank_option_title] || "Please select...") }.merge(params[:select_params].to_h),
                        { class: "custom-select", required: params[:required] }.merge(params[:html_params].to_h)
  help_tag = if params[:help].present?
    %(<small class="form-text text-muted" id="#{tag}_help">#{params[:help]}</small>)
  end

  "    <div class=\"form-group\">\n      \#{label_tag}\n      \#{select_tag}\n      \#{help_tag(tag, params)}\n      \#{invalid_feedback(params)}\n    </div>\n  HTML\nend\n".html_safe

#sf_submit(*params_numbered, **params) ⇒ Object



115
116
117
118
119
120
# File 'app/helpers/shopapp3_form_helper.rb', line 115

def sf_submit(*params_numbered, **params)
  rails ArgumentError unless params_numbered.count <= 1
  params_numbered << 'Submit' if params_numbered.count < 1
  # label = 'Submit'
  %(<button name="#{params[:name]}" type="submit" class="btn btn-primary float-right ml-2 mt-3 #{params[:class]}">#{params_numbered[0]}</button>).html_safe
end

#sf_text_field(f, tag, params = {}) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/helpers/shopapp3_form_helper.rb', line 2

def sf_text_field(f, tag, params = {})
  unless params.key?(:label)
    params[:label] = tag.to_s.capitalize
  end
  label_tag = f.label tag, params[:label] unless params[:label].nil?
  input_tag = f.text_field tag,
                           value: params[:value],
                           placeholder: params[:placeholder],
                           class: ["form-control", params[:class]].join(' '),
                           required: params[:required],
                           'aria-describedby' => "#{tag}_help",
                           readonly: params[:readonly],
                           onkeyup: params[:onkeyup],
                           pattern: params[:pattern],
                           title: params[:title]

  "    <div class=\"form-group\">\n      \#{label_flag(tag, params)}\n      \#{label_tag}\n      \#{input_tag}\n      \#{help_tag(tag, params)}\n      \#{invalid_feedback(params)}\n    </div>\n  HTML\nend\n".html_safe