Module: Lato::ComponentsHelper

Included in:
ApplicationHelper
Defined in:
app/helpers/lato/components_helper.rb

Instance Method Summary collapse

Instance Method Details

#_lato_form_input_options(form, key, options, action_change_event, classes = '') ⇒ Object

Forms



74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/helpers/lato/components_helper.rb', line 74

def _lato_form_input_options(form, key, options, action_change_event, classes = '')
  # setup classes
  options[:class] ||= []
  options[:class].push(classes)
  options[:class].push('is-invalid') if form.object && form.object.errors[key] && form.object.errors[key].any?

  # setup stimulus
  options[:data] ||= {}
  options[:data][:action] ||= ''
  options[:data][:action] += " #{action_change_event}->lato-form#onInputChange"
  options[:data][:lato_form_target] = 'input'
end

#lato_data_badge(label, color = 'primary') ⇒ Object

Data



188
189
190
# File 'app/helpers/lato/components_helper.rb', line 188

def lato_data_badge(label, color = 'primary')
   :span, label, class: "badge rounded-pill bg-#{color}"
end

#lato_data_user(label, image_url = nil) ⇒ Object



192
193
194
195
196
197
198
199
# File 'app/helpers/lato/components_helper.rb', line 192

def lato_data_user(label, image_url = nil)
  image_url ||= image_path('lato/user-150x150')

   :div, class: 'd-flex align-items-center' do
    concat  :div, '', class: 'border border-2 rounded-circle me-2', style: "background-position: center; background-size: cover; background-repeat: no-repeat; background-image: url(#{image_url}); width: 30px; height: 30px;"
    concat  :small, label, class: 'text-black'
  end
end

#lato_form_errors(instance, options = {}) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/helpers/lato/components_helper.rb', line 100

def lato_form_errors(instance, options = {})
  return unless instance.errors.any?

  options[:class] ||= []
  options[:class] += %w[alert alert-danger]
  options[:class] += %w[alert-dismissible fade show] unless options[:fixed]

  errors_list = (:ul, class: %w[mb-0 ps-3]) do
    instance.errors.collect do |error|
       :li, error.full_message
    end.join.html_safe
  end

   :div, options do
    concat (:span, 'Si sono verificati i seguenti errori:')
    concat errors_list
    concat button_tag('', type: 'button', class: 'btn-close', data: { bs_dismiss: 'alert' }) unless options[:fixed]
  end
end

#lato_form_item_input_check(form, key, label, options = {}) ⇒ Object



151
152
153
154
155
156
157
158
159
160
# File 'app/helpers/lato/components_helper.rb', line 151

def lato_form_item_input_check(form, key, label, options = {})
  _lato_form_input_options(form, key, options, :change, 'form-check-input')

  # TO-DO: Trovare il modo di calcolare l'id dato da rails a check_input_tag e metterlo nell'attributo :for di check_label_tag

   :div, class: 'form-check' do
    concat form.check_box(key, options)
    concat label_tag(key, raw(label), class: 'form-check-label')
  end
end

#lato_form_item_input_email(form, key, options = {}) ⇒ Object



139
140
141
142
143
# File 'app/helpers/lato/components_helper.rb', line 139

def lato_form_item_input_email(form, key, options = {})
  _lato_form_input_options(form, key, options, :keyup, 'form-control')

  form.email_field key, options
end

#lato_form_item_input_file(form, key, options = {}) ⇒ Object



168
169
170
171
172
# File 'app/helpers/lato/components_helper.rb', line 168

def lato_form_item_input_file(form, key, options = {})
  _lato_form_input_options(form, key, options, :change, 'form-control')

  form.file_field key, options
end

#lato_form_item_input_number(form, key, options = {}) ⇒ Object



133
134
135
136
137
# File 'app/helpers/lato/components_helper.rb', line 133

def lato_form_item_input_number(form, key, options = {})
  _lato_form_input_options(form, key, options, :keyup, 'form-control')

  form.number_field key, options
end

#lato_form_item_input_password(form, key, options = {}) ⇒ Object



145
146
147
148
149
# File 'app/helpers/lato/components_helper.rb', line 145

