Module: YummyGuide::Administrate::FilterControlsHelper

Defined in:
app/helpers/yummy_guide/administrate/filter_controls_helper.rb

Instance Method Summary collapse

Instance Method Details

#admin_filter_body_modal_mount?(modal_mount) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 168

def admin_filter_body_modal_mount?(modal_mount)
  modal_mount.to_s == "body"
end

#admin_filter_button_container(button_label:, form_markup: nil, modal_id: nil) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 147

def admin_filter_button_container(button_label:, form_markup: nil, modal_id: nil)
  data = { admin_filter_controls: true }
  data[:admin_filter_modal_id] = modal_id if modal_id

  (:div, id: "reserv-filter-options", data: data) do
    safe_join([
      link_to(button_label, "javascript:void(0)", class: "button"),
      form_markup
    ].compact)
  end
end

#admin_filter_controls(dashboard: nil, page: nil, path: nil, clear_path: nil, search_options: {}, form: :search_options, method: :get, hidden_fields: {}, root_hidden_fields: {}, filter_locals: {}, extra_actions: [], button_label: "Filter", title: "Filter Options", submit_label: "Filter", close_label: "Close", modal_mount: :inline, modal_content_for: :admin_filter_modals) ⇒ Object

Raises:

  • (ArgumentError)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
70
71
72
73
74
75
76
77
78
79
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 6

def admin_filter_controls(
  dashboard: nil,
  page: nil,
  path: nil,
  clear_path: nil,
  search_options: {},
  form: :search_options,
  method: :get,
  hidden_fields: {},
  root_hidden_fields: {},
  filter_locals: {},
  extra_actions: [],
  button_label: "Filter",
  title: "Filter Options",
  submit_label: "Filter",
  close_label: "Close",
  modal_mount: :inline,
  modal_content_for: :admin_filter_modals
)
  dashboard ||= admin_filter_dashboard_from_page(page)
  scope = form.to_s
  current_values = admin_filter_current_values(search_options)
  base_locals = filter_locals.merge(
    dashboard: dashboard,
    page: page,
    search_options: search_options,
    current_values: current_values,
    form_scope: scope
  )
  fields = admin_filter_visible_fields(dashboard, base_locals)
  return if fields.blank?

  path ||= admin_filter_dashboard_setting(dashboard, :filter_path, :FILTER_PATH, base_locals)
  raise ArgumentError, "admin_filter_controls requires path or dashboard FILTER_PATH" if path.blank?

  clear_path ||= admin_filter_dashboard_setting(dashboard, :filter_clear_path, :FILTER_CLEAR_PATH, base_locals) || path
  locals = filter_locals.merge(
    dashboard: dashboard,
    page: page,
    path: path,
    clear_path: clear_path,
    search_options: search_options,
    current_values: current_values,
    form_scope: scope
  )

  form_markup = admin_filter_form(
    fields: fields,
    scope: scope,
    path: path,
    clear_path: clear_path,
    method: method,
    current_values: current_values,
    hidden_fields: hidden_fields,
    root_hidden_fields: root_hidden_fields,
    locals: locals,
    extra_actions: extra_actions,
    title: title,
    submit_label: submit_label,
    close_label: close_label
  )

  if admin_filter_body_modal_mount?(modal_mount)
    modal_id = admin_filter_next_modal_id
    content_for(modal_content_for, admin_filter_modal_root(modal_id: modal_id, form_markup: form_markup))

    return admin_filter_button_container(button_label: button_label, modal_id: modal_id)
  end

  safe_join([
    (:div, "", class: "modal_overlay"),
    admin_filter_button_container(button_label: button_label, form_markup: form_markup)
  ])
end

#admin_filter_current_values(raw_values) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 224

def admin_filter_current_values(raw_values)
  values =
    if raw_values.respond_to?(:to_unsafe_h)
      raw_values.to_unsafe_h
    elsif raw_values.respond_to?(:to_h)
      raw_values.to_h
    else
      raw_values || {}
    end

  values.deep_stringify_keys
end

#admin_filter_dashboard_constant(dashboard, constant_name) ⇒ Object



101
102
103
104
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 101

def admin_filter_dashboard_constant(dashboard, constant_name)
  target = dashboard.is_a?(Class) ? dashboard : dashboard.class
  target.const_get(constant_name, false) if target.const_defined?(constant_name, false)
end

#admin_filter_dashboard_from_page(page) ⇒ Object



81
82
83
84
85
86
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 81

