Module: CrmFormHelper

Included in:
AddressesController, OrdersController, QuotesController
Defined in:
app/helpers/crm_form_helper.rb

Overview

View helper for CRM forms.

Instance Method Summary collapse

Instance Method Details

#blocking_submit_button(options = {}) ⇒ String

Renders a submit button that blocks the UI while the form submits.

Parameters:

  • options (Hash) (defaults to: {})

    button configuration

Options Hash (options):

  • confirm (String, nil)

    turbo-confirm message shown before submitting

  • data (Hash, nil)

    extra data attributes merged into the button

  • extra_class (String, nil)

    extra CSS classes (defaults to 'mx-2 px-4')

  • button_class (String, nil)

    Bootstrap button variant (defaults to 'success')

  • name (String)

    button name attribute (defaults to 'commit')

  • value (String)

    button value attribute (defaults to '1')

  • id (String, nil)

    button id attribute

  • label (String)

    button label (defaults to 'Continue')

Returns:

  • (String)

    the rendered button HTML



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/crm_form_helper.rb', line 49

def blocking_submit_button(options = {})
  data_hash = { controller: 'ui-blocker', turbo_submits_with: 'Please wait…' }
  data_hash[:turbo_confirm] = options[:confirm] if options[:confirm].present?
  data_hash.merge!(options[:data]) if options[:data].present?
  extra_class = options.delete(:extra_class) || 'mx-2 px-4'
  css_class = "btn btn-#{options[:button_class] || 'success'} #{extra_class}"
  button_tag(type: 'submit',
             name: options[:name] || 'commit',
             value: options[:value] || '1',
             data: data_hash,
             id: options[:id],
             class: css_class) do
    (options[:label] || 'Continue').html_safe
  end
end

#boolean_select(form, field, value, prompt = false, disabled = false) ⇒ String

Renders a Yes/No dropdown for a boolean attribute.

Parameters:

  • form (SimpleForm::FormBuilder)

    the form builder

  • field (Symbol, String)

    the attribute name

  • value (Boolean, nil)

    the selected value

  • prompt (Boolean) (defaults to: false)

    whether to include a blank prompt

  • disabled (Boolean) (defaults to: false)

    whether the select is disabled

Returns:

  • (String)

    the rendered select HTML



12
13
14
# File 'app/helpers/crm_form_helper.rb', line 12

def boolean_select(form, field, value, prompt = false, disabled = false)
  form.select field, options_for_select([["Yes", true], ["No", false]], selected: value), { prompt: prompt }, { disabled: disabled, class: "form-control", style: "max-width:200px" }
end

#datalist(id, values) ⇒ String

Renders an HTML datalist element.

Parameters:

  • id (String)

    the datalist id

  • values (Array<String>)

    the option values

Returns:

  • (String)

    the rendered datalist HTML



166
167
168
169
170
171
172
# File 'app/helpers/crm_form_helper.rb', line 166

def datalist(id, values)
  (:datalist, id: id) do
    values.each do |n|
      concat tag.option(value: n)
    end
  end
end

#edit_buttonString

Renders the standard edit icon button.

Returns:

  • (String)

    the rendered edit link HTML



18
19
20
# File 'app/helpers/crm_form_helper.rb', line 18

def edit_button
  fa_icon('pen-to-square', text: 'Edit')
end

#form_style_attribute_display(label, display = nil, options = {}) { ... } ⇒ String

Displays a form line with the label and display value using the same style
as the form builder simple form uses, pass the value as display parameter
of use a block

Parameters:

  • label (String)

    the label text

  • display (String, nil) (defaults to: nil)

    the value to display (a block may be given instead)

  • options (Hash) (defaults to: {})

    display configuration

Options Hash (options):

  • raw (Boolean)

    render the value in a span instead of a disabled input

  • multi_line (Boolean)

    render the value in a disabled textarea

  • hint (String, nil)

    hint text shown below the value

Yields:

  • [] the value to display when display is omitted

Returns:

  • (String)

    the rendered HTML



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/helpers/crm_form_helper.rb', line 128

def form_style_attribute_display(label, display = nil, options = {})
   :div, class: 'form-group row' do
    (:label, label, class: 'optional col-sm-3 col-form-label') +
      (:div, class: 'col') do
        display_value = display || (yield if block_given?)
        output = if options[:raw]
                   tag.span display_value
                 elsif options[:multi_line]
                   text_area_tag 'label', display_value, class: 'form-control', disabled: true
                 else
                   text_field_tag 'label', display_value, class: 'form-control', disabled: true
                 end
        output << tag.small(options[:hint], class: 'form-text form-hint text-body-secondary') if options[:hint]
        output.html_safe
      end
  end
end

#opportunity_local_sales_rep_input(form, options = {}) ⇒ String

Renders the local sales rep select input for an opportunity form.

Parameters:

  • form (SimpleForm::FormBuilder)

    the form builder

  • options (Hash) (defaults to: {})

    input configuration

