Module: YummyGuide::Administrate::FilterControlsHelper

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

Instance Method Summary collapse

Instance Method Details

#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") ⇒ 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
# 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"
)
  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
  )

  safe_join([
    (:div, "", class: "modal_overlay"),
    (:div, id: "reserv-filter-options") do
      safe_join([
        link_to(button_label, "javascript:void(0)", class: "button"),
        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
        )
      ])
    end
  ])
end

#admin_filter_current_values(raw_values) ⇒ Object



187
188
189
190
191
192
193
194
195
196
197
198
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 187

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



95
96
97
98
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 95

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



75
76
77
78
79
80
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 75

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



82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 82

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



174
175
176
177
178
179
180
181
182
183
184
185
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 174

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



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

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



109
110
111
112
113
114
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
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 109

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/helpers/yummy_guide/administrate/filter_controls_helper.rb', line 141

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



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

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_visible_fields(dashboard, locals) ⇒ Object



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

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