<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title></title>
    <description>The mussings of a guy who tries to code.</description>
    <link>https://vjustov.me/</link>
    <atom:link href="https://vjustov.me/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Fri, 18 Sep 2026 17:45:45 +0200</pubDate>
    <lastBuildDate>Fri, 18 Sep 2026 17:45:45 +0200</lastBuildDate>
    <generator>Jekyll v4.0.1</generator>
    
      <item>
        <title>ActiveRecord Repositories?</title>
        <description>&lt;p&gt;For the last couple of weeks I have been refactoring an app at work, and the idea of using a repository object to constrain the access to the db has been on my mind the whole time.
We’re currently boarding the hexagonal architecture wagon, and after watching &lt;a href=&quot;&quot;&gt;Architecture: The Lost Years&lt;/a&gt; and &lt;a href=&quot;&quot;&gt;Decoupling from Rails&lt;/a&gt; they gave us a little insight on which way to go. This is a not-so-green-field app, and we are using Rails, of course, with ActiveRecord. This means a lot of work with measly gains., But sometimes you can’t fight the river you are swimming on so we decided to embrace AR.&lt;/p&gt;

&lt;p&gt;And then, when working with this simple interactor object, I noticed this repeated pattern of using the ever so common:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;13&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;update&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model_params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# things went well&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# something went wrong&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Because of &lt;code class=&quot;highlighter-rouge&quot;&gt;.create&lt;/code&gt; I wondered &quot;wouldn’t it be nice if there was an &lt;code class=&quot;highlighter-rouge&quot;&gt;.update&lt;/code&gt; or a &lt;code class=&quot;highlighter-rouge&quot;&gt;.destroy&lt;/code&gt;?&quot; and in fact there are. The catch is that these methods always return an instance whether or not they were saved to the db. So now the code looks like this:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;model&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;Model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;model_params&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;model&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;persisted&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;?&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;things&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;went&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;well&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;else&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;#&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;something&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;went&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrong&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Which I think is sort of nice, I’m using ActiveRecord::Base class methods to define the API of my &quot;repository&quot; and only using the model instance as an &quot;entity&quot; (Yes, sure, I still have persistence logic in the entity. And  in fact, I’m using it, but that can be a little to much to ask of AR).&lt;/p&gt;

&lt;p&gt;Before you close the post, yes i know these methods actually do the same thing I was doing before, but it serves as a &quot;boundary&quot; between the two, and although it’s not perfect, it leaves us with a clear protocol (or port, in &lt;a href=&quot;alistair.cockburn.us/Hexagonal+architecture&quot;&gt;Cockburn’s terms&lt;/a&gt;) which would allow us to switch away from Active::Record easily.&lt;/p&gt;

&lt;h2 id=&quot;there-are--some-problems-with-this-though&quot;&gt;There are  some problems with this though:&lt;/h2&gt;

&lt;h4 id=&quot;we-dont-have-a-method-to-save-an-entity-to-the-db&quot;&gt;We don’t have a method to save an entity to the db:&lt;/h4&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;model.active = false
Model.save(model)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A &lt;code class=&quot;highlighter-rouge&quot;&gt;.save&lt;/code&gt; method would be a nice addition to these methods, otherwise we would have to use the &lt;code class=&quot;highlighter-rouge&quot;&gt;.update&lt;/code&gt;, with all the changes made to the object; or defining our own class method to save these differences to the db.&lt;/p&gt;

&lt;p&gt;Because our models serve two purposes i wouldn’t want to inherit from any new class, I would do something along the concerns lines:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;module Repository
  def save(entity)
    entity.save
  end
end

class Order &amp;lt; ActiveRecord::Base
  extend Repository
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;applying-authorization-policies-can-get-in-the-way&quot;&gt;Applying authorization policies can get in the way.&lt;/h4&gt;

