sunlightlabs / opencongress

An open website for opening Congress.
http://www.opencongress.org
GNU General Public License v3.0
48 stars 16 forks source link

Person scopes and basic filtering infrastructure #496

Closed plantfansam closed 9 years ago

plantfansam commented 9 years ago

This PR filters People based on this model. Essentially, we whitelist scopes on a model, then apply them sequentially via the filter class method, which is added to a model through the Filterable module (include Filterable). It looks like this: ModelName.filter(filtering_params)

By default, a model's filterable fields and filtering defaults are defined in ModelName.filterable_fields. Conventionally, one calls ModelName.filter(filtering_params) in the controller, where filtering_params is a second controller method that applies the filtering defaults and whitelist.

filtering_params takes an optional hash of filtering defaults, which is useful if defaults must be transformed based on filtering parameters (e.g. in the PeopleController, there should be no default on_date if a for_congress param is passed in). If no hash is passed to filtering_params, non-nil values in ModelName.filterable_fields are regarded as defaults. E.G., in the following, :parameter_with_default defaults to "default_value", while :parameter_without_default will not be applied unless specified:

HashWithIndifferentAccess.new({
    :basic => {
        :parameter_with_default => "default_value",
        :parameter_without_default => nil
   }
})
Cspeisman commented 9 years ago

Looks good :+1:

yay for tests!