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



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

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



224
225
226
# File 'app/helpers/lato/components_helper.rb', line 224

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

#lato_data_user(label, image_url = nil) ⇒ Object



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

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



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

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



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

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



198
199
200
201
202
# File 'app/helpers/lato/components_helper.rb', line 198

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



204
205
206
207
208
# File 'app/helpers/lato/components_helper.rb', line 204

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



157
158
159
160
161
# File 'app/helpers/lato/components_helper.rb', line 157

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



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

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



151
152
153
154
155
# File 'app/helpers/lato/components_helper.rb', line 151

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



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

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



180
181
182
183
184
# File 'app/helpers/lato/components_helper.rb', line 180

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



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

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



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

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



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

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



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

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



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

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
68
69
# 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('/', '_')
  pagination_options = options[:pagination_options] || nil

  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,
    pagination_options: pagination_options,
    custom_actions: options[:custom_actions] || {}
  )
end

#lato_index_dynamic_label(params = {}) ⇒ Object



71
72
73
# File 'app/helpers/lato/components_helper.rb', line 71

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



75
76
77
# File 'app/helpers/lato/components_helper.rb', line 75

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



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

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