<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Bogdan Gusiev's blog</title>
 <link href="http://gusiev.com/atom.xml" rel="self"/>
 <link href="http://gusiev.com"/>
 <updated>2023-04-24T18:36:01+03:00</updated>
 <id>http://gusiev.com/</id>
 <author>
   <name>Bogdan Gusiev</name>
   <email>agresso@gmail.com</email>
 </author>
 
 
 <entry>
   <title>"Naming Things" Solution</title>
   <link href="http://gusiev.com/2017/11/naming-things-problem"/>
   <updated>2017-11-23T00:00:00+02:00</updated>
   <id>http://gusiev.com/2017/11/naming-things-problem</id>
   <content type="html">&lt;p&gt;Naming things in software is really a big deal. It gets harder and harder over time when a code base gets larger.
I’ve never encountered an article that would try to address the issue. So here, I will try to define good name criteria as well as naming strategies that appeared to be useful in my experience.&lt;/p&gt;

&lt;!--more--&gt;

&lt;h2 id=&quot;name-like-your-users-do&quot;&gt;Name like your users do&lt;/h2&gt;

&lt;p&gt;The most basic idea of giving a thing like class, method or function a good name is to name by how your users suppose to call it.
Like if there is a tweet in twitter I would assume that there should be a thing named “Tweet” inside twitter source code.
It is as simple as it is, but when you model things right your internal infrastructure just reflects the actual interaction between software users. This principle applies well to the structure we call OOD: the right OOD has maximum number of classes that your users can see through software interface and minimum number of classes that your users can not see. And those things that users can see should have their name from how your users call it or would call it.&lt;/p&gt;

&lt;p&gt;Good example when things can get a different name:&lt;/p&gt;

&lt;p&gt;Lets say we have Article and Email and both of them are having a header. But in the article it is usually called “title” but in the email it is usually called “subject”. If you don’t have any shared code between the two, it is better to stick with different names rather than the same: it will make you closer to how your users call it and will avoid unnecessary naming collision that is described later.&lt;/p&gt;

&lt;p&gt;It is worse noting that this is not the only one principle to follow. There can be a number of reasons you may go with a different name rather than the name that comes naturally. They are described below.&lt;/p&gt;

&lt;h2 id=&quot;avoid-simplifications-through-naming&quot;&gt;Avoid simplifications through naming&lt;/h2&gt;

&lt;p&gt;If there is some completely new thing that needs to be named for both end users and software, ensure that there is no other thing that has the same name in your software.
Human brain tend to simplify the world around us and use the same name for different things.&lt;/p&gt;

&lt;p&gt;Here is a good example:
Suppose your software allows people to subscribe to newsletter and to unsubscribe from email notifications.
In this case, human brain interprets this two possibilities as logically opposite while they are not: there is actually three states the person can have: subscribed to newsletter, unsubscribed from email notifications and undefined (or default); or even 2 independent options with “Yes/No” states. Even though all software developers in your team understand the difference, you can not expect all people in the world to understand that.
In order to avoid the automatic simplification, you’d better call those states: opted in for newsletter (or simply “opted_in” internally) and unsubscribed from email notifications (or simply “unsubscribed” internally).&lt;/p&gt;

&lt;p&gt;The idea that “Opted In” should stuck as a term for newsletter subscription and “Unsubscribed” should stuck for email notifications. When people speak fast, they will always shorten those terms. It means that through the daily conversation certain verbs will be used instead of describing the entire action. Example: “How many subscribed people do we have?” will have some uncertainty in case “to subscribe” will be used for both: describing newsletter subscription and notification emails subscription instead of just one.&lt;/p&gt;

&lt;h2 id=&quot;naming-uniqueness&quot;&gt;Naming Uniqueness&lt;/h2&gt;

&lt;p&gt;Here is a good example from my practice on how naming uniqueness can be broken:
English has two words “Company” and “Campaign”. In my native tongue they actually translated into a single word: “Компания” /compania/. It is better to just avoid using these both terms for naming in the international team. While this is a rare case, it gives the idea on how naming problem appears: Russian conversation don’t make a difference between 2 things that are pretty different. Same case appears naturally within a single language all the time.&lt;/p&gt;

&lt;p&gt;The other source of naming collision can still be a fast speech between team members.
Suppose your software works with “WebPageSnapshot” as well as “ServerSnapshot”
When people speak fast, they will tend to use “Snapshot” for both terms. It can lead to misunderstanding in daily conversation.
The phrase: “I made a patch that touches a snapshot source code, please take a look” doesn’t define the actual place to look at. It will almost certainly lead to time waste from people assumptions they understood each other. Either the speaker or the listener could not be aware that both types of snapshot exist and they may feel natural with the assumption that “Snapshot” can only mean one of the types.
Please only use adjectives to distinguish between terms if they are actually same thing at some level of abstraction like:
“VideoFile” and “ImageFile” are just “File” at some levels in your software (in other words: share code and behaviour).&lt;/p&gt;

&lt;p&gt;Using same adjectives to ensure things are connected by their names is a good idea.
Consider the business rule: “Affiliate Member” can participate in “Affiliate Campaign”. 
We will not make any confusion by using same adjective for both terms. Avoiding adjectives in fast conversation will not be that misleading: “The member with email ‘bogdan@example.com’ has joined the campaign 2 days ago”
A business rule like: ‘“Member Affiliate” can participate in “Campaign Affiliate”’ is worse than a weapon of mass destruction for communication inside your team. 
The general rule: ensure that shorten versions of names are easy to use in daily conversations.&lt;/p&gt;

&lt;h2 id=&quot;naming-internal-objects&quot;&gt;Naming internal objects&lt;/h2&gt;

&lt;p&gt;What I mean by internal here is a thing that is pretty known inside your software but completely unseen by the end users.
Examples from Rails: “UsersController” or “UsersHelper”&lt;/p&gt;

&lt;p&gt;Note that there is no problem in calling every controller like “WhateverController” (with the same suffix) because all of those controllers actually share code and behaviour, so they are the same thing at some basic level while “WebPageSnapshot” and “ServerSnapshot” were not in the previous example.
Some objects are just internal - end users don’t interact with them directly. There are some principles that would allow to give them a good name too.&lt;/p&gt;

&lt;p&gt;At first, ensure that internal thing doesn’t take a name of the thing that is actually available to the end users. This is very important to do: leave good names for things your users care most and can interact with.&lt;/p&gt;

&lt;p&gt;At second,  it is good to give them a longer names to encode more information on what they do. There could be understanding illusion connected to a name. Internals of your software tend to do some specific work. When you give them a short name, software developers tend to assume they understand what they do. It can be a complete illusion.&lt;/p&gt;

&lt;p&gt;At third, sometimes it is even a good idea to give a confusing name to something.
 You may avoid that illusion by giving a name that would cause software developer to never assume what this thing does before looking at it.&lt;br /&gt;
 Choose wisely if you want your developers to assume what a method does based on name or not. These assumptions can be misleading for complex cases. In my project, we have a method called &lt;code&gt;def fuck_short_url(text)&lt;/code&gt;. It has more comments than lines of code: it makes a very specific replacement of links inside a text fragment and we don’t want any developer to assume they know what it does based on name. Here is the method source code for the most curious readers: &lt;a href=&quot;https://gist.github.com/7a823c15ac350aaf948668f0fc3a3238&quot;&gt;https://gist.github.com/7a823c15ac350aaf948668f0fc3a3238&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;There could be 3 types of names:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Direct name - like your users call it&lt;/li&gt;
  &lt;li&gt;Descriptive name - long and describing the function of an object or method&lt;/li&gt;
  &lt;li&gt;Strange Name - eliminates the name based assumption&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Good principles to follow:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Follow naming that was already given by software users or community/business overall&lt;/li&gt;
  &lt;li&gt;Name things uniquely to avoid confusion&lt;/li&gt;
  &lt;li&gt;Avoid naming collision through simplification of terms and shortening of actions&lt;/li&gt;
  &lt;li&gt;Give longer names to more complex things&lt;/li&gt;
  &lt;li&gt;Give shorter names to things that are frequently used&lt;/li&gt;
&lt;/ol&gt;

</content>
 </entry>
 
 <entry>
   <title>Guiding Software Development Principles</title>
   <link href="http://gusiev.com/2017/07/guiding-software-development-principles"/>
   <updated>2017-07-07T00:00:00+03:00</updated>
   <id>http://gusiev.com/2017/07/guiding-software-development-principles</id>
   <content type="html">&lt;p&gt;It is becoming popular to talk about software development principles from time to time. The new major concepts appear and die due to the fact that most of them are designed to be perfect but they are not. If you would stay in IT long enough, you encounter articles and talks where people discovered an amazing concept “X” that solved all their development problems and brought their team to inevitable success. Unfortunately that “permanent success” is usually temporary or even a self-deception. The truth is: there is no silver bullets in software development just like in any other engineering process. Engineering of world class products requires precise resources control, trade offs and balance between easy and complex, fast and slow, and beautiful and ugly. Here, I will try to explain my guiding software development principles as well as the why-s.&lt;/p&gt;

&lt;!--more--&gt;

&lt;h2 id=&quot;priority-matters&quot;&gt;Priority Matters&lt;/h2&gt;

&lt;p&gt;We often forget that most principles don’t match not only with the real solutions but also each other: you can not swim and guard clothing at the same time. So first thing you need to consider is priority of your principles. Some of them should be more important than others. In this way you can solve contradiction between them: if there are two principles and they can not be followed at the same time, pick the higher priority one.&lt;/p&gt;

&lt;h2 id=&quot;less-is-more&quot;&gt;Less is More&lt;/h2&gt;

&lt;p&gt;There should be very limited number of principles to follow. More principles under the same concept just creates more contradiction and leaves less room to be situational and follow additional guidelines that would fit best for given problem. I think the ideal number in your “guiding principles” that apply &lt;strong&gt;almost&lt;/strong&gt; to everything in your software should be 3. Higher numbers are possible.&lt;/p&gt;

&lt;p&gt;Christian God was so kind that he left us with 12 that still have very small contradiction like: should I steal a weapon from a person that wants to kill me tomorrow? That’s an amazing result for human moral that is at least 1000x times older than software development. Based on our level of understanding of software engineering, we can effort only 3 at the same time.&lt;/p&gt;

&lt;h2 id=&quot;build-for-humans&quot;&gt;Build for Humans&lt;/h2&gt;

&lt;p&gt;Software is always build by humans and for humans. Software should do what it should do in a convenient, fast and robust way. If any principle violates the usefulness of software, it can be only partially followed. And that is fine because principles are imperfect. They should not be taken as dogmas because they are defined imperfectly by imperfect people.&lt;/p&gt;

&lt;h2 id=&quot;getting-to-the-core&quot;&gt;Getting to the core&lt;/h2&gt;

&lt;p&gt;So here are principles that guide me through software development sprints as well as through software development marathons:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Software should do what it should do.&lt;/li&gt;
  &lt;li&gt;Model the real world&lt;/li&gt;
  &lt;li&gt;Build an Architecture&lt;/li&gt;
  &lt;li&gt;Track Dependencies&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Lets go through them one by one:&lt;/p&gt;

&lt;h2 id=&quot;1-model-the-real-world&quot;&gt;1. Model the real world&lt;/h2&gt;

&lt;p&gt;This is the most important and genius principles I ever encounter that is even older than computers comes for analogue computers: model the real world.
Try to be as close as possible to the piece of world you are modelling in the software: if there are users of your software, there should be a User in your code. If you simulate an iPhone, there should be an essence of iPhone in your software that performs all of its functions no matter how many of them there are. Also this principle requires you to name things right making them closer to processes they simulate. There will be always a gap between how the process is simulated in software and the real process, but this gap needs to be as close as possible to ensure software does things efficiently.&lt;/p&gt;

&lt;p&gt;We tend to think that a software is complex. But the complexity comes from several factors. First is the processes being modeled are complex - here you would need to rethink them to make them simpler. But the second reason is software developer didn’t model them right. If you model things as they are, you eliminate second factor. If you model things even better than they are, you can even eliminate the first factor. It is worth mentioning that is only possible if you have a power to alter the process or the process you are modeling doesn’t exist yet. This is mostly the case for the modern software. If you are about to make a “select your seat” functionality for the plane ticket, there is no way you can simplify plane seat locations. If you make a new e-commerce web site, you are an author of most processes and can make product finding functionality as simple as possible until you reach a fact that it needs to fit not just your needs but also all your users needs which are quite different.&lt;/p&gt;

&lt;h2 id=&quot;2-build-an-architecture&quot;&gt;2. Build an Architecture&lt;/h2&gt;

&lt;p&gt;Architecture is often seen as a luxurius term that can only be understoon and build by an elite part of development team. Sometimes in outsource companies this is given to the team by someone like company CTO that is not even part of the team. But architecture is something more trivial and someone everyone in a team is building. Lets try to define architecture in the most basic way:
Architecture is the code that is used by your software the most number of times.
As you model more and more processes, the code base grows. It needs to be generalized and reused. We all know which problems come from unreused code: bug fixes and changes need to be made more than once that inevitably leads to the lack of consistency in software where buttons that would suppose to do the same thing do it in a slightly or not so slightly different way. But code reuse is more than that: architecture is just a code reuse metric. If you have a code that is easy to reuse, the architecture arises on its own because people tend to reuse existing code instead of writing new. From the other hand, you can measure the “architectureness” of any code piece including third party(aka gems) by counting how many times it is used. That definition is well aligned with the popular idea that architecture is hard to change: if you reuse a particular piece more, it gets harder and harder to change it because the change affects more places. Following this principle also requires a good test coverage. The most reused code is usually the most covered by tests. You can not test everything, but you can define your priority based on number of usages of particular module.&lt;/p&gt;

&lt;h2 id=&quot;3-track-dependencies&quot;&gt;3. Track Dependencies&lt;/h2&gt;

&lt;p&gt;This is the most advanced and the most complex principle of all three. I have heard so many times: “Hey, why couldn’t we just break up our code into reusable modules that are independent?” or “Can we just write many independent micro-services?”. Yes, this is all about real breaking of software into independent pieces. Many theoretical-software-developers think it is a silver bullet, but it is not. Firstly because you violate Principle 1: model the real world. If things are interconnected and mutually dependent in the real world, you can not model them differently. It is a big talent to be able to find those possibilities with the minimum sacrifices of principles 1 and 2 which are more important to follow, but still sometimes violated due to the need of breaking software into independent modules.
It is worse clarifying that there are 3 level of dependencies:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Mutually Dependent&lt;/li&gt;
  &lt;li&gt;One-way Dependent&lt;/li&gt;
  &lt;li&gt;Independent&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here is more formal definition: if you have code blocks A and B, they are totally independent if each block can perform its functions (aka pass all tests written) without other one. They are one-way dependent if A can perform its work without B or vice versa. And, finally, they are mutually dependent if neither can perform its work without another. In most cases, when Rails developers talk about several modules in their app, that is just a bullshit because there is no way one model can perform its work without others. They all are mutually dependent. And this is a very effective design because this is how the real world works: no modules - everything is mutually dependent as much as flowers and bees. You may start modeling pollination process from Bee class and Flower class and even consider them “independent modules” but in a few hours you would end up referencing one from another all the time.&lt;/p&gt;

&lt;p&gt;The name of the principle as “Track Dependencies” comes from the fact that there are 3 level of interdependencies and you should at least stop thinking too abstract about them: figure out how much they are dependent and control that. Usually making things completely independent is unnecessary within one application. One-way dependent is good enough. You in-app dependency tree should be based on your vendor libraries dependency tree that is very well defined. You may consider your Gemfile dependency tree as the ideal dependency tree. Maybe you will come along the same structure in your app but it will be 10 times harder due to reasons that are hard to explain in a small article.&lt;/p&gt;

&lt;p&gt;There is far more to say about it, but I’ll probably make a dedicated article about “Track Dependencies” later.&lt;/p&gt;

&lt;h2 id=&quot;0-software-should-do-what-it-should-do&quot;&gt;0. Software should do what it should do.&lt;/h2&gt;

&lt;p&gt;As I said earlier. You should always sacrifice principles if you can not build software that meets all the requirements with them.
It is worth admitting that this is due to the fact that you are not a perfect software developer. Maybe the task wants you to be 2x better at software development than you are. And you can not become 2x better in a blink of an eye while your project has deadlines. Most likely you would be able to complete the task following all your principles with infinite resources, but they will not happen any time soon. You need sacrifices due lack of time, brain power, people and restrictions from security, performance, usability and other sources. Those are main reasons that causes the code to look ugly and complex, but never give up the beauty completely.&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;There is more to say about each principle above. It lacks examples but it is hard to find good ones that don’t require a lot of context to understand. I’ll write a more detailed article on each principle. Also, even thought the priority of principles is pretty important to follow, there might be exceptions. Exceptions are hard to formalize. You should judge them based on your practical experience and still make an internal note why the priority was changes for a particular case, but not the rest.&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Something is wrong with Single Reponsibility Principle</title>
   <link href="http://gusiev.com/2016/01/single-responsibility-principle-srp-criticism"/>
   <updated>2016-01-12T00:00:00+02:00</updated>
   <id>http://gusiev.com/2016/01/single-responsibility-principle-srp-criticism</id>
   <content type="html">&lt;p&gt;I’ve never been a fan of Single Responsibility Principle aka SRP&lt;/p&gt;

&lt;p&gt;If any physical thing I used has multiple responsibilities, why would software be built from SRP things?&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;The role of information technology software is to build a model of the outer world. But is there many things in real world that follow SRP?&lt;/p&gt;

&lt;p&gt;SRP feels so elegant from the first glance: why isn’t it wonderful that every tool you have does only one specific job. And all your tools are always sharp and you know exactly which tool is designed for what kind of job.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://www.evansclarke.com.au/Lib/tools.JPG&quot; width=&quot;300px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Well, in fact even common instruments are usually multifunctional.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://upload.wikimedia.org/wikipedia/commons/8/84/Claw-hammer.jpg&quot; width=&quot;300px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It feels like hammer should be a perfect example of SRP, but it is not. 
Hint: Pay attention to what is on opposite sides of the handle: these are two different ends designed to do completely different job.
It is a good example how solution is done based on the process but not on SRP. SRP is too idealistic even for a hammer.
Convenience requires to merge two instruments into one to make it easy to switch from riving in to pulling out.
It always requires in depth integration of functions into processes.&lt;/p&gt;

&lt;p&gt;The pliers show even deeper integration in their design, allowing many processes associated with a wire to be done with using a single tool:&lt;/p&gt;

&lt;p&gt;And this multi-functionality factor expands as we move to more complex and famous devices. Like:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://store.storeimages.cdn-apple.com/4428/as-images.apple.com/is/image/AppleInc/aos/published/images/i/ph/iphone6/plus/iphone6-plus-box-space-gray-2014?wid=478&amp;amp;hei=595&amp;amp;fmt=jpeg&amp;amp;qlt=95&amp;amp;op_sharpen=0&amp;amp;resMode=bicub&amp;amp;op_usm=0.5,0.5,0,0&amp;amp;iccEmbed=0&amp;amp;layer=comp&amp;amp;.v=1411520743411&quot; width=&quot;200px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Moving into another direction: none of two things on the image below follows SRP.
Both of them are carrying not just an electrical charge, but some mass and spin as well. It tells that their behaviour is described by their electromagnetic and gravitational responsibilities. See that two is still not one.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://science.jrank.org/article_images/ep201102/science/science982.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Same thing happen in software world: utilities are better when designed to support a specific process and classes are made to simulate real world object that have tons of responsibilities.&lt;/p&gt;

&lt;p&gt;So next time you blame ActiveRecord::Base for having 200+ methods, try to think how well it is designed to support a CRUD process of a database entity.&lt;/p&gt;

&lt;h3 id=&quot;summary&quot;&gt;Summary&lt;/h3&gt;

&lt;p&gt;Principles discusses in software development always were very hot mainly because all engineering principles are pretty controversial. But I can definitely say that SRP is one of the most controversial among them.&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Choosing a Templating Language</title>
   <link href="http://gusiev.com/2015/02/criterias-template-language-erb-haml"/>
   <updated>2015-02-06T00:00:00+02:00</updated>
   <id>http://gusiev.com/2015/02/criterias-template-language-erb-haml</id>
   <content type="html">&lt;p&gt;Whenever you are serious about choosing the frontend template engine to generate HTML, here is some advice how you can make your choice easier and 
more effective. There are too many template languages right now, but there are only a few main criteria how you can limit your selection to 2-3 of them before
getting into details.&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;Here they are:&lt;/p&gt;

&lt;h2 id=&quot;logic-less-or-with-programming-language&quot;&gt;Logic-less OR with Programming language&lt;/h2&gt;

