Class: LcpRuby::Pages::ScopeFilterSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/lcp_ruby/pages/scope_filter_set.rb

Overview

Sibling of ‘Pages::FilterForm` for the `scope_filters:` page YAML block. Scope filters are simpler than filter_form fields: values are predefined scope-name keys (e.g. `lifecycle: active`), there’s no type coercion, no Tom Select wrapper, no cascade.

Plain Ruby object (no VirtualForm synthetic class) — scope_filters have no typed attributes, just selected scope keys.

Spec: docs/design/page_filters_as_virtual_forms.md § 4 (scope_filters: declaration) + § 5 (controller integration).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(page:, params:) ⇒ ScopeFilterSet

Returns a new instance of ScopeFilterSet.



20
21
22
23
24
25
# File 'lib/lcp_ruby/pages/scope_filter_set.rb', line 20

def initialize(page:, params:)
  @page = page
  @raw_params = (params[:scope_filter] || params["scope_filter"] || {}).then do |raw|
    raw.respond_to?(:to_unsafe_h) ? raw.to_unsafe_h : raw
  end
end

Instance Attribute Details

#pageObject (readonly)

Returns the value of attribute page.



18
19
20
# File 'lib/lcp_ruby/pages/scope_filter_set.rb', line 18

def page
  @page
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/lcp_ruby/pages/scope_filter_set.rb', line 27

def any?
  page.scope_filter_definitions.any?
end

#eachObject

Yields [name, selected_scope_key, definition] for each declared scope_filter. The selected scope key is the URL value if it’s a declared scope on this filter, otherwise the ‘default:`, otherwise nil. Drops anything else silently — attacker-supplied scope keys never reach `model_class.public_send`.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/lcp_ruby/pages/scope_filter_set.rb', line 36

def each
  return enum_for(:each) unless block_given?

  page.scope_filter_definitions.each do |sf|
    name = sf["name"].to_s
    declared = (sf["scopes"] || {}).keys.map(&:to_s)
    requested = @raw_params[name].to_s
    selected = if declared.include?(requested)
      requested
    else
      sf["default"]&.to_s
    end
    yield(name, selected, sf)
  end
end

#selected_for(name) ⇒ Object



52
53
54
# File 'lib/lcp_ruby/pages/scope_filter_set.rb', line 52

def selected_for(name)
  find { |n, _, _| n.to_s == name.to_s }&.dig(1)
end