Class: Avo::Filters::BaseFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/avo/filters/base_filter.rb

Constant Summary collapse

PARAM_KEY =
:encoded_filters

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments: {}) ⇒ BaseFilter

Returns a new instance of BaseFilter.



38
39
40
# File 'lib/avo/filters/base_filter.rb', line 38

def initialize(arguments: {})
  @arguments = arguments
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



14
15
16
# File 'lib/avo/filters/base_filter.rb', line 14

def arguments
  @arguments
end

Class Method Details

.decode_filters(filter_params) ⇒ Object



25
26
27
# File 'lib/avo/filters/base_filter.rb', line 25

def decode_filters(filter_params)
  JSON.parse(Base64.decode64(filter_params))
end

.encode_filters(filter_params) ⇒ Object



29
30
31
# File 'lib/avo/filters/base_filter.rb', line 29

def encode_filters(filter_params)
  Base64.encode64(filter_params.to_json)
end

.get_empty_messageObject



33
34
35
# File 'lib/avo/filters/base_filter.rb', line 33

def get_empty_message
  empty_message || I18n.t("avo.no_options_available")
end

Instance Method Details

#applied_filtersObject

Fetch the applied filters from the params



68
69
70
71
72
73
74
75
76
77
# File 'lib/avo/filters/base_filter.rb', line 68

def applied_filters
  # Return empty hash if no filters are present
  return {} if (filters_from_params = params[PARAM_KEY]).blank?

  # Return empty hash if the filters are not a string, decode_filters method expects a Base64 encoded string
  # Dynamic filters also use the "filters" param key, but they are not Base64 encoded, they are a hash
  return {} if !filters_from_params.is_a?(String)

  self.class.decode_filters filters_from_params
end

#applied_or_default_value(applied_filters) ⇒ Object

Get the applied value this filter. If it’s not present return the default value.



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/avo/filters/base_filter.rb', line 54

def applied_or_default_value(applied_filters)
  # Get the values for this particular filter
  applied_value = applied_filters[self.class.to_s]

  # Return that value if present
  return applied_value unless applied_value.nil?

  # Return that default
  default
rescue
  default
end

#apply_query(request, query, value) ⇒ Object



42
43
44
45
46
# File 'lib/avo/filters/base_filter.rb', line 42

def apply_query(request, query, value)
  value.stringify_keys! if value.is_a? Hash

  apply(request, query, value)
end

#current_userObject



20
21
22
# File 'lib/avo/filters/base_filter.rb', line 20

def current_user
  Avo::Current.user
end

#idObject



48
49
50
# File 'lib/avo/filters/base_filter.rb', line 48

def id
  self.class.name.underscore.tr("/", "_")
end

#visible_in_view(resource: nil, parent_resource: nil) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/avo/filters/base_filter.rb', line 79

def visible_in_view(resource: nil, parent_resource: nil)
  return true if visible.blank?

  # Run the visible block if available
  Avo::ExecutionContext.new(
    target: visible,
    params: params,
    parent_resource: parent_resource,
    resource: resource,
    arguments: arguments
  ).handle
end