Module: Airview::ApplicationHelper

Defined in:
app/helpers/airview/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#airview_active_sort_directionObject



91
92
93
# File 'app/helpers/airview/application_helper.rb', line 91

def airview_active_sort_direction
  (params[:direction].presence || @active_view&.sorts&.dig("direction") || "asc").to_s
end

#airview_active_sort_fieldObject



87
88
89
# File 'app/helpers/airview/application_helper.rb', line 87

def airview_active_sort_field
  params[:sort].presence || @active_view&.sorts&.dig("sort")
end

#airview_attachment_cell(record, field) ⇒ Object



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'app/helpers/airview/application_helper.rb', line 236

def airview_attachment_cell(record, field)
  return tag.span("No file", class: "airview-reference-empty") unless airview_field_available?(record, field)

  attachment = record.public_send(field.name)
  return tag.span("No file", class: "airview-reference-empty") unless attachment.attached?

  blob = attachment.blob
  path = airview_attachment_path(attachment)

  link_to(path, class: "airview-attachment", target: "_blank", rel: "noopener") do
    if blob.image?
      image_tag(path, alt: blob.filename.to_s, class: "airview-attachment-thumb")
    else
      tag.span(blob.filename.to_s, class: "airview-attachment-file")
    end
  end
end

#airview_cell_value(record, field) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/helpers/airview/application_helper.rb', line 16

def airview_cell_value(record, field)
  value = airview_field_value(record, field)
  return tag.span("", class: "airview-empty-value") unless airview_field_available?(record, field)

  case field.type
  when :boolean
    tag.span(value ? "" : "", class: ["airview-check", ("is-on" if value)])
  when :belongs_to
    airview_reference_pill(record, field)
  when :has_many
    airview_collection_pills(record, field)
  when :attachment
    airview_attachment_cell(record, field)
  when :select
    value.present? ? tag.span(value, class: "airview-pill") : tag.span("", class: "airview-empty-value")
  when :date
    value.present? ? l(value.to_date, format: :default) : tag.span("", class: "airview-empty-value")
  when :datetime
    value.present? ? l(value, format: :short) : tag.span("", class: "airview-empty-value")
  else
    tag.span(value, class: "airview-cell-text")
  end
end

#airview_collection_pills(record, field) ⇒ Object



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'app/helpers/airview/application_helper.rb', line 213

def airview_collection_pills(record, field)
  return tag.span("0 records", class: "airview-reference-empty") unless airview_field_available?(record, field)

  records = record.public_send(field.name).limit(3).to_a
  count = airview_collection_count(record, field)

  return tag.span("0 records", class: "airview-reference-empty") if count.to_i.zero?

  linked_resource = airview_resource_for_model(field.model)
  collection_path = airview_collection_path(record, field, linked_resource)
  cards = records.map do |related|
    airview_collection_record_card(related, field, linked_resource)
  end

  cards << airview_collection_count_pill(count - records.size, collection_path) if count > records.size

  tag.div(
    safe_join(cards),
    class: "airview-reference-list airview-reference-list--cards",
    title: collection_path ? "Open #{field.label}" : nil
  )
end

#airview_column_action_methodObject



111
112
113
# File 'app/helpers/airview/application_helper.rb', line 111

def airview_column_action_method
  @active_view ? :patch : :get
end

#airview_column_action_params(action, field) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'app/helpers/airview/application_helper.rb', line 99

def airview_column_action_params(action, field)
  columns = @visible_fields.map { |visible_field| visible_field.name.to_s }
  columns = reorder_columns(columns, field.name.to_s, action)
  columns -= [field.name.to_s] if action == :hide

  if @active_view
    { columns_present: "1", columns: }
  else
    request.query_parameters.merge(columns_present: "1", columns:)
  end
end

#airview_column_action_pathObject



95
96
97
# File 'app/helpers/airview/application_helper.rb', line 95

def airview_column_action_path
  @active_view ? resource_view_path(@resource.key, @active_view) : resource_path(@resource.key)
end

#airview_current_table_pathObject



115
116
117
118
119
120
# File 'app/helpers/airview/application_helper.rb', line 115

def airview_current_table_path
  query = request.query_parameters.except(:record_id, :delete_record_id, :return_to)
  query_string = query.to_query

  query_string.present? ? "#{request.path}?#{query_string}" : request.path
end

#airview_delete_impacts(record) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'app/helpers/airview/application_helper.rb', line 182

def airview_delete_impacts(record)
  record.class.reflect_on_all_associations.filter_map do |association|
    dependent = association.options[:dependent]
    next unless dependent

    {
      name: association.name.to_s.humanize,
      dependent: dependent.to_s.humanize.downcase,
      count: airview_association_count(record, association),
      action: airview_dependent_action(dependent)
    }
  end
end

#airview_field_value(record, field) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'app/helpers/airview/application_helper.rb', line 5

def airview_field_value(record, field)
  return nil unless airview_field_available?(record, field)

  value = record.public_send(field.name)
  return value if field.collection_association?
  return Airview.record_label(value, field.label_method) if field.association? && value
  return value.to_json if field.type == :json && value.present?

  value
end

#airview_filter_field_picker(index, field) ⇒ Object



173
174
175
176
177
178
179
180
# File 'app/helpers/airview/application_helper.rb', line 173

def airview_filter_field_picker(index, field)
  safe_join(
    [
      hidden_field_tag("filters[#{index}][field]", field&.name, data: { airview_field_target: true }),
      airview_filter_field_picker_menu(index, field)
    ]
  )
end

#airview_filter_operator_options(field, selected = nil) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'app/helpers/airview/application_helper.rb', line 133