&lt;p&gt;The most common way to generate a template that would seem obvious to any developer would be using programming language.
Example: ERB for Ruby, JSP for Java etc.
That is a pretty straight way to implement loops, conditions and more rare view structures.&lt;/p&gt;

&lt;p&gt;On the other hand, logic-less templates like Mustache restrict you from using complicated structures in the template forcing you to extract and put them to a different layer.&lt;/p&gt;

&lt;p&gt;Example of the same condition in logic-less or programming language template engines:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erb&quot; data-lang=&quot;erb&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;%&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;posts&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty?&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;comments&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty?&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;x&quot;&gt;  &amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;Delete&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;&amp;lt;%&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;In logic-less template language, it would look like:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erb&quot; data-lang=&quot;erb&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;%&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;possible_to_delete_user?&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;x&quot;&gt;  &amp;lt;a href=&amp;quot;#&amp;quot;&amp;gt;Delete&amp;lt;/a&amp;gt;&lt;/span&gt;
&lt;span class=&quot;cp&quot;&gt;&amp;lt;%&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%&amp;gt;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;possible_to_delete_user?&lt;/span&gt;
  &lt;span class=&quot;vi&quot;&gt;@user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;posts&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty?&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;comments&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;empty?&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;That looks more clean but less flexible as the logic-less template forces you to make quality even if you would like to save time instead.&lt;/p&gt;

&lt;p&gt;In general I would recommend Logic-Free languages for a huge team with lots of beginner developers that need a strong force — like language restriction — to be controlled.&lt;/p&gt;

&lt;h2 id=&quot;frontend-rendering-support&quot;&gt;Frontend Rendering support&lt;/h2&gt;

&lt;p&gt;Frontend rendering can be the ultimate reason to switch to this type of template language. The decision here usually comes from more strong reasons of the application design rather than template language properties.
I will assume that you already know if you want to use frontend rendering or not.
And in case of yes, you would need to think if it is ok to have a single language  for both — front and back end — or a different one for backend rendering.
Two template languages overhead can be not so large overhead sometimes. For example, when the backend templates are only used for static layout like html head, or footer and header of the page, or admin pages.&lt;/p&gt;

&lt;p&gt;It is pretty obvious that such a template language needs to use JavaScript in some way even though it is CoffeeScript.&lt;/p&gt;

&lt;h2 id=&quot;minimalistic-or-html-compatible&quot;&gt;Minimalistic OR HTML Compatible&lt;/h2&gt;

&lt;p&gt;HTML is being blamed for too ambiguous structures: you should always repeat yourself with closing tags, often used html attributes don’t have shortcuts etc.
Minimalistic template languages are not trying to look like HTML and provide a convenient way to write an XML-like structure.
When I write HAML instead of ERB, my fingers run a significantly smaller distance.&lt;/p&gt;

&lt;p&gt;On the other hand, minimalistic templates can be hard to understand for old school software developers that parse HTML on autopilot.
If your team has too many developers like that, then a Minimalistic template engine isn’t worth it.&lt;/p&gt;

&lt;h2 id=&quot;sandbox-mode-support&quot;&gt;Sandbox Mode support&lt;/h2&gt;

&lt;p&gt;The Sandbox Mode support requirement is pretty exotic. It is only required if you would like to allow users to change the appearance of site pages significantly
by allowing them to edit the template themselves without modifying the site source code.
Maybe it is not obvious to everyone, but in this case, template language would need to support many limitations to the template source code that will prevent your server from blowing up.
The most trivial example of how a template could blow up a server would be loops nested to each other one hundred times that will consume too many resources.
Sandbox is a super mode of the logic-less template. When not just full power programming language is not allowed, but additional limitations exist to make any possible template safe.
Most popular template languages like ERB, HAML or EJS (just like ERB but Javascript instead of Ruby) don’t support Sandbox. But here are two that do: &lt;a href=&quot;http://github.com/shopify/liquid&quot;&gt;liquid&lt;/a&gt; and &lt;a href=&quot;https://github.com/stefankroes/scribble&quot;&gt;scribble&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;p&gt;After a clear answer to what kind of template you need, the choice becomes more obvious as there will be only a few template engines matching given requirements.
I’ve even spent some time letting to build this nice table for you:&lt;/p&gt;

&lt;table class=&quot;bordered&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Logic-less&lt;/th&gt;
      &lt;th&gt;FrontEnd&lt;/th&gt;
      &lt;th&gt;Minimalistic&lt;/th&gt;
      &lt;th&gt;Variants&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;mustache, handlebars&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt;hamstache&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;mustache, curly&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt;Haml-coffee, Jade&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;EJS, ECO&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt;HAML, Slim&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;ERB +alternative compilers&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;sandbox&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;sandbox&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;sandbox&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;yes&lt;/td&gt;
      &lt;td&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;sandbox&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;no&lt;/td&gt;
      &lt;td&gt;Liquid, Scribble&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;This table doesn’t include all variants, but I’ve tried to fill in as many cells in the last column giving at least 2 variants if possible.&lt;/p&gt;

&lt;p&gt;The current software world of Ruby &amp;amp; JavaScript doesn’t give full spectrum of choices when you would like to have Sandbox mode support.
As I said before: Sandbox Mode is very rare requirement.&lt;/p&gt;

&lt;p&gt;But in other cases you have enough choices. It is strange that there is no logic-less, frontend and minimalistic template engine now. If there is one - please suggest me in a comment.&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>My Talk from RubyC conference</title>
   <link href="http://gusiev.com/2014/07/rubyc-conference-video-parsers-slides"/>
   <updated>2014-07-25T00:00:00+03:00</updated>
   <id>http://gusiev.com/2014/07/rubyc-conference-video-parsers-slides</id>
   <content type="html">&lt;p&gt;Here is a video from the excellent &lt;a href=&quot;http://rubyc.eu/&quot;&gt;Rubyc&lt;/a&gt; conference. Hope you will find this video funny as well:
&lt;a href=&quot;https://www.youtube.com/watch?v=OlXdngQUix8&quot;&gt;All about parsers in ruby&lt;/a&gt;
&lt;!--more--&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Took part in a podcast with Alexey Vasiliev</title>
   <link href="http://gusiev.com/2014/05/podcast-interview-le0pard-open-source-contribution"/>
   <updated>2014-05-27T00:00:00+03:00</updated>
   <id>http://gusiev.com/2014/05/podcast-interview-le0pard-open-source-contribution</id>
   <content type="html">&lt;p&gt;
Last weekend I took part in the a podcast with Alexey Vasiliev. We talked about my path in Open Source, Rails and IT industry in ukraine.

&lt;/p&gt;
&lt;p&gt;
It is all in Russian.
You can listen it here:
&lt;br/&gt;
&lt;strong&gt;
  &lt;a href=&quot;http://www.rwpod.com/posts/2014/05/24/cafe-002.html&quot;&gt;http://www.rwpod.com/posts/2014/05/24/cafe-002.html&lt;/a&gt;
&lt;/strong&gt;
&lt;/p&gt;

&lt;!--more--&gt;



</content>
 </entry>
 
 <entry>
   <title>Minimalistic State Machine: Less is More</title>
   <link href="http://gusiev.com/2014/04/state-machine-minemalistic-validation-perfection-changes"/>
   <updated>2014-04-01T00:00:00+03:00</updated>
   <id>http://gusiev.com/2014/04/state-machine-minemalistic-validation-perfection-changes</id>
   <content type="html">&lt;p&gt;I have tried a bunch of ActiveRecord’s state machine gems.
Their functionality is very close to each other. It allows to validate a state transition
and reinvent ruby programming language to define transition methods.
The fact that all of them produces custom API to define a ruby method doesn’t look right…
&lt;!--more--&gt;&lt;/p&gt;

&lt;h3 id=&quot;samples-using-common-state-machine-gems&quot;&gt;Samples using common state machine gems&lt;/h3&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# State Machine gem example&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;state_machine&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:initial&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:parked&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:approve!&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;transition&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:pending&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:approved&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;after_transition&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:pending&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:approved&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:send_approval_email&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# Workflow gem example&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;workflow&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:pending&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;event&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:approve!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:transitions_to&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:approved&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:after_transition&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:send_approval_email&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;# And finally plain ruby/rails example&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;approve!&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;update_attributes!&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;approved&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;send_approval_email&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Maybe I don’t know all modern trends but ruby version looks more human readable to me.&lt;/p&gt;

&lt;h3 id=&quot;so-why-do-we-still-tend-to-use-state-machine-gems&quot;&gt;So why do we still tend to use state machine gems&lt;/h3&gt;

&lt;p&gt;The main reason is state change validation. It won’t be easy to always check if it is possible to transition object from state A to state B in plain Ruby.
This part of state machine is something that really matter.&lt;/p&gt;

&lt;p&gt;In my head it appears as some kind of allowed changes map:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;order_state_changes_map&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
  &lt;span class=&quot;kp&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:pending&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Initial state is always pending&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:pending&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:approved&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:rejected&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Pending can be changed to to paid and delivered&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:approved&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:paid&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Delivered can only be changed to paid&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;If the state machine’s main goal is to validate changes than let’s implement it as a validation:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Order&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;AR&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Base&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;validates&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:changes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;order_state_changes_map&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;With this pattern you can define state change methods in ruby:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;validates&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:state&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:changes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; 
  &lt;span class=&quot;kp&quot;&gt;nil&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:pending&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Initial state is always pending&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:pending&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:approved&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:rejected&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Pending can be changed to to paid and delivered&lt;/span&gt;
  &lt;span class=&quot;ss&quot;&gt;:approved&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:paid&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Delivered can only be changed to paid&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;upproved!&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# ....&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;upproved?&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# ....&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now it looks really readable and a person who never saw this kind of API before can figure out what is happening.&lt;/p&gt;

&lt;p&gt;Give a try to: &lt;a href=&quot;https://github.com/bogdan/changes_validator&quot;&gt;&lt;strong&gt;ChangesValidator&lt;/strong&gt;&lt;/a&gt; in your app&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Datagrid slides from Ruby Meditation 3</title>
   <link href="http://gusiev.com/2013/11/datagrid-slides-report-framework"/>
   <updated>2013-11-04T00:00:00+02:00</updated>
   <id>http://gusiev.com/2013/11/datagrid-slides-report-framework</id>
   <content type="html">&lt;p&gt;Here are my &lt;a href=&quot;http://gusiev.com/slides/datagrid/index.html#/&quot;&gt;slides about Datagrid gem&lt;/a&gt; from Ruby Meditation Conference last weekend.
Datagrid is a gem I’ve released almost 3 years ago. It is focused on table data representation and now allowes a full power flexibility to be called a Framework.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://github.com/bogdan/datagrid&quot;&gt;http://github.com/bogdan/datagrid&lt;/a&gt;&lt;/p&gt;

&lt;!--more--&gt;
</content>
 </entry>
 
 <entry>
   <title>Support validation through ajax in Rails 4.x</title>
   <link href="http://gusiev.com/2013/05/rails-ajax-validation-form-submit-javascript"/>
   <updated>2013-05-24T00:00:00+03:00</updated>
   <id>http://gusiev.com/2013/05/rails-ajax-validation-form-submit-javascript</id>
   <content type="html">&lt;p&gt;I have a constant feeling that in a modern web every web form should be submitted with AJAX not the old retro way with POST request. This is better for numerous reasons.&lt;/p&gt;

&lt;p&gt;At first, it gives better user experience
At second, AJAX is faster than regular request.
At the end,  it doesn’t require a work to redraw the page with it’s previous state if validation fails.&lt;/p&gt;

&lt;p&gt;Currently I am trying to make rails app support this out of the box.
And here is first small step to this goal:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/rails/rails/pull/8638&quot;&gt;This patch&lt;/a&gt; allows a complete validation though ajax using active model validators.
This is a &lt;a href=&quot;http://rails-ajax-validation.herokuapp.com/developers/new&quot;&gt;live demo&lt;/a&gt; of what it allows to do.
If you feel especially interested in this feature, say your +1 here or on github.&lt;/p&gt;

&lt;!--more--&gt;
</content>
 </entry>
 
 <entry>
   <title>Attach git commits to pivotal stories</title>
   <link href="http://gusiev.com/2013/04/git-story-id-pivotal-tracker-integration"/>
   <updated>2013-04-08T00:00:00+03:00</updated>
   <id>http://gusiev.com/2013/04/git-story-id-pivotal-tracker-integration</id>
   <content type="html">&lt;p&gt;I heard about 10 different ways to organize work flow around pivotaltracker and git and about 5 gems to support that process.
All of them seems abusing for me: the outcome of pivotal/git integration is a git commit attached to pivotal story and the rest of conventions like naming branches or merge policy doesn’t make any sense in long time perspective and is just a waste of time in fact.&lt;/p&gt;

&lt;p&gt;That’s why I’ve decided to make the most trivial integration that covers this idea and don’t provide additional complexity:
Meet &lt;a href=&quot;https://github.com/bogdan/git-storyid&quot;&gt;git-storyid&lt;/a&gt; gem - the only one git/pivotaltracker integration that saves your time.
&lt;!--more--&gt;&lt;/p&gt;

