<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dmitriy Zaporozhets on Medium]]></title>
        <description><![CDATA[Stories by Dmitriy Zaporozhets on Medium]]></description>
        <link>https://medium.com/@dzaporozhets?source=rss-6e79d7fd7896------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Yc4tWe01x2-NXT-f.jpg</url>
            <title>Stories by Dmitriy Zaporozhets on Medium</title>
            <link>https://medium.com/@dzaporozhets?source=rss-6e79d7fd7896------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 05:37:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dzaporozhets/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Learning Clojure — publish day]]></title>
            <link>https://medium.com/@dzaporozhets/learning-clojure-publish-day-3845e821ed4a?source=rss-6e79d7fd7896------2</link>
            <guid isPermaLink="false">https://medium.com/p/3845e821ed4a</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[clojure]]></category>
            <dc:creator><![CDATA[Dmitriy Zaporozhets]]></dc:creator>
            <pubDate>Mon, 02 Nov 2015 21:53:22 GMT</pubDate>
            <atom:updated>2015-12-02T15:54:21.590Z</atom:updated>
            <content:encoded><![CDATA[<p>My hobby was learning Clojure programming language for last few months. For me it’s always easier to learn when you have proper examples. Like source code of already existing application. At least it gives you idea about how things can work. So I decided to contribute to this “examples” code base. As a result of my hobby I made a simple web application written on Clojure. Its open source and maybe someone will find it useful for learning Clojure.</p><p>Here is a source code — <a href="https://gitlab.com/dzaporozhets/checked/tree/master">https://gitlab.com/dzaporozhets/checked/tree/master</a>.</p><p>Star it if you like it :)</p><p>P.S.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*5t7TTJp1OTP4h7Zf.png" /></figure><p><em>Originally published at </em><a href="http://blog.dzaporozhets.me/build-while-learning/"><em>blog.dzaporozhets.me</em></a><em> on November 2, 2015.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3845e821ed4a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learning Clojure — day 7]]></title>
            <link>https://medium.com/@dzaporozhets/learning-clojure-day-7-a6b11f1ebb2f?source=rss-6e79d7fd7896------2</link>
            <guid isPermaLink="false">https://medium.com/p/a6b11f1ebb2f</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[clojure]]></category>
            <dc:creator><![CDATA[Dmitriy Zaporozhets]]></dc:creator>
            <pubDate>Tue, 14 Jul 2015 17:23:24 GMT</pubDate>
            <atom:updated>2015-12-02T15:55:10.447Z</atom:updated>
            <content:encoded><![CDATA[<p>Since my main focus are web applications I use Clojure for same purpose. That means I need to find common libraries for building basic functionality of any web application: sessions, routing and rendering html</p><p>From what I read so far you should not use monolithic framework like Rails in Clojure world. Instead you build your application from small libraries that serve certain purpose.</p><h3>Compojure: routing for your application</h3><p>Simple and elegant defining of routes in your application</p><pre>(defroutes app-routes<br>    (GET &quot;/&quot; [] &quot;Hello World&quot;)<br>    (POST &quot;/login&quot; [email password] <br>                   (handle-login email password)))</pre><p>Compojure also allows you to generate skeleton for your application with</p><pre>lein new compojure hello-world</pre><p>It will give you few directories and files you need for your journey.</p><p>Repo — <a href="https://github.com/weavejester/compojure">https://github.com/weavejester/compojure</a></p><h3>Ring: http server abstraction</h3><p>It will start your application at port 3000 with command</p><pre>lein ring server</pre><p>And give you a lot of tools for working with request and response.</p><pre>(response &quot;Hello World&quot;)<br><br>=&gt; {:status 200<br>    :headers {}<br>    :body &quot;Hello World&quot;}</pre><p>Repo — <a href="https://github.com/ring-clojure/ring">https://github.com/ring-clojure/ring</a></p><h3>Hiccup: rendering HTML</h3><p>It will allow you to generate html with Clojure code.</p><pre>(html [:div {:class &quot;foo&quot;} &quot;bar&quot;]) <br>=&gt; &quot;&lt;div class=\&quot;foo\&quot;&gt;bar&lt;/div&gt;&quot;</pre><p>With hiccup you don’t need separate template files. In example below we get users from database and render them in rows:</p><pre>(defn users []<br>  (db/all-users))<br><br>(defn user-row [user]<br>  [:li (:name user)])<br><br>(defn users-page []<br>  (html<br>    [:ul<br>     (map user-row users)]))</pre><p>Repo — <a href="https://github.com/weavejester/hiccup">https://github.com/weavejester/hiccup</a></p><h3>Feels good</h3><p>This 3 libraries allows us to make a web application that dynamically render html. It is a bit more work in compare to Rails or other all-in-one frameworks but you will be surprised by how clean and flexible your code looks. WIN.</p><p><em>Originally published at </em><a href="http://blog.dzaporozhets.me/learning-clojure-2/"><em>blog.dzaporozhets.me</em></a><em> on July 14, 2015.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a6b11f1ebb2f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learning Clojure — day 1]]></title>
            <link>https://medium.com/@dzaporozhets/learning-clojure-day-1-6a17466f68f0?source=rss-6e79d7fd7896------2</link>
            <guid isPermaLink="false">https://medium.com/p/6a17466f68f0</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[clojure]]></category>
            <dc:creator><![CDATA[Dmitriy Zaporozhets]]></dc:creator>
            <pubDate>Mon, 06 Jul 2015 10:59:32 GMT</pubDate>
            <atom:updated>2015-12-02T15:50:27.509Z</atom:updated>
            <content:encoded><![CDATA[<p>I spent several years doing Ruby. I still use it for my work. And I am going to use it in future as easy and fast way to build web apps. But its perfect time for something new. Something different. I looked at Elixir but syntax reminds me of Ruby. I looked at Go but I dont want another C-like syntax and 8 spaces indentation. One of my friends was super excited about Clojure. After an hour spent with Clojure I liked it:</p><ul><li>work with JVM -&gt; good</li><li>lisp kind of syntax -&gt; good</li><li>sane indentation -&gt; good</li><li>can be used for building web apps -&gt; awesome</li></ul><h4>What I learned</h4><ol><li>For start you need JVM and Leiningen (kind of Bundler and irb from Ruby world) <a href="http://leiningen.org/">http://leiningen.org/</a></li><li>Heroku has nice tutorial how to build sample web applicaiton using Clojure + Postgres and deploy it.<br><a href="https://devcenter.heroku.com/articles/clojure-web-application">https://devcenter.heroku.com/articles/clojure-web-application</a></li><li>Clojure has awesome website with docs <a href="http://clojuredocs.org/">http://clojuredocs.org/</a></li></ol><p><em>Originally published at </em><a href="http://blog.dzaporozhets.me/learning-clojure-day-1/"><em>blog.dzaporozhets.me</em></a><em> on July 6, 2015.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6a17466f68f0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rails migration, mysql, composite index and remove_column]]></title>
            <link>https://medium.com/@dzaporozhets/rails-migration-mysql-composite-index-and-remove-column-175ddae11506?source=rss-6e79d7fd7896------2</link>
            <guid isPermaLink="false">https://medium.com/p/175ddae11506</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Dmitriy Zaporozhets]]></dc:creator>
            <pubDate>Tue, 23 Dec 2014 09:50:26 GMT</pubDate>
            <atom:updated>2015-12-02T15:49:20.877Z</atom:updated>
            <content:encoded><![CDATA[<p>Today I made a mistake I want to share with you. I need to remove one column from table. And this column was part of composite index with uniqueness property.</p><p>The application I am working on supports 2 db adapters: mysql and postgres. And after I deployed my code — the migration for mysql did not work :(</p><p>What I had:</p><pre># DB table<br>users:  <br>  name: string<br>  domain: string <br>  username: string<br><br># Composite index on 2 fields<br>add_index :users, [:username, :domain]</pre><p>Example of users:</p><pre>&lt;User1 name: &#39;John&#39;, domain: &#39;example.com&#39;, username: &#39;john&#39;&gt;  <br>&lt;User1 name: &#39;Mike&#39;, domain: &#39;example.com&#39;, username: &#39;mike&#39;&gt;</pre><p>So index should not allow duplicate values for combination username+domain.</p><p>How my migration looked like:</p><pre>def up  <br>  remove_column :users, :username<br>  remove_column :users, :domain<br>end</pre><p>After I run it on PostgreSQL it just sucessfully removed column.</p><p>But MySQL was not so friendly to me. When it tried to remove column it fails with next exception:</p><pre>Mysql2::Error: Duplicate entry &#39;example.com&#39; for key &#39;index_users_on_username_and_domain&#39;: ALTER TABLE `users` DROP `username`</pre><p>Looks like when it tried to remove username column it decided to rebuild index using only domain column. But since index is uniq — it does not allow duplicate items.</p><p>How to make it works on mysql then? Just remove index before removing column:</p><pre>def up  <br>  if index_exists?(:users, [:username, :domain])<br>    remove_index :users, [:username, :domain]<br>  end<br><br>  remove_column :users, :username<br>  remove_column :users, :domain<br>end</pre><p>In this case MySQL will be satisfied and migration will work on both databases :)</p><p><em>Originally published at </em><a href="http://blog.dzaporozhets.me/rails-migration-mysql-composite-index-and-remove_column/"><em>blog.dzaporozhets.me</em></a><em> on December 23, 2014.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=175ddae11506" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rails 4: STI belongs to polymorphic STI]]></title>
            <link>https://medium.com/@dzaporozhets/rails-4-sti-belongs-to-polymorphic-sti-a0052db21d8e?source=rss-6e79d7fd7896------2</link>
            <guid isPermaLink="false">https://medium.com/p/a0052db21d8e</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Dmitriy Zaporozhets]]></dc:creator>
            <pubDate>Tue, 16 Sep 2014 15:02:04 GMT</pubDate>
            <atom:updated>2015-12-02T15:44:15.472Z</atom:updated>
            <content:encoded><![CDATA[<p>Its hard to find a good example how to use single table inheritance (STI) and polymorphic relation combined in Rails 4. I did not find one when I need it. So I decided to share with you my working example. It does not explain anything but it gives you a working code you may want to investigate in solving your own task.</p><p>What we have:</p><ul><li>Group — AR model inherited from other model (STI)</li><li>Member — AR model with polymorphic association to any source.</li><li>GroupMember — inherited from Member (STI). Belongs to Group via polymorphic source relation.</li></ul><p>Why all this?</p><p>I want one model/db_table Member I can re-use for group, project, team membership.</p><ul><li>I want GroupMember, ProjectMember classes inherited from Member for prettier and cleaner code.</li></ul><p>So I started from this non-working code:</p><pre># Table: members<br># source_id: integer<br># source_type: string<br>class Member &lt; ActiveRecord::Base  <br>  belongs_to :source, polymorphic: true<br>end<br><br>class GroupMember &lt; Member  <br>  # we want to re-use existing source association.<br>  belongs_to :group <br>end<br><br>class Group &lt; Namespace  <br>  # I want query only GroupMembers that belongs to this group<br>  has_many :group_members<br>end</pre><p>And ended with this working code:</p><pre>class Member &lt; ActiveRecord::Base  <br>  belongs_to :source, polymorphic: true<br>end<br><br>class GroupMember &lt; Member  <br>  belongs_to :group, class_name: &#39;Group&#39;, foreign_key: &#39;source_id&#39;<br>end<br><br>class Group &lt; Namespace  <br>  has_many :group_members, as: :source, class_name: &#39;GroupMember&#39;<br>end</pre><p>One interesting thing you may want to know: Rails puts the Parent STI class name in source_type. In our case it will be Namespace instead of Group because Group inherited from Namespace</p><pre>group = Group.first  <br>member = GroupMember.new  <br>member.source = group  <br>puts member.source_type  <br># =&gt; Namespace</pre><p><em>Originally published at </em><a href="http://blog.dzaporozhets.me/rails-4-sti-belongs-to-polymorphic-sti/"><em>blog.dzaporozhets.me</em></a><em> on September 16, 2014.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a0052db21d8e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rails: move some logic from models]]></title>
            <link>https://medium.com/@dzaporozhets/rails-move-some-logic-from-models-684fb43ad160?source=rss-6e79d7fd7896------2</link>
            <guid isPermaLink="false">https://medium.com/p/684fb43ad160</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Dmitriy Zaporozhets]]></dc:creator>
            <pubDate>Mon, 16 Jun 2014 19:08:11 GMT</pubDate>
            <atom:updated>2018-06-01T09:50:42.251Z</atom:updated>
            <content:encoded><![CDATA[<p>If you are Ruby on Rails developer you may know about tiny controllers and fat models principle. One negative effect of this is models quickly becomes a huge dump with a lot of logic that should not be here. If you saw a model with 1k lines, big amount of callbacks, validations etc you probably know what I mean.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*1U2jZ2nYgbKlTgeW.jpg" /></figure><h4>Example</h4><p>Let’s make some example of typical rails app:</p><pre>class BooksController  <br>  def create<br>    @book = Book.create(params[:book])<br>  end<br>end<br><br>class Book &lt; ActiveRecord::Base  <br>  before_create :get_papers<br>  after_create :print<br>  after_save :notify_readers, if: -&gt; { Config.notifications.enabled? }<br><br>  def notify_readers<br>    Notification.new.<br>      notify_about_new_book(self, self.readers)<br>  end<br>end</pre><h4>Disadvantages</h4><p>It has few disadvantages from my point of view:</p><ul><li>We reference a lot of other classes/methods from themodel.</li><li>It’s hard to keep in mind chain of actions during object creation. Especially when you get more complex cases.</li><li>In addition to validators, associations, all possible instance methods etc we have bigger class.</li><li>At some point, you need to to create Book without notification in another place. Here all ugly if..else appears.</li></ul><p>Maybe people afraid of creating new classes or don’t know about such possibility. Because usually, I see all application code divided between controllers and models :(</p><h4>Alternative way</h4><p>Lets extract object creation in separate class. We will call it BookCreateService. It will have only single public method execute that will do exactly what we need — create a book. But with all side actions.</p><pre>class BooksController  <br>  def create<br>    @book = BookCreateService.execute(params[:book])<br>  end<br>end<br><br>class Book &lt; ActiveRecord::Base  <br>end<br><br>class BookCreateService  <br>  def self.execute(book_params)<br>    @book = Book.new(book_params)<br><br>    if @book.get_papers &amp;&amp; @book.save<br>      @book.print<br><br>      if Config.notifications.enabled?<br>         Notification.new.notify_about_new_book(book, book.readers)<br>      end<br>    end<br><br>    @book<br>  end<br>end</pre><p>So we get rid of all callbacks and class references from model leaving it clean and simple. Of course, it requires from us writing a separate class for creating a book and for updating a book, and maybe even for deleting a book too.</p><p>But its cool because:</p><ul><li>you see actual create/update process in one method instead of collecting all possible callbacks in your mind</li><li>it makes you model smaller</li><li>if you need to create a model in another place of code without print and notification — you just do Book.create</li><li>your tests are faster because you execute print and get_papers only when you test Service class. Otherwise, all your callbacks executed in every place where you create FactoryGirl.create(:book)</li></ul><p>Where to put such classes? I think it ok to use app/services/</p><ul><li>fat models are ok if they are small and thin :)</li><li>callbacks are harder to understand vs coherently execution of methods</li><li>use classes. You are not limited to models and controllers.</li></ul><h4>Used in a real world?</h4><p>Actually yes. Checkout GitLab source code for good(almost) example of services usage — <a href="https://github.com/gitlabhq/gitlabhq/tree/master/app/services">https://github.com/gitlabhq/gitlabhq/tree/master/app/services</a></p><p><em>Originally published at </em><a href="http://blog.dzaporozhets.me/rails-move-some-logic-from-models/"><em>blog.dzaporozhets.me</em></a><em> on June 16, 2014.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=684fb43ad160" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>