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



90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/helpers/lato/components_helper.rb', line 90

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



222
223
224
# File 'app/helpers/lato/components_helper.rb', line 222

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

#lato_data_user(label, image_url = nil) ⇒ Object



226
227
228
229
230
231
232
233
# File 'app/helpers/lato/components_helper.rb', line 226

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

   :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



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/helpers/lato/components_helper.rb', line 116

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, "#{I18n.t('lato.there_are_some_errors')}:")
    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



167
168
169
170
171
172
173
174
175
176
# File 'app/helpers/lato/components_helper.rb', line 167

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_date(form, key, options = {}) ⇒ Object



196
197
198
199
200
# File 'app/helpers/lato/components_helper.rb', line 196

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

  form.date_field key, options
end

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



202
203
204
205
206
# File 'app/helpers/lato/components_helper.rb', line 202

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

  form.datetime_field key, options
end

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



155
156
157
158
159
# File 'app/helpers/lato/components_helper.rb', line 155

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



184
185
186
187
188
# File 'app/helpers/lato/components_helper.rb', line 184

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



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

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



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

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



178
179
180
181
182
# File 'app/helpers/lato/components_helper.rb', line 178

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



143
144
145
146
147
# File 'app/helpers/lato/components_helper.rb', line 143

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_input_textarea(form, key, options = {}) ⇒ Object



190
191
192
193
194
# File 'app/helpers/lato/components_helper.rb', line 190

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

  form.text_area key, options
end

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



136
137
138
139
140
141
# File 'app/helpers/lato/components_helper.rb', line 136

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



103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/helpers/lato/components_helper.rb', line 103

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



208
209
210
211
212
213
214
215
216
217
# File 'app/helpers/lato/components_helper.rb', line 208

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/helpers/lato/components_helper.rb', line 45

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 = options[:model_name] || collection.model.name
  model_name_underscore = options[:model_name] || model_name.underscore.gsub('/', '_')

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

#lato_index_dynamic_label(params = {}) ⇒ Object



69
70
71
# File 'app/helpers/lato/components_helper.rb', line 69

def lato_index_dynamic_label(params = {})
  'Please override the method lato_index_dynamic_label in your application_helper.rb'
end

#lato_index_dynamic_value(params = {}) ⇒ Object



73
74
75
# File 'app/helpers/lato/components_helper.rb', line 73

def lato_index_dynamic_value(params = {})
  'Please override the method lato_index_dynamic_value in your application_helper.rb'
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_navbar_nav_locales_item(options = {}) ⇒ Object



15
16
17
18
19
# File 'app/helpers/lato/components_helper.rb', line 15

def lato_navbar_nav_locales_item(options = {})
  flag = options[:flag] || false

  render 'lato/components/navbar_nav_locales_item', flag: flag
end

#lato_operation(operation) ⇒ Object

Operation



80
81
82
83
84
85
# File 'app/helpers/lato/components_helper.rb', line 80

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

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

Page head



36
37
38
39
40
# File 'app/helpers/lato/components_helper.rb', line 36

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



24
25
26
27
28
29
30
31
# File 'app/helpers/lato/components_helper.rb', line 24

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