Class: Filter::ApplyFilter
- Inherits:
-
Object
- Object
- Filter::ApplyFilter
- Defined in:
- lib/filter/apply_filter.rb
Defined Under Namespace
Classes: NoFilterError
Instance Attribute Summary collapse
-
#filter_name ⇒ Object
readonly
Returns the value of attribute filter_name.
-
#filter_value ⇒ Object
readonly
Returns the value of attribute filter_value.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#relation ⇒ Object
readonly
Returns the value of attribute relation.
Instance Method Summary collapse
-
#initialize(filter_name, filter_value, relation) ⇒ ApplyFilter
constructor
A new instance of ApplyFilter.
- #perform ⇒ Object
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_name ⇒ Object (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_value ⇒ Object (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_class ⇒ Object (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 |
#relation ⇒ Object (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
#perform ⇒ Object
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 |