Class: Mensa::Filter
- Inherits:
-
Object
- Object
- Mensa::Filter
- Includes:
- ConfigReaders
- Defined in:
- app/tables/mensa/filter.rb
Instance Attribute Summary collapse
-
#column ⇒ Object
readonly
Returns the value of attribute column.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Class Method Summary collapse
Instance Method Summary collapse
-
#as ⇒ Object
This defines how the filter should be displayed in the value popover :select => as a select input :checkbox => as a checkbox input :string => as a text input.
- #collection ⇒ Object
- #filter_scope(record_scope) ⇒ Object
- #formatted_value ⇒ Object
-
#initialize(column:, config:, table:) ⇒ Filter
constructor
A new instance of Filter.
- #input_type ⇒ Object
- #multiple? ⇒ Boolean
- #operator_label ⇒ Object
- #operator_with_value? ⇒ Boolean
- #operators ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(column:, config:, table:) ⇒ Filter
Returns a new instance of Filter.
33 34 35 36 37 |
# File 'app/tables/mensa/filter.rb', line 33 def initialize(column:, config:, table:) @column = column @config = self.class.definition.merge(config || {}) @table = table end |
Instance Attribute Details
#column ⇒ Object (readonly)
Returns the value of attribute column.
9 10 11 |
# File 'app/tables/mensa/filter.rb', line 9 def column @column end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
9 10 11 |
# File 'app/tables/mensa/filter.rb', line 9 def table @table end |
Class Method Details
.OPERATORS ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'app/tables/mensa/filter.rb', line 17 def OPERATORS [ [:is, I18n.t("mensa.operators.is"), true], [:isnt, I18n.t("mensa.operators.isnt"), true], [:matches, I18n.t("mensa.operators.matches"), true], [:does_not_match, I18n.t("mensa.operators.does_not_match"), true], [:gt, I18n.t("mensa.operators.gt"), true], [:gteq, I18n.t("mensa.operators.gteq"), true], [:lt, I18n.t("mensa.operators.lt"), true], [:lteq, I18n.t("mensa.operators.lteq"), true], [:is_current, I18n.t("mensa.operators.is_current"), false], [:is_empty, I18n.t("mensa.operators.is_empty"), false] ].freeze end |
Instance Method Details
#as ⇒ Object
This defines how the filter should be displayed in the value popover :select => as a select input :checkbox => as a checkbox input :string => as a text input
57 58 59 |
# File 'app/tables/mensa/filter.rb', line 57 def as config[:as] end |
#collection ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'app/tables/mensa/filter.rb', line 43 def collection return unless config&.key?(:collection) if config[:collection].is_a? Proc table.original_view_context.instance_exec(&config[:collection]) else config[:collection] end end |
#filter_scope(record_scope) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/tables/mensa/filter.rb', line 77 def filter_scope(record_scope) if scope record_scope.instance_exec(normalize(value), &scope) else case operator when :is_empty if column.type == :string record_scope.where("#{column.attribute_for_condition} IS NULL OR #{column.attribute_for_condition} = ''") else record_scope.where("#{column.attribute_for_condition} IS NULL") end when :is_current record_scope.where("#{column.attribute_for_condition} = ?", Current.send(column.name)) when :matches record_scope.where("#{column.attribute_for_condition} LIKE ?", "%#{normalize(value)}%") when :does_not_match record_scope.where("#{column.attribute_for_condition} NOT LIKE ?", "%#{normalize(value)}%") when :is val = value.is_a?(Array) ? value : normalize(value) record_scope.where(column.attribute_for_condition => val) when :isnt val = value.is_a?(Array) ? value : normalize(value) record_scope.where.not(column.attribute_for_condition => val) when :gt record_scope.where(column.table.model.arel_table[column.attribute_for_condition].gt(normalize(value))) when :lt record_scope.where(column.table.model.arel_table[column.attribute_for_condition].lt(normalize(value))) when :gteq record_scope.where(column.table.model.arel_table[column.attribute_for_condition].gteq(normalize(value))) when :lteq record_scope.where(column.table.model.arel_table[column.attribute_for_condition].lteq(normalize(value))) else # Ignore unknown operators record_scope end end end |
#formatted_value ⇒ Object
67 68 69 70 71 72 73 74 75 |
# File 'app/tables/mensa/filter.rb', line 67 def formatted_value = if value.is_a?(Array) value.map { |entry| label_for_value(entry, ) }.join(", ") else label_for_value(value, ) end end |
#input_type ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'app/tables/mensa/filter.rb', line 137 def input_type case column.type when :integer "number" when :date "date" when :datetime "datetime-local" else "text" end end |
#multiple? ⇒ Boolean
39 40 41 |
# File 'app/tables/mensa/filter.rb', line 39 def multiple? !!multiple end |
#operator_label ⇒ Object
129 130 131 |
# File 'app/tables/mensa/filter.rb', line 129 def operator_label Mensa::Filter.OPERATORS.find { |op| op[0] == operator }[1] end |
#operator_with_value? ⇒ Boolean
133 134 135 |
# File 'app/tables/mensa/filter.rb', line 133 def operator_with_value? Mensa::Filter.OPERATORS.find { |op| op[0] == operator }[2] end |
#operators ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'app/tables/mensa/filter.rb', line 115 def operators operators = Mensa::Filter.OPERATORS.dup if config[:operators].present? operators = operators.select { |op| config[:operators].include?(op[0]) } else operators.delete_if { |op| op[0] == :is_current } unless Current.method_defined?(column.name, false) operators.delete_if { |op| [:matches, :does_not_match].include?(op[0]) } if collection.present? operators.delete_if { |op| [:matches, :does_not_match].include?(op[0]) } if column.type == :integer || column.type == :date || column.type == :datetime operators.delete_if { |op| [:is, :isnt].include?(op[0]) } if column.type == :date || column.type == :datetime operators.delete_if { |op| [:gt, :lt, :gteq, :lteq].include?(op[0]) } if column.type == :string || column.type.blank? end operators end |
#to_s ⇒ Object
61 62 63 64 65 |
# File 'app/tables/mensa/filter.rb', line 61 def to_s parts = [column.human_name, operator_label] parts << formatted_value if formatted_value.present? && operator_with_value? parts.join(" ") end |