Module: Airview::ApplicationHelper

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

Instance Method Summary collapse

Instance Method Details

#airview_active_sort_directionObject



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

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

#airview_active_sort_fieldObject



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

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

#airview_attachment_cell(record, field) ⇒ Object



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'app/helpers/airview/application_helper.rb', line 227

def airview_attachment_cell(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



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

def airview_cell_value(record, field)
  value = airview_field_value(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



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'app/helpers/airview/application_helper.rb', line 206

def airview_collection_pills(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



107
108
109
# File 'app/helpers/airview/application_helper.rb', line 107

def airview_column_action_method
  @active_view ? :patch : :get
end

#airview_column_action_params(action, field) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'app/helpers/airview/application_helper.rb', line 95

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



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

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

#airview_current_table_pathObject



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

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



177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/helpers/airview/application_helper.rb', line 177

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
# File 'app/helpers/airview/application_helper.rb', line 5

def airview_field_value(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



168
169
170
171
172
173
174
175
# File 'app/helpers/airview/application_helper.rb', line 168

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



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/helpers/airview/application_helper.rb', line 129

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"]]
            when :date, :datetime
              [["is", "equals"], ["before", "before"], ["after", "after"], ["is empty", "is_empty"]]
            else
              [
                ["contains", "contains"],
                ["equals", "equals"],
                ["starts with", "starts_with"],
                ["is empty", "is_empty"]
              ]
            end

  options_for_select(options, selected)
end

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



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/helpers/airview/application_helper.rb', line 149

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



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

def airview_input_for(record, field)
  value = airview_field_value(record, field)
  name = "record[#{field.name}]"

  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



118
119
120
121
122
123
# File 'app/helpers/airview/application_helper.rb', line 118

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



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

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



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'app/helpers/airview/application_helper.rb', line 243

def airview_reference_input(record, field, name)
  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



191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'app/helpers/airview/application_helper.rb', line 191

def airview_reference_pill(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



77
78
79
80
81
# File 'app/helpers/airview/application_helper.rb', line 77

def airview_sort_indicator(active, direction)
  return nil unless active

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


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/airview/application_helper.rb', line 59

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