def admin_filter_dashboard_from_page(page)
  return page.dashboard if page.respond_to?(:dashboard)
  return unless page.respond_to?(:instance_variable_defined?) && page.instance_variable_defined?(:@dashboard)

  page.instance_variable_get(:@dashboard)
end

#admin_filter_dashboard_setting(dashboard, method_name, constant_name, locals) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 88

def admin_filter_dashboard_setting(dashboard, method_name, constant_name, locals)
  return if dashboard.blank?

  value =
    if dashboard.respond_to?(method_name)
      dashboard.public_send(method_name)
    else
      admin_filter_dashboard_constant(dashboard, constant_name)
    end

  admin_filter_evaluate_value(value, locals)
end

#admin_filter_evaluate_value(value, locals) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 211

def admin_filter_evaluate_value(value, locals)
  return value unless value.respond_to?(:call)

  case value.arity
  when 0
    value.call
  when 1
    value.call(self)
  else
    value.call(self, locals)
  end
end

#admin_filter_evaluated_hash(values) ⇒ Object



206
207
208
209
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 206

def admin_filter_evaluated_hash(values)
  values = admin_filter_evaluate_value(values, {})
  (values || {}).to_h
end

#admin_filter_form(fields:, scope:, path:, clear_path:, method:, current_values:, hidden_fields:, root_hidden_fields:, locals:, extra_actions:, title:, submit_label:, close_label:) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 115

def admin_filter_form(fields:, scope:, path:, clear_path:, method:, current_values:, hidden_fields:, root_hidden_fields:, locals:, extra_actions:, title:, submit_label:, close_label:)
  form_with(
    url: path,
    scope: scope,
    method: method,
    html: {
      class: "filter-form",
      data: {
        yummy_guide_administrate_filter_form: true,
        admin_filter_form: true
      }
    }
  ) do |f|
    safe_join([
      admin_filter_hidden_fields(scope, hidden_fields, root_hidden_fields),
      admin_filter_form_header(title: title, close_label: close_label),
      (:div, class: "filter-form__body") do
        (:table, class: "filter_table") do
          safe_join(fields.map { |field| field.row(self, f, scope, current_values, locals) })
        end
      end,
      (:div, class: "filter-form__actions") do
        safe_join([
          link_to("Clear", clear_path, class: "button button--outline-primary"),
          Array(extra_actions),
          f.submit(submit_label, class: "submit_filter")
        ].flatten)
      end
    ])
  end
end

#admin_filter_form_header(title:, close_label:) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 178

def admin_filter_form_header(title:, close_label:)
  (:div, class: "filter-form__header") do
    safe_join([
      (:h2, title, class: "filter-form__title"),
      button_tag(
        "x",
        type: "button",
        class: "filter-form__close",
        data: { behavior: "filter-form-close" },
        aria: { label: close_label },
        title: close_label
      )
    ])
  end
end

#admin_filter_hidden_fields(scope, hidden_fields, root_hidden_fields) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 194

def admin_filter_hidden_fields(scope, hidden_fields, root_hidden_fields)
  root_tags = admin_filter_evaluated_hash(root_hidden_fields).map do |key, value|
    hidden_field_tag(key, value)
  end

  scoped_tags = admin_filter_evaluated_hash(hidden_fields).map do |key, value|
    hidden_field_tag("#{scope}[#{key}]", value)
  end

  safe_join(root_tags + scoped_tags)
end

#admin_filter_modal_root(modal_id:, form_markup:) ⇒ Object



159
160
161
162
163
164
165
166
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 159

def admin_filter_modal_root(modal_id:, form_markup:)
  (:div, id: modal_id, class: "admin-filter-modal-root", data: { admin_filter_modal_root: true }) do
    safe_join([
      (:div, "", class: "modal_overlay"),
      form_markup
    ])
  end
end

#admin_filter_next_modal_idObject



172
173
174
175
176
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 172

def admin_filter_next_modal_id
  @admin_filter_controls_modal_index ||= 0
  @admin_filter_controls_modal_index += 1
  "admin-filter-modal-#{@admin_filter_controls_modal_index}"
end

#admin_filter_visible_fields(dashboard, locals) ⇒ Object



106
107
108
109
110
111
112
113
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 106

def admin_filter_visible_fields(dashboard, locals)
  return [] if dashboard.blank?

  YummyGuide::Administrate::Filters::Resolver
    .attributes_for(dashboard)
    .values
    .select { |field| field.visible?(self, locals) }
end