jacopo beschi: yet another web guy

Ruby on rails Archive

Different caching strategies for Rails.cache and ActionController::Caching

Created at: Jun/19/2017 jacopo beschi

If you need to use different caching strategies for your Rails.cache and your ActionController::Cache (used for fragment caching) just put the following in your config/environments/env.rb file:

config.action_controller.perform_caching = true # this is used for your Fragment cache config.action_controller.cache_store = :file_store, 'action_controller_tore_path' # this is used for Rails.cache config.cache_store = :file_store, 'cache_store_path'

Read on

Thinner Rails Model with the Finder Pattern

Created at: Mar/20/2017 jacopo beschi

Hello everybody, today I want to share with you a pattern learned from gitlab: the finder pattern.

Most of the time when you want to find an item based on a set of different conditions you do something like this:

class Project
    def issues_for_user_filtered_by(user, filter)
        # A lot of logic not related to project model itself

By doing that you end up having a lot of methods that does not really belong to the Model logic. A better solution is through the finder pattern:

issues = IssuesFinder.new(project, user, filter).execute

What the finder does is to accept a set of parameters and return an ActiveRecord::Associations::CollectionProxy that you can use for further filtering/mapping operations.

You can find a complete example of a finder following this link

Read on

JSON API error handling with ruby

Created at: Mar/29/2016 jacopo beschi

Hello folks, after being inactive for a while I am finally back!
This post is about a technique that I found useful for general error handling with REST API and ruby on rails, but the concept works for any programming language/framework. Most of the time programmers handles api errors without a general pattern, that's bad because it's obvious that is more error prone, by using a general pattern you can also do some interestic automatic operations such as error logging and messaging to a monitoring service.

Read on

Ruby: Dynamically create callbacks with metaprogramming

Created at: Jun/12/2015 jacopo beschi

Hello guys, in this post I'll explain you how you can handle callbacks with metaprogramming on ruby on rails. But before going deeper into detail you should ask me the reason of that: why shall you use callbacks instead of using general oop techniques? For example in a classic oop design given that you have:

Class X
   def method_x(*args)
      #do something
Read on

Testing for custom Rails validators with Rspec and metaprogramming

Created at: Apr/25/2015 jacopo beschi

Hello guys, some days ago I've made a custom validator for Rails and I wanted to test that it was used correctly in my model (which uses ActiveModel::Model). As a brief preface you have to know that to test for the common Rails validators you can use the shoulda matchers library.

Read on