jacopo beschi: yet another web guy

Design pattern Archive

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
    end
end

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

Javascript multiple inheritance with ES5 and AngularJs

Created at: Mar/31/2016 jacopo beschi

In this article I'll show you how you can use multiple inheritance(trait) with Javascript EcmaScript 5 and AngularJS 1.0. AngularJS offer you a method: angular.extend that allow you to extend any object with the values and methods of other objects. In this article I show you how you can create a Dog. Dog is an animal but is also a mammal, for this reason a Dog needs to extend animal and also Mammal classes. Below is the code to create a Dog:

Read on

Test double php introduction

Created at: Nov/20/2014 jacopo beschi

When you start unit testing your code you don't want to test a full behavior of your code but just a little part in isolation to the rest. In order do do that you need to isolate your class from the other context, test doubles are tools (aka patterns) that allow you to do that. There are five common test double:

  1. Dummy
  2. Stub
Read on

PHP: why and when static is evil

Created at: Nov/09/2014 jacopo beschi

Hello guys, many pepole asked me in various situation if they should use static in their code, for this reason i decided to briefly explain why static is generally a bad choiceh and in which cases you can use it.

Read on

PHP Value Object

Created at: Oct/13/2014 jacopo beschi

Let's talk about, Value objects. The first thing to undersdand is why and when they are needed. Generally when you save some data you pair with a variable/attribute a value. In every programming language there are many type of values, for example strings,numbers and so on; imagine now that you need to save the number of apples and user have bought:

<?php 
$total_apples = 20;
Read on