&lt;p&gt;In this project, I’m working with &lt;a href=&quot;https://github.com/elabs/pundit&quot;&gt;Pundit&lt;/a&gt; and normally when editing or destroying a resource we first fetch the resource, ask our policies if we are allowed to update/destroy that resource and then, and only then, update/destroy said resource.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;order = Order.find(order_id)
policy(order, :update?)
order.update(order_params)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;But, because now you are doing the fetching and the updating in the same command you’d have to customize your policy to use the resource’s id instead of the object and make the minimum query possible. This probably means that your queries aren’t going to be pretty:&lt;/p&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class PagePolicy &amp;lt; ApplicationPolicy
  def show?
    manager? || user_can_access_pages?
  end

  def user_can_access_pages?
    user.clients.includes(papers: :pages).pluck(&apos;pages.id&apos;).include?(record_id)
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;policy(order_id, :update?)
Order.update(order_id, order_params)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h4 id=&quot;eager-loading-associations&quot;&gt;Eager loading associations&lt;/h4&gt;

&lt;p&gt;There are cases where you want to fetch the object and its  associations in a single query. For that ActiveRecord uses the &lt;code class=&quot;highlighter-rouge&quot;&gt;.includes&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;.preload&lt;/code&gt;, &lt;code class=&quot;highlighter-rouge&quot;&gt;.eager_load&lt;/code&gt; methods which we cannot use on the instance.&lt;/p&gt;

&lt;p&gt;Aesthetically speaking &lt;code class=&quot;highlighter-rouge&quot;&gt;Newspaper.includes(:pages).update(newspaper_id, newspaper_params)&lt;/code&gt; looks and reads horrible.&lt;/p&gt;

&lt;p&gt;And this breakes the whole boundary idea we spoke about earlier.&lt;/p&gt;

&lt;p&gt;###So,
Let me know, on the comments section or on twitter,  what you think about what you just read. This is just an idea that I’ve been fiddling about on paper, no real attempt has been made to achieve this &lt;strong&gt;YET&lt;/strong&gt;.&lt;/p&gt;
</description>
        <pubDate>Sat, 20 Jun 2015 00:00:00 +0200</pubDate>
        <link>https://vjustov.me/2015/activerecord-repositories/</link>
        <guid isPermaLink="true">https://vjustov.me/2015/activerecord-repositories/</guid>
        
        
      </item>
    
      <item>
        <title>The quest for a sane ruby environment on windows x64</title>
        <description>&lt;p&gt;You tried ruby in a class and are starting to think about installing it on your laptop and using it for a forthcoming proyect. You google a bit and notice that most of the ruby comunity runs on macs and ubuntu’s, some of them VMs, for some particular reason you dont want to run a full Virtual machine to code in and your workflow in windows is smooth enough to give it a try (or perhaps you are a windows fanboy).&lt;/p&gt;

&lt;p&gt;You decide to pave the road ahead and want to install all you’ll ever need for your new journey with ruby on windows.&lt;/p&gt;

&lt;p&gt;In building my actual development environment for my x64 i have come across my fair share of roadblocks and issues that would have been easily avoided had I known all this. My goal is for this environment to be easy to use, easy to adopt and flexible enough to at the least &quot;try&quot; to catch up onto ruby for other platforms. Needless to say, ruby on windows is kinda like IE in the browser wars.&lt;/p&gt;

&lt;h3 id=&quot;tldr&quot;&gt;TL;DR&lt;/h3&gt;
&lt;p&gt;In order to have an awesome webdev env:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Install ruby/rails.&lt;/li&gt;
  &lt;li&gt;Install pik to manage rubies -v. Use a path with no spaces.&lt;/li&gt;
  &lt;li&gt;Make sure to install devkit for each ruby installed through pik.&lt;/li&gt;
  &lt;li&gt;x64 rubies cannot be installed through pik.&lt;/li&gt;
  &lt;li&gt;Use Vagrant for sharing your development env with other devs.&lt;/li&gt;
&lt;/ul&gt;