&lt;h2 id=&quot;installation&quot;&gt;Installation&lt;/h2&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;&lt;/span&gt;gem install git-storyid&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;usage&quot;&gt;Usage&lt;/h2&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;&lt;/span&gt;git storyid -m &lt;span class=&quot;s2&quot;&gt;&amp;quot;Initial implementation of campaign tags&amp;quot;&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Api token (https://www.pivotaltracker.com/profile): a56f0e9a4fbXXXXXXXXXXXXXX&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Use SSL (y/n): y&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Your pivotal initials (e.g. BG): BG&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# Project ID: 494XXX&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now interactive menu is displayed to select one of &lt;strong&gt;Started stories&lt;/strong&gt; owned by you:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;[1] Removing Billing Page
[2] Welcome Email
[3] Email Shares -  Capture
[4] Speed up activities by dates aggregation
[5] Mass Email to Customer List - thurs AM
[6] Investigate production error
[7] Tag campaign insertion points

Indexes(csv): 7
[campaign-tags 3020407]  [#44116647] Initial implementation of campaign tags
 1 file changed, 1 insertion(+), 2 deletions(-)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Result commit:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;commit 3020407e92cb125083cf50ad494ff15169a7f2e6
Author: Bogdan Gusiev &amp;lt;youremail@gmail.com&amp;gt;
Date:   Fri Mar 15 12:42:32 2013 +0200

[#44116647] Initial implementation of campaign tags

Feature: Tag campaign insertion points and 
campaigns with an identifier, 
so only campaigns with matching identifier will get shown&lt;/code&gt;&lt;/pre&gt;

&lt;h2 id=&quot;how-should-i-name-branches&quot;&gt;How should I name branches?&lt;/h2&gt;

&lt;p&gt;Branch names is not something that you should focus on. Unlike commit messages, branches are removed from history after merge. That is why you should name them in a way that takes less of your time. I personally prefer to have only one branch for myself called &lt;code&gt;bogdandev&lt;/code&gt; when I work on a project. When the feature is not done - there is no sense to work on another feature.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Background Jobs Frameworks: Resque vs DelayedJob</title>
   <link href="http://gusiev.com/2013/04/background-jobs-frameworkds-delayed-job-resque-slides"/>
   <updated>2013-04-08T00:00:00+03:00</updated>
   <id>http://gusiev.com/2013/04/background-jobs-frameworkds-delayed-job-resque-slides</id>
   <content type="html">&lt;p&gt;Here are my slides from Ruby Meditation meet-up in Kiev last weekend.
Covers some internals and some features of DelayedJob and Resque.
&lt;a href=&quot;http://x.gusiev.com/background-jobs-frameworks&quot;&gt;http://x.gusiev.com/background-jobs-frameworks&lt;/a&gt;
&lt;!--more--&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Where to work as a rubyist in Ukraine</title>
   <link href="http://gusiev.com/2013/02/working-in-curebit-ycombinator-ukraine-job"/>
   <updated>2013-02-15T00:00:00+02:00</updated>
   <id>http://gusiev.com/2013/02/working-in-curebit-ycombinator-ukraine-job</id>
   <content type="html">&lt;p&gt;Last year I had a lot of changes in my career: I had to switch jobs twice while planning to do it only once. Most of the projects I was working on before was about building time wasting features to raise more money for more time wasting features. I wanted to find the right project for myself: A project that brings something good to people.  Ruby is extremely good for startups, but easy investments is a decease for most of them in Ukraine. In September I met Allan Grant, CEO of Curebit. Curebit has a great team of sales and marketing people but was lacking great engineers.
Allan was the first business guy in my life that was more focused on building product than raising money — build features endless cycle. So we started to work together.&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;&lt;img src=&quot;https://d2vzmlm7xciaqk.cloudfront.net/asset-vb3f26d3/images/curebit-logo-transparent.png&quot; style=&quot;margin: 5px auto; display: block;height: 50px&quot; /&gt;
Curebit is a San Francisco-based startup that recently opened an office in Kiev, and I’d like to invite you to interview with us.&lt;/p&gt;

&lt;p&gt;Allan is also a Rails developer (&lt;a href=&quot;http://github.com/allangrant&quot;&gt;http://github.com/allangrant&lt;/a&gt;) of Ukrainian origin.  Our company, Curebit, raised &lt;a href=&quot;http://www.crunchbase.com/company/curebit&quot;&gt;$1.2 million&lt;/a&gt; from great investors like YCombinator (the creators of Hacker News) and 500Startups.  The company is small (10 people) but profitable, and growing.  We are a product company working on interesting architectural challenges and we are making the best product in our market (referral systems for e-commerce companies). We are now moving fast from being a good product to being a good platform. This fact give us really advanced challenges to fight with.&lt;/p&gt;

&lt;p&gt;The team in Ukraine is only 6 people right now, but everyone is AMAZING and we want to get the best developers on-board.  I am leading the engineering team, and Sergii Iurevych (OOCSS expert) leads design &amp;amp; front-end development. We pay everyone generously a San Francisco level salary because we know that our country has engineers that are worth it.&lt;/p&gt;

&lt;p&gt;Most importantly (this is hard to explain in blog post), we have unique values that make Curebit a great place to work.  We prefer to cut features/move deadlines instead of giving up code quality/polish.  We don’t keep track of vacation days, because everyone we work with loves their work.  We’re building a company to create value in the world, not just for profit.  We have a culture, and we don’t write it down or talk about — we just live it.&lt;/p&gt;

&lt;p&gt;We would love to meet you, even if you are not looking for a job right now. Please shoot us email to &lt;a href=&quot;mailto:bogdan@curebit.com&quot;&gt;bogdan@curebit.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want to see our product, check out the site at &lt;a href=&quot;http://curebit.com&quot;&gt;http://curebit.com&lt;/a&gt; (feel free to create fake account), or play with this demo account: &lt;a href=&quot;http://curebit.com/demo&quot;&gt;http://curebit.com/demo&lt;/a&gt; (it’s an old demo that doesn’t show much).&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Rails Contribution slides from ruby meetup in Kiev</title>
   <link href="http://gusiev.com/2012/12/rails-contribution-slides-from-ruby-meetup"/>
   <updated>2012-12-11T00:00:00+02:00</updated>
   <id>http://gusiev.com/2012/12/rails-contribution-slides-from-ruby-meetup</id>
   <content type="html">&lt;p&gt;Here are slides I’ve shown on ruby meetup in Kiev.
It’s all about my expirience with &lt;a href=&quot;http://gusiev.com/slides/rails_contribution/static/#1&quot;&gt;power hacking rails internals&lt;/a&gt;.
Give some inside on how ActiveSupport Callbacks and Rails Router are working.&lt;/p&gt;

&lt;!--more--&gt;

</content>
 </entry>
 
 <entry>
   <title>Smart Javascript Initialization Strategy</title>
   <link href="http://gusiev.com/2012/10/crazy-javascript-initialization-strategy"/>
   <updated>2012-10-08T00:00:00+03:00</updated>
   <id>http://gusiev.com/2012/10/crazy-javascript-initialization-strategy</id>
   <content type="html">&lt;p&gt;The JavaScript initialization process is a pain in the ass since dynamic HTML and AJAX became popular. We need to take care all the time about what we need to initialize after every change in DOM. 
Thanks to &lt;code&gt;jQuery#live&lt;/code&gt; and &lt;code&gt;jQuery#on&lt;/code&gt;: We have a not-so-right but still very effective solution. It allows us to fix the problem with initialization of typical events.
But initialization is not always about binding DOM events. In more advanced cases, it could be wrapping widget class over an element or a custom jQuery method call like a &lt;code&gt;date-picker&lt;/code&gt;.&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;Here is an example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;// This line will be required not only on `$(docuemnt).ready` &lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;// but every time an update in DOM occurs that adds some new date input on a page.&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;.date-picker&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;datePicker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Or advanced example:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ImageGallery&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(){&lt;/span&gt;
  &lt;span class=&quot;cm&quot;&gt;/* Class with 100+ lines of code here */&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt; 
&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;.image-gallery&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ImageGallery&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;In the example above, we see two sections: definition and initialization.
This fact makes code hard to follow as moving from class to DOM element in HTML template requires at least two transitions in your editor.&lt;/p&gt;

&lt;p&gt;And when a large page update comes after an AJAX query, this problem is getting even worse. All initializers should called again:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;/profile/edit&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;response&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;nx&quot;&gt;showSomeProfileEditingPopup&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;.profile-birth-date&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;datePicker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;ImageGallery&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;.profile-image-gallery&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// More initialization here&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;tired-of-handling-things-like-this&quot;&gt;Tired of handling things like this?&lt;/h3&gt;

&lt;h2 id=&quot;change-the-way-of-thinking-about-initialization&quot;&gt;Change the way of thinking about initialization&lt;/h2&gt;

&lt;p&gt;Initialization code is a bridge between actual JS class and DOM element.
In 99% of cases, it has one-to-one or one-to-many relation.
Then why do we always need that intermediate initialization code?&lt;/p&gt;

&lt;p&gt;The DOM element could have the class itself:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;profile-image-gallery&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;data-class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;ImageGallery&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now your way from DOM element to JS class requires only one transition.
Moreover, all DOM elements could be initialized with one call. It needs to run over DOM and find all elements with &lt;code&gt;data-class&lt;/code&gt; attributes 
that are not initialized and wrap them with a specified class:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Initializer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;perform&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&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;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;[data-class]:not(.initialized)&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;each&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;element&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;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;addClass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;initialized&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
      &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;klass&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;window&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)];&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;klass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;element&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;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This code bit will probably require some additional functionality to support options, but the general idea is strong enough to handle all possible complexity in the future.
Now, whenever DOM changes, everything we need to do is call:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;perform&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;In order to attach third-party libraries to this initialization process, we need to create a simple wrapping class around them:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;DatePicker&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;element&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;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;element&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;datePicker&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now we are able to convert inputs to date pickers with a unified initialization process:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;text&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;profile[birth_date]&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;data-class&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;DatePicker&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;JavaScript coding is annoying for most people who came from other languages because it keeps things fragmented that should be bound together.
In order to avoid this problem, we need to invent conventions to follow, so that every person in a team understands that.
But the cost of convention described here is not that much and makes code more clean and easier to follow.&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Strict validation is a main stream</title>
   <link href="http://gusiev.com/2012/09/rails-strict-validation-popularity"/>
   <updated>2012-09-17T00:00:00+03:00</updated>
   <id>http://gusiev.com/2012/09/rails-strict-validation-popularity</id>
   <content type="html">&lt;p&gt;It’s nice to see that &lt;a href=&quot;/2012/01/rails-32-features-changelog/#strict_validation_concept&quot;&gt;strict validation feature&lt;/a&gt; that I’ve &lt;a href=&quot;https://github.com/rails/rails/commit/8620bf90c5e486e1ec44b9aabb63f8c848668ed2&quot;&gt;done almost a year ago&lt;/a&gt; is &lt;a href=&quot;http://robots.thoughtbot.com/post/31728620503/refactoring-replace-conditional-with-polymorphism&quot;&gt;finally getting popular&lt;/a&gt;. Spend a moment and understand what it does by &lt;a href=&quot;http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates-21&quot;&gt;Rails documentation&lt;/a&gt;.&lt;/p&gt;

&lt;!--more--&gt;

</content>
 </entry>
 
 <entry>
   <title>Full text search - Solr(Sunspot) vs PostgreSQL</title>
   <link href="http://gusiev.com/2012/08/postgresql-full-text-search-solr"/>
   <updated>2012-08-16T00:00:00+03:00</updated>
   <id>http://gusiev.com/2012/08/postgresql-full-text-search-solr</id>
   <content type="html">&lt;p&gt;This is short notes about my experience with full text search based on PostgreSQL and Solr.
Solr was primary used with a help of &lt;a href=&quot;https://github.com/sunspot/sunspot&quot;&gt;Sunspot gem&lt;/a&gt; that was definitely a good idea, 
but unfortunately caused some drawbacks. PostgreSQL provides full text search out of the box and to my opinion doesn’t need any 
additional ruby specific tools other than ORM.
&lt;!--more--&gt;&lt;/p&gt;

&lt;h2 id=&quot;tldr&quot;&gt;TL;DR&lt;/h2&gt;

&lt;p&gt;PostgreSQL approach is simple and fit well to agile project.
Solr is pretty powerful, but you need to pay for that power. And this is significant cost.
Think twice if you really need this power from the first day of full text search in your project.&lt;/p&gt;

&lt;h2 id=&quot;search-index&quot;&gt;Search index&lt;/h2&gt;

&lt;p&gt;Solr search index is something you create almost manually by defining a new data schema.
Sunspot sets &lt;code&gt;after_save&lt;/code&gt; hooks to your model to update it’s solr index.&lt;/p&gt;

&lt;p&gt;Here is an example of schema definition based on Sunspot gem:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Product&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Base&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;searchable&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:title&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:material&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:category&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;category&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;text&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:brand&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;brand&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# do&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;PostgreSQL search index might be yet another column in the database, set by pre save hook:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Product&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Base&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;before_validation&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:set_searchable_content&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;protected&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;set_searchable_content&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;searchable_content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;material&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;category&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;brand&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot; &amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And don’t forget about database index:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;CREATE&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;INDEX&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;source_idx&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;ON&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;products&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;USING&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;gin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_tsvector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;english&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;searchable_content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt; &lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;While this is very basic use case, it shows up the direction where your code will go in each variant.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Advanced high level DSL with a lot of “unknown unknowns” in Solr case&lt;/li&gt;
  &lt;li&gt;Just ruby code that everyone would understand from the first look in case of Postgres&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As an example of how you would solve more advanced problem, I want to show facet search implementation:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;c1&quot;&gt;# Suppose to receive a list of ids to count between different categories&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;search_facets&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ids&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blank?&lt;/span&gt;
  &lt;span class=&quot;no&quot;&gt;ProductCategory&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;product_categories.id, count(product_categories.id) as product_count&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;
     &lt;span class=&quot;n&quot;&gt;joins&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:products&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;
     &lt;span class=&quot;n&quot;&gt;where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;products.id&amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ids&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;
     &lt;span class=&quot;n&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;product_categories.id&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;While Solr based facet would be easier:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Product&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;search&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;keywords&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:query&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;facet&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:category_id&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paginate&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:page&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:page&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:per_page&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;At first look you might say that Solr way is more clear, but you still don’t know what it does in deep details.
Sunspot gives DSL is yet another language to query data. It includes all types of operations (e.g. &lt;a href=&quot;https://github.com/sunspot/sunspot/wiki/Scoping-by-attribute-fields&quot;&gt;comparison operators&lt;/a&gt;, while postgres lets use SQL - language you should know already.&lt;/p&gt;

&lt;p&gt;There is also easy to spot that Solr and Sunspot tool chain brings more features to you out of the box.
But this is where Solr advantages ends.
Note stat Solr config file is not as readable as Sunspot DSL. 
Here is a short example that tells Solr to do some string transformation before create a search index:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;fieldType&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;text&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;solr.TextField&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;omitNorms=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;false&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;analyzer&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;tokenizer&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;solr.StandardTokenizerFactory&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- remove punctualtion --&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;filter&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;solr.StandardFilterFactory&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- lower case letters --&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;filter&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;solr.LowerCaseFilterFactory&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;&amp;lt;!-- language semantic support --&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;filter&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;solr.SnowballPorterFilterFactory&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;language=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;English&amp;quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;/analyzer&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/fieldType&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;When you go deeper into solr you might need to deal with configs like example above.
While in case of PostgreSQL you can do any string transformation using ruby.&lt;/p&gt;

&lt;h2 id=&quot;deployment-and-testing&quot;&gt;Deployment and Testing&lt;/h2&gt;

&lt;p&gt;Solr is a standalone application and you need to take care about Solr configuration. It is a part of a code since some search business logic depends on it. This might require:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;different solr config files across different branches&lt;/li&gt;
  &lt;li&gt;test suite that covers some config options&lt;/li&gt;
  &lt;li&gt;different solr instances launched for development and test environments&lt;/li&gt;
  &lt;li&gt;a need to rebuild search index when you switch branches&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Postgres built in search doesn’t have these problems at all. This will give a significant performance boost in agile process.  Deployment, testing and switching branches doesn’t require any full text search specific action.&lt;/p&gt;

&lt;h2 id=&quot;reindex-process&quot;&gt;Reindex process&lt;/h2&gt;

&lt;p&gt;Reindex complexity comes from several factors.&lt;/p&gt;

&lt;p&gt;First of them is complicated search index that cover more than one model (Ex: &lt;code&gt;Product.belongs_to :brand&lt;/code&gt; and &lt;code&gt;Brand#title&lt;/code&gt; should be a part of &lt;code&gt;Product&lt;/code&gt; index).
It makes both approaches more complex as business logic becomes more complex.&lt;/p&gt;

&lt;p&gt;But in case of Solr this is not the only one thing to care.
With database size growth you gain more problems. Solr index do not have that nice migration mechanism and dump transfer tools as Relational databases.
No matter which gem you will use to connect Solr and Relational database: you need to take care when search index schema changes.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Conceptually I would like my database to respond for every data search operation e.g. full text search.
This is where PostgreSQL is going. But this feature is not that strong now e.g. it still lacks spell checking and result highlight features.
Solr is old school tool having all full text search features you ever imagine, but it’s heavy configuration file and technology stack should be easier.&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>How many clicks do you need to debug ajax request?</title>
   <link href="http://gusiev.com/2012/06/javascript-ajax-debug-popup-response"/>
   <updated>2012-06-16T00:00:00+03:00</updated>
   <id>http://gusiev.com/2012/06/javascript-ajax-debug-popup-response</id>
   <content type="html">&lt;p&gt;Here I am gonna use Chrome browser as an example:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Open context menu&lt;/li&gt;
  &lt;li&gt;Click inspect element.&lt;/li&gt;
  &lt;li&gt;Click “Network” tab&lt;/li&gt;
  &lt;li&gt;Spawn again an ajax request (at least one click. Sometimes more)&lt;/li&gt;
  &lt;li&gt;Click on failed request at Network tab&lt;/li&gt;
  &lt;li&gt;Click “Preview” or “Response” subtab&lt;/li&gt;
&lt;/ol&gt;

&lt;!--more--&gt;

&lt;p&gt;&lt;a href=&quot;http://i.imm.io/skBN.png&quot;&gt;
  &lt;img width=&quot;400px&quot; src=&quot;http://i.imm.io/skBN.png&quot; /&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;this-should-change&quot;&gt;THIS SHOULD CHANGE&lt;/h3&gt;

&lt;p&gt;Why we can not automate this daily routine work?&lt;/p&gt;

&lt;p&gt;With &lt;a href=&quot;https://github.com/bogdan/jquery-ajax-debug&quot;&gt;this tiny script&lt;/a&gt; you don’t need any:
Error response will be displayed in Popup at once after it occur.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://i.imm.io/skDh.png&quot;&gt;
  &lt;img width=&quot;400px&quot; src=&quot;http://i.imm.io/skDh.png&quot; /&gt;
&lt;/a&gt;&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Assumption Driven Approach</title>
   <link href="http://gusiev.com/2012/06/assumption-driven-development"/>
   <updated>2012-06-06T00:00:00+03:00</updated>
   <id>http://gusiev.com/2012/06/assumption-driven-development</id>
   <content type="html">&lt;p&gt;This post shows some examples of how things can go wrong way because of people actions based on assumptions but not on facts.
It is more conceptual than practical, so be patient and don’t blame my grammar mistakes too much.
&lt;!--more--&gt;&lt;/p&gt;

&lt;h3 id=&quot;evil-of-assumptions&quot;&gt;Evil of assumptions&lt;/h3&gt;

&lt;p&gt;This is my favorite example of how wrong assumption can be:&lt;/p&gt;

&lt;p&gt;Very popular ruby questions for newbies:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;What is the difference between `private` and `protected` methods?
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Question seems good and reasonable. But most people don’t know the right answer even after years with ruby.
Most people assume that &lt;code&gt;private&lt;/code&gt; methods can not be called from inherited classes, but &lt;code&gt;protected&lt;/code&gt; can be.&lt;/p&gt;

&lt;p&gt;But that’s only an assumption. Have you ever tried to ensure that it’s right?
The answer is: &lt;strong&gt;This is not true&lt;/strong&gt;. Private method can be called from inherited class in the same way as protected.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class A
  private
  def a
    puts 'hello'
  end
end

class B &amp;lt; A
  def b
    a
  end
end

B.new.b # No Exception here
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href=&quot;http://gusiev.com/2010/04/ruby18-private-protected-incapsulatio/&quot;&gt;More about private and public methods here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This is good example on how myths can spread the world:
Almost every person is surprised by the output of program above.&lt;/p&gt;

&lt;p&gt;This makes me feel that many interviewers ask questions while don’t know what they are asking about.
From this point I can state that a lot of ruby interviews goes in a wrong direction by providing an assumption as a fact.&lt;/p&gt;

&lt;h3 id=&quot;class-variables-assumption&quot;&gt;Class variables assumption&lt;/h3&gt;

&lt;p&gt;Yet another assumption that can be made based on ruby documentation is connected with ruby class variables:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;class A
  @@x = 2
  def self.pr
    @@x
  end
end

class B &amp;lt; A
  @@x = 5
end

puts B.pr
puts A.pr
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can assume that last two lines produce 5 and 2, but they both produce 5.
Because class variables are shared across all classes.&lt;/p&gt;

&lt;h3 id=&quot;assumption-example-in-the-performance-field&quot;&gt;Assumption example in the performance field&lt;/h3&gt;

&lt;p&gt;Most popular assumptions field is performance. And here we come up with the most recent bad assumption example:&lt;/p&gt;

&lt;p&gt;Ruby 2.0 offers lazy enumeration feature.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(1...100000).to_a.lazy.select{|a| a.odd? }.map{|a| a**2}.to_a
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In short: there will be only one iteration over an array with no intermediate array between &lt;code&gt;select&lt;/code&gt; and &lt;code&gt;map&lt;/code&gt;.
If you are an assumption-driven person, you already assumed that &lt;code&gt;#lazy&lt;/code&gt; not only saves your memory, but also improve performance.&lt;/p&gt;

&lt;p&gt;And problem is that it is not true. &lt;a href=&quot;https://bugs.ruby-lang.org/issues/6183&quot;&gt;&lt;code&gt;#lazy&lt;/code&gt; is very slow&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;how-to-use-assumption-in-a-right-way&quot;&gt;How to use assumption in a right way?&lt;/h3&gt;

&lt;p&gt;Why we are still using assumptions a lot? Because they are cheap: they allow us to move faster without proofing that we are moving the right direction.&lt;/p&gt;

&lt;p&gt;Here are some MUSTs that we need to follow:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Always make sure that you are 100% right when you are interviewing other person.&lt;/li&gt;
  &lt;li&gt;Never make assumptions in performance field.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And here are some more conceptual advices:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Make assumptions depend on facts, but not other assumptions.&lt;/li&gt;
  &lt;li&gt;Prove any assumption with fact before going with another assumption&lt;/li&gt;
  &lt;li&gt;Do not tell people about assumptions as if they are facts.&lt;/li&gt;
  &lt;li&gt;When we can’t make a good assumption from the beginning, maybe it’s time to gather more facts&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>Benchmark your performance patches</title>
   <link href="http://gusiev.com/2012/05/benchmark-performance-patch-ruby-diff"/>
   <updated>2012-05-01T00:00:00+03:00</updated>
   <id>http://gusiev.com/2012/05/benchmark-performance-patch-ruby-diff</id>
   <content type="html">&lt;p&gt;After a dozen performance patches to many gems want to share some practical experience I gain. 
Tools I’ve picked up was &lt;strong&gt;perftools.rb&lt;/strong&gt; and ruby &lt;strong&gt;built-in benchmark&lt;/strong&gt; library.
They are fit well for cases when optimization stays at Ruby level and doesn’t require to fix something in native extensions or dig into IO operations.&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;General flow on how to tackle performance is clear:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/tmm1/perftools.rb&quot;&gt;perftools.rb&lt;/a&gt; shows slow method calls in a nice call graph format.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://ruby-doc.org/stdlib-1.9.2/libdoc/benchmark/rdoc/Benchmark.html&quot;&gt;Benchmark&lt;/a&gt; proves that we made an improvement.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Perftools does all the hard work for you.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;require 'perftools'
PerfTools::CpuProfiler.start(&quot;/tmp/profile_result&quot;) do
  # code to profile
end
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Run test and open result:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;gem install perftools.rb
ruby test.rb
pprof.rb --gif /tmp/profile_result &amp;gt; /tmp/profile_result.gif
$IMAGE_VIEWER /tmp/profile_result.gif 
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Example output: &lt;strong&gt;Rails route generator call graph&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://postimage.org/image/fy4fxhzvr/full/&quot;&gt;
  &lt;img src=&quot;/images/perf_sample.gif&quot; width=&quot;600px&quot; /&gt;
&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is short instruction how to read call graph:&lt;/p&gt;

&lt;p&gt;Each block represents a method that was called. Percentage in a block shows how much time was spend in current method with and without all it’s nested calls comparing to overall time. Arrow with number shows how many times parent method called it’s children method.&lt;/p&gt;

&lt;p&gt;That’s it. All you need to do now is find blocks with most percentage and try reduce them.&lt;/p&gt;

&lt;p&gt;This flow works fine till the moment when benchmark results before and after patch should be compared. 
Ruby Benchmark can not merge them together and represent in a human readable form like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;Running benchmark with current working tree
Checkout HEAD^
Running benchmark with HEAD^
Checkout to previous HEAD again

                    user     system      total        real
----------------------------------headers parsing when long
After patch:    0.100000   0.000000   0.100000 (  0.089926)
Before patch:   0.700000   0.000000   0.700000 (  0.697444)

----------------------------------headers parsing when tiny
After patch:    0.000000   0.000000   0.000000 (  0.009930)
Before patch:   0.020000   0.000000   0.020000 (  0.024283)

---------------------------------headers parsing when empty
After patch:    0.010000   0.000000   0.010000 (  0.002160)
Before patch:   0.000000   0.000000   0.000000 (  0.002354)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So, decided to create a library that fixes this problem.
Try out &lt;strong&gt;&lt;a href=&quot;https://github.com/bogdan/diffbench&quot;&gt;DiffBench&lt;/a&gt;&lt;/strong&gt; if you ever come up with performance patch.&lt;/p&gt;

&lt;p&gt;DiffBench in action examples:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Rails
    &lt;ul&gt;
      &lt;li&gt;ActiveRecord - &lt;a href=&quot;https://github.com/rails/rails/pull/5467&quot;&gt;#5467&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;ActiveModel - &lt;a href=&quot;https://github.com/rails/rails/pull/5431&quot;&gt;#5431&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;ActiveSupport - &lt;a href=&quot;https://github.com/rails/rails/pull/4493&quot;&gt;#4493&lt;/a&gt;&lt;/li&gt;
      &lt;li&gt;ActionPack - &lt;a href=&quot;https://github.com/rails/rails/pull/5957&quot;&gt;#5957&lt;/a&gt;&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Mail - &lt;a href=&quot;https://github.com/mikel/mail/pull/369&quot;&gt;#396&lt;/a&gt;, &lt;a href=&quot;https://github.com/mikel/mail/pull/366&quot;&gt;#366&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</content>
 </entry>
 
 <entry>
   <title>Web Forms 2.0: Submit with Ajax and Client Side Validation</title>
   <link href="http://gusiev.com/2012/03/javascript-ajax-submit-form-validation"/>
   <updated>2012-03-01T00:00:00+02:00</updated>
   <id>http://gusiev.com/2012/03/javascript-ajax-submit-form-validation</id>
   <content type="html">&lt;p&gt;Web 2.0 world force us to build Forms in a new level of quality. AJAX and dynamic HTML changes a way how forms should work internally. Let’s call it Forms 2.0. And of course this facts bring a lot of complexity every time we deal with forms. Sometimes such form makes a real problem comparing to regular Web 1.0 form. Here will be a talk how to make Forms 2.0 as simple as Forms 1.0.&lt;/p&gt;

&lt;!--more--&gt;

&lt;h2 id=&quot;sumbit-with-ajax&quot;&gt;Sumbit with Ajax&lt;/h2&gt;

&lt;p&gt;Submit a form with AJAX have never been a problem:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ajax&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;url&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;action&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;serialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;form&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;method&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
  &lt;span class=&quot;nx&quot;&gt;success&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&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;c1&quot;&gt;//do&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;More over submit with AJAX doesn’t require to rerender a page if it was not successfull: less server load and no need to support ‘new’ and ‘edit’ mode in HTML template. 
Problem that comes up with ajax submit is  Validation.
If you ever find yourself stuck with syncing client and server side validation or bug that form doesn’t have some data that was there before submit then you know what I am talking about too.&lt;/p&gt;

&lt;h2 id=&quot;solving-validation-problem&quot;&gt;Solving validation problem&lt;/h2&gt;

&lt;p&gt;There are number of visions in the Ruby community on how to solve the problem: some people prefer stay with Forms 1.0. Some people generate JavaScript validators based on ActiveModel validators. Other guys write their own validation in JavaScript and perform it on both client and server side.
The complexity of all these solution goes beyond believe. And here I am going to describe much simpler way of doing it.&lt;/p&gt;

&lt;p&gt;What if we pass the validation result as JSON from backend in a simple key:value format like:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;is not valid&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;password&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;is too short&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;It’s fast as wind and easy to implement:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;No SQL queries during validation request
    &lt;ul&gt;
      &lt;li&gt;Except uniqueness, that is more likely a feature than a problem&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;No Rendering just small JSON object as response&lt;/li&gt;
  &lt;li&gt;No additional controller action - just modify existing one&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;from-assumptions-to-coding&quot;&gt;From Assumptions to Coding&lt;/h2&gt;

&lt;p&gt;As we agreed - controller should return JSON instead of HTML:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-diff&quot; data-lang=&quot;diff&quot;&gt;&lt;span&gt;&lt;/span&gt; class UsersController &amp;lt; ApplicationController
   def create
     @user = User.new(params[:user])
     if @user.save
&lt;span class=&quot;gd&quot;&gt;-      redirect_to root_path&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+      render :json =&amp;gt; {:redirect =&amp;gt; root_path}&lt;/span&gt;
     else
&lt;span class=&quot;gd&quot;&gt;-      render :action =&amp;gt; :new&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+      render :json =&amp;gt; {:errors =&amp;gt; user.errors}&lt;/span&gt;
     end
   end
 end&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;All we need to do after that is just assign errors to the form to the prepared spots identified with HTML5 custom attribute:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-diff&quot; data-lang=&quot;diff&quot;&gt;&lt;span&gt;&lt;/span&gt; &amp;lt;form id=&amp;quot;new_user&amp;quot; action=&amp;quot;/users&amp;quot; method=&amp;quot;post&amp;quot;&amp;gt;
&lt;span class=&quot;gd&quot;&gt;-  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+  &amp;lt;div class=&amp;quot;field&amp;quot; validate=&amp;quot;email&amp;quot;&amp;gt;&lt;/span&gt;
     &amp;lt;label for=&amp;quot;user_email&amp;quot;&amp;gt;Email&amp;lt;/label&amp;gt;&amp;lt;br /&amp;gt;
     &amp;lt;input id=&amp;quot;user_email&amp;quot; name=&amp;quot;user[email]&amp;quot; type=&amp;quot;text&amp;quot; /&amp;gt;
   &amp;lt;/div&amp;gt;
&lt;span class=&quot;gd&quot;&gt;-  &amp;lt;div class=&amp;quot;field&amp;quot;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;gi&quot;&gt;+  &amp;lt;div class=&amp;quot;field&amp;quot; validate=&amp;quot;password&amp;quot;&amp;gt;&lt;/span&gt;
     &amp;lt;label for=&amp;quot;user_password&amp;quot;&amp;gt;Password&amp;lt;/label&amp;gt;&amp;lt;br /&amp;gt;
     &amp;lt;input id=&amp;quot;user_password&amp;quot; name=&amp;quot;user[password]&amp;quot; type=&amp;quot;password&amp;quot; /&amp;gt;
   &amp;lt;/div&amp;gt;
   &amp;lt;div class=&amp;quot;actions&amp;quot;&amp;gt;
     &amp;lt;input name=&amp;quot;commit&amp;quot; type=&amp;quot;submit&amp;quot; value=&amp;quot;Create User&amp;quot; /&amp;gt;
   &amp;lt;/div&amp;gt;
 &amp;lt;/form&amp;gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;All the dirty work can be done by &lt;a href=&quot;https://github.com/bogdan/ajaxsubmit&quot;&gt;AjaxSubmit&lt;/a&gt; library 
that implements all the ideas described above:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-js&quot; data-lang=&quot;js&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;&amp;#39;#new_user&amp;#39;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;ajaxForm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The main goal reached: Make super modern AJAX form is as easy as regular form.
If you still can’t believe that it is so easy - check out &lt;a href=&quot;http://ajaxsubmit.herokuapp.com&quot;&gt;LIVE demo&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;ajaxsubmit-is-it-flexible&quot;&gt;AjaxSubmit: Is it flexible?&lt;/h2&gt;

&lt;p&gt;AjaxSubmit is a library that has 2 years of experience behind. About 7 different engineers made their contribution during this time.
Take a look at the &lt;a href=&quot;https://github.com/bogdan/ajaxsubmit#readme&quot;&gt;Read Me&lt;/a&gt; for more information about API, callbacks and configuration options.&lt;/p&gt;

&lt;p&gt;You can also checkout &lt;a href=&quot;http://mailtrap.io&quot;&gt;Mailtrap&lt;/a&gt; that is a perfect example of how your site could look like with this library 
in every web form.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>New Rails Release with a few features from me</title>
   <link href="http://gusiev.com/2012/01/rails-32-features-changelog"/>
   <updated>2012-01-23T00:00:00+02:00</updated>
   <id>http://gusiev.com/2012/01/rails-32-features-changelog</id>
   <content type="html">&lt;p&gt;Upcoming Rails 3.2 release has many useful features.&lt;/p&gt;

&lt;p&gt;Some of them are coming from me:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;ActiveRecord #pluck method&lt;/li&gt;
  &lt;li&gt;Strict validation concept&lt;/li&gt;
  &lt;li&gt;Customizable mass assignment sanitizer behavior&lt;/li&gt;
  &lt;li&gt;Gotcha for &lt;code&gt;select[multiple]&lt;/code&gt; tag&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is a more detailed description than in the &lt;a href=&quot;https://gist.github.com/1472145&quot;&gt;Changelog&lt;/a&gt;.&lt;/p&gt;

&lt;!--more--&gt;

&lt;h2 id=&quot;pluck-method&quot;&gt;Pluck method&lt;/h2&gt;

&lt;p&gt;People around made a &lt;a href=&quot;https://github.com/rails/rails/commit/a382d60f6abc94b6a965525872f858e48abc00de&quot;&gt;lot of PR on it&lt;/a&gt;. So, You probably already heard about it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pluck&lt;/code&gt; can be used to query a single column from the underlying table of a model. It accepts a column name as argument and returns an array of values of the specified column with the corresponding data type.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:active&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pluck&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# SELECT id FROM clients WHERE active = 1&lt;/span&gt;

&lt;span class=&quot;no&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;uniq&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pluck&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:role&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c1&quot;&gt;# SELECT DISTINCT role FROM clients&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;a href=&quot;http://edgeguides.rubyonrails.org/active_record_querying.html#pluck&quot;&gt;Pluck in Rails guides&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;strict-validation-concept&quot;&gt;Strict validation concept&lt;/h2&gt;

&lt;p&gt;Some validation are not connected with end user and breaking it is more likely a bug than problem with user input. A few constraints can be embed to DB(like foreign keys), but most of them can not. And it’s a good idea to use AM validators to do such checks as well.&lt;/p&gt;

&lt;p&gt;Use case:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Article&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;AR&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Base&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;validates&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:author&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:presence&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Article&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# =&amp;gt; false&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;code&gt;#author&lt;/code&gt; is usually set in controller from &lt;code&gt;current_user&lt;/code&gt;. If somebody forgets to do it - user see error: 
&lt;code&gt;Author cann't be blank&lt;/code&gt;. 
And it is not right to show error message to user that he is not able to fix.
More over DEV team is not notified about the problem.&lt;/p&gt;

&lt;p&gt;In order to fix that You can use &lt;a href=&quot;https://github.com/rails/rails/commit/8620bf90c5e486e1ec44b9aabb63f8c848668ed2&quot;&gt;validate! method&lt;/a&gt; that generates validator that always raises exception when fails:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Article&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;AR&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Base&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;validates!&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:author&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:presence&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;save&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# raises ActiveModel::StrictValidationFailed&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;customizing-mass-assignment-sanitizer&quot;&gt;Customizing Mass Assignment Sanitizer&lt;/h2&gt;

&lt;p&gt;Added an ability to specify &lt;a href=&quot;https://github.com/rails/rails/commit/aa2639e746d8af5d7673bbbbbccbe868edeb0161&quot;&gt;your own behavior on mass assignment protection&lt;/a&gt;, controlled by option:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;ActiveModel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;MassAssignmentSecurity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mass_assignment_sanitizer&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The main idea behind this change is to make Mass Assignment Sanitizer to raise exceptions in development and test environments while leaving logging behavior in production. This will be the &lt;a href=&quot;https://github.com/rails/rails/commit/0fab8c388ea9cfcace0907102697c78a68762be3&quot;&gt;default Rails application template&lt;/a&gt; in version 3.2. It will allow to detect mass assignment protection issues caused by bug in code more effectively before they will be deployed to production.&lt;/p&gt;

&lt;h2 id=&quot;power-hacking-selectmultiple&quot;&gt;Power hacking select[multiple]&lt;/h2&gt;

&lt;p&gt;Quote from Rails documentation that comes along with &lt;a href=&quot;https://github.com/rails/rails/commit/faba406fa15251cdc9588364d23c687a14ed6885&quot;&gt;the patch&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;The HTML specification says:&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;multiple&lt;/code&gt; parameter passed to select and all options got deselected 
web browsers do not send any value to server. Unfortunately this introduces a gotcha:
if an &lt;code&gt;User&lt;/code&gt; model has many &lt;code&gt;roles&lt;/code&gt; and have &lt;code&gt;role_ids&lt;/code&gt; accessor, and in the form that edits roles of the user
the user deselects all roles from &lt;code&gt;role_ids&lt;/code&gt; multiple select box, no &lt;code&gt;role_ids&lt;/code&gt; parameter is sent. So,
any mass-assignment idiom like&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;&lt;/span&gt;  &lt;span class=&quot;vi&quot;&gt;@user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;update_attributes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;wouldn’t update roles.&lt;/p&gt;

&lt;p&gt;To prevent this the helper generates an auxiliary hidden field before
every multiple select. The hidden field has the same name as multiple select and blank value.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-html&quot; data-lang=&quot;html&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;hidden&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;user[role_ids]&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;select&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;user_role_ids&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;user[role_ids]&amp;quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;multiple&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&amp;quot;multiple&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;
  ....
&lt;span class=&quot;p&quot;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;select&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This way, the client either sends only the hidden field (representing
the deselected multiple select box), or both fields. Since the HTML specification
says key/value pairs have to be sent in the same order they appear in the
form, and parameters extraction gets the last occurrence of any repeated
key in the query string, that works for ordinary forms.&lt;/p&gt;

&lt;p&gt;Unfortunately this trick can introduce a side effects described well in this &lt;a href=&quot;http://stackoverflow.com/questions/8929230/why-is-the-first-element-always-blank-in-my-rails-multi-select-using-an-embedde&quot;&gt;stackoverflow question&lt;/a&gt;.
If you know an effective way to fix that - let me know.&lt;/p&gt;

&lt;h4 id=&quot;have-fun-with-upgrading&quot;&gt;Have fun with upgrading&lt;/h4&gt;
</content>
 </entry>
 
 <entry>
   <title>New release of Datagrid: Mongoid, Rails Engines and HTML columns support</title>
   <link href="http://gusiev.com/2012/01/datagrid-reports-release-gem"/>
   <updated>2012-01-10T00:00:00+02:00</updated>
   <id>http://gusiev.com/2012/01/datagrid-reports-release-gem</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;https://github.com/bogdan/datagrid&quot;&gt;Datagrid&lt;/a&gt; version 0.5.0 has been released.&lt;/p&gt;

&lt;p&gt;Datagrid is a Ruby on Rails plugin that helps you to build and &lt;a href=&quot;http://datagrid.herokuapp.com&quot;&gt;represent table-like data&lt;/a&gt; with:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Customizable filtering&lt;/li&gt;
  &lt;li&gt;Columns&lt;/li&gt;
  &lt;li&gt;Sort order&lt;/li&gt;
  &lt;li&gt;Localization&lt;/li&gt;
  &lt;li&gt;Export to CSV&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here are some major features from new release.
&lt;!--more--&gt;&lt;/p&gt;

&lt;h3 id=&quot;mongoid-support&quot;&gt;Mongoid support&lt;/h3&gt;

&lt;p&gt;Previously Datagrid supported only ActiveRecord ORM. Now Mongoid is also available: &lt;a href=&quot;http://datagrid.herokuapp.com/document_reports&quot;&gt;see the demo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;New datagrid driver architecture allows to add yet another ORM in less then &lt;a href=&quot;https://github.com/bogdan/datagrid/blob/master/lib/datagrid/drivers/mongoid.rb&quot;&gt;40 lines of code&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;html-columns&quot;&gt;HTML columns&lt;/h3&gt;

&lt;p&gt;In order to use view helpers to render columns You can use &lt;code&gt;:html&lt;/code&gt; option.
This will call column block in the view context:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:completed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:html&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&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;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;completed?&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;image_tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;green.gif&amp;quot;&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;image_tag&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;red.gif&amp;quot;&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;c1&quot;&gt;# or do it in partial&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:actions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:html&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&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;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;asset&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;render&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:partial&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;admin/assets/actions&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:object&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asset&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;table-customization-with-rails-engines&quot;&gt;Table customization with Rails Engines&lt;/h3&gt;

&lt;p&gt;Rails Engines allows you to embed views into Rails plugins and make them customizable.
If You need serious customization of datagrid &lt;code&gt;&amp;lt;table&amp;gt;&lt;/code&gt; and HTML Columns doesn’t help, you can customize datagrid internal views by running:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span&gt;&lt;/span&gt;rake datagrid:copy_partials&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This will create the following files in your Rails root directory:&lt;/p&gt;

&lt;pre&gt;
&lt;code&gt;
|~app/
| ~views/
|   ~datagrid/
|     |-_head.html.erb
|     |-_row.html.erb
|     |_table.html.erb
&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;Now You are able to customize whatever You want.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Improve your Email delivery code with Mailtrap</title>
   <link href="http://gusiev.com/2011/11/development-smtp-server"/>
   <updated>2011-11-18T00:00:00+02:00</updated>
   <id>http://gusiev.com/2011/11/development-smtp-server</id>
   <content type="html">&lt;p&gt;Today, I want introduce the &lt;a href=&quot;http://mailtrap.io&quot;&gt;Mailtrap web service&lt;/a&gt; aimed to help developers build and improve their email delivery functionality.&lt;/p&gt;

&lt;p&gt;Usually people use same SMTP settings for all environments, but this actually creates many problems in Staging and Development environments. Mailtrap is a special SMTP service for these environments aimed to solve specific problems.
&lt;!--more--&gt;&lt;/p&gt;

&lt;h3 id=&quot;problems&quot;&gt;Problems&lt;/h3&gt;

&lt;h4 id=&quot;testing-user-registration-is-hell&quot;&gt;Testing user registration is hell&lt;/h4&gt;

&lt;p&gt;In order to register yet another user You need to have new unique email address that should be hosted somewhere.&lt;/p&gt;

&lt;h4 id=&quot;perform-mass-email-sending-is-a-problem-too&quot;&gt;Perform mass email sending is a problem too&lt;/h4&gt;

&lt;p&gt;Suppose you have a daily email to all customers and need check if your delivery logic works fine. In this case you need to know what emails were send, which customers received them. And again you need a batch of email addresses to test with.&lt;/p&gt;

&lt;h4 id=&quot;discuss-email-layout-and-functional-problems-with-product-team&quot;&gt;Discuss email layout and functional problems with product team&lt;/h4&gt;

&lt;p&gt;In order to share email You need to forward it to some inbox or attach to bug ticket as file. This is still not as easy as it could be.&lt;/p&gt;

&lt;h4 id=&quot;send-emails-to-real-customers-by-mistake&quot;&gt;Send emails to real customers by mistake&lt;/h4&gt;

&lt;p&gt;In some cases copy production database to staging is a good idea. But You have a risk that real emails appear on development servers. You need to take care about that.&lt;/p&gt;

&lt;p&gt;My personal score is about 2500 emails sent by mistake at a time.
Some people I know told me that their personal record is &lt;strong&gt;more than 2 millions emails sent by mistake&lt;/strong&gt;.
(Share your personal fuckup story in comments.)&lt;/p&gt;

&lt;h3 id=&quot;existing-solutions&quot;&gt;Existing solutions&lt;/h3&gt;

&lt;p&gt;You can go in many ways to fix the problem e.g.:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;setup the script on your smtp server that changes recipient email&lt;/li&gt;
  &lt;li&gt;use services like mailinator.com (only in some cases)&lt;/li&gt;
  &lt;li&gt;use custom Ruby specific gems like mail_view or mailcatcher.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;All of them solves only some of the problems described above.&lt;/p&gt;

&lt;h3 id=&quot;our-idea&quot;&gt;Our Idea&lt;/h3&gt;

&lt;p&gt;Development SMTP service should work in a different way then production one: It should not deliver emails but just gather them in one place and provide a shared access for team to debug it. Mailtrap is the first SMTP service built specifically for this purpose.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://mailtrap.io/assets/marketing/banner-cfb4ba4d680e3d51ed5d08641b8501b7.png&quot; style=&quot;width: 693px&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Unlike many alternative solutions, Mailtrap is platform independent, doesn’t require any daemon to run and is just an SMTP server from application stand point.  All you need to do is &lt;strong&gt;setup host, username and password&lt;/strong&gt;.&lt;/p&gt;

&lt;h3 id=&quot;features&quot;&gt;Features&lt;/h3&gt;

&lt;p&gt;This is not about high load, not about delivery statistics but about:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Ability to access ANY sent email without registration&lt;/li&gt;
  &lt;li&gt;Share Email is as easy as copypasting the URL&lt;/li&gt;
  &lt;li&gt;Developer tools for debugging &amp;amp; improving email template:
    &lt;ul&gt;
      &lt;li&gt;Syntax highlight&lt;/li&gt;
      &lt;li&gt;Easy view source&lt;/li&gt;
      &lt;li&gt;Apply CSS reset&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/li&gt;
  &lt;li&gt;Access to the history&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;try-mailtrap-right-now&quot;&gt;Try &lt;a href=&quot;http://mailtrap.io&quot;&gt;Mailtrap&lt;/a&gt; right now&lt;/h2&gt;

</content>
 </entry>
 
 <entry>
   <title>Slides: Fighting with fat models</title>
   <link href="http://gusiev.com/2011/11/slides-rails-fat-model-service-code"/>
   <updated>2011-11-11T00:00:00+02:00</updated>
   <id>http://gusiev.com/2011/11/slides-rails-fat-model-service-code</id>
   <content type="html">&lt;p&gt;Here are my slides from &lt;a href=&quot;http://rubyc.eu&quot;&gt;RubyC&lt;/a&gt; conference.&lt;/p&gt;

&lt;p&gt;This presentation covers a hot topic for projects that are more than a year old - it’s fat models. The problem appear naturally as models are the best place to allocate business logic code. There are number of existing techniques to manage large amount of code in Rails application. But they are suitable only in a half of use cases. For the rest of them we can use &lt;strong&gt;Traits&lt;/strong&gt; pattern that is described well in this presentation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;a href=&quot;http://gusiev.com/slides/fighting_fat_models2&quot;&gt;Fighting with fat models&lt;/a&gt;&lt;/strong&gt;
&lt;!--more--&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Resque-way</title>
   <link href="http://gusiev.com/2011/10/resque-plugins-framework-scaling"/>
   <updated>2011-10-17T00:00:00+03:00</updated>
   <id>http://gusiev.com/2011/10/resque-plugins-framework-scaling</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;http://www.engineyard.com/blog/2011/the-resque-way/&quot;&gt;My article about Resque&lt;/a&gt; was posted on Engine Yard last week. It has very good notes from EY engineers and doesn’t have my regular grammar mistakes. Enjoy your reading.
&lt;!--more--&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Advanced RSpec Techniques presentation</title>
   <link href="http://gusiev.com/2011/07/rspec-context-behavior-subject-slides"/>
   <updated>2011-07-08T00:00:00+03:00</updated>
   <id>http://gusiev.com/2011/07/rspec-context-behavior-subject-slides</id>
   <content type="html">&lt;p&gt;I’ve always pay attention to Rspec and posted some articles about &lt;a href=&quot;/tags/rspec.html&quot;&gt;Advanced Rspec features&lt;/a&gt;.
Right now about 2k tests written by me personally in 3 years of development with Rails.&lt;/p&gt;

&lt;p&gt;Here is the slides from my talk about all invented patterns and best practices: &lt;br /&gt;
&lt;strong&gt;&lt;a href=&quot;http://gusiev.com/dorspec#1&quot;&gt;Do rspec: fogotten features of RSpec&lt;/a&gt;&lt;/strong&gt;
&lt;!--more--&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Reporting made easy by Datagrid gem for Rails</title>
   <link href="http://gusiev.com/2011/06/ruby-datagrid-gem-report"/>
   <updated>2011-06-26T00:00:00+03:00</updated>
   <id>http://gusiev.com/2011/06/ruby-datagrid-gem-report</id>
   <content type="html">&lt;p&gt;4 years ago I was working on some enterprise projects with a lot of reports. From that time I was thinking about &lt;a href=&quot;https://github.com/bogdan/datagrid&quot;&gt;perfect report gem&lt;/a&gt; that would provide easy DSL for making filters and sortable columns to build reports and make it all reusable with standard OOP techniques. Since that time this idea never left my head and now finally I have enough knowledge and opportunities to build such tool.&lt;/p&gt;

&lt;!--more--&gt;

&lt;h3 id=&quot;defining-a-grid&quot;&gt;Defining a grid&lt;/h3&gt;

&lt;p&gt;The idea of Datagrid DSL is to define a scope of ActiveRecord models and define different independent criterias to filter this scope. 
Than convert filtered data to table view with defined columns. So, typical datagrid report consists of:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A scope of records to query data&lt;/li&gt;
  &lt;li&gt;Filters with parameters to make a subsets of this data&lt;/li&gt;
  &lt;li&gt;Columns to display this data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And we can easily split their definition with the following ruby DSL&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SimpleReport&lt;/span&gt;

  &lt;span class=&quot;kp&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Datagrid&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;includes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:category&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:enum&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:select&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&amp;quot;first&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;second&amp;quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:group_id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:multiple&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:group_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:header&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Group&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;joins&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;where&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:groups&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;


  &lt;span class=&quot;n&quot;&gt;column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:order&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;groups.name&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;group&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;column&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:active&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:header&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;Activated&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:order&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;disabled&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;grid-instance-api&quot;&gt;Grid instance API&lt;/h3&gt;

&lt;p&gt;And now we can create and manipulate reports:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;report&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;SimpleReport&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;ss&quot;&gt;:group_id&lt;/span&gt;          &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;ss&quot;&gt;:category&lt;/span&gt;          &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;first&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;ss&quot;&gt;:order&lt;/span&gt;             &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:group&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;ss&quot;&gt;:descending&lt;/span&gt;        &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;report&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assets&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# =&amp;gt; Array of User instances: &lt;/span&gt;
              &lt;span class=&quot;c1&quot;&gt;# SELECT * FROM users WHERE users.group_id in (1,2) AND users.logins_count &amp;gt;= 1 AND users.category = &amp;#39;first&amp;#39; ORDER BY groups.name DESC&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;report&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assets&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;paginate&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:page&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;params&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:page&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# =&amp;gt; Yes, it is&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;report&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;header&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# =&amp;gt; [&amp;quot;Group&amp;quot;, &amp;quot;Name&amp;quot;, &amp;quot;Activated&amp;quot;]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;report&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rows&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# =&amp;gt; [&lt;/span&gt;
              &lt;span class=&quot;c1&quot;&gt;#      [&amp;quot;Steve&amp;quot;, &amp;quot;Spammers&amp;quot;, true],&lt;/span&gt;
              &lt;span class=&quot;c1&quot;&gt;#      [ &amp;quot;John&amp;quot;, &amp;quot;Spoilers&amp;quot;, true],&lt;/span&gt;
              &lt;span class=&quot;c1&quot;&gt;#      [&amp;quot;Berry&amp;quot;, &amp;quot;Good people&amp;quot;, false]&lt;/span&gt;
              &lt;span class=&quot;c1&quot;&gt;#    ]&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;report&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;   &lt;span class=&quot;c1&quot;&gt;# =&amp;gt; [ header, *rows]&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;report&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to_csv&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# =&amp;gt; Yes, it is&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;feel-the-power-and-flexibility-by-examples&quot;&gt;Feel the Power and Flexibility by examples&lt;/h3&gt;

&lt;p&gt;I love flexibility. That is why datagrid has a lot of things for non trivial use cases.
In order to proof that I’ll show you some examples of what you can do:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://gist.github.com/7ba4267aa25b6e37eb44&quot;&gt;Range filters&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gist.github.com/4e84d2dad2f2362a9ab4&quot;&gt;Time sheets aggregation report&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gist.github.com/8b91694edec900de84be&quot;&gt;Daily statistics&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://gist.github.com/106acfc3fe689564896c&quot;&gt;Extending built in methods&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;documentation&quot;&gt;Documentation&lt;/h3&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/bogdan/datagrid/wiki&quot;&gt;Datagrid WIKI&lt;/a&gt; is main documentation source of the gem.
Also feel free to ask questions right after this post.&lt;/p&gt;

&lt;h3 id=&quot;in-my-todo-list&quot;&gt;In my TODO list&lt;/h3&gt;

&lt;p&gt;Current version of datagrid gem have fully document back-end staff. It’s production ready - we are running three projects that use datagrid. Now I am working on front-end that is already part of the distribution, but not yet documented.&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Patching dbext vim plugin to support Rails naming conventions</title>
   <link href="http://gusiev.com/2011/05/dbext-vim-table-names-convention-patch"/>
   <updated>2011-05-30T00:00:00+03:00</updated>
   <id>http://gusiev.com/2011/05/dbext-vim-table-names-convention-patch</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;http://www.vim.org/scripts/script.php?script_id=356&quot;&gt;dbext&lt;/a&gt; is an excellent vim plugin that allows you to execute SQL queries directly from vim. For example: &lt;code&gt;select * from &amp;lt;word under cursor&amp;gt;&lt;/code&gt;. When dbext is used with Rails there is a problem that you can work with some variation of table name like:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;UserRole&lt;/li&gt;
  &lt;li&gt;user_role_id&lt;/li&gt;
  &lt;li&gt;user_role_ids&lt;/li&gt;
  &lt;li&gt;user_role&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But still wants to use it as table name in your database queries so that you won’t type it manually.&lt;/p&gt;

&lt;!--more--&gt;

&lt;p&gt;So, I made it to automatically convert word under cursor into a table name with the following configuration options:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;let g:dbext_table_names_number = 2 
    # 0 - disabled, 1 - singular, 2 - plural
let g:dbext_table_names_case = 2 
    # 0 - disabled, 1 - camelcase, 2 - underscore
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Also there is an option to automatically strip &lt;code&gt;_id&lt;/code&gt; suffix from word to convert it to table name:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;let g:dbext_table_names_strip_id = 1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;All these options match Rails table name convention by default.
Staff in &lt;a href=&quot;http://github.com/bogdan/dbext&quot;&gt;my dbext fork&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Once you install it in your vim runtime put your cursor on any table name variation like example above and run &lt;code&gt;:DBSelectFromTable&lt;/code&gt; to get:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;select * from user_roles&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Other commands are also patched. Please let me know if any bugs found.&lt;/p&gt;

&lt;p&gt;Don’t know if non-rails developers would love this patch but pretty sure that it won’t annoy them. 
Please support &lt;a href=&quot;https://github.com/zklinger/dbext/pull/1&quot;&gt;my pull request&lt;/a&gt; if you like it.&lt;/p&gt;

</content>
 </entry>
 
 <entry>
   <title>Logging HTTP api calls just like database queries</title>
   <link href="http://gusiev.com/2011/05/http-api-logging-rails"/>
   <updated>2011-05-24T00:00:00+03:00</updated>
   <id>http://gusiev.com/2011/05/http-api-logging-rails</id>
   <content type="html">Http api calls are pretty similar to SQL queries - they query data from external source to process them in ruby space. So I think it's a good idea to log them just like ActiveRecord does with SQL queries. 
&lt;pre&gt;&lt;code&gt;  Location Load (25.1ms)   SELECT * FROM &quot;locations&quot; WHERE (&quot;locations&quot;.&quot;id&quot; = 2548) ORDER BY title&lt;/code&gt;&lt;/pre&gt;

&lt;!--more--&gt;
This simple peace of code could be very helpful in case of 
&lt;a href=&quot;http://github.com/railsware/http_logger&quot;&gt;
    debugging HTTP api in ruby&lt;/a&gt;.
&lt;a href=&quot;https://github.com/railsware/http_logger/blob/master/screenshots/solr.png&quot;&gt;Screenshot&lt;/a&gt; with the result


Installation is simple:

&lt;pre&gt;&lt;code&gt;gem install http_logger&lt;/code&gt;&lt;/pre&gt;

And:

&lt;pre&gt;&lt;code&gt;require 'http_logger'

Net::HTTP.logger = Logger.new(...) 
    # Default: Rails.logger if defined?(Rails)

Net::HTTP.colorize = true 
    # Default: true&lt;/code&gt;&lt;/pre&gt;

</content>
 </entry>
 
 <entry>
   <title>Finally get rid of WordPress and PHP</title>
   <link href="http://gusiev.com/2011/05/migrate-wordpress-jekyll-post-preview"/>
   <updated>2011-05-20T00:00:00+03:00</updated>
   <id>http://gusiev.com/2011/05/migrate-wordpress-jekyll-post-preview</id>
   <content type="html">&lt;p&gt;After a few tries migrate to blog engine with programming language I know well, I finally found 
&lt;a href=&quot;http://github.com/mojombo/jekyll&quot;&gt;jekyll&lt;/a&gt; - lightweight static site generator. My main concerns in this choice was:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Edit posts in my favorite text editor with favorite markup&lt;/li&gt;
  &lt;li&gt;NO PHP&lt;/li&gt;
  &lt;li&gt;Syntax highlight out of the box&lt;/li&gt;
&lt;/ul&gt;

&lt;!--more--&gt;

&lt;h3 id=&quot;migration-from-wordpress&quot;&gt;Migration from WordPress&lt;/h3&gt;

&lt;p&gt;Three main migration points was:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Posts that was migrated with &lt;a href=&quot;http://google.com/search?q=wordpress%20jekyll%20migrate&quot;&gt;several lines of ruby code&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Comments that imported to &lt;a href=&quot;http://wordpress.org/extend/plugins/disqus-comment-system/&quot;&gt;disqus&lt;/a&gt; as jekyll is static site engine so the comments should be hosted somewhere else&lt;/li&gt;
  &lt;li&gt;Generate &lt;a href=&quot;http://www.google.com.ua/search?sourceid=chrome&amp;amp;client=ubuntu&amp;amp;channel=cs&amp;amp;ie=UTF-8&amp;amp;q=jekyll+tag+cloud&quot;&gt;tag cloud and posts by tag pages&lt;/a&gt; that also have a lot of workarounds in the web&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One problem I am currently have is “post preview” feature that is currently unsupported in jekyll.
My current approach for this looks like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;post.content | replace_first:'&amp;lt;!--more--&amp;gt;','&amp;lt;!--' | append:'--&amp;gt;' 
&lt;/code&gt;&lt;/pre&gt;

&lt;h3 id=&quot;problems&quot;&gt;Problems&lt;/h3&gt;

&lt;p&gt;Jekyll looks not very extensible. Some methods are not available in the API. Example: integrate SASS require more pain the ass than it should be. I am planning to fix that in a few days. Another problem I ran into is that github native support don’t allow you any extension. My suggestion is to generate static content locally with your own code on top of jekyll and upload static content to git repository.&lt;/p&gt;

&lt;p&gt;Currently I build site locally from &lt;a href=&quot;http://github.com/bogdan/blog&quot;&gt;source code&lt;/a&gt; and push static content into &lt;a href=&quot;http://github.com/bogdan/bogdan.github.com&quot;&gt;separated repository&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;result&quot;&gt;Result&lt;/h3&gt;

&lt;p&gt;About 12 hours of work to transfer 30 posts and their comments. 
Reworked site design to be more clean.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>JavaScript injection in JSON inserted directly into script tag</title>
   <link href="http://gusiev.com/2011/04/javascript-injection-in-json-inserted-directly-into-script-tag"/>
   <updated>2011-04-06T00:00:00+03:00</updated>
   <id>http://gusiev.com/2011/04/javascript-injection-in-json-inserted-directly-into-script-tag</id>
   <content type="html">Pass data from backend to javascript is done in many different ways. One of the most simple is to inject(&amp;lt;%= %&amp;gt;) value as function argument inside of script tag. Unfortunately this pattern has well known XSS vulnerability but in a little different form than same injection in html template.
&lt;!--more--&gt;
&lt;pre&gt;&lt;code&gt;&amp;lt;script type=&quot;text/javascript&quot;&amp;gt;
    App.initizalizeSomething(&lt;%= data.to_json %&gt;)
&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/pre&gt;

The reason is that browser treat &lt;strong&gt;&amp;lt;/script&amp;gt;&lt;/strong&gt; as close script tag no matter where is it inserted into script. So, the script tag can be closed unexpectedly and opened again with any code if the &lt;em&gt;data&lt;/em&gt; argument will contain correctly formed sequence, like:
&lt;pre&gt;&lt;code&gt;&amp;lt;/script&amp;gt;&amp;lt;script&amp;gt;alert('hello')&amp;lt;/script&amp;gt;&lt;/code&gt;&lt;/pre&gt;

Use &lt;em&gt;#html_escape&lt;/em&gt; helper is wrong here because it has different type of escaping.
For example you don't need to escape double quote in this case.

Rails core team is aware of that problem and implemented special helper:
&lt;pre&gt;&lt;code&gt;# A utility method for escaping HTML entities in JSON strings
# using \uXXXX JavaScript escape sequences for string literals:
#
#   json_escape('is a &amp;gt; 0 &amp;amp; a &lt; 10?')
#   # =&gt; is a \u003E 0 \u0026 a \u003C 10?
#
# Note that after this operation is performed the output is not
# valid JSON. In particular double quotes are removed:
#
#   json_escape('{&amp;quot;name&amp;quot;:&amp;quot;john&amp;quot;,&amp;quot;created_at&amp;quot;:&amp;quot;2010-04-28T01:39:31Z&amp;quot;,&amp;quot;id&amp;quot;:1}')
#   # =&gt; {name:john,created_at:2010-04-28T01:39:31Z,id:1}
#
# This method is also aliased as +j+, and available as a helper
# in Rails templates:
#
#   &lt;%=j @person.to_json %&gt;
#
def json_escape(s)&lt;/code&gt;&lt;/pre&gt;
Implementation can be found in &lt;a href=&quot;https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/string/output_safety.rb#L53&quot; target=&quot;_blank&quot;&gt;Rails source code&lt;/a&gt;. 
</content>
 </entry>
 
 <entry>
   <title>Do git commits associated to hoptoad errors</title>
   <link href="http://gusiev.com/2011/03/do-git-commits-associated-to-hoptoad-errors"/>
   <updated>2011-03-28T00:00:00+03:00</updated>
   <id>http://gusiev.com/2011/03/do-git-commits-associated-to-hoptoad-errors</id>
   <content type="html">We are trying to build a better development workflow by connecting all our tools together.
So, this time we tried to connect HopToad Application with git commits because sometimes it's better to just paste the link to hoptoad error rather than describe all steps to reproduce. This idea was resulted in the command line interface for hoptoad api with a little git integration - &lt;a target=&quot;_blank&quot; href=&quot;http://github.com/railsware/shelltoad&quot;&gt;Shelltoad&lt;/a&gt;.
&lt;!--more--&gt;
&lt;br/&gt;&lt;br/&gt;
Currently, in order to resolve hoptoad error we do the following.&lt;br/&gt;
Check the list of errors:
&lt;pre&gt;&lt;code&gt;$ shelltoad
[#3374331] production ActionController::MethodNotAllowed: Only put requests are allowed
[#4023713] production ActiveRecord::StatementInvalid: PGError: ERROR:
       duplicate key value violates unique constraint &quot;index_companies_on_slug&quot; :
       INSERT INTO &quot;companies&quot; .....
&lt;/code&gt;&lt;/pre&gt;
View the information on the required error.
 '713' is automatically guess for 4023713
&lt;pre&gt;&lt;code&gt;
$ shelltoad 713
ActiveRecord::StatementInvalid: PGError: ERROR: duplicate key value violates unique constraint
 &quot;index_companies_on_slug&quot; : INSERT INTO &quot;companies&quot;
(&quot;slug&quot;, &quot;created_at&quot;, &quot;title&quot;, &quot;updated_at&quot;, &quot;external_url&quot;, &quot;logo_id&quot;, &quot;custom&quot;) VALUES('abbott-associates
/var/data/www/apps/startwire/shared/bundle/ruby/1.8/gems/activerecord-
2.3.8/lib/active_record/connection_adapters/abstract_adapter.rb:221
/var/data/www/apps/startwire/releases/20110218113457/app/models/company.rb:50
/var/data/www/apps/startwire/releases/20110218113457/app/utils/network_map/populate.rb:9
/var/data/www/apps/startwire/releases/20110218113457/app/utils/network_map/populate.rb:74
/var/data/www/apps/startwire/releases/20110218113457/app/utils/network_map/populate.rb:70
....
&lt;/code&gt;&lt;/pre&gt;
Fix the error in the code ...
Prepare git commit:
&lt;pre&gt;&lt;code&gt;
$ git add .
&lt;/code&gt;&lt;/pre&gt;
Commit the fix to git with connection to hoptoad issue id
&lt;pre&gt;&lt;code&gt;
$ shelltoad commit 713
[dev 47f09ec]     http://xxx.hoptoadapp.com//errors/4023713
 1 files changed, 1 insertions(+), 1 deletions(-)
&lt;/code&gt;&lt;/pre&gt;

Commit message will look like this:
&lt;code&gt;&lt;pre&gt;http://startdatelabs.hoptoadapp.com//errors/4023713

ActiveRecord::StatementInvalid: PGError: ERROR: duplicate key value violates unique constraint
 &quot;index_companies_on_slug&quot; : INSERT INTO &quot;companies&quot;
(&quot;slug&quot;, &quot;created_at&quot;, &quot;title&quot;, &quot;updated_at&quot;, &quot;external_url&quot;, &quot;logo_id&quot;, &quot;custom&quot;) VALUES('abbott-associates&lt;/pre&gt;&lt;/code&gt;


This help us maintain the links between git and hoptoad automatically.
</content>
 </entry>
 
 <entry>
   <title>Debug ajax requests in more easy way</title>
   <link href="http://gusiev.com/2011/03/ajax-jquery-debug-javascript"/>
   <updated>2011-03-18T00:00:00+02:00</updated>
   <id>http://gusiev.com/2011/03/ajax-jquery-debug-javascript</id>
   <content type="html">When I have to debug ajax requests, I always feel pain in the ass accessing the exception message and backtrace. It require at least 3 clicks to get them and only in the plain html format, that web framework use to generate. Is it better to see it at once in the browser popup?  
&lt;!--more--&gt;
&lt;br/&gt;&lt;br/&gt;
This can be done with Ajax callbacks offered by javascript framework(if yours doesn't - get rid of it). We can catch errors there and display popup. JQuery example with #ajaxError callback that fires on every unsuccessful ajax call:
&lt;pre&gt;&lt;code&gt;(function() {
      var popup = null;
      $(document).ajaxError(function(event, xhr, settings, exception) {
        if (popup) {
          popup.close();
          popup = null;
        }
        popup = window.open(null, &quot;error&quot;, &quot;width=800,height=600&quot;);
        $(popup.document.documentElement).html(xhr.responseText);
        return true;
      });
    }());&lt;/code&gt;&lt;/pre&gt;

Checked in FF and Chrome(IE is not a browser).
&lt;a href=&quot;http://img6.imagebanana.com/img/drl8wpd9/screenshot_014.png&quot;&gt;Screenshot&lt;/a&gt; with the result. Be aware of browser popup blocker. &lt;br/&gt;
You always have a control when popup the error. I like it to be &quot;in any case&quot;. But You can customize it with &lt;em&gt;xhr.status&lt;/em&gt; and &lt;em&gt;if&lt;/em&gt; statement on the server side.
&lt;br/&gt;&lt;br/&gt;
In some cases it might be useful not only in the development but in production as well. But it should be an iframe inside of pretty html layout, so that designers won't blame programmers too much.
</content>
 </entry>
 
 <entry>
   <title>Dynamic context in Rspec - don't repeat yourself</title>
   <link href="http://gusiev.com/2010/11/dynamic-context-in-rspec-dont-repeat-yourself"/>
   <updated>2010-11-03T00:00:00+02:00</updated>
   <id>http://gusiev.com/2010/11/dynamic-context-in-rspec-dont-repeat-yourself</id>
   <content type="html">We used to have less problems with subject and behavior when writing our unit tests. And &lt;strong&gt;context&lt;/strong&gt; is what testing is all about.
 Web apps manage data and the same method can return different values based on current database state.
Advanced rspec features make it possible to use very effective technique to organize tests. 
In order to reflect your non-linear business process in unit tests you can not only nest contexts in each other but also make your context more dynamic.
&lt;!--more--&gt;

&lt;h3&gt;Context callback&lt;/h3&gt;
Rspec &lt;code&gt;#let&lt;/code&gt; method is used to lazy load things into context.
In spite of lazy initialization, let blocks are defined before the &lt;code&gt;before each&lt;/code&gt; hook. 
This allow us to create context callback.
&lt;br/&gt;&lt;br/&gt;
Understand by example. 
In this example orders should be delivered to confirmed customer account just after creation and should not be delivered if the account is not confirmed yet.
&lt;pre&gt;&lt;code&gt;describe Order do
  context &quot;after create&quot; do #defining a partial context
    before(:each) do
      subject.customer.confirmed = _confirmed
      subject.save!
    end

    context &quot;when customer is confirmed&quot; do 
      let(:_confirmed) { true }
      it { should be_delivered }
    end
    
    context &quot;when customer is not confirmed&quot; do 
      let(:_confirmed) { false }
      it { should_not be_delivered }
    end
  end
end&lt;/code&gt;&lt;/pre&gt;

Here you can see partial context definition and custom behavior in two nested contexts.
I used underscore prefix in order to identify the callback method, that is defined differently in different contexts.&lt;br/&gt; &lt;br/&gt;
This happened because &lt;code&gt;let&lt;/code&gt; as method is defined at once but &lt;code&gt;before&lt;/code&gt; block is called only when &lt;code&gt;it&lt;/code&gt; is reached.

&lt;h3&gt;Testing utility function&lt;/h3&gt;
Another example that is a kind of pattern matching(Erlang term), designed to test utility functions.&lt;br/&gt;
Suppose we have a boolean expression evaluation function:
&lt;pre&gt;&lt;code&gt;describe Expression
  describe &quot;.run&quot; do

    subject { Expression.run(arg) }

    context &quot;with '&amp;amp;' where both true&quot; do
      let(:arg) { &quot;true &amp;amp; true&quot; }
      it {should be_true}
    end

    context &quot;with '&amp;amp;' where one false&quot; do
      let(:arg) { &quot;false &amp;amp; true&quot; }
      it {should be_false}
    end
    ........
  end
end&lt;/code&gt;&lt;/pre&gt;

Very good strategy to run same function with different arguments.

&lt;h3&gt;Summary&lt;/h3&gt;

Rspec is far ahead of all unit testing frameworks. Unlike most of Rspec clones (e.g. for other programming languages), Rspec authors got in deep to the testing problems and invent flexible and elegant syntax.
</content>
 </entry>
 
 <entry>
   <title>Good passwords theory</title>
   <link href="http://gusiev.com/2010/09/good-passwords-theory"/>
   <updated>2010-09-30T00:00:00+03:00</updated>
   <id>http://gusiev.com/2010/09/good-passwords-theory</id>
   <content type="html">All of us are using passwords and most of us are typing them several times a day. And everyone knows the criteria to generate a good password. Random passwords are very strong from protection point of view but not from usability point of view. 
No body think about password unforgettable-ness and less-painful-to-type-ness. I saw many passwords that are very painful because they are just random. 

And I think it's nice to lose some randomness and make it easier to type and remember.
There is still a lot of enough strong passwords that match this criteria as well. 
&lt;!--more--&gt;
&lt;br/&gt;
Try to type and remember something like:
&lt;pre&gt;&lt;code&gt;sudosuamigo1;
s3host-large
you&amp;me2gether&lt;/code&gt;&lt;/pre&gt;
Instead of:
&lt;pre&gt;&lt;code&gt;1aWhuyZ
!nzu?Us0
n]1m!uaA&lt;/code&gt;&lt;/pre&gt;
 
And you feel much better.
</content>
 </entry>
 
 <entry>
   <title>Populate database pattern - why is it so cool?</title>
   <link href="http://gusiev.com/2010/08/populate-db-pattern-fake-data"/>
   <updated>2010-08-26T00:00:00+03:00</updated>
   <id>http://gusiev.com/2010/08/populate-db-pattern-fake-data</id>
   <content type="html">Once my co-worker propose an idea to fill database with some fake data because we didn't have a  gui forms yet and we have to show something that works to customer. That was a start for great pattern - &lt;strong&gt;populator&lt;/strong&gt;.
&lt;!--more--&gt;

&lt;h3&gt;Advantages&lt;/h3&gt;
Later we didn't drop that functionality because it was helpful for everyone to always see what's new appear in application. And we find more and more useful aspects of having populator. It was so easy to attach new people to project because we deliver a short set of data within a source code that demonstrates everything we have in our app. Now whole team has a full data set and see if any page doesn't work because of a side effect. Frontend people feel more comfortable with populator because they don't care of how to create many assets in order to see pagination and things like that . Also UI issues became easier to detect because every element present on a screen for every person letting him detect browser specific bugs. 

&lt;h3&gt; Implementation&lt;/h3&gt;

I won't recommend any specific library to populate the db except the one to generate fake(Lorem ipsum) content. We prefer &lt;a href=&quot;http://github.com/stympy/faker&quot;&gt;Faker&lt;/a&gt; but that is up to personal choice.
Important note is that inserting data directly into db is bad idea because all application logic is ignored in this case: validation will be ignored and counters cache might be broken. That is why Populate gem is not recommended.&lt;br/&gt;&lt;br/&gt;
One nice improvement we made is pass _SCALE_ parameter to populator in order to control how many objects should be generated. Use minimal amount if you need to test that populator works and regular amount to get working system.&lt;br/&gt;
Another good idea is soft save(ignore if not valid) and output the number of created objects after at the end. This letting you if something went wrong during populate process.&lt;br/&gt;
&lt;br/&gt;
Here is an example of how it may look like:
&lt;pre&gt;&lt;code&gt;User.transaction do
  # apply the SCALE parameter to the argument. 
  # Default SCALE is 10 so 10 * 1 = 10. 
  # Generate 10 users by default
  scale(1).times do 
    user = User.new
    user.email = Faker::Internet.email
    user.first_name = Faker::Name.first_name
    user.last_name = Faker::Name.last_name
    user.password = user.password_confirmation = 'monkey'
    user.save # this may be false in case of uniqueness restriction or other problems
  end
end


users = User.all
puts &quot;Users: #{users.count}&quot; # make sure we actually made some users &lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;At the end&lt;/h3&gt;
I beleive that Populator should be a part of the build of every project. Yes, it needs some support but in fact it saves lots and lots of time.
</content>
 </entry>
 
 <entry>
   <title>Ultimate rspec matcher to test named_scope or scoped</title>
   <link href="http://gusiev.com/2010/07/bdd-rspec-matcher-to-test-named_scope-scoped-rails-3"/>
   <updated>2010-07-11T00:00:00+03:00</updated>
   <id>http://gusiev.com/2010/07/bdd-rspec-matcher-to-test-named_scope-scoped-rails-3</id>
   <content type="html">After having a good practice on using &lt;a href=&quot;http://gusiev.com/2010/06/ultimate-rspec-matcher-to-test-validation/&quot;&gt;Ultimate rspec matcher to test validation&lt;/a&gt; I think it's time to implement one for testing named scopes - custom finders. Testing these finders is daily task. Here is how it can be done with minimum amount of code and maximum readability.
&lt;!--more--&gt;
&lt;h3&gt;Discovery &lt;del&gt;(Animal Planet)&lt;/del&gt;&lt;/h3&gt;

What do we expect from the custom finder?
We expect that it should find assets A, B, C and should not find assets D, E, F.
And sometimes the order is important: it should find A, B C with exact order.

With respect to &lt;em&gt;let&lt;/em&gt; rspec feature let's take an example: Product has and belongs to many categories. We need to have a scope to filter products within the specified category:

&lt;pre&gt;&lt;code&gt;describe &quot;#by_category_id&quot; do
      let(:given_category) do 
        Factory.create(:given_category)
      end
 

      let(:product_in_given_category) do
        Factory.create(
          :product,
          :categories =&gt; [category]
        )
      end

      let(:product_not_in_given_category) do
        Factory.create(
          :product,
          :categories =&gt; [Factory.create(:category)]
        )
      end

      # This might be tricky to redefine subject as the finder result
      # but in this way we can delegate the matcher to subject and 
      # avoid writing test descriptions.
      subject { Product.by_category_id(given_category.id) }

      it { should discover(product_in_given_category) }
      it { should_not discover(product_not_in_given_category) }

    end
&lt;/code&gt;&lt;/pre&gt; 

Factory girl was used in this example because factories kickass when we test finders. As you can see the example has a perfect readability with no one line of plain English text. I didn't include the description in my examples but you can easily make them if they make sense for you.&lt;br/&gt;
Note: Be aware of the lazy loading of your finder. &lt;em&gt;let&lt;/em&gt; is initialized lazy too. You should make sure it is called before the actual query to the database.
If you don't want to care about lazy loading their is &lt;em&gt;let!&lt;/em&gt; method that could be easily copy-pasted from Rspec 2.0. Unlike &lt;em&gt;let&lt;/em&gt; it doesn't have lazy initialization:
&lt;pre&gt;&lt;code&gt;def let!(name, &amp;block)
  let(name, &amp;block)
  before { __send__(name) } 
end
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Testing sort order&lt;/h3&gt;

If the ordering is done in non-trivial way let's &lt;em&gt;discover.with_exact_order&lt;/em&gt;. 
&lt;pre&gt;&lt;code&gt;describe &quot;#most_commented named scope&quot; do
  let(:uncommented_post) { Factory.create(:post)}
  let!(:less_commented_post) { Factory.create(:post, :comments =&gt; [Factory.build(:comment)])}
  let!(:more_commented_post) { 
    Factory.create(:post, :comments =&gt; [Factory.build(:comment), Factory.build(:comment)])}
  }

  subject { described_class.most_commented }
 it {should discover(more_commented_post, less_commented_post).with_exact_order }
 it {should_not discover(uncommented_post) }
end&lt;/code&gt;&lt;/pre&gt;

Be careful with default order. MySQL and Postgres sort objects as they were created by default.
That is why generate objects in reverse order e.g. &lt;em&gt;less_commented_post&lt;/em&gt; before &lt;em&gt;more_commented_post&lt;/em&gt; is important to make sure that ordering is your code behavior rather than default db behavior.
 
&lt;h3&gt; Summary &lt;/h3&gt;
I 've add this matcher to the &lt;a href=&quot;http://gusiev.com/2010/06/ultimate-rspec-matcher-to-test-validation/&quot;&gt;previous one&lt;/a&gt;. Both matchers are available here &lt;a href=&quot;http://github.com/bogdan/accept_values_for&quot;&gt;accept_values_for&lt;/a&gt;. Let's start thinking of what else we can do.
</content>
 </entry>
 
 <entry>
   <title>Ultimate rspec matcher to test validation</title>
   <link href="http://gusiev.com/2010/06/ultimate-rspec-matcher-to-test-validation"/>
   <updated>2010-06-30T00:00:00+03:00</updated>
   <id>http://gusiev.com/2010/06/ultimate-rspec-matcher-to-test-validation</id>
   <content type="html">After a first thousand of tests using Rspec I fount it very annoying to repeat my self testing the standard code as spec code is usually twice longer than code it test. I've started to look for a way to simplify the patterns and make it reusable. Among other nice rspec tricks there is possibility to write custom Rspec matchers. Spec the validation is just two lines of code for each attribute now.
&lt;!--more--&gt;


&lt;h3&gt; Existing solutions &lt;/h3&gt;
After doing some search around I found Shoulda and remarkable gems that provide a way of doing this. Here is one of remarkable variant (others are just ugly): 
&lt;pre&gt;&lt;code&gt;it { should validate_numericality_of(:age, :greater_than =&gt; 18, :only_integer =&gt; true) }&lt;/code&gt;&lt;/pre&gt;
But that is huge contradiction with BDD methodology because it doesn't make you think about behavior. People use to copy-paste code from model to spec and all possible bugs within. As the result &lt;strong&gt;nothing is tested&lt;/strong&gt;... I don't even say about &quot;test first&quot;.

&lt;h3&gt; Easy DSL and Implementation &lt;/h3&gt;
When I implement validation everything I care about is: what values should be accepted and what values should not. An easy DSL that realize this pattern would be:
&lt;pre&gt;&lt;code&gt;describe User do
  it { should accept_values_for(:email, &quot;john@example.com&quot;, &quot;lambda@gusiev.com&quot;) }
  it { should_not accept_values_for(:email, &quot;invalid&quot;, nil, &quot;a@b&quot;, &quot;john@.com&quot;) }
end&lt;/code&gt;&lt;/pre&gt; 
That's it! Two lines of code per attribute. And that is perfectly fine for &quot;test first&quot; principle.&lt;br/&gt;
Rspec authors take care about custom Rspec matcher. So the implementation is very easy.
Fork me on github: &lt;a href=&quot;http://github.com/bogdan/accept_values_for&quot;&gt;accept_values_for rpec matcher&lt;/a&gt;. The &lt;strong&gt;accept_values_for&lt;/strong&gt; pattern is a true BDD way and unlike Remarkable it really do testing.

&lt;h3&gt; validates_uniqueness_of &lt;/h3&gt; 
This is very special validation because you always need more then one record to test it. So, the pattern for uniqueness validation is:
&lt;pre&gt;&lt;code&gt;describe User do
  context &quot;if user with email 'aa@bb.com' exists&quot; do
    before do
      User.create!(@valid_attributes.merge(:email =&amp;gt; 'aa@bb.com')
    end
    it { should_not accept_values_for(:email, &quot;aa@bb.com&quot;) }
  end
end&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt; Summary &lt;/h3&gt;
Custom matcher is just 50 lines of code that make your life much easier. Do not scare to write your own. Custom matcher to test ActiveRecord#named_scope coming soon.
</content>
 </entry>
 
 <entry>
   <title>Advanced non-flat controllers hierarchy with rails</title>
   <link href="http://gusiev.com/2010/06/nesting-namespace-route-non-flat-controllers-hierarchy-rails"/>
   <updated>2010-06-08T00:00:00+03:00</updated>
   <id>http://gusiev.com/2010/06/nesting-namespace-route-non-flat-controllers-hierarchy-rails</id>
   <content type="html">REST became the best practice for organizing CRUD server side interface. 
But what to do when we have something more than hello-world application that 
goes forward from data CRUD and provides many presentations 
of the same data with different filters and different layouts?
&lt;!--more--&gt;
&lt;br/&gt;

&lt;h3&gt;How the flat controller architecture goes to hell&lt;/h3&gt;

Starting from REST-full controller people use to stick to them and add more and more actions to the same class. And that is how the controller code goes to hell:

&lt;pre&gt;&lt;code&gt;class UsersController &amp;lt; ApplicationController
#
# Filters
#
skip_before_filter :check_user_is_valid, :only =&amp;gt; [:edit, :update]
before_filter :require_user, :only =&amp;gt; [
:update, :edit, :settings, :disconnect_twitter, :connect_facebook_account, :google_contacts
]

before_filter :load_user, :except =&amp;gt; [
:index, :new, :create, :remote_validate_email, :autocomplete_for_user_full_name,
:remote_validate_facebook_uid, :google_contacts
]

before_filter :check_permissions, :only =&amp;gt; [ :edit, :update, :settings, :update_photo, :connect_facebook_account ]

before_filter :load_invitation, :only =&amp;gt; [:new, :create]
before_filter :ensure_authenticated_to_facebook, :only =&amp;gt; :connect_facebook_account
before_filter :initialize_form, :only =&amp;gt; [:edit]

#
# And 20 almost independent actions goes here
# And the twice longer tests file for this controller
#

end
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt; Namespaces and nesting &lt;/h3&gt;
The root of the problem is in ignoring the following simple rule:
&lt;strong&gt;The default choice for implementing new feature is do it in other controller&lt;/strong&gt;
It's always better to have 20 controllers with one action then one controller with 20 actions.
Usually there is no compromise variant. And 20 controllers with one action ain't as bad as you imagine.&lt;br/&gt;
&lt;br/&gt;

In order to handle the large number of controller we are actively using the namespaces and nested resources features. Let me give an example: User has many projects, Project belongs to category. We need to list projects per user and per category. In the flat hierarchy you would be confused but namespaces and nesting solves the problem.&lt;br/&gt;

&lt;pre&gt;&lt;code&gt;map.resources :categories do |c|
  c.namespace :categories do |categories|
    categories.resouces :projects
  end
end
map.resources :users do |u|
  c.namespace :users do |users|
    users.resouces :projects
  end
end

class Categories::ProjectsController

  def index
    @projects = ....
  end
end

class Users::ProjectsController

  def index
    @projects = ....
  end
end&lt;/code&gt;&lt;/pre&gt;


Everyone heard about restful authentication but not so many people applied this idea to other not so restful from the first sight things. Like TwitterConnectionController:

&lt;pre&gt;&lt;code&gt;class Users::TwitterConnectionController &lt; ApplicationController

  def create
  end

  def destroy
  end
end&lt;/code&gt;&lt;/pre&gt;

Many people will be pushing all such staff to UsersController until their editors would run out of memory.

&lt;h3&gt;Out of scope&lt;/h3&gt;

State control actions are not in REST but should be one day. Placing them in the same controller with CRUD is generally a good idea:

&lt;pre&gt;&lt;code&gt;class ArticlesController 

  def {new, create, update, edit, destroy}
  end

  def publish
    @article.publish!
    redirect_to articles_path
  end

end&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Some sugar from generators&lt;/h3&gt;

Rails &lt;em&gt;generate&lt;/em&gt; script supports namespaces very well:
&lt;pre&gt;&lt;code&gt;$ ./script/generate &lt;del&gt;rspec_&lt;/del&gt;controller users/categories
      create  app/controllers/users
      create  app/helpers/users
      create  app/views/users/categories
      create  spec/controllers/users
      create  spec/helpers/users
      create  spec/views/users/categories
      create  spec/controllers/users/categories_controller_spec.rb
      create  spec/helpers/users/categories_helper_spec.rb
      create  app/controllers/users/categories_controller.rb
      create  app/helpers/users/categories_helper.rb&lt;/code&gt;&lt;/pre&gt;

And classes created in appropriate namespace and folder as well as specs for them.
The only one thing you should do manually is add routes. 

&lt;h3&gt;Drawbacks&lt;/h3&gt;

Every engeneering solution has it's drawbacks. While you have different controllers that operates on the same classes you might need to reuse functionality among them.
I preffer to solve it by mixing in a module:
&lt;pre&gt;&lt;code&gt;class Users::ProjectController

 include UserNestedResource

end&lt;/code&gt;&lt;/pre&gt;

Some people use inheritance instead. Both ways are almost the same. Nothing hard here as well.

&lt;h3&gt;Summary&lt;/h3&gt;

At the end I 'll just repeat it again:&lt;br/&gt; 
Default policy for placing two actions that has some kind of connection is &lt;strong&gt;SPLIT&lt;/strong&gt;.
</content>
 </entry>
 
 <entry>
   <title>DOM element class - css reference or javascript reference?</title>
   <link href="http://gusiev.com/2010/04/dom-element-class-html-javascript-reference"/>
   <updated>2010-04-20T00:00:00+03:00</updated>
   <id>http://gusiev.com/2010/04/dom-element-class-html-javascript-reference</id>
   <content type="html">Designer have used element class attribute for CSS styling for ages. Nowadays classes are also used in javascript selectors. This overlapping cause additional problems: Changes in js may cause broken design and changes in design may cause broken js. The last one may be hard to detect especially when design and css are handled by different person.
&lt;!--more--&gt;
&lt;hr/&gt;
Element class seems the best way to query the element from DOM in js code. Other variants like it's relative possition to other elements are very fragile. Let's say that we have the following DOM:
&lt;pre&gt;&lt;code&gt;&amp;lt;a&amp;gt; Delete&amp;lt;/a&amp;gt;
&amp;lt;a&amp;gt; Edit&amp;lt;/a&amp;gt;&lt;/code&gt;&lt;/pre&gt;
And both links are handled with javascript. How would you separate them? 
&lt;ul&gt;
	&lt;li&gt;&lt;strong&gt;By order&lt;/strong&gt;(first, second) will cause problems if designer would like to change the order.&lt;/li&gt;
	&lt;li&gt;&lt;strong&gt;By id&lt;/strong&gt; will fail if you would have a list of elements where each has these links&lt;/li&gt;
	&lt;li&gt;&lt;strong&gt;By label&lt;/strong&gt; is just a fuck up and won't work for multi language application&lt;/li&gt;
&lt;/ul&gt;
The only one variant that provide the best stability is reference &lt;strong&gt;By class&lt;/strong&gt;.
But as stated above class also serves for another purpose - styling. 

The thing that might help is a special prefix(for example &quot;js&quot;) for all 'JavaScript classes'.
Like:
&lt;pre&gt;&lt;code&gt;&amp;lt;a class=&quot;js-new-comment big-blue-button&quot;&amp;gt; New Comment &amp;lt;/a&amp;gt;&lt;/code&gt;&lt;/pre&gt;

And the convention that classes with prefix should only be used in javascript selectors and classes without prefix in CSS.
&lt;br/&gt;&lt;br/&gt;
Yes, sometimes it may look like:
&lt;pre&gt;&lt;code&gt;&amp;lt;input type='button' class=&quot;js-follow-button follow-button&quot;&amp;gt; Follow this person &amp;lt;/input&amp;gt;&lt;/code&gt;&lt;/pre&gt;
But that makes sense to eliminate maintenance problem described above.
</content>
 </entry>
 
 <entry>
   <title>Encapsulation in Ruby: 'private' doesn't give expected level of privacy</title>
   <link href="http://gusiev.com/2010/04/ruby18-private-protected-incapsulatio"/>
   <updated>2010-04-05T00:00:00+03:00</updated>
   <id>http://gusiev.com/2010/04/ruby18-private-protected-incapsulatio</id>
   <content type="html">The classic OOP pattern usually called encapsulation implemented in ruby with &lt;code&gt;private&lt;/code&gt; and &lt;code&gt;protected&lt;/code&gt; &lt;del&gt;keywords&lt;/del&gt; methods. The distinction between API and implementation works great with both. The problem is that &lt;code&gt;private&lt;/code&gt; doesn't hide method for inherited classes. 
&lt;!--more--&gt;
&lt;hr/&gt;
See the simple example below:

&lt;pre&gt;&lt;code&gt;
class A
  private
  def implementation
    puts 'private A'
  end
end

class B &lt; A
  def api
    implementation
  end
end

B.new.api
&lt;/code&gt;&lt;/pre&gt;

And we won't get exception at the last line.&lt;br/&gt;
So, in fact &lt;code&gt;private&lt;/code&gt; do not do what you expect it to do. Use cases that distinguish &lt;code&gt;private&lt;/code&gt; and &lt;code&gt;protected&lt;/code&gt; are more specific.
At first: &lt;code&gt;implementation&lt;/code&gt; method can not be called like &lt;code&gt;self.implementation&lt;/code&gt; even from class A.
&lt;pre&gt;&lt;code&gt;class A
  def api
    self.implementation #exception here
  end
  private
  def implementation
    puts 'private A'
  end
end

A.new.api
&lt;/code&gt;&lt;/pre&gt;
Seamless pure gap. You can do such call if the method would be protected. Can't imagine the case when this limitation is useful. &lt;br/&gt;
&lt;br/&gt;
The second difference is a bit esoteric. Two instances of the same class can access protected methods of each other but not private methods.

&lt;pre&gt;&lt;code&gt;class A
  def api(another) #suppose to receive an instance of a
    implementation == another.implementation
  end
  protected
  def implementation
    puts 'private A'
  end
end
A.new.api(A.new)
&lt;/code&gt;&lt;/pre&gt;

Fine for &lt;code&gt;protected&lt;/code&gt; but fail for &lt;code&gt;private&lt;/code&gt;. Well, this kind of protection do not make a real because of it's rareness.
&lt;h3&gt;My conclusion: don't use private at all&lt;/h3&gt;

You might do any decision on the points above.&lt;br/&gt;
My conclusion is to not use &lt;code&gt;private&lt;/code&gt; at all, because it doesn't give the level of privacy that I expect. Difference form &lt;code&gt;protected&lt;/code&gt; is so pure that it do not have any real benefits. 


</content>
 </entry>
 
 <entry>
   <title>A dream to develop application that I will use</title>
   <link href="http://gusiev.com/2010/03/quality-develop-application-improve-usability-design"/>
   <updated>2010-03-29T00:00:00+03:00</updated>
   <id>http://gusiev.com/2010/03/quality-develop-application-improve-usability-design</id>
   <content type="html">Developing sites for communities you never join, enterprise applications for company you never work in, services you never find useful... a serious problem of IT industry. All misunderstanding between customers and developers come from that reason.
&lt;br/&gt;&lt;br/&gt;
It would be so great if we could always feel ourselves as a user of our applications and make  something useful, but not spend time on improving features that won't be familiar. Being a user of the developed application makes it really possible to develop ergonomic and pretty design.
&lt;br/&gt;&lt;br/&gt;
Unfortunately most of the companies are ignoring that simple rule and unfortunately in some cases it is hard to follow. But anyway that is the only one way to build the best product.
</content>
 </entry>
 
 <entry>
   <title>Advanced SQL and scopes stack with ActiveRecord</title>
   <link href="http://gusiev.com/2010/01/sql-queries-activerecord-rails-named-scopes-stack"/>
   <updated>2010-01-24T00:00:00+02:00</updated>
   <id>http://gusiev.com/2010/01/sql-queries-activerecord-rails-named-scopes-stack</id>
   <content type="html">&lt;p&gt;If you ever work with rails application that is a little bit more complex than a simple CRUD you would know that some of the ActiveRecord magic doesn’t work for complex SQL queries. I am primary talking about scopes stack feature.&lt;br /&gt;
&lt;!--more--&gt;&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;Let’s review the following scope that suppose to be usable in different combinations with others:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;scope&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:network_of&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;lambda&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
&lt;span class=&quot;ss&quot;&gt;:select&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;u.*&amp;quot;&lt;/span&gt;
&lt;span class=&quot;ss&quot;&gt;:from&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;users u, followings f1, followings f2&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;ss&quot;&gt;:conditions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;f1.follower_id = &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; AND &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; 
&lt;span class=&quot;s2&quot;&gt;&amp;quot;f1.followed_id = f2.follower_id AND &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&amp;quot;f2.followed_id = u.id&amp;quot;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;It suppose to returns all people that are followed by people that are followed by the given person(second circle).
From the SQL point of view that is the simpliest and fastest way to do that with a plain SQL.
But this solution will have some issues with Active Record scopes stack magic.
See that &lt;code&gt;User.network_for(current_user).all(:limit =&amp;gt;5)&lt;/code&gt; will result in SQL exception primary because &lt;code&gt;:limit =&amp;gt; 5&lt;/code&gt; doesn’t know about the table alias &lt;code&gt;users u&lt;/code&gt;.
We can not use it in fact.&lt;br /&gt;
The second problem comes to the foreground when we will try to use ActiveRecord features like
&lt;code&gt;User.network_for(current_user).all(:include =&amp;gt; :orders)&lt;/code&gt;. ActiveRecord handles :include in the very different ways and in some cases you will see the SQL exception here as well.&lt;br /&gt;
The problem is that :include sometimes appends some joins to the query that is concatenated to the last table in the :from parameter. To solve that we should make “users” table to be the last one declared in :from parameter.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:select&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;users.*&amp;quot;&lt;/span&gt;
&lt;span class=&quot;ss&quot;&gt;:from&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;followings f1, followings f2, users&amp;quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
&lt;span class=&quot;ss&quot;&gt;:conditions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&amp;quot;f1.follower_id = &lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; AND &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; 
&lt;span class=&quot;s2&quot;&gt;&amp;quot;f1.followed_id = f2.follower_id AND &amp;quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;
&lt;span class=&quot;s2&quot;&gt;&amp;quot;f2.followed_id = users.id&amp;quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Summary I would say that using &lt;code&gt;:joins&lt;/code&gt; instead of &lt;code&gt;:from/:conditions&lt;/code&gt; would give more flexibility and stackability to your scopes but sometimes &lt;code&gt;:from&lt;/code&gt; is more clear and here you got the tip how to use it.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Vim as IDE - new development era for me</title>
   <link href="http://gusiev.com/2009/12/vim-as-ruby-on-rails-ide-development-perfomanc"/>
   <updated>2009-12-01T00:00:00+02:00</updated>
   <id>http://gusiev.com/2009/12/vim-as-ruby-on-rails-ide-development-perfomanc</id>
   <content type="html">I was using Vim to do the minor editing in config files for ages. Vim is advanced on basic navigation and editing operations. However I was always prefer IDE for programming because of it's specific to language navigation('go to definition' feature is awesome!), integrated debugger. But while gaining the experience I noticed that all advanced features began to be less important then basic ones. And you should try Vim if you feel the same.&lt;br/&gt;
&lt;!--more--&gt;
&lt;hr/&gt;

The basic concept of vim is that: Vim think in the same way as you do. For example: I want to delete 'd' everything inside 'i' brackets ')' and vim understands my command: di) &lt;br/&gt;
You shouldn't type a dozen of button to move cursor around and finally delete the selected fragment. 
It's hard to adapt because people are use to think in the way that there text editor do. Vim does vice versa. Some people are saying that they don't like vim because it make them think a lot. That's wrong! Vim is trying to make you free from thinking about editing and concentrate on the content.&lt;br/&gt;

General editing in Vim considered up to every small details. Ask yourself if there is some usual task that I am doing inefficiently and search the Vim feature to optimize that in 3-5 keys. And in ninety nine percent of cases you will find it as built in vim command or a plugin.
</content>
 </entry>
 
 <entry>
   <title>Objects behaviour inheritance with RSpec</title>
   <link href="http://gusiev.com/2009/10/objects-behaviour-inheritance-with-rspec"/>
   <updated>2009-10-27T00:00:00+02:00</updated>
   <id>http://gusiev.com/2009/10/objects-behaviour-inheritance-with-rspec</id>
   <content type="html">About half of a year ago I was writing about &lt;a href=&quot;http://gusiev.com/2009/05/what-do-you-expect-from-interface/&quot;&gt;object interface and Liskov Substitution Princeple&lt;/a&gt;. In short: Any class instance that extends the base class should pass all &lt;span style=&quot;text-decoration: line-through;&quot;&gt;unit tests&lt;/span&gt; behaviour tests written for base class instance. It was a surprise for me that this concept has already been implemented in RSpec.&lt;br/&gt;&lt;br/&gt;

&lt;!--more--&gt;
My previous article was primary inspired by Java programming language and it's interface concept. Unlike Java, Ruby does not have interfaces, but behaviour inheritance is still actual for both languages. RSpec seems the first testing framework that provide the ability to validate LSP and behavior inheritance with &lt;code&gt;it_should_behave_like&lt;/code&gt;. &lt;br/&gt;
With Ruby modules(mixins) feature we can build reusable code and include it in different classes&lt;a href=&quot;http://ruby-doc.org/core/classes/Module.html&quot;&gt;(read more)&lt;/a&gt;. With RSpec we can bundle the tests as well.&lt;br/&gt;

&lt;br/&gt;

Let's review the following module that uses one of the Rails callback and adds some logging:&lt;br/&gt;
&lt;pre&gt;&lt;code&gt;
module LoggedModel
  def after_save
    super
    handle_logging
  end
end
&lt;/code&gt;&lt;/pre&gt;
and the some tests group for this module:&lt;br/&gt;
&lt;pre&gt;&lt;code&gt;
describe LoggableModel
  it &quot;should be loggable&quot; do
    LoggableModel.should ...
  end
end&lt;/code&gt;&lt;/pre&gt;
Now, we have a tested code that is going to be used in many cases like this:&lt;br/&gt;
&lt;pre&gt;&lt;code&gt;class MyModel
  include LoggableModel
  def after_save
    do_some_other_thing
  end
end&lt;/code&gt;&lt;/pre&gt;

OK, let's see what we have: after_save in MyModel overwrites after_save in LoggableModel and breaks the logging. This is simplest example when the behavior inheritance may be broken. Rspec shared examples groups allows you to ensure that the code in LoggableModel is used correctly from any inherited class. Let's change the definithin of LoggableModel tests.&lt;br/&gt;
&lt;pre&gt;&lt;code&gt;
shared_examples_for &quot;logged model&quot; do
  it &quot;should be loggable&quot; do
    subject.should...
  end
end
&lt;/code&gt;
&lt;/pre&gt;
'Subject' is the ultimate RSpec magic that let us make a simple abstraction with the tested class and reuse these shared examples in MyModel spec:
&lt;pre&gt;
&lt;code&gt;describe MyModel do
  it_should_behave_like 'loggable model'
end&lt;/code&gt;
&lt;/pre&gt;
In this way we will rerun the LoggableModel examples for MyModel and make sure that it's behavior wasn't broken.
</content>
 </entry>
 
 <entry>
   <title>Computer science ain't as good as it should be</title>
   <link href="http://gusiev.com/2009/07/computer-science-programming-criticism-object-database-design"/>
   <updated>2009-07-30T00:00:00+03:00</updated>
   <id>http://gusiev.com/2009/07/computer-science-programming-criticism-object-database-design</id>
   <content type="html">When I start studying computer science in the University class I truly believe that it is all I need to get started writing good programs. Algorithms &gt; Functional programming &gt; OOP &gt; Databases &gt; MVC is a step-by-step path of all people that learn programming of the web application. But...
&lt;!--more--&gt;
&lt;br/&gt;&lt;br/&gt;
Programming basics like conditions, variables, cycles and functions are mandatory for everyone just like writing and speaking for a baby.&lt;br/&gt; 

But more advanced knowledge are less useful and optional. All of these:
&lt;ul&gt;
	&lt;li&gt;Object hierarchy&lt;/li&gt;
	&lt;li&gt;Normalized database model&lt;/li&gt;
&lt;/ul&gt;
are turned into dust by more important
&lt;ul&gt;
	&lt;li&gt;Do it as fast as possible&lt;/li&gt;
	&lt;li&gt;Customer wants!&lt;/li&gt;
&lt;/ul&gt;

Each framework declare it's own way to organize the application Classes. If you are working on CRUD(create-read-update-delete) data application you don't need to think about object hierarchy yourself anymore. All you need to do is understand where you need place the function that handle data processing. And the principles a well declined in the framework manual. 
Normalized database is only a myth in a real world application. In 80% of cases the database design is evident to the developer. In other 20% the database couldn't be normalized at all.&lt;br/&gt;
&lt;br/&gt;

When you start learning programming try to let your knowledge be &quot;moved to production&quot; as fast as possible. In other words: Have a programming practice as fast as possible and you will understand what kind of programming theory you really need. Concentrate your education on all cases you met and avoid learning things that you could possible come across.   






</content>
 </entry>
 
 <entry>
   <title>When BDD user stories is productive</title>
   <link href="http://gusiev.com/2009/06/bdd-user-stories-productive-tdd-criticism"/>
   <updated>2009-06-29T00:00:00+03:00</updated>
   <id>http://gusiev.com/2009/06/bdd-user-stories-productive-tdd-criticism</id>
   <content type="html">When you are skilled Unit tests writer it is not very easy to estimate the potential of BDD. Moreover in most cases BDD is used as a human-readable wrapper for unit tests. Do you really think that programmers need such abstract layer? Of course not... Users needs them.&lt;br/&gt;
&lt;!--more--&gt;
&lt;hr/&gt;

Saying Users I mean every non technical person who will treat to application quality some how.
Featured innovation of BDD is &lt;strong&gt;User Stories&lt;/strong&gt; - a human-readable bit of specification with technical test implementation behind it. &lt;br/&gt;
Let's review the following example from the blog specification:
&lt;pre&gt;&lt;code&gt;
  Given I signed up as Author
    When I write article &quot;TDD and BDD&quot;
    And text of article is &quot;BDD is ....&quot;
    And I post article
    Then I should see &quot;Article is created&quot;
    And I should see article title &quot;TDD and BDD&quot;
&lt;/code&gt;&lt;/pre&gt;
As you can see unlike TDD human readability is the term that was brought to the high priority in BDD. But that is not yet a BDD story.&lt;br/&gt;
We need the programming implementation for each phrase in the story, like this:
&lt;pre&gt;&lt;code&gt;
&quot;I signed up as Author&quot;:
     signInAsAuthor();
&lt;/code&gt;&lt;/pre&gt;

Programmer's task is to chose the implementation that will validate described behavior. User doesn't know about any programming back end of the story. When the test failed he just knows that some behavior of the application is not working properly any more. &lt;br/&gt;
From this point of view the key principle of user story implementation is that it should &lt;strong&gt;guarantee&lt;/strong&gt; with the most high probability that the user can complete described actions.&lt;br/&gt; 

Therefore user stories are the most productive when the implementation is done using &lt;strong&gt;GUI Robots&lt;/strong&gt; like SWT bot, Selenium.


</content>
 </entry>
 
 <entry>
   <title>Pragmatic review of Google Wave technology</title>
   <link href="http://gusiev.com/2009/06/pragmatic-review-view-google-wave-criticism-technolog"/>
   <updated>2009-06-08T00:00:00+03:00</updated>
   <id>http://gusiev.com/2009/06/pragmatic-review-view-google-wave-criticism-technolog</id>
   <content type="html">Have a good time watching the &lt;a href=&quot;http://www.youtube.com/watch?v=v_UyVmITiYQ&quot;&gt;Google Wave presentation video&lt;/a&gt;. Looks like an excellent open technology that will make our life easier. I like the overall idea. It is really awesome. But I personally admit some things that seems useless to me...&lt;br/&gt;
&lt;!--more--&gt;
&lt;hr/&gt;
Starting from the very beginning I need to say: Wow! Now we can organize all communication threads to the widespread object called &lt;strong&gt;Wave&lt;/strong&gt; that will be readable and editable by any other application. No more browsing dozen of sites and tracking all changes around - all interested waves can be reorganized in the way you want.&lt;br/&gt;
Let's review the features now:&lt;br/&gt;
&lt;ul&gt;
	&lt;li&gt; Act as Wiki. Wiki can be handled via Waves too, but not sure about parallel editing. That looks sexy on presentation but has no sense in action.&lt;/li&gt;
	&lt;li&gt;Translation to another languages. I would like to have that on my desktop. Don't think it would be useful then doing the real time communication. Machine translation never was good enough.&lt;/li&gt;	
  &lt;li&gt; Spell checker based on language model. Many people around worked on language model and no one has built it strong enough to handle automatic spell check. Let's wait for release and see if Google guys reach the success here.&lt;/li&gt;

&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Double select from a single table in complex SQL query.</title>
   <link href="http://gusiev.com/2009/06/avoid-nested-select-double-single-table-complex-query"/>
   <updated>2009-06-02T00:00:00+03:00</updated>
   <id>http://gusiev.com/2009/06/avoid-nested-select-double-single-table-complex-query</id>
   <content type="html">When I start learning SQL I have to write a lot of nested queries when working on complex select statements from a single table. Now, I feel more comfortable with it and show how to avoid nesting. There are cases when you have to compare one row of the table with all others to get the result:
&lt;ul&gt;
	&lt;li&gt;Select next  element to current by the value of some field&lt;/li&gt;
	&lt;li&gt;Select records that are unique by some complex expression&lt;/li&gt;
	&lt;li&gt;Select all records that have the same parameter as given record.&lt;/li&gt;
&lt;/ul&gt;
You can get rid of nesting in all above cases. Let's review the example.
&lt;!--more--&gt;

&lt;hr /&gt;Let's say we have the list of elements in `elements` table and we have the id of some element in this table. Now we have to get the next element from the list ordered by `sort_field`.
That could be easily done with the nested select statement:
&lt;pre&gt;
&lt;code&gt;select * from elements e 
where e.sort_field &gt;= 
            (select * from elements where element_id = #value#) and
         e.element_id != #value#
order by e.sort_field limit 1;
&lt;/code&gt;
&lt;/pre&gt;
Looks not very cool.
Let's do the select statement from `element` table twice to avoid nesting:
&lt;pre&gt;&lt;code&gt;-- The e1 is the result row and e2 is a helper row 
--that was picked up using nested select previously.
select e1.* from elements e1, elements e2
--conditional statement will look like
where e1.sort_field &amp;gt;= e2.sort_field and 
     e1.element_id != e2.element_id and 
     e2.element_id = #value#
-- and the end of the query remains almost the same
order by e1.sort_field limit 1
&lt;/code&gt;&lt;/pre&gt;

Do not scare of double select from single table. That is the same technique as if they were different tables.


</content>
 </entry>
 
 <entry>
   <title>How to choose a sexy title for your article</title>
   <link href="http://gusiev.com/2009/05/how-to-choose-a-good-sexy-title-for-your-article"/>
   <updated>2009-05-13T00:00:00+03:00</updated>
   <id>http://gusiev.com/2009/05/how-to-choose-a-good-sexy-title-for-your-article</id>
   <content type="html">Most of the people will not spend a minute reading your article if they wouldn't get interested from the very beginning. What resides at the beginning of any article? &lt;strong&gt;It is the title of course.&lt;/strong&gt;
That is why choose a good title is very important for every content posted in the net.
&lt;!--more--&gt;
&lt;hr/&gt;

Title should not reflect the overall idea of your article, let it &lt;b&gt;just be cool&lt;/b&gt;. Attracting users attention is more important. It should be the key point of what are you righting about&lt;br/&gt; 
&lt;br/&gt;

Here is my idea: &lt;strong&gt;Title looks cool for the reader if it is close to his thoughts. &lt;/strong&gt;&lt;br/&gt;
Reader should feel himself close to the topic you are going to speak about.
Let's find out and see what do our mind filled in and describe some title choosing principles:

&lt;h4&gt;Emotions&lt;/h4&gt;
Emotions acts like a catalyst. Expressing strong emotion in the title will give you the most effective result. People are getting interested not only when your emotion correspond to their but also vice versa.&lt;br/&gt;
Example: &lt;a href=&quot;/2009/05/jboss-seam-usage-makes-the-application-code-crazy/&quot;&gt;JBoss Seam usage makes the application code crazy!&lt;/a&gt;

&lt;h4&gt;Questions&lt;/h4&gt;
In most cases the reader is somebody who want to know more about the world. Put a question in the title and it will engage everybody who wants to know the answer. &lt;br/&gt;
Like this one: &lt;a href=&quot;/2009/05/what-do-you-expect-from-interface/&quot;&gt;What do you expect from the interface?&lt;/a&gt; 


&lt;h4&gt;Famous topics&lt;/h4&gt;
&lt;ul&gt;
	&lt;li&gt;Humor is top theme on every forum, every blog, every site all the time. Joking title is a good way to awake the interest.&lt;/li&gt;
	&lt;li&gt;Sex. You noticed the title of this article, right?&lt;/li&gt;
	&lt;li&gt;Weather&lt;/li&gt;
	&lt;li&gt;Politics&lt;/li&gt;
	&lt;li&gt;Sports&lt;/li&gt;	
        &lt;li&gt;etc.&lt;/li&gt;
&lt;/ul&gt;
&lt;br/&gt;
Making a slight reference in the title to one of above mentioned topics will attract much more attention to your article&lt;br/&gt;
I hope this information was helpful. Thanks for reading.








</content>
 </entry>
 
 <entry>
   <title>What do you expect from the interface?</title>
   <link href="http://gusiev.com/2009/05/what-do-you-expect-from-interface"/>
   <updated>2009-05-05T00:00:00+03:00</updated>
   <id>http://gusiev.com/2009/05/what-do-you-expect-from-interface</id>
   <content type="html">Read a great article today - &lt;a href=&quot;http://www.google.com.ua/url?sa=t&amp;source=web&amp;ct=res&amp;cd=3&amp;url=http%3A%2F%2Fwww.objectmentor.com%2Fresources%2Farticles%2Flsp.pdf&amp;ei=xjb_Sbi-EJbItge-ocmSDA&amp;usg=AFQjCNFnNI0DmzofjWDQEGILAT-W1L8Mtw&amp;sig2=okCh8NuqsQslKMTSWOlaTw&quot;&gt; Liskov Substitution Principle (LSP)&lt;/a&gt;. It let me understand that Interface is not just a list of methods.&lt;br/&gt;
&lt;!--more--&gt;
&lt;hr/&gt;
Let's review the following utility function:
&lt;div class=&quot;bordered&quot;&gt;
&lt;code&gt;public static void processList(List list) {
          list.clear();
          list.add(new Object());
}&lt;/code&gt;
&lt;/div&gt;
It accepts the object that implements &lt;em&gt;java.utils.List&lt;/em&gt; interface as the parameter.
What do you expect to receive in result? The list with one element, right?&lt;br/&gt;
But does the result really meet your expectation? Who knows, the object that implements list interface do not guarantee you 'Act as container' behavior. It just implements all the methods of the interface and nothing else.&lt;br/&gt;
Actually all methods of the interface &lt;strike&gt;should have&lt;/strike&gt; have a notation of their usage. If you do not follow these notations - you should not inherit your object from the interface even if the object has all the methods of the interface.&lt;br/&gt;
&lt;br/&gt;
&lt;strong&gt;Idea:&lt;/strong&gt; Maybe one day programming languages will have unit tests as part of interface and validate each instance that implements this interface with them.



</content>
 </entry>
 
 <entry>
   <title>JBoss Seam usage makes the application code crazy!</title>
   <link href="http://gusiev.com/2009/05/jboss-seam-usage-makes-the-application-code-crazy"/>
   <updated>2009-05-04T00:00:00+03:00</updated>
   <id>http://gusiev.com/2009/05/jboss-seam-usage-makes-the-application-code-crazy</id>
   <content type="html">Seam Application has a number of good innovations but let's find out if we lose some advantages that we had before. I started application development with JBoss Seam one year ago. With the code base growth I have had more and more problems in workflow development .
&lt;!--more--&gt;
&lt;hr/&gt; 
I noticed that there are many problems which we have not had before, for example: 
&lt;ul&gt;
	&lt;li&gt;Utility methods calls are not straight and clear now.&lt;/li&gt;
	&lt;li&gt;Find all usages IDE has feature became useless because of mass EL calls.&lt;/li&gt;
	&lt;li&gt;There is much of the meta programming in such a severe OOP language as Java.&lt;/li&gt;
	&lt;li&gt;It is impossible to debug JSF templates&lt;/li&gt;
	&lt;li&gt;Seam Exceptions look so unclear.&lt;/li&gt;
&lt;/ul&gt;

Programming with Seam has all disadvantages of script languages like PHP or Ruby.
 I definitely admit that some features like Conversations and Native AJAX support made a big step forward. There is a lot of information in the net about How cool is seam but I tried to reveal the underside.&lt;br/&gt;
&lt;br/&gt;
Think twice before switching to Seam. 
</content>
 </entry>
 
 <entry>
   <title>Update and create timestamps with MySQL</title>
   <link href="http://gusiev.com/2009/04/update-and-create-timestamps-with-mysql"/>
   <updated>2009-04-30T00:00:00+03:00</updated>
   <id>http://gusiev.com/2009/04/update-and-create-timestamps-with-mysql</id>
   <content type="html">A lot of relational tables need created and update timestamps columns. I prefer having them for all tables with no exception. However, most of applications I am working on are running MySQL. MySQL has minor limitation on timestamps. Unfortunately you can create only one time stamp column that has &lt;em&gt; DEFAULT NOW()&lt;/em&gt; value. Read more to see how to avoid this limitation. &lt;!--more--&gt;
&lt;hr/&gt;
The simplest way to do this is create the following columns in the table:
&lt;div class=&quot;bordered&quot;&gt;&lt;code&gt;stamp_created timestamp default now(), 
stamp_updated timestamp default now() on update now()) &lt;/code&gt;&lt;/div&gt;
But MySQL will return the following error: &lt;br/&gt; 
&lt;em&gt;ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_ in DEFAULT or ON UPDATE clause&lt;/em&gt; &lt;br/&gt;
You can use only one of the definitions in one table. However here is the way how to create both timestamps columns:
&lt;pre&gt;&lt;code&gt; create table test_table( 
id integer not null auto_increment primary key, 
stamp_created timestamp default '0000-00-00 00:00:00', 
stamp_updated timestamp default now() on update now() 
); &lt;/code&gt;&lt;/pre&gt;
Note that it is necessary to enter nulls into both columns during &quot;insert&quot;:
&lt;pre&gt;&lt;code&gt; mysql&amp;gt; insert into test_table(stamp_created, stamp_updated) values(null, null); 
Query OK, 1 row affected (0.06 sec)
mysql&amp;gt; select * from t5; 
+----+---------------------+---------------------+ 
| id | stamp_created       | stamp_updated       |
+----+---------------------+---------------------+
|  2 | 2009-04-30 09:44:35 | 2009-04-30 09:44:35 |
+----+---------------------+---------------------+
2 rows in set (0.00 sec)  
mysql&amp;gt; update test_table set id = 3 where id = 2; 
Query OK, 1 row affected (0.05 sec) Rows matched: 1  Changed: 1  Warnings: 0  
mysql&amp;gt; select * from test_table;
+----+---------------------+---------------------+
| id | stamp_created       | stamp_updated       | 
+----+---------------------+---------------------+ 
|  3 | 2009-04-30 09:44:35 | 2009-04-30 09:46:59 | 
+----+---------------------+---------------------+ 
2 rows in set (0.00 sec)  
mysql&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
</content>
 </entry>
 
 <entry>
   <title>Aphorisms about programming</title>
   <link href="http://gusiev.com/2009/04/some-programming-aphorisms"/>
   <updated>2009-04-29T00:00:00+03:00</updated>
   <id>http://gusiev.com/2009/04/some-programming-aphorisms</id>
   <content type="html">I am not clever. I am just yet another way to use google.&lt;br/&gt;
Good programmer should be lazy.&lt;br/&gt;
The hole in your pocket is not critical unless you have a hole in your other pocket.&lt;br/&gt;
&lt;!--more--&gt;
&lt;hr/&gt;
Software is like sex - better when it is free. Linus Torvalds.&lt;br/&gt;
Sometimes the theory meets practice. Practice wins. Always.&lt;br/&gt;
10% of your zeals brings you 90% of success. Other 90% just 10% of success.&lt;br/&gt;
About haste: The perfection of a clock is not in the speed of the movement, but in its correctness.&lt;br/&gt;
Don't be satisfied with an apparent fix; full understanding is the standard.&lt;br/&gt;
If you only know one language, no matter how well you know it, you're not a great programmer.&lt;br/&gt;
The only &quot;best practice&quot; you should be using all the time is &quot;Use Your Brain&quot;.&lt;br/&gt;
</content>
 </entry>
 
 <entry>
   <title>Ant task to install tomcat service26px</title>
   <link href="http://gusiev.com/2009/04/ant-task-to-install-tomcat-service"/>
   <updated>2009-04-28T00:00:00+03:00</updated>
   <id>http://gusiev.com/2009/04/ant-task-to-install-tomcat-service</id>
   <content type="html">If you are familiar to run Apache Tomcat as Windows service, you can install it with the ant task. &lt;i&gt;build.xml&lt;/i&gt; file sample under the cut line.
&lt;!--more--&gt;
&lt;hr/&gt;
First of all let's do some properties setup:

&lt;pre&gt;&lt;code&gt;catalina.home=d:/java/tomcat-6.0.18
tomcat.executable=${catalina.home}/bin/tomcat6.exe
tomcat.service.executable=\
${catalina.home}/bin/service.bat
tomcat.service.name=myproject
# parameters that will be used in the jvm
tomcat.service.jvm.ms=128
tomcat.service.jvm.mx=256
&lt;/code&gt;&lt;/pre&gt;
Service startup task consists of 5 exec commands:
&lt;ul&gt;
	&lt;li&gt;Stop service if currently running&lt;/li&gt;
	&lt;li&gt;Delete service if installed&lt;/li&gt;
	&lt;li&gt;Install service with bat script&lt;/li&gt;
	&lt;li&gt;Update service with some parameters&lt;/li&gt;
	&lt;li&gt;Start service&lt;/li&gt;
&lt;/ul&gt;

Here is the implementation:
&lt;pre&gt;&lt;code&gt;
    &amp;lt;target name=&quot;tomcat.service.install&quot; description=&quot;Install tomcat service&quot;&amp;gt;
        
        &amp;lt;exec command=&quot;net stop ${tomcat.service.name}&quot;
              failifexecutionfails=&quot;false&quot;
              failonerror=&quot;false&quot;/&amp;gt;

        &amp;lt;exec command=&quot;${tomcat.executable} //DS//${tomcat.service.name}&quot;
              failifexecutionfails=&quot;false&quot;
              failonerror=&quot;false&quot;/&amp;gt;
        &amp;lt;exec executable=&quot;${tomcat.service.executable}&quot; 
                  failifexecutionfails=&quot;true&quot; failonerror=&quot;true&quot;&amp;gt;
            &amp;lt;env key=&quot;CATALINA_HOME&quot; value=&quot;${catalina.home}&quot;/&amp;gt;
            &amp;lt;arg value=&quot;install&quot;/&amp;gt;
            &amp;lt;arg value=&quot;${tomcat.service.name}&quot;/&amp;gt;
        &amp;lt;/exec&amp;gt;
       &amp;lt;exec executable=&quot;${tomcat.executable}&quot;&amp;gt;
            &amp;lt;arg value=&quot;//US//${tomcat.service.name}&quot;/&amp;gt;
            &amp;lt;arg value=&quot;--JvmMs=${jvm.ms}&quot;/&amp;gt;
            &amp;lt;arg value=&quot;--JvmMx=${jvm.mx}&quot;/&amp;gt;
            &amp;lt;arg value=&quot;--Startup=auto&quot;/&amp;gt;
        &amp;lt;/exec&amp;gt;
        &amp;lt;exec command=&quot;net start ${tomcat.service.name}&quot; 
                  failifexecutionfails=&quot;true&quot; failonerror=&quot;true&quot;/&amp;gt;
    &amp;lt;/target&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
</content>
 </entry>
 
 <entry>
   <title>Clear upload file input field </title>
   <link href="http://gusiev.com/2009/04/clear-upload-file-input-field"/>
   <updated>2009-04-23T00:00:00+03:00</updated>
   <id>http://gusiev.com/2009/04/clear-upload-file-input-field</id>
   <content type="html">Many web developers came to the problem that they are not able to change value of file input field from Java Script in the web application. There is no access to value field of file input tag because of security restriction. However there is a trick how to erase this field if you need it in your application. &lt;br/&gt;
&lt;!--more--&gt;
&lt;hr/&gt;
You can simply redraw the html block where your input tag is located. In this case all data will remain the same except the selected file value.

&lt;pre&gt;&lt;code&gt;
&amp;lt;div id=&quot;uploadFile_div&quot;&amp;gt;
&amp;lt;input type=&quot;file&quot; class=&quot;fieldMoz&quot; id=&quot;uploadFile&quot; 
            onkeydown=&quot;return false;&quot; size=&quot;40&quot; name=&quot;uploadFile&quot;/&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;a onclick=&quot;clearFileInputField('uploadFile_div')&quot; 
                         href=&quot;javascript:noAction();&quot;&amp;gt;Clear&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
Java Script function below looks strange but acts exactly in the way we want:
&lt;pre&gt;&lt;code&gt; 
&amp;lt;script&amp;gt;
function clearFileInputField(tagId) {
    document.getElementById(tagId).innerHTML = 
                    document.getElementById(tagId).innerHTML;
}
&amp;lt;/script&amp;gt;
            
&lt;/code&gt;&lt;/pre&gt;
</content>
 </entry>
 
 <entry>
   <title>Components of success </title>
   <link href="http://gusiev.com/2009/04/success-components"/>
   <updated>2009-04-10T00:00:00+03:00</updated>
   <id>http://gusiev.com/2009/04/success-components</id>
   <content type="html">People from different countries are reaching success in completely different way. And that is good because no matter who you are you can be successful. I heard a lot of success stories and now can summarize - what components are required to build your own success story...&lt;br/&gt;
&lt;!--more--&gt;
&lt;br/&gt;
Here are the components:&lt;br/&gt;
&lt;h5&gt; Hard work &lt;/h5&gt;
That's it. This is may be the most important one. Don't think you can be successful while sitting on the chair and do nothing. 

&lt;h5&gt; Intellect &lt;/h5&gt;
Many people work hard whole life and stay where they are without moving ahead. That is a pity that sometimes hard work is done in the wrong direction. Also try to look several steps ahead and see the perspective of the work you do.

&lt;h5&gt;Risk&lt;/h5&gt;
Risk is a very helpful component. You can go the most reliable way.  But in fact add some risk can seriously improve the result. Do something without risk is impossible because there is always a risk that your work is useless.

&lt;h5&gt;Luck&lt;/h5&gt;
Yes, that's it. This component is very important. All above components are nothing without luck and there is also some good and bad tricks in our way that we can not forecast.

</content>
 </entry>
 
 <entry>
   <title>Brain async</title>
   <link href="http://gusiev.com/2009/04/brain-async"/>
   <updated>2009-04-09T00:00:00+03:00</updated>
   <id>http://gusiev.com/2009/04/brain-async</id>
   <content type="html">Many people came to the question: Why do our brain is having so many gaps: forget important info, quickly change opinion, stuck when doing usual actions, etc? Well, try to look from the different side.&lt;br/&gt;
Our brain is about 4 million of neurons('processors') that works completely async: neural impulses follow though brain without any rules and permissions, every neuron process the information in there own way, no body is able to re implement such a large information processing system. From this point of gap are normal and true solutions and that is being solved by our brain every day is a miracle.
</content>
 </entry>
 
 <entry>
   <title>Technical documentation</title>
   <link href="http://gusiev.com/2009/04/technical-documentation"/>
   <updated>2009-04-08T00:00:00+03:00</updated>
   <id>http://gusiev.com/2009/04/technical-documentation</id>
   <content type="html">There is no one technical book I read more than a half. Is everybody doing this way?
I recommend book authors to explain themselves in more short form.
</content>
 </entry>
 
 <entry>
   <title>Upload files with Selenium IDE</title>
   <link href="http://gusiev.com/2009/04/upload-files-with-selenium-ide"/>
   <updated>2009-04-03T00:00:00+03:00</updated>
   <id>http://gusiev.com/2009/04/upload-files-with-selenium-ide</id>
   <content type="html">I started using selenium about 2 weeks ago. Find it as a very good QA tool.
But after a few successfully written tests I met the problem: Selenium is not able to use file input field. JavaScript permission restriction doesn't allow it.&lt;br/&gt;&lt;br/&gt;

Here is my solution written with &lt;a target=&quot;_blank&quot; href=&quot;http://jinvoke.com&quot;&gt;JInvoke library&lt;/a&gt;: Jinvoke provides the classes to simulate the input to file chooser form.&lt;br/&gt;

&lt;!--more--&gt;
&lt;hr/&gt;
The problem I meat is selenium is stuck when simulate the click on upload file input. I have to launch the concurent thread that do the file name input. Thread code(Note file name should be given in Java format like 'c:/boot.ini):

&lt;pre&gt;&lt;code&gt;import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.*;

/**
 * @author Bogdan Gusiev
 *         Date 29.03.2009
 */
public class FileChooserThread extends Thread {

    public FileChooserThread(String file) {
        super(new FileRunner(file));
    }
}

class FileRunner implements Runnable {

    private String fullName;

    public FileRunner(String fileName) {
        this.fullName = fileName;
    }

    public void run() {
        try {
            Thread.sleep(1000);
            Robot robot = new Robot(); //input simulation class
            for (char c : fullName.toCharArray()) {
                if (c == ':') {
                    robot.keyPress(KeyEvent.VK_SHIFT);
                    robot.keyPress(KeyEvent.VK_SEMICOLON);
                    robot.keyRelease(KeyEvent.VK_SHIFT);
                } else if (c == '/') {
                    robot.keyPress(KeyEvent.VK_BACK_SLASH);
                } else {
                    robot.keyPress(KeyStroke.getKeyStroke(
                                   Character.toUpperCase(c), 0).getKeyCode());
                }
            }
            robot.keyPress(KeyEvent.VK_ENTER);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}&lt;/code&gt;&lt;/pre&gt;
Here is selenium call method:

&lt;pre&gt;&lt;code &gt;protected void chooseFile(String element, String fileName) {
           Number positionLeft = selenium.getElementPositionLeft(element);
           Number positionTop = selenium.getElementPositionTop(element);
           new FileChooserThread(fileName).start(); //launch input thread.
           //this method will held current thread while FileChooser gives the file name
           selenium.clickAt(&quot;file&quot;, positionLeft + &quot;,&quot; + positionTop); 
}&lt;/code&gt;&lt;/pre&gt;
</content>
 </entry>
 
 
</feed>
