jhund / filterrific

Filterrific is a Rails Engine plugin that makes it easy to filter, search, and sort your ActiveRecord lists.
http://filterrific.clearcove.ca
MIT License
910 stars 125 forks source link

Unable to initialize filterrific for a model from outside of a class scope #175

Closed pospieszynski closed 5 years ago

pospieszynski commented 5 years ago

Consider the requirement that a model should have a filter declared for each attribute and each assosciation. I don't want to manually whitelist all filters in each model since I know the rule - model should declare with\${attribute_name or associationname} filters.

I have decided to move filterrific initialization to rails initializer. Why? Because at this stage all models are loaded and I can safely access information about its attributes and associations using some reflection methods/metaprogramming in order to compose array of filter names.

However, when I do i.e

Car.filterrific(available_filters: array_of_available_filter_names)

in initializers/filterrific.rb - although it does not raise any error, it does not seem to successfully initialize filterrific. It raises an error "undefined method filterrific default_filter params'" when controller tries to apply filters.

Suprisingly when I do the same in models/car.rb under the class scope (outside of it) everything does work..

For some reason filterrific is not able to decorate model class with necessary methods when called this way from other file..Could anyone explain this behaviour? Thank you in advance.