&lt;hr /&gt;
&lt;p&gt;Before we start with anything, I must say that I encourage this setup if you are planning on toying with ruby on the long run. if you only wanna try rails to see if it fits your needs then you can make do with just the rails installer. It is strongly recomended that your user does not contain any spaces in its username. For those of you still using XP you have two options: one is setting a &lt;code class=&quot;highlighter-rouge&quot;&gt;%PIK_HOME%&lt;/code&gt; to a folder without spaces, that folder will become the place were pik will download and install all gems and rubiesand the other one is to create a symbolic link to the &lt;code class=&quot;highlighter-rouge&quot;&gt;C:/document and settings/&lt;/code&gt; folder and use that to &quot;mask&quot; the spaces.
&lt;strong&gt;UPDATE:&lt;/strong&gt; If you ever plan on debugging gems (and you eventually will.). you must set a &lt;code class=&quot;highlighter-rouge&quot;&gt;%BUNDLE_EDITOR%&lt;/code&gt; to the path of your editor of choice in order to use &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;gt;bundle open&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you are like me, you probably went the &quot;&lt;a href=&quot;http://rubyinstaller.org/&quot;&gt;RubyInstaller&lt;/a&gt; (or &lt;a href=&quot;http://rubyinstaller.org/&quot;&gt;RailsInstaller&lt;/a&gt; for that matter.) way&quot;, which works marvelously and i wouldnt recommend any other way. Luis Lavena and the crew at OneClick have done a marvelous job into making ruby to run as smoothly as posible with Win32 (But this is another topic.).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/content/images/2014/Jan/railsinstaller_PNG-1.png&quot; alt=&quot;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;With these installers you only need to follow the wizard and by the end of it, you will have ruby (and rails, if you used the RailsInstaller), git, DevKit, Bundler, RubyGems. Its fair to point out that RailsInstaller does not have a stable release for ruby 2.0 yet.&lt;/p&gt;

&lt;p&gt;Later on, you will start working on a proyect that requires a diferent &lt;code class=&quot;highlighter-rouge&quot;&gt;ruby -v&lt;/code&gt; than the one you currently have installed. For that us, windows folk use &lt;a href=&quot;https://github.com/vertiginous/pik&quot;&gt;Pik&lt;/a&gt;.  Pik is a Ruby Version Manager on Windows. It still misses some of the features of its *nix counterpart RVM, like gemsets. While in RVM you can change your active gems by using a gemset. Pik has a fixed set of gems for each project sharing that ruby.
&lt;strong&gt;UPDATE:&lt;/strong&gt; Pik development have been halted. Luis Lavena now encourages the use of &lt;a href=&quot;https://bitbucket.org/jonforums/uru&quot;&gt;Uru&lt;/a&gt;, an cross-platform version manager.Which in contrast, does support gemsets. (I haven’t used it but will give it a try soon enough)&lt;/p&gt;

