Class: Filter::ApplyFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/filter/apply_filter.rb

Defined Under Namespace

Classes: NoFilterError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filter_name, filter_value, relation) ⇒ ApplyFilter

Returns a new instance of ApplyFilter.



6
7
8
9
10
11
# File 'lib/filter/apply_filter.rb', line 6

def initialize(filter_name, filter_value, relation)
  @filter_name = filter_name.to_sym
  @filter_value = filter_value
  @relation = relation
  @model_class = determine_model_class
end

Instance Attribute Details

#filter_nameObject (readonly)

Returns the value of attribute filter_name.



4
5
6
# File 'lib/filter/apply_filter.rb', line 4

def filter_name
  @filter_name
end

#filter_valueObject (readonly)

Returns the value of attribute filter_value.



4
5
6
# File 'lib/filter/apply_filter.rb', line 4

def filter_value
  @filter_value
end

#model_classObject (readonly)

Returns the value of attribute model_class.



4
5
6
# File 'lib/filter/apply_filter.rb', line 4

def model_class
  @model_class
end

#relationObject (readonly)

Returns the value of attribute relation.



4
5
6
# File 'lib/filter/apply_filter.rb', line 4

def relation
  @relation
end

Instance Method Details

#performObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/filter/apply_filter.rb', line 13

def perform
  if filter_missing?
    raise NoFilterError, "#{model_class.model_name} neither responds to nor has a column named `#{filter_name}`."
  elsif column_exists?
    relation.where(filter_name => filter_value)
  elsif filter_accepts_arguments?
    relation.send(filter_name, filter_value)
  elsif apply_filter?
    relation.send(filter_name)
  else
    relation
  end
end