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



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

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



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

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



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

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



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

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: onclick) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# 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: 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,
            }
  )
  #%button.btn.btn-danger.btn-sm Reject
end

#sf_bool_value(value) ⇒ Object



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

def sf_bool_value(value)
    symbol = value ? 'check-outline' : 'close-outline'
    mdi_tag "#{symbol} 24px"
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]

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

#sf_model_validation(model) ⇒ Object



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

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

#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"

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

#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

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

#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]

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