Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Refactoring an action in a controller to use a filterer pattern

Refactoring an action in a controller to use a filterer pattern

JoseLuis Torres

October 17, 2017
Tweet

More Decks by JoseLuis Torres

Other Decks in Programming

Transcript

  1. Refactoring an action in a controller to use a Filterer

    pattern JoseLuis Torres Stone Crop Tech
  2. In the beginning Rails is an awesome framework where everything

    had a place and you can only create added value with your code.
  3. Idea Let's make Thin controllers and Thin models an option

    again. There are many OO patterns out there that we are still missing in the Rails projects. Today, we will implement a Filterer pattern to break those searches in smaller parts.
  4. Start Not everything can be solved with this pattern. Sometimes

    it needs to be split into smaller pieces. Make sure you have tests.
  5. Why? Almost every project needs a search or a filter

    to find something. `Index` actions it’s most of the time the natural place to “grow” a filterer. Tests are easier when you have an specialized class than trying to test everything from a controller.
  6. Why? It is repeatable across different models/ entities and controllers.

    Organized code in a predictable way makes the project more maintainable. Messy code it’s not fun to work with.
  7. Facts Most of time when we arrive to a new

    project we try to do the best thing possible to be productive. With the time some controllers could be become nightmares.
  8. Facts Developers try to follow the existing pattern to avoid

    having multiple ways of doing a common thing. Projects/teams can have different ways to organize the code. Most of the time there's a search or a filter already in place for a given entity or set of entities.
  9. Alternatives Often we try to use tools to solve a

    specific problem like Solr or ElasticSearch. But in other cases it can be solved with AR scopes. A Filterable using Concerns Ransack Gem
  10. What’s a Filterer object? Filter Attributes Filter Options for Selects

    Responds with the filtered results ArelBuilder sub class