Class: Plutonium::Query::Filters::Boolean

Inherits:
Plutonium::Query::Filter show all
Defined in:
lib/plutonium/query/filters/boolean.rb

Overview

Boolean filter for true/false columns

Examples:

Basic usage

filter :active, with: :boolean

With custom labels

filter :published, with: :boolean, true_label: "Published", false_label: "Draft"

Instance Attribute Summary

Attributes inherited from Plutonium::Query::Filter

#key

Instance Method Summary collapse

Methods inherited from Plutonium::Query::Filter

lookup

Methods included from Definition::Presentable

#description, #icon, #label

Constructor Details

#initialize(true_label: "Yes", false_label: "No") ⇒ Boolean

Returns a new instance of Boolean.



13
14
15
16
17
# File 'lib/plutonium/query/filters/boolean.rb', line 13

def initialize(true_label: "Yes", false_label: "No", **)
  super(**)
  @true_label = true_label
  @false_label = false_label
end

Instance Method Details

#apply(scope, value:) ⇒ Object



19
20
21
22
23
24
# File 'lib/plutonium/query/filters/boolean.rb', line 19

def apply(scope, value:)
  return scope if value.blank?

  bool_value = ActiveModel::Type::Boolean.new.cast(value)
  scope.where(key => bool_value)
end

#customize_inputsObject



26
27
28
29
30
31
# File 'lib/plutonium/query/filters/boolean.rb', line 26

def customize_inputs
  input :value,
    as: :select,
    choices: [[@true_label, "true"], [@false_label, "false"]],
    include_blank: "All"
end