&lt;p&gt;There are two ways of installing pik; an installer and using the gem. as you may imagine we will walk down the gem path in order to familiarize the uninitiated (if any).&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;gem install pik
Fetching: pik-0.2.8.gem
...
Successfully installed pik-0.3.0
1 gem installed
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;After the gem is installed, you can use the ‘pik_install’ script to install the pik executable. Install pik to a location that’s in your path, but someplace other than your &lt;code class=&quot;highlighter-rouge&quot;&gt;/ruby/bin&lt;/code&gt;. For instance, the directory &lt;code class=&quot;highlighter-rouge&quot;&gt;C:/bin&lt;/code&gt; is in my path:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;path
PATH=c:\\pik;C:\\Program Files\\Windows Resource Kits\\Tools\\;c:\\ruby\\Ruby-186-p383\\bin;`
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So I run:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;pik_install C:\\pik
Thank you for using pik.
Installing to C:\\bin
...
pik is installed
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now you can install your x86 rubies through pik by doing &lt;code class=&quot;highlighter-rouge&quot;&gt;pik install ruby 2.0&lt;/code&gt; or &lt;code class=&quot;highlighter-rouge&quot;&gt;pik install jruby&lt;/code&gt; (also IronRuby). For each of these installed rubies you will have to enhanece its actual and future gems with DevKit by downloading the corresponding devkit -v and afterwards you will need to cd into the DevKit directory (which in my case is the one in the folder created by rails installer.). once there you will use the newly installed version of your ruby.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;gt;cd C:/RailsInstaller/Devkit1.9.3
&amp;gt;pik use 
&amp;gt;ruby dk.rb init
&amp;gt;ruby dk.rb install
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Devkit’s &lt;code class=&quot;highlighter-rouge&quot;&gt;init&lt;/code&gt; flag will create a config file with all the path to the rubies that this particular devkit will enhance. After validation the &lt;code class=&quot;highlighter-rouge&quot;&gt;install&lt;/code&gt; will proceed with the installation. If you already installed it and something went wrong you might need to add the &lt;code class=&quot;highlighter-rouge&quot;&gt;-f&lt;/code&gt; flag to force installation.&lt;/p&gt;

&lt;p&gt;With this you have a nice environment ready, but another good addition to the bundle is vagrant. Vagrant is a tool for automating development environments so that each and everyone of the members of a team can work with the same tools. It removes the &quot;it works on my machine&quot; variable from the equation.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Instead of building a virtual machine from scratch, which would be a slow and tedious process, Vagrant uses a base image to quickly clone a virtual machine. These base images are known as boxes in Vagrant, and specifying the box to use for your Vagrant environment is always the first step after creating a new Vagrantfile.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;First we’ll need to install Vagrant and virtual box (because it’s my preferred flavor, you can use VMware, AWS, or &lt;a href=&quot;http://docs.vagrantup.com/v2/providers/&quot;&gt;many other providers&lt;/a&gt;). After you have run both wizards, you must fetch a box(or boxes) and add it to your &quot;warehouse&quot; (get it? boxes are mostly on warehouses, HAHAHAH… ok, i’ll shut up).&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ vagrant box add base http://files.vagrantup.com/precise32.box
$ vagrant init
$ vagrant up
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The first command adds a box at the given url and stores it under the name &lt;code class=&quot;highlighter-rouge&quot;&gt;base&lt;/code&gt;. The second command initializes the current directory to be a vagrant environment and creates a vagrantfile, which you can later edit to fine tune the configuration of your virtual environment. Vagrant up then proceeds to create and configure the guest machine according to the specification in your vagrant file.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The primary function of the Vagrantfile is to describe the type of machine required for a project, and how to configure and provision these machines. Vagrantfiles are called Vagrantfiles because the actual literal filename for the file is Vagrantfile (casing doesn’t matter). Vagrantfile is supposed to be committed to version control. This allows other developers involved in the project to check out the code, run vagrant up, and be on their way.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After running the above three commands, you’ll have a fully running virtual machine in VirtualBox running Ubuntu 12.04 LTS 32-bit (Thats the precise32.box). You can SSH into this machine with &lt;code class=&quot;highlighter-rouge&quot;&gt;vagrant ssh&lt;/code&gt;, and when you’re done playing around, you can remove all traces of it with &lt;code class=&quot;highlighter-rouge&quot;&gt;vagrant destroy&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;###In Summary&lt;/p&gt;

&lt;p&gt;Your development environment is the place were you spend most of the time working, tweeking and throwing commands. Put the effort in to ensure it’s not just functional but pleasant to use.&lt;/p&gt;
</description>
        <pubDate>Sun, 02 Feb 2014 00:00:00 +0100</pubDate>
        <link>https://vjustov.me/2014/the-quest-for-a-sane-ruby-environment-on-windows-x64/</link>
        <guid isPermaLink="true">https://vjustov.me/2014/the-quest-for-a-sane-ruby-environment-on-windows-x64/</guid>
        
        
      </item>
    
  </channel>
</rss>