def airview_filter_operator_options(field, selected = nil)
  options = case field.type
            when :boolean
              [["is checked", "is_true"], ["is unchecked", "is_false"]]
            when :integer, :float, :decimal
              [["=", "equals"], [">", "gt"], ["<", "lt"], [">=", "gte"], ["<=", "lte"], ["is empty", "is_empty"], ["is not empty", "is_not_empty"]]
            when :date, :datetime
              [["is", "equals"], ["before", "before"], ["after", "after"], ["is empty", "is_empty"], ["is not empty", "is_not_empty"]]
            else
              [
                ["contains", "contains"],
                ["equals", "equals"],
                ["starts with", "starts_with"],
                ["is empty", "is_empty"],
                ["is not empty", "is_not_empty"]
              ]
            end

  options_for_select(options, selected)
end

#airview_filter_value_input(index, field, value = nil) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'app/helpers/airview/application_helper.rb', line 154

def airview_filter_value_input(index, field, value = nil)
  name = "filters[#{index}][value]"

  case field&.type
  when :boolean
    select_tag(
      name,
      options_for_select([%w[checked true], %w[unchecked false]], value.to_s),
      data: { airview_filter_value: true }
    )
  when :date, :datetime
    date_field_tag(name, value, data: { airview_filter_value: true })
  when :integer, :float, :decimal
    number_field_tag(name, value, step: "any", placeholder: "Value", data: { airview_filter_value: true })
  else
    text_field_tag(name, value, placeholder: "Enter a value", data: { airview_filter_value: true })
  end
end

#airview_input_for(record, field) ⇒ Object



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

def airview_input_for(record, field)
  value = airview_field_value(record, field)
  name = "record[#{field.name}]"
  return text_field_tag(name, nil, disabled: true) unless airview_field_available?(record, field)

  case field.type
  when :boolean
    check_box_tag(name, "1", ActiveModel::Type::Boolean.new.cast(value), disabled: field.readonly?)
  when :text, :json
    text_area_tag(name, value, rows: 2, disabled: field.readonly?)
  when :select
    select_tag(name, options_for_select(field.option_values, value), include_blank: true, disabled: field.readonly?)
  when :belongs_to
    airview_reference_input(record, field, name)
  when :date
    date_field_tag(name, value, disabled: field.readonly?)
  when :datetime
    datetime_local_field_tag(name, value&.strftime("%Y-%m-%dT%H:%M"), disabled: field.readonly?)
  else
    text_field_tag(name, value, disabled: field.readonly?)
  end
end

#airview_modal_close_pathObject



122
123
124
125
126
127
# File 'app/helpers/airview/application_helper.rb', line 122

def airview_modal_close_path
  return_to = params[:return_to].to_s
  return return_to if return_to.start_with?("/") && !return_to.start_with?("//")

  airview_current_table_path
end

#airview_record_modal_path(resource, record) ⇒ Object



129
130
131
# File 'app/helpers/airview/application_helper.rb', line 129

def airview_record_modal_path(resource, record)
  resource_path(resource.key, record_id: record.id, return_to: airview_current_table_path)
end

#airview_reference_input(record, field, name) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'app/helpers/airview/application_helper.rb', line 254

def airview_reference_input(record, field, name)
  return text_field_tag(name, nil, disabled: true) unless airview_field_available?(record, field)

  related = record.public_send(field.name)

  safe_join(
    [
      hidden_field_tag(name, record.public_send(field.attribute_name), data: { airview_reference_target: true }),
      airview_reference_preview(related, field),
      tag.div(
        class: "airview-reference-picker",
        data: { airview_reference_url: resource_references_path(@resource.key, field.name) }
      ) do
        safe_join(
          [
            search_field_tag(
              nil,
              nil,
              placeholder: "Find linked record",
              autocomplete: "off",
              data: { airview_reference_picker: true }
            ),
            tag.div("", class: "airview-reference-results", data: { airview_reference_results: true }),
            airview_reference_clear_button(record, field)
          ].compact
        )
      end
    ]
  )
end

#airview_reference_pill(record, field) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'app/helpers/airview/application_helper.rb', line 196

def airview_reference_pill(record, field)
  return tag.span("", class: "airview-empty-value") unless airview_field_available?(record, field)

  related = record.public_send(field.name)
  return tag.span("+ Link record", class: "airview-reference-empty") unless related

  label = Airview.record_label(related, field.label_method)
  linked_resource = airview_resource_for_model(field.model)
  path = linked_resource ? airview_record_modal_path(linked_resource, related) : nil

  if path
    link_to(label, path, class: "airview-reference-pill")
  else
    tag.span(label, class: "airview-reference-pill")
  end
end

#airview_sort_indicator(active, direction) ⇒ Object



81
82
83
84
85
# File 'app/helpers/airview/application_helper.rb', line 81

def airview_sort_indicator(active, direction)
  return nil unless active

  tag.span(direction == "desc" ? "" : "", class: "airview-sort-indicator")
end


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/helpers/airview/application_helper.rb', line 63

def airview_sort_link(resource, field)
  active = airview_active_sort_field == field.name.to_s
  current_direction = airview_active_sort_direction
  direction = active && current_direction != "desc" ? "desc" : "asc"

  link_to(
    resource_path(resource.key, request.query_parameters.merge(sort: field.name, direction:)),
    class: ["airview-sort-link", ("is-active" if active)]
  ) do
    safe_join(
      [
        tag.span(field.label, class: "airview-column-label"),
        airview_sort_indicator(active, current_direction)
      ].compact
    )
  end
end