def lato_form_item_input_password(form, key, options = {})
  _lato_form_input_options(form, key, options, :keyup, 'form-control')

  form.password_field key, options
end

#lato_form_item_input_select(form, key, select_options = [], options = {}) ⇒ Object



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

def lato_form_item_input_select(form, key, select_options = [], options = {})
  _lato_form_input_options(form, key, options, :change, 'form-control')

  form.select key, select_options, {}, options
end

#lato_form_item_input_text(form, key, options = {}) ⇒ Object



127
128
129
130
131
# File 'app/helpers/lato/components_helper.rb', line 127

def lato_form_item_input_text(form, key, options = {})
  _lato_form_input_options(form, key, options, :keyup, 'form-control')

  form.text_field key, options
end

#lato_form_item_label(form, key, label = nil, options = {}) ⇒ Object



120
121
122
123
124
125
# File 'app/helpers/lato/components_helper.rb', line 120

def lato_form_item_label(form, key, label = nil, options = {})
  options[:class] ||= []
  options[:class].push('form-label')

  form.label key, label, options
end

#lato_form_notices(options = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/helpers/lato/components_helper.rb', line 87

def lato_form_notices(options = {})
  return unless notice

  options[:class] ||= []
  options[:class] += %w[alert alert-success]
  options[:class] += %w[alert-dismissible fade show] unless options[:fixed]

   :div, options do
    concat notice
    concat button_tag('', type: 'button', class: 'btn-close', data: { bs_dismiss: 'alert' }) unless options[:fixed]
  end
end

#lato_form_submit(form, label, options = {}) ⇒ Object



174
175
176
177
178
179
180
181
182
183
# File 'app/helpers/lato/components_helper.rb', line 174

def lato_form_submit(form, label, options = {})
  options[:class] ||= []
  options[:class].push('btn')
  options[:class].push('btn-primary')

  options[:data] ||= {}
  options[:data][:lato_form_target] = 'submit'

  form.submit label, options
end

#lato_index(collection, options = {}) ⇒ Object

Index



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/helpers/lato/components_helper.rb', line 39

def lato_index(collection, options = {})
  key = options[:key] || 'default'

  @_lato_index ||= {}
  @_lato_index[key] ||= {}
  columns = options[:columns] || @_lato_index[key][:columns] || collection.column_names || []
  sortable_columns = @_lato_index[key][:sortable_columns] || []
  searchable_columns = @_lato_index[key][:searchable_columns] || []
  model_name_underscore = options[:model_name] || collection.model.name.gsub('/', '_').underscore

  render(
    'lato/components/index',
    key: key,
    collection: collection,
    columns: columns,
    sortable_columns: sortable_columns,
    searchable_columns: searchable_columns,
    model_name_underscore: model_name_underscore,
    custom_actions: options[:custom_actions] || {}
  )
end

#lato_navbar_nav_item(key, path, &block) ⇒ Object

Navbar



6
7
8
9
10
11
12
13
# File 'app/helpers/lato/components_helper.rb', line 6

def lato_navbar_nav_item(key, path, &block)
  active = request.path == path
  active = @navbar_key == key if @navbar_key

  render 'lato/components/navbar_nav_item', active: active, path: path do
    yield if block
  end
end

#lato_operation(operation) ⇒ Object

Operation



64
65
66
67
68
69
# File 'app/helpers/lato/components_helper.rb', line 64

def lato_operation(operation)
  render(
    'lato/components/operation',
    operation: operation
  )
end

#lato_page_head(title, breadcrumbs = [], &block) ⇒ Object

Page head



30
31
32
33
34
# File 'app/helpers/lato/components_helper.rb', line 30

def lato_page_head(title, breadcrumbs = [], &block)
  render 'lato/components/page_head', title: title, breadcrumbs: breadcrumbs do
    yield if block
  end
end

#lato_sidebar_nav_item(key, path, &block) ⇒ Object

Sidebar



18
19
20
21
22
23
24
25
# File 'app/helpers/lato/components_helper.rb', line 18

def lato_sidebar_nav_item(key, path, &block)
  active = request.path == path
  active = @sidebar_key == key if @sidebar_key

  render 'lato/components/sidebar_nav_item', active: active, path: path do
    yield if block
  end
end