Options Hash (options):

  • placeholder (String, nil)

    placeholder text for the tom-select input

Returns:

  • (String)

    the rendered input HTML



70
71
72
73
74
75
76
# File 'app/helpers/crm_form_helper.rb', line 70

def opportunity_local_sales_rep_input(form, options = {})
  form.input :local_sales_rep_id, label: 'Local Sales Rep',
          as: :tom_select,
          collection: Employee.local_sales_rep_options_for_select,
          include_blank: true,
          placeholder: options[:placeholder]
end

#sales_support_rep_input(form, options = {}) ⇒ String

Renders the sales support rep select input.

Parameters:

  • form (SimpleForm::FormBuilder)

    the form builder

  • options (Hash) (defaults to: {})

    input configuration

Options Hash (options):

  • placeholder (String, nil)

    placeholder text for the tom-select input

Returns:

  • (String)

    the rendered input HTML



109
110
111
112
113
114
115
# File 'app/helpers/crm_form_helper.rb', line 109

def sales_support_rep_input(form, options = {})
  form.input :sales_support_rep_id, label: 'Sales Support Rep',
          as: :tom_select,
          collection: Employee.primary_sales_rep_options_for_select,
          include_blank: true,
          placeholder: options[:placeholder]
end

#submit_cancel(options = {}) ⇒ String

Renders the shared submit/cancel button bar partial.

Parameters:

  • options (Hash) (defaults to: {})

    locals forwarded to the _submit_cancel partial

Options Hash (options):

  • cancel (String, nil)

    URL for the cancel link (falls back to @return_path)

  • confirm_text (String, nil)

    confirmation message for the submit button

  • cancel_label (String)

    label for the cancel link (defaults to 'Cancel')

  • cancel_data (Hash, nil)

    data attributes for the cancel link

  • submit_label (String)

    label for the submit button (defaults to 'Save')

  • submit (Hash)

    submit button details, with :label, :id and :data keys

  • delete (String, nil)

    URL to render a delete button

  • previous (String, nil)

    URL to render a previous-step button

  • previous_label (String)

    label for the previous button (defaults to 'Previous')

Returns:

  • (String)

    the rendered partial HTML



34
35
36
# File 'app/helpers/crm_form_helper.rb', line 34

def submit_cancel(options = {})
  render partial: "/submit_cancel", locals: options
end

#technical_rep_input(form, options = {}) ⇒ String

Renders the primary technical rep select input.

Parameters:

  • form (SimpleForm::FormBuilder)

    the form builder

  • options (Hash) (defaults to: {})

    input configuration

Options Hash (options):

  • placeholder (String, nil)

    placeholder text for the tom-select input

Returns:

  • (String)

    the rendered input HTML



83
84
85
86
87
88
89
# File 'app/helpers/crm_form_helper.rb', line 83

def technical_rep_input(form, options = {})
  form.input :technical_support_rep_id, label: 'Technical Rep 1',
          as: :tom_select,
          collection: Employee.technical_support_rep_options_for_select,
          include_blank: true,
          placeholder: options[:placeholder]
end

#technical_rep_sec_input(form, options = {}) ⇒ String

Renders the secondary technical rep select input.

Parameters:

  • form (SimpleForm::FormBuilder)

    the form builder

  • options (Hash) (defaults to: {})

    input configuration

Options Hash (options):

  • placeholder (String, nil)

    placeholder text for the tom-select input

Returns:

  • (String)

    the rendered input HTML



96
97
98
99
100
101
102
# File 'app/helpers/crm_form_helper.rb', line 96

def technical_rep_sec_input(form, options = {})
  form.input :technical_support_rep_sec_id, label: 'Technical Rep 2',
          as: :tom_select,
          collection: Employee.technical_support_rep_options_for_select,
          include_blank: true,
          placeholder: options[:placeholder]
end

#yes_no_blank_boolean_collection_for_select(blank_name = 'n/a') ⇒ Array<Array(String, Boolean)>

Builds a Yes/No collection with a configurable blank label.

Parameters:

  • blank_name (String) (defaults to: 'n/a')

    the label for the blank option

Returns:

  • (Array<Array(String, Boolean)>)

    the select options



158
159
160
# File 'app/helpers/crm_form_helper.rb', line 158

def yes_no_blank_boolean_collection_for_select(blank_name = 'n/a')
  yes_no_boolean_collection_for_select(blank: blank_name)
end

#yes_no_boolean_collection_for_select(blank: nil) ⇒ Array<Array(String, Boolean)>

Builds a Yes/No collection for select inputs.

Parameters:

  • blank (String, nil) (defaults to: nil)

    optional blank label inserted with an empty value

Returns:

  • (Array<Array(String, Boolean)>)

    the select options



149
150
151
152
153
# File 'app/helpers/crm_form_helper.rb', line 149

def yes_no_boolean_collection_for_select(blank: nil)
  r = [['Yes', true], ['No', false]]
  r.insert(0, [blank, '']) if blank
  r
end