<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://blog.neerajk.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://blog.neerajk.com/" rel="alternate" type="text/html" /><updated>2026-12-03T14:19:12+00:00</updated><id>https://blog.neerajk.com/feed.xml</id><title type="html">Neeraj Kumar</title><subtitle>ReactJs, NodeJs, Ruby on Rails Expert, Architect &amp; Evangelist | Full Stack Web Developer | Programmer | Open Source Contributor | RubyConf Speaker</subtitle><entry><title type="html"></title><link href="https://blog.neerajk.com/active-resource-changeable/" rel="alternate" type="text/html" title="" /><published>2016-12-06T00:00:00+00:00</published><updated>2016-12-06T00:00:00+00:00</updated><id>https://blog.neerajk.com/active-resource-changeable</id><content type="html" xml:base="https://blog.neerajk.com/active-resource-changeable/"><![CDATA[<p>As <a href="https://github.com/rails/rails/tree/master/activerecord">ActiveRecord</a> provides you to get all the changes of your unsaved object, <a href="https://rubygems.org/gems/active_resource_changeable">active_resource_changeable</a> gem also serves the same purpose by calling its <code class="language-plaintext highlighter-rouge">changes</code> method.</p>

<h2 id="installation">Installation</h2>

<p>Add this line to your application’s Gemfile:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="n">gem</span> <span class="s1">'active_resource_changeable'</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>And then execute:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>$ bundle
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Or install it yourself as:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>$ gem install active_resource_changeable
</pre></td></tr></tbody></table></code></pre></div></div>

<h2 id="usage">Usage</h2>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="kp">include</span> <span class="no">ActiveResourceChangeable</span>
</pre></td></tr></tbody></table></code></pre></div></div>
<p>into your model and then you can call changes method on its objects. For example.</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">User</span> <span class="o">&lt;</span> <span class="no">ActiveResource</span><span class="o">::</span><span class="no">Base</span>
  <span class="nb">self</span><span class="p">.</span><span class="nf">site</span> <span class="o">=</span> <span class="s2">""</span>

  <span class="kp">include</span> <span class="no">ActiveResourceChangeable</span>

<span class="k">end</span>

<span class="n">user</span> <span class="o">=</span> <span class="no">User</span><span class="p">.</span><span class="nf">find</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="n">user</span><span class="p">.</span><span class="nf">firstname</span> <span class="o">=</span> <span class="s2">"Neeraj1"</span>
<span class="n">user</span><span class="p">.</span><span class="nf">lastname</span> <span class="o">=</span> <span class="s2">"Kumar1"</span>
<span class="n">user</span><span class="p">.</span><span class="nf">email</span> <span class="o">=</span> <span class="s2">"neeraj1.kumar1@mailinator.com"</span>
<span class="n">user</span><span class="p">.</span><span class="nf">username</span> <span class="o">=</span> <span class="s2">"neeraj1"</span>

<span class="n">user</span><span class="p">.</span><span class="nf">changes</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"firstname"</span><span class="o">=&gt;</span><span class="p">[</span><span class="s2">"Neeraj"</span><span class="p">,</span> <span class="s2">"Neeraj1"</span><span class="p">],</span> <span class="s2">"lastname"</span><span class="o">=&gt;</span><span class="p">[</span><span class="s2">"Kumar"</span><span class="p">,</span> <span class="s2">"Kumar1"</span><span class="p">],</span> <span class="s2">"username=&gt;["</span><span class="n">neeraj</span><span class="s2">", "</span><span class="n">neeraj1</span><span class="p">],</span> <span class="s2">"email"</span><span class="o">=&gt;</span><span class="p">[</span><span class="s2">"neeraj.kumar@mailinator.com"</span><span class="p">,</span> <span class="s2">"neeraj1.kumar@mailinator.com"</span><span class="p">]}</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><a href="https://rubygems.org/gems/active_resource_changeable">ActiveResourceChangeable</a> works for nested ActiveResource objects too and gives the result something like</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="p">{</span><span class="s2">"companyName"</span><span class="o">=&gt;</span><span class="p">[</span><span class="s2">"Skipped Levels 034"</span><span class="p">,</span> <span class="s2">"ABC Company"</span><span class="p">],</span>
 <span class="s2">"address"</span><span class="o">=&gt;</span><span class="p">{</span><span class="s2">""</span><span class="o">=&gt;</span><span class="p">{</span><span class="s2">"line1"</span><span class="o">=&gt;</span><span class="p">[</span><span class="s2">"1234 Skipped Levels 034"</span><span class="p">,</span> <span class="s2">"line1"</span><span class="p">]},</span> <span class="s2">"countryType"</span><span class="o">=&gt;</span><span class="p">{</span><span class="s2">"code"</span><span class="o">=&gt;</span><span class="p">[</span><span class="s2">"DEU"</span><span class="p">,</span> <span class="s2">"IND"</span><span class="p">]}},</span>
 <span class="s2">"countryType"</span><span class="o">=&gt;</span><span class="p">{</span><span class="s2">""</span><span class="o">=&gt;</span><span class="p">{</span><span class="s2">"code"</span><span class="o">=&gt;</span><span class="p">[</span><span class="s2">"DEU"</span><span class="p">,</span> <span class="s2">"IND"</span><span class="p">]}},</span>
 <span class="s2">"statusType"</span><span class="o">=&gt;</span><span class="p">{</span><span class="s2">""</span><span class="o">=&gt;</span><span class="p">{</span><span class="s2">"code"</span><span class="o">=&gt;</span><span class="p">[</span><span class="s2">"A"</span><span class="p">,</span> <span class="s2">"B"</span><span class="p">]}}}</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h2 id="development">Development</h2>

<p>After checking out the repo, run <code class="language-plaintext highlighter-rouge">bin/setup</code> to install dependencies. Then, run <code class="language-plaintext highlighter-rouge">rake test</code> to run the tests. You can also run <code class="language-plaintext highlighter-rouge">bin/console</code> for an interactive prompt that will allow you to experiment.</p>

<h2 id="contributing">Contributing</h2>

<p>Bug reports and pull requests are welcome on GitHub at https://github.com/neerajkumar/active_resource_changeable. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the <a href="http://contributor-covenant.org">Contributor Covenant</a> code of conduct.</p>]]></content><author><name>neeraj</name></author><category term="Ruby" /><category term="Rails" /><category term="ActiveRecord" /><category term="ActiveResource" /><category term="ActiveResourceChangeable" /><category term="ruby" /><category term="rails" /><category term="ActiveRecord" /><category term="ActiveResource" /><category term="ActiveResourceChangeable" /><summary type="html"><![CDATA[ActiveRecord and ActiveResource are homogeneous in nature. But ActiveResource is reserved for APIs. ActiveResourceChangeable follows ActiveRecord's pattern and provides the changes of different attributes of ActiveResource object.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blog.neerajk.com/assets/images/activerecord.jpeg" /><media:content medium="image" url="https://blog.neerajk.com/assets/images/activerecord.jpeg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Associate with ActiveResourceAssociatable</title><link href="https://blog.neerajk.com/associate-with-active-resource-associatable/" rel="alternate" type="text/html" title="Associate with ActiveResourceAssociatable" /><published>2016-08-23T00:00:00+00:00</published><updated>2016-08-23T00:00:00+00:00</updated><id>https://blog.neerajk.com/associate-with-active-resource-associatable</id><content type="html" xml:base="https://blog.neerajk.com/associate-with-active-resource-associatable/"><![CDATA[<p>A communication of a web app with others is not a surprise, but it is the demand of mobile world’s era. Because new apps want to maintain a centralized DB which could be shared with other apps very easily and very frequently. Recently, I also worked on a similar distributed web app which provided me an idea to implement a rubygem.</p>

<p>ActiveResource is a ruby library which acts same as ActiveRecord, but perform operations over API. <a href="https://rubygems.org/gems/active_resource_associatable">ActiveResourceAssociatable</a> is also a ruby library which consolidates same kind of associations methods as ActiveRecord provides. However it could be achieved by own customized methods, but <a href="https://rubygems.org/gems/active_resource_associatable">ActiveResourceAssociatable</a> provides smarter way of accessing your ActiveRecord and ActiveResource methods. <a href="https://rubygems.org/gems/active_resource_associatable">ActiveResourceAssociatable</a> comes with some options too which will make you more easy to use this gem.</p>

<h1 id="activeresourceassociatable">ActiveResourceAssociatable</h1>
<hr />
<p>This gem is used to establish the association between your ActiveResource class to ActiveRecord class. Why? Because there is no other ruby library to provide such functionality. Its simple and taking care of all options, scopes etc with your associations.</p>

<h2 id="installation">Installation</h2>
<hr />
<p>Add this line to your application’s Gemfile:</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="n">gem</span> <span class="s1">'active_resource_associatable'</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>And then execute:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>$ bundle
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Or install it yourself as:</p>

<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>$ gem install active_resource_associatable
</pre></td></tr></tbody></table></code></pre></div></div>

<h2 id="usage">Usage</h2>
<hr />
<p><code class="language-plaintext highlighter-rouge">include ActiveResourceAssociatable</code> in your ActiveRecord or ActiveResource model. Now you can easily call the methods of either of your ActiveRecord or ActiveResource models. This gem facilitates you to call ActiveResource objects in the same way as you call in any of your ActiveRecord model. It supports calls <code class="language-plaintext highlighter-rouge">has_one_activeresource</code>, <code class="language-plaintext highlighter-rouge">belongs_to_activeresource</code>, <code class="language-plaintext highlighter-rouge">has_many_activeresources</code>, <code class="language-plaintext highlighter-rouge">has_and_belongs_to_many_activeresources</code> and <code class="language-plaintext highlighter-rouge">has_many_through_activeresources</code>.</p>

<h4 id="has-one-activeresource">Has One ActiveResource</h4>
<p>Account is non ActiveResource class which is maintaining has one relationship with ActiveRecord class User.</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">User</span> <span class="o">&lt;</span> <span class="no">ActiveResource</span><span class="o">::</span><span class="no">Base</span>
  <span class="nb">self</span><span class="p">.</span><span class="nf">site</span> <span class="o">=</span> <span class="s2">""</span>

  <span class="kp">include</span> <span class="no">ActiveResourceAssociatable</span>

  <span class="n">belongs_to_activeresource</span> <span class="ss">:account</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">Account</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
  <span class="kp">include</span> <span class="no">ActiveResourceAssociatable</span>

  <span class="n">has_one_activeresource</span> <span class="ss">:user</span><span class="p">,</span> <span class="ss">class_name: </span><span class="s1">'UserResource'</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Whereas Reader is a ActiveResource class, but maintaining has one relationship with Account.</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">Reader</span> <span class="o">&lt;</span> <span class="no">ActiveResource</span><span class="o">::</span><span class="no">Base</span>
  <span class="nb">self</span><span class="p">.</span><span class="nf">site</span> <span class="o">=</span> <span class="s2">""</span>

  <span class="kp">include</span> <span class="no">ActiveResourceAssociatable</span>

  <span class="n">has_one_activeresource</span> <span class="ss">:account</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">Account</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
  <span class="kp">include</span> <span class="no">ActiveResourceAssociatable</span>

  <span class="n">belongs_to_activeresource</span> <span class="ss">:reader</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h4 id="belongs-to-activeresource">Belongs To ActiveResource</h4>
<p>The same example which explained above in Has One ActiveResource section could be an example of belongs_to_activeresource.
<code class="language-plaintext highlighter-rouge">belongs_to_activeresource</code> can also be applied in case of <code class="language-plaintext highlighter-rouge">has_many_activeresources</code> associations.</p>

<h4 id="has-many-activeresources">Has Many ActiveResources</h4>
<p>User is ActiveResource class which maintains the has many relationships with non ActiveResource class Book.</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">UserResource</span> <span class="o">&lt;</span> <span class="no">ActiveResource</span><span class="o">::</span><span class="no">Base</span>
  <span class="nb">self</span><span class="p">.</span><span class="nf">site</span> <span class="o">=</span> <span class="s2">""</span>

  <span class="kp">include</span> <span class="no">ActiveResourceAssociatable</span>

  <span class="n">has_many_activeresources</span> <span class="ss">:books</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">Book</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
  <span class="kp">include</span> <span class="no">ActiveResourceAssociatable</span>

  <span class="n">belongs_to_activeresource</span> <span class="ss">:user</span><span class="p">,</span> <span class="ss">class_name: </span><span class="s1">'UserResource'</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Now you can easily call <code class="language-plaintext highlighter-rouge">user.books</code> which will return the array of objects of non ActiveResource class Book.</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">Library</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
  <span class="kp">include</span> <span class="no">ActiveResourceAssociatable</span>

  <span class="n">has_many_activeresources</span> <span class="ss">:readers</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p><code class="language-plaintext highlighter-rouge">has_many_activeresources</code> also comes with options <code class="language-plaintext highlighter-rouge">class_name</code>.</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">Reader</span> <span class="o">&lt;</span> <span class="no">ActiveResource</span><span class="o">::</span><span class="no">Base</span>
  <span class="nb">self</span><span class="p">.</span><span class="nf">site</span> <span class="o">=</span> <span class="s2">""</span>

  <span class="kp">include</span> <span class="no">ActiveResourceAssociatable</span>

  <span class="n">has_many_activeresources</span> <span class="ss">:studying_materials</span><span class="p">,</span> <span class="ss">class_name: </span><span class="s2">"Book"</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">Book</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
  <span class="kp">include</span> <span class="no">ActiveResourceAssociatable</span>

  <span class="n">belongs_to_activeresource</span> <span class="ss">:reader</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Now you can call <code class="language-plaintext highlighter-rouge">reader.studying_materials</code> which will return the array of objects of non ActiveResource class Book.</p>

<p>User can also call books using a specific foreign key. For Example,</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">UserResource</span> <span class="o">&lt;</span> <span class="no">ActiveResource</span><span class="o">::</span><span class="no">Base</span>
  <span class="nb">self</span><span class="p">.</span><span class="nf">site</span> <span class="o">=</span> <span class="s2">""</span>

  <span class="kp">include</span> <span class="no">ActiveResourceAssociatable</span>

  <span class="n">has_many_activeresources</span> <span class="ss">:books</span><span class="p">,</span> <span class="ss">foreign_key: </span><span class="s2">"user_id"</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h4 id="has-and-belongs-to-many-activeresources">Has And Belongs To Many ActiveResources</h4>
<p><code class="language-plaintext highlighter-rouge">active_resource_associatable</code> provides many to many associations between ActiveResource and ActiveRecord. 
User class is a ActiveResource class and can have many images. Likewise, Image which is a ActiveRecord class can have many users.</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">User</span> <span class="o">&lt;</span> <span class="no">ActiveResource</span><span class="o">::</span><span class="no">Base</span>
  <span class="nb">self</span><span class="p">.</span><span class="nf">site</span> <span class="o">=</span> <span class="s2">""</span>

  <span class="kp">include</span> <span class="no">ActiveResourceAssociatable</span>

  <span class="n">has_and_belongs_to_many_activeresources</span> <span class="ss">:images</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">Image</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
  <span class="kp">include</span> <span class="no">ActiveResourceAssociatable</span>

  <span class="n">has_and_belongs_to_many_activeresources</span> <span class="ss">:users</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>You can use class_name and foreign_key as in <code class="language-plaintext highlighter-rouge">has_many_activeresources</code>.</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">UserResource</span> <span class="o">&lt;</span> <span class="no">ActiveResource</span><span class="o">::</span><span class="no">Base</span>
  <span class="nb">self</span><span class="p">.</span><span class="nf">site</span> <span class="o">=</span> <span class="s2">""</span>

  <span class="kp">include</span> <span class="no">ActiveResourceAssociatable</span>

  <span class="n">has_and_belongs_to_many_activeresources</span> <span class="ss">:images</span><span class="p">,</span> <span class="ss">foreign_key: </span><span class="s2">"user_id"</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">Image</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
  <span class="kp">include</span> <span class="no">ActiveResourceAssociatable</span>

  <span class="n">has_and_belongs_to_many_activeresources</span> <span class="ss">:users</span><span class="p">,</span> <span class="ss">class_name: </span><span class="s1">'UserResource'</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h4 id="has-many-through-activeresources">Has Many Through ActiveResources</h4>
<p><code class="language-plaintext highlighter-rouge">has_many_through_activeresources</code> can also be achieved using <code class="language-plaintext highlighter-rouge">active_resource_associatable</code>. User can have many friends through friendships. Similarly, a friend can also have many users through friendships.</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">UserResource</span> <span class="o">&lt;</span> <span class="no">ActiveResource</span><span class="o">::</span><span class="no">Base</span>
  <span class="nb">self</span><span class="p">.</span><span class="nf">site</span> <span class="o">=</span> <span class="s2">""</span>

  <span class="kp">include</span> <span class="no">ActiveResourceAssociatable</span>

  <span class="n">has_many_through_activeresources</span> <span class="ss">:friends</span><span class="p">,</span> <span class="ss">through: :friendships</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">Friend</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>

  <span class="kp">include</span> <span class="no">ActiveResourceAssociatable</span>

  <span class="n">has_many_through_activeresources</span> <span class="ss">:users</span><span class="p">,</span> <span class="ss">through: :friendships</span><span class="p">,</span> <span class="ss">class_name: </span><span class="s1">'UserResource'</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
</pre></td><td class="rouge-code"><pre><span class="k">class</span> <span class="nc">Friendship</span> <span class="o">&lt;</span> <span class="no">ActiveRecord</span><span class="o">::</span><span class="no">Base</span>
  <span class="kp">include</span> <span class="no">ActiveResourceAssociatable</span>
  
  <span class="n">belongs_to_activeresource</span> <span class="ss">:user</span>
  <span class="n">belongs_to</span> <span class="ss">:friend</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h2 id="conclusion">Conclusion</h2>
<hr />
<p>Every ruby or rails programmer should try this rubygem once. I believe you will really like it. Please don’t forget to share your valuable feedback.</p>]]></content><author><name>neeraj</name></author><category term="Ruby" /><category term="Rails" /><category term="ActiveRecord" /><category term="ActiveResource" /><category term="ActiveResourceAssociatable" /><category term="ruby" /><category term="rails" /><category term="ActiveRecord" /><category term="ActiveResource" /><category term="ActiveResourceAssociatable" /><summary type="html"><![CDATA[ActiveRecord and ActiveResource are homogeneous in nature. But ActiveResource is reserved for APIs. ActiveResourceAssociatable follows ActiveRecord's pattern and provides similar association pattern. However it could be achieved by customized methods in models, but ActiveResourceAssociatable provides smarter way. Because world is expanding with smarter folks and a smart folk adopts smarter way of working...]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blog.neerajk.com/assets/images/activeresource.png" /><media:content medium="image" url="https://blog.neerajk.com/assets/images/activeresource.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Roda versus Rails</title><link href="https://blog.neerajk.com/roda-versus-rails/" rel="alternate" type="text/html" title="Roda versus Rails" /><published>2016-07-31T00:00:00+00:00</published><updated>2016-07-31T00:00:00+00:00</updated><id>https://blog.neerajk.com/roda-versus-rails</id><content type="html" xml:base="https://blog.neerajk.com/roda-versus-rails/"><![CDATA[<p>Few days back, I heard about <a href="http://roda.jeremyevans.net/">Roda</a> which could suppose to be a replacement of <a href="guides.rubyonrails.org">Rails</a>. <a href="http://roda.jeremyevans.net/">Roda</a> is a routing tree web toolkit, designed for building fast and maintainable web applications in ruby. The creator of <a href="http://roda.jeremyevans.net/">Roda</a> is <a href="https://github.com/jeremyevans">Jeremy Evans</a>. <code class="language-plaintext highlighter-rouge">Rails is dead</code> we hear it every years and it is more widespread after twitter’s jump from <a href="guides.rubyonrails.org">Rails</a> to Scala.</p>

<p>According to <a href="https://github.com/jeremyevans">Jeremy Evans</a>, <a href="http://roda.jeremyevans.net/">Roda</a> provides Simplicity, Reliability, Extensibility and Performance. <a href="http://roda.jeremyevans.net/">Roda</a> has low per-request overhead, and the use of a routing tree and intelligent caching of internal datastructures makes it significantly faster than popular ruby web frameworks. But as per my opinion, Rails is good and getting more mature, stable and popularity every year. Here I will compare these two over some points.</p>

<h3 id="prerequisites">Prerequisites</h3>
<hr />
<p>I am assuming that you have some basic knowledge of Roda and have built at least one app in Roda. Otherwise, please have a look at http://roda.jeremyevans.net.</p>

<h3 id="comparison">Comparison</h3>
<hr />

<div class="row">
	<div class="col-md-6">
		<a href="guides.rubyonrails.org">Rails</a> is a stable framework which is very old and already having a large community support. Also it supports lot of different rubygems.
	</div>
	<div class="col-md-6">
		Most of rubygems are independent ruby libraries. Therefore all rubygems which are not tightly tied with Rails and activerecord can be used with <a href="http://roda.jeremyevans.net">Roda</a> too. Additionally, <a href="http://roda.jeremyevans.net">Roda</a> has its own compatible rubygems e.g. roda-i18n, newrelic-roda, roda-basic-auth, roda-rest_api, rodauth etc. But still these are not having proper documentation.
	</div>
</div>
<div class="row">
	<div class="col-md-6">
		<a href="guides.rubyonrails.org">Rails</a> is a stable framework which is very old and already having a large community support. Also it supports lot of different rubygems.
	</div>
	<div class="col-md-6">
		Most of rubygems are independent ruby libraries. Therefore all rubygems which are not tightly tied with Rails and activerecord can be used with <a href="http://roda.jeremyevans.net">Roda</a> too. Additionally, <a href="http://roda.jeremyevans.net">Roda</a> has its own compatible rubygems e.g. roda-i18n, newrelic-roda, roda-basic-auth, roda-rest_api, rodauth etc. But still these are not having proper documentation.
	</div>
</div>
<div class="row">
	<div class="col-md-6">
		<a href="https://github.com/plataformatec/devise">devise</a> is a rubygem which we use for our authentication purpose. It provides not only the authentication, but it also provides lot of flexibility while integration. It also opens the scope of integration of authentication of different social networking e.g. facebook, twitter, google etc.
	</div>
	<div class="col-md-6">
		<a href="http://roda.jeremyevans.net">Roda</a> also comes with <a href="https://github.com/beno/roda-auth">roda-auth</a> which provides simple authentication. It contains omniauth, but not maintaining any documentation to implement in <a href="http://roda.jeremyevans.net">Roda</a> app.
	</div>
</div>
<div class="row">
	<div class="col-md-6">
		The size of core contributors (with at least 3 commits) is more than 100. | The size of core contributors (with at least 3 commits) is significantly lesser. 
	</div>
	<div class="col-md-6">
		It is good for mid level as well as bigger application. | As compared over http://roda.jeremyevans.net/why.html with Sinatra, <a href="http://roda.jeremyevans.net">Roda</a> is good for small applications.
	</div>
</div>
<div class="row">
	<div class="col-md-6">
		It is good for mid level as well as bigger application.
	</div>
	<div class="col-md-6">
		As compared over http://roda.jeremyevans.net/why.html with Sinatra, <a href="http://roda.jeremyevans.net">Roda</a> is good for small applications.
	</div>
</div>
<div class="row">
	<div class="col-md-6">
		People are having more interest to search about <a href="guides.rubyonrails.org">Rails</a>.
	</div>
	<div class="col-md-6">
		Interest on <a href="http://roda.jeremyevans.net">Roda</a> is significantly less.
	</div>
</div>
<p><br />
<img src="/assets/images/roda-vs-rails.png" alt="Interest over time of Roda and Ruby on Rails" width="740" height="338" class="img-responsive" /></p>

<h3 id="conclusion">Conclusion</h3>
<hr />
<p>I think every rails programmer should try <a href="http://roda.jeremyevans.net/">Roda</a> once. It will provide you a better understanding and knowledge about different rubygems. You can get the opportunity to dig deeper into ruby libraries and their usage too.</p>]]></content><author><name>neeraj</name></author><category term="Ruby" /><category term="Rails" /><category term="Roda" /><category term="ruby" /><category term="rails" /><category term="Roda" /><category term="jeremy evans" /><summary type="html"><![CDATA[Rails is old web framework with MVC architecture. It is more stable, mature and popular. Roda is relatively new and claims to be a substitute of Rails. Is it really better than Rails?]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blog.neerajk.com/assets/images/roda.svg" /><media:content medium="image" url="https://blog.neerajk.com/assets/images/roda.svg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Is ActiveSupport::Concern really Another Concern?</title><link href="https://blog.neerajk.com/is-active-support-concern-really-another-concern/" rel="alternate" type="text/html" title="Is ActiveSupport::Concern really Another Concern?" /><published>2014-11-26T00:00:00+00:00</published><updated>2014-11-26T00:00:00+00:00</updated><id>https://blog.neerajk.com/is-active-support-concern-really-another-concern</id><content type="html" xml:base="https://blog.neerajk.com/is-active-support-concern-really-another-concern/"><![CDATA[<p>In Ruby world, <a href="guides.rubyonrails.org">Rails</a> is a well known web-framework and <a href="https://github.com/rails/rails/tree/master/activesupport">ActiveSupport</a> is an integrated component. <a href="https://github.com/rails/rails/tree/master/activesupport">ActiveSupport</a> is a ruby gem which comes with rails by default. But, we can also use this library or gem separately. <a href="https://github.com/rails/rails/tree/master/activesupport">ActiveSupport</a> is very easy to understand upto the extend that if you are planning to read the source code of rails, then start with <a href="https://github.com/rails/rails/tree/master/activesupport">ActiveSupport</a>. So, today I am going to throw some light on <a href="https://github.com/rails/rails/blob/master/activesupport/lib/active_support/concern.rb">ActiveSupport::Concern</a> which is also one of the component of <a href="https://github.com/rails/rails/tree/master/activesupport">ActiveSupport</a>. Is ActiveSupport::Concern really Another Concern?</p>

<p>This post is for those who are new to ActiveSupport. Who constantly do utility work like encoding, or decoding JSON, generating random number, number to currency conversion etc. But never got the chance to find out the reason behind this. Have you ever looked into the source code of any rubygem where you found the statement like <code class="language-plaintext highlighter-rouge">extend ActiveSupport::Concern</code>. If yes, are you familiar with this statement? Why do we use this? Trust me <a href="https://github.com/rails/rails/blob/master/activesupport/lib/active_support/concern.rb">ActiveSupport::Concern</a> is not a concern, but to make your life easy.</p>

<p>ActiveSupport::Concern is useful in mixin or modules. It is more useful whenever you are building your own rubygem.</p>

<h3 id="without-activesupportconcern">Without ActiveSupport::Concern</h3>
<hr />

<p>We can definitely achieve our goals without ActiveSupport::Concern. But it will insist you to write more code and introduce more complexity in your code base.</p>

<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="rouge-code"><pre><span class="c1"># my Foo mixin or module</span>
<span class="k">module</span> <span class="nn">Foo</span>
  <span class="k">module</span> <span class="nn">ClassMethods</span>
    <span class="k">def</span> <span class="nf">class_method</span>
      <span class="nb">puts</span> <span class="s2">"I am inside bar method"</span>
    <span class="k">end</span>
  <span class="k">end</span>

  <span class="k">module</span> <span class="nn">InstanceMethods</span>
    <span class="k">def</span> <span class="nf">instance_method</span>
      <span class="nb">puts</span> <span class="s2">"I am inside foo method"</span>
    <span class="k">end</span>
  <span class="k">end</span>

  <span class="k">def</span> <span class="nc">self</span><span class="o">.</span><span class="nf">included</span><span class="p">(</span><span class="n">base</span><span class="p">)</span>
    <span class="n">base</span><span class="p">.</span><span class="nf">send</span> <span class="ss">:include</span><span class="p">,</span> <span class="no">InstanceMethods</span>
    <span class="n">base</span><span class="p">.</span><span class="nf">send</span> <span class="ss">:extend</span><span class="p">,</span> <span class="no">ClassMethods</span>
    <span class="nb">puts</span> <span class="s2">"FooBar included"</span>
  <span class="k">end</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="c1"># my Bar class</span>
<span class="k">class</span> <span class="nc">Bar</span>
  <span class="kp">include</span> <span class="no">Foo</span>
<span class="k">end</span>

<span class="no">Bar</span><span class="p">.</span><span class="nf">class_method</span>
<span class="no">Bar</span><span class="p">.</span><span class="nf">new</span><span class="p">.</span><span class="nf">instance_method</span>
</pre></td></tr></tbody></table></code></pre></div></div>
<p>Here I created a Foo module and included this module into my Bar class. Whenever <code class="language-plaintext highlighter-rouge">include Foo</code> will execute, it will trigger my overridden method <code class="language-plaintext highlighter-rouge">self.included(base)</code> method in Foo module. base is the reference of the class in which I have included my module. And then Inside <code class="language-plaintext highlighter-rouge">self.included(base)</code>, I am manually including and extending my ClassMethods and InstanceMethonds.</p>

<p>But I could say that this is not a good code. Why? Because first, I am over-ridding included method which is not good. Second, it is decreasing the readability of code. People have to struggle with code in order to understand the whole process.</p>

<h3 id="with-activesupportconcern">With ActiveSupport::Concern</h3>
<p>Now the same code could be written with the help of ActiveSupport::Concern. Trust me it will reduce the complexity and increase the readability.</p>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="rouge-code"><pre><span class="c1"># my Foo mixin or module</span>
<span class="nb">require</span> <span class="s1">'active_support/concern.rb'</span>

<span class="k">module</span> <span class="nn">Foo</span>
  <span class="kp">extend</span> <span class="no">ActiveSupport</span><span class="o">::</span><span class="no">Concern</span>

  <span class="k">module</span> <span class="nn">ClassMethods</span>
    <span class="k">def</span> <span class="nf">class_method</span>
      <span class="nb">puts</span> <span class="s2">"I am inside bar method"</span>
    <span class="k">end</span>
  <span class="k">end</span>

  <span class="k">def</span> <span class="nf">instance_method</span>
    <span class="nb">puts</span> <span class="s2">"I am inside foo method"</span>
  <span class="k">end</span>

  <span class="n">included</span> <span class="k">do</span>
    <span class="nb">puts</span> <span class="s2">"FooBar included"</span>
  <span class="k">end</span>
<span class="k">end</span>
</pre></td></tr></tbody></table></code></pre></div></div>
<div class="language-ruby highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="c1"># my Bar class</span>
<span class="k">class</span> <span class="nc">Bar</span>
  <span class="kp">include</span> <span class="no">Foo</span>
<span class="k">end</span>

<span class="no">Bar</span><span class="p">.</span><span class="nf">class_method</span>
<span class="no">Bar</span><span class="p">.</span><span class="nf">new</span><span class="p">.</span><span class="nf">instance_method</span>
</pre></td></tr></tbody></table></code></pre></div></div>
<p>module <code class="language-plaintext highlighter-rouge">InstanceMethods</code> is also removed because methods inside Foo module will come under <code class="language-plaintext highlighter-rouge">InstanceMethods</code> module by default. You can also notice that <code class="language-plaintext highlighter-rouge">self.included(base)</code> is also changed to included only. Now You can override included method for your other useful purpose.</p>]]></content><author><name>neeraj</name></author><category term="Ruby" /><category term="Rails" /><category term="ActiveSupport" /><category term="ActiveSupport Concern" /><category term="ruby" /><category term="rails" /><category term="active_support" /><summary type="html"><![CDATA[Is ActiveSupport::Concern really Another Concern?]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blog.neerajk.com/assets/images/as-concern.png" /><media:content medium="image" url="https://blog.neerajk.com/assets/images/as-concern.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">How To Wire up Ruby on Rails and Ampersand.js As a Single Page Application</title><link href="https://blog.neerajk.com/How-To-Wire-Up-Ruby-On-Rails-And-Ampersandjs-As-A-Single-Page-Application/" rel="alternate" type="text/html" title="How To Wire up Ruby on Rails and Ampersand.js As a Single Page Application" /><published>2014-09-25T00:00:00+00:00</published><updated>2014-09-25T00:00:00+00:00</updated><id>https://blog.neerajk.com/How-To-Wire-Up-Ruby-On-Rails-And-Ampersandjs-As-A-Single-Page-Application</id><content type="html" xml:base="https://blog.neerajk.com/How-To-Wire-Up-Ruby-On-Rails-And-Ampersandjs-As-A-Single-Page-Application/"><![CDATA[<p><a href="http://ampersandjs.com">Ampersand.js</a> is a latest web framework like any other javascript framework e.g. <a href="https://angularjs.org/">Angularjs</a>. By definition,</p>
<blockquote>
  <p><a href="http://ampersandjs.com">Ampersand.js</a> - A highly modular, loosely coupled, non-frameworky framework for building advanced JavaScript apps.</p>
</blockquote>

<p>This definition is correct. Ampersandjs is really a non-frameworky framework. Now question comes, <b>Why Ampersand.js?</b>. Because we love <a href="http://backbonejs.org/">Backbone.js</a>, and <a href="http://ampersandjs.com">Ampersand.js</a> is written on the top of <a href="http://backbonejs.org/">Backbone.js</a>. For more details, please refer http://ampersandjs.com.</p>

<p>In this article, I am explaining you to build a simple single page application (not simple as much as any hello world or todo app :)) using rails and ampersand.js. The application will be wired-up with a Rails application and will fetch all its data from rails generated APIs. In fact, behind the scene, Rails application is providing the APIs directly from the file <code class="language-plaintext highlighter-rouge">app/assets/javascripts/ampersandtest/data.json</code>. You can also fetch the data from any other application too using your web-service call. The source code is residing on my github repository <a href="https://github.com/neerajkumar/betfair_nav_demo">Source Code</a> and demo is available on <a href="http://rails-angular-ampersand.herokuapp.com/ampersand_clients">Demo</a>. Let’s start from scratch.</p>

<h3 id="basic-installation">Basic Installation</h3>
<hr />
<p>Install the ampersand command line by running this in a terminal:</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>npm <span class="nb">install </span>ampersand <span class="nt">-g</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>then go into rails project directory and run the command</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>npm init 
</pre></td></tr></tbody></table></code></pre></div></div>

<p>this will create package.json file in your rails root directory. Now</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre>npm <span class="nb">install </span>browserify <span class="nt">--save-dev</span>

npm <span class="nb">install </span>underscore <span class="nt">--save</span> 
</pre></td></tr></tbody></table></code></pre></div></div>

<p>will automatically add the dependencies into package.json file. The package.json file will look like</p>

<div class="language-json highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="rouge-code"><pre><span class="p">{</span><span class="w">
  </span><span class="nl">"name"</span><span class="p">:</span><span class="w"> </span><span class="s2">"betfair_nav_demo"</span><span class="p">,</span><span class="w">
  </span><span class="nl">"version"</span><span class="p">:</span><span class="w"> </span><span class="s2">"0.0.0"</span><span class="p">,</span><span class="w">
  </span><span class="nl">"description"</span><span class="p">:</span><span class="w"> </span><span class="s2">"Rails App Betfair Nav Demo with Ampersand"</span><span class="p">,</span><span class="w">
  </span><span class="nl">"main"</span><span class="p">:</span><span class="w"> </span><span class="s2">"app.js"</span><span class="p">,</span><span class="w">
  </span><span class="nl">"scripts"</span><span class="p">:</span><span class="w"> </span><span class="p">{</span><span class="w">
    </span><span class="nl">"test"</span><span class="p">:</span><span class="w"> </span><span class="s2">"echo </span><span class="se">\"</span><span class="s2">Error: no test specified</span><span class="se">\"</span><span class="s2"> &amp;&amp; exit 1"</span><span class="w">
  </span><span class="p">},</span><span class="w">
  </span><span class="nl">"author"</span><span class="p">:</span><span class="w"> </span><span class="s2">"Neeraj Kumar"</span><span class="p">,</span><span class="w">
  </span><span class="nl">"license"</span><span class="p">:</span><span class="w"> </span><span class="s2">"ISC"</span><span class="w">
</span><span class="p">}</span><span class="w">
</span></pre></td></tr></tbody></table></code></pre></div></div>
<p>here app.js file will be responsible to enable your ampersand.js app.</p>

<h3 id="framework-structure--work-flow">Framework Structure &amp; Work Flow</h3>
<hr />
<p>Now, just have a look at app.js file now.</p>
<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
</pre></td><td class="rouge-code"><pre><span class="kd">var</span> <span class="nx">_</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">underscore</span><span class="dl">'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">Router</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">./router</span><span class="dl">'</span><span class="p">);</span>
<span class="c1">//var tracking = require('./helpers/metrics'); global mixpanel</span>
<span class="kd">var</span> <span class="nx">MainView</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">./views/main</span><span class="dl">'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">Me</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">./models/me</span><span class="dl">'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">BetfairFactory</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">./models/betfair_factory</span><span class="dl">'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">domReady</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">domready</span><span class="dl">'</span><span class="p">);</span>

<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="p">{</span>
    <span class="c1">// this is the the whole app initter</span>
    <span class="na">blastoff</span><span class="p">:</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
        <span class="kd">var</span> <span class="nb">self</span> <span class="o">=</span> <span class="nb">window</span><span class="p">.</span><span class="nx">app</span> <span class="o">=</span> <span class="k">this</span><span class="p">;</span>

        <span class="c1">// create our global 'me' object and an empty collection for our betfair_factory models.</span>
        <span class="nb">window</span><span class="p">.</span><span class="nx">me</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Me</span><span class="p">();</span>
        <span class="k">this</span><span class="p">.</span><span class="nx">betfair_roots</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">BetfairFactory</span><span class="p">();</span>

        <span class="c1">// init our URL handlers and the history tracker</span>
        <span class="k">this</span><span class="p">.</span><span class="nx">router</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Router</span><span class="p">();</span>

        <span class="c1">// wait for document ready to render our main view</span>
        <span class="c1">// this ensures the document has a body, etc.</span>
        <span class="nx">domReady</span><span class="p">(</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
            <span class="c1">// init our main view</span>
            <span class="kd">var</span> <span class="nx">mainView</span> <span class="o">=</span> <span class="nb">self</span><span class="p">.</span><span class="nx">view</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">MainView</span><span class="p">({</span>
                <span class="na">model</span><span class="p">:</span> <span class="nx">me</span><span class="p">,</span>
                <span class="na">el</span><span class="p">:</span> <span class="nb">document</span><span class="p">.</span><span class="nx">body</span>
            <span class="p">});</span>

            <span class="c1">// ...and render it</span>
            <span class="nx">mainView</span><span class="p">.</span><span class="nx">render</span><span class="p">();</span>

            <span class="c1">// listen for new pages from the router</span>
            <span class="nb">self</span><span class="p">.</span><span class="nx">router</span><span class="p">.</span><span class="nx">on</span><span class="p">(</span><span class="dl">'</span><span class="s1">newPage</span><span class="dl">'</span><span class="p">,</span> <span class="nx">mainView</span><span class="p">.</span><span class="nx">setPage</span><span class="p">,</span> <span class="nx">mainView</span><span class="p">);</span>

            <span class="c1">// we have what we need, we can now start our router and show the appropriate page</span>
            <span class="nb">self</span><span class="p">.</span><span class="nx">router</span><span class="p">.</span><span class="nx">history</span><span class="p">.</span><span class="nx">start</span><span class="p">({</span><span class="na">pushState</span><span class="p">:</span> <span class="kc">true</span><span class="p">,</span> <span class="na">root</span><span class="p">:</span> <span class="dl">'</span><span class="s1">/</span><span class="dl">'</span><span class="p">});</span>
        <span class="p">});</span>
    <span class="p">},</span>

    <span class="c1">// This is how you navigate around the app.</span>
    <span class="c1">// this gets called by a global click handler that handles</span>
    <span class="c1">// all the &lt;a&gt; tags in the app.</span>
    <span class="c1">// it expects a url without a leading slash.</span>
    <span class="c1">// for example: "costello/settings".</span>
    <span class="na">navigate</span><span class="p">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">page</span><span class="p">)</span> <span class="p">{</span>
        <span class="kd">var</span> <span class="nx">url</span> <span class="o">=</span> <span class="p">(</span><span class="nx">page</span><span class="p">.</span><span class="nx">charAt</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="o">===</span> <span class="dl">'</span><span class="s1">/</span><span class="dl">'</span><span class="p">)</span> <span class="p">?</span> <span class="nx">page</span><span class="p">.</span><span class="nx">slice</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="p">:</span> <span class="nx">page</span><span class="p">;</span>
        <span class="k">this</span><span class="p">.</span><span class="nx">router</span><span class="p">.</span><span class="nx">history</span><span class="p">.</span><span class="nx">navigate</span><span class="p">(</span><span class="nx">url</span><span class="p">,</span> <span class="p">{</span><span class="na">trigger</span><span class="p">:</span> <span class="kc">true</span><span class="p">});</span>
    <span class="p">}</span>
<span class="p">};</span>

<span class="c1">// run it</span>
<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span><span class="p">.</span><span class="nx">blastoff</span><span class="p">();</span>
</pre></td></tr></tbody></table></code></pre></div></div>
<p>We also maintain a router which will responsible for navigation to different URLs.</p>
<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="rouge-code"><pre><span class="kd">var</span> <span class="nx">Router</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">ampersand-router</span><span class="dl">'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">HomePage</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">./pages/home</span><span class="dl">'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">CollectionDemo</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">./pages/collection-demo</span><span class="dl">'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">EventsCollectionView</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">./pages/events-collection</span><span class="dl">'</span><span class="p">)</span>
<span class="kd">var</span> <span class="nx">EventTypesCollectionView</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">./pages/event-types-collection</span><span class="dl">'</span><span class="p">)</span>

<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="nx">Router</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
    <span class="na">routes</span><span class="p">:</span> <span class="p">{</span>
        <span class="dl">''</span><span class="p">:</span> <span class="dl">'</span><span class="s1">home</span><span class="dl">'</span><span class="p">,</span>
        <span class="dl">'</span><span class="s1">collections</span><span class="dl">'</span><span class="p">:</span> <span class="dl">'</span><span class="s1">collectionDemo</span><span class="dl">'</span><span class="p">,</span>
        <span class="dl">'</span><span class="s1">event_types/:id</span><span class="dl">'</span><span class="p">:</span> <span class="dl">'</span><span class="s1">EventTypesCollectionView</span><span class="dl">'</span><span class="p">,</span>
        <span class="dl">'</span><span class="s1">events/:id</span><span class="dl">'</span><span class="p">:</span> <span class="dl">'</span><span class="s1">EventsCollectionView</span><span class="dl">'</span><span class="p">,</span>
        <span class="dl">'</span><span class="s1">(*path)</span><span class="dl">'</span><span class="p">:</span> <span class="dl">'</span><span class="s1">catchAll</span><span class="dl">'</span>
    <span class="p">},</span>
    <span class="c1">// ------- ROUTE HANDLERS ---------</span>
    <span class="na">home</span><span class="p">:</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
        <span class="k">this</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="dl">'</span><span class="s1">page</span><span class="dl">'</span><span class="p">,</span> <span class="k">new</span> <span class="nx">HomePage</span><span class="p">({</span>
            <span class="na">model</span><span class="p">:</span> <span class="nx">me</span>
        <span class="p">}));</span>
    <span class="p">},</span>
<span class="p">});</span>
</pre></td></tr></tbody></table></code></pre></div></div>
<p>According to router, the homepage is residing in <code class="language-plaintext highlighter-rouge">pages/home</code>. The home file is</p>
<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
</pre></td><td class="rouge-code"><pre><span class="kd">var</span> <span class="nx">PageView</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">./base</span><span class="dl">'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">templates</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">../templates</span><span class="dl">'</span><span class="p">);</span>

<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="nx">PageView</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
    <span class="na">pageTitle</span><span class="p">:</span> <span class="dl">'</span><span class="s1">home</span><span class="dl">'</span><span class="p">,</span>
    <span class="na">template</span><span class="p">:</span> <span class="nx">templates</span><span class="p">.</span><span class="nx">pages</span><span class="p">.</span><span class="nx">home</span>
<span class="p">});</span>
</pre></td></tr></tbody></table></code></pre></div></div>
<p>But in our app, we are displaying the collection view on homepage.</p>
<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
</pre></td><td class="rouge-code"><pre>    <span class="nx">home</span><span class="p">:</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
        <span class="k">this</span><span class="p">.</span><span class="nx">trigger</span><span class="p">(</span><span class="dl">'</span><span class="s1">page</span><span class="dl">'</span><span class="p">,</span> <span class="k">new</span> <span class="nx">CollectionDemo</span><span class="p">({</span>
            <span class="na">model</span><span class="p">:</span> <span class="nx">me</span><span class="p">,</span>
            <span class="na">collection</span><span class="p">:</span> <span class="nx">app</span><span class="p">.</span><span class="nx">betfair_roots</span>
        <span class="p">}));</span>
    <span class="p">},</span>
</pre></td></tr></tbody></table></code></pre></div></div>
<p>Here app.betfair_roots is mapping to a model and will also be responsible to fetch the json data through ajax call. It will actually call the file <code class="language-plaintext highlighter-rouge">/models/betfair_factory.js</code> file.</p>
<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
</pre></td><td class="rouge-code"><pre><span class="kd">var</span> <span class="nx">Collection</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">ampersand-rest-collection</span><span class="dl">'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">BetfairRoot</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">./betfair_root</span><span class="dl">'</span><span class="p">);</span>

<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="nx">Collection</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
    <span class="na">mainIndex</span><span class="p">:</span> <span class="dl">'</span><span class="s1">id</span><span class="dl">'</span><span class="p">,</span>
    <span class="na">model</span><span class="p">:</span> <span class="nx">BetfairRoot</span><span class="p">,</span>
    <span class="na">url</span><span class="p">:</span> <span class="dl">'</span><span class="s1">/api/betfair_roots</span><span class="dl">'</span>
<span class="p">});</span>
</pre></td></tr></tbody></table></code></pre></div></div>
<p>Now, we have come to our homepage which is also a collection-demo page and rendering the collection of root nodes. The file is residing in <code class="language-plaintext highlighter-rouge">./pages/collection-demo.js</code> file.</p>
<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="rouge-code"><pre><span class="kd">var</span> <span class="nx">PageView</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">./base</span><span class="dl">'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">templates</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">../templates</span><span class="dl">'</span><span class="p">);</span>
<span class="kd">var</span> <span class="nx">BetfairRootView</span> <span class="o">=</span> <span class="nx">require</span><span class="p">(</span><span class="dl">'</span><span class="s1">../views/betfair_root</span><span class="dl">'</span><span class="p">);</span>

<span class="nx">module</span><span class="p">.</span><span class="nx">exports</span> <span class="o">=</span> <span class="nx">PageView</span><span class="p">.</span><span class="nx">extend</span><span class="p">({</span>
    <span class="na">pageTitle</span><span class="p">:</span> <span class="dl">'</span><span class="s1">Event Types</span><span class="dl">'</span><span class="p">,</span>
    <span class="na">template</span><span class="p">:</span> <span class="nx">templates</span><span class="p">.</span><span class="nx">pages</span><span class="p">.</span><span class="nx">collectionDemo</span><span class="p">,</span>
    <span class="na">render</span><span class="p">:</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
        <span class="k">this</span><span class="p">.</span><span class="nx">renderWithTemplate</span><span class="p">();</span>
        <span class="k">this</span><span class="p">.</span><span class="nx">renderCollection</span><span class="p">(</span><span class="k">this</span><span class="p">.</span><span class="nx">collection</span><span class="p">,</span> <span class="nx">BetfairRootView</span><span class="p">,</span> <span class="k">this</span><span class="p">.</span><span class="nx">getByRole</span><span class="p">(</span><span class="dl">'</span><span class="s1">betfair-roots-list</span><span class="dl">'</span><span class="p">));</span>
        <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="k">this</span><span class="p">.</span><span class="nx">collection</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">this</span><span class="p">.</span><span class="nx">fetchCollection</span><span class="p">();</span>
        <span class="p">}</span>
    <span class="p">},</span>
    <span class="na">fetchCollection</span><span class="p">:</span> <span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>
        <span class="k">this</span><span class="p">.</span><span class="nx">collection</span><span class="p">.</span><span class="nx">fetch</span><span class="p">();</span>
        <span class="k">return</span> <span class="kc">false</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">});</span>
</pre></td></tr></tbody></table></code></pre></div></div>
<p>Here</p>
<ol>
  <li>pageTitle which is referring to page title.</li>
  <li>template: it is referring to the template where all collection will be displayed.</li>
  <li>render: it represents the function. The statement <code class="language-plaintext highlighter-rouge">this.renderCollection(this.collection, BetfairRootView, this.getByRole('betfair-roots-list'));</code> is explaining that it will render all the collection nodes in terms of BetfairRootView objects and place will be the div element with role ‘betfair-roots-list’.</li>
  <li><code class="language-plaintext highlighter-rouge">this.fetchCollection()</code> statement is for triggering the ajax call again if collection data is not present.</li>
</ol>

<p>Now lets have a look at templates file where it will be rendering the collection data.</p>
<div class="language-javascript highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
4
</pre></td><td class="rouge-code"><pre><span class="c1">// pages/collectionDemo.jade compiled template</span>
<span class="nx">templatizer</span><span class="p">[</span><span class="dl">"</span><span class="s2">pages</span><span class="dl">"</span><span class="p">][</span><span class="dl">"</span><span class="s2">collectionDemo</span><span class="dl">"</span><span class="p">]</span> <span class="o">=</span> <span class="kd">function</span> <span class="nx">tmpl_pages_collectionDemo</span><span class="p">()</span> <span class="p">{</span>
  <span class="k">return</span> <span class="dl">'</span><span class="s1">&lt;section class="page pageOne"&gt;&lt;ul role="betfair-roots-list" class="list-group"&gt;&lt;/ul&gt;&lt;/section&gt;</span><span class="dl">'</span><span class="p">;</span>
<span class="p">};</span>
</pre></td></tr></tbody></table></code></pre></div></div>
<h3 id="conclusion">Conclusion</h3>
<hr />
<p>That’s it. This is the basic structure and work-flow of any basic ampersand.js application with rails. The source code is residing on my github repository <a href="https://github.com/neerajkumar/betfair_nav_demo">Source Code</a> and demo is available on <a href="http://rails-angular-ampersand.herokuapp.com/ampersand_clients">Demo</a>. With the help of source code, you can get to know more about the links and navigation to different pages of the application. You can write your own business logic and bind it with your Ampersand.js application. Lot more can be done.
Thanks for reading my blog. :)</p>]]></content><author><name>neeraj</name></author><category term="Ruby" /><category term="Rails" /><category term="Ampersand.js" /><category term="Single Page Application" /><category term="SPA" /><category term="ruby" /><category term="rails" /><category term="ampersand.js" /><category term="spa" /><category term="git" /><summary type="html"><![CDATA[How To Wire up Ruby on Rails and Ampersand.js As a Single Page Application]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blog.neerajk.com/assets/images/ampersand.png" /><media:content medium="image" url="https://blog.neerajk.com/assets/images/ampersand.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Smart Deployment using Hubot</title><link href="https://blog.neerajk.com/Smart-Deployment-using-Hubot/" rel="alternate" type="text/html" title="Smart Deployment using Hubot" /><published>2014-06-18T00:00:00+00:00</published><updated>2014-06-18T00:00:00+00:00</updated><id>https://blog.neerajk.com/Smart-Deployment-using-Hubot</id><content type="html" xml:base="https://blog.neerajk.com/Smart-Deployment-using-Hubot/"><![CDATA[<p>Github is an invariably well-known name in the world of open source. Githut consistently provides a propitious and favorable platforms for any betterment, reform or innovation in the technology which could also discern as an your open source contribution. <a href="https://hubot.github.com">Hubot</a> is also one of such examples which posses the sovereign potential to metamorphose the process of deployment of rails apps. I came across the term hubot last year and I also heard that numerous enter-prize level organizations benefiting their processes by hubot. My curiosity enabled me up-to the extent that now I am writing a blog :).</p>

<p><a href="https://hubot.github.com">Hubot</a> describes its work as an amazing campfire bot who plays the vital role of an user, deployed on heroku, and will add into your campfire chat room as a normal user through heroku URL. However, the heroku app will be revealing as an error page which could cause a confusion in your mind about credibility of the chat bot.</p>

<p>First of all, deploy a dummy app on heroku which could be initiated using this command.</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>gem <span class="nb">install </span>heroku
</pre></td></tr></tbody></table></code></pre></div></div>

<p>I am assuming here that you are familiar some what with heroku and have created your profile and deployed some app on heroku. If not, please refer https://id.heroku.com/signup or https://devcenter.heroku.com/articles/git.
Then setup the heroku app:</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="rouge-code"><pre>heroku create <span class="nt">--stack</span> 
git push heroku master 
heroku ps:scale <span class="nv">web</span><span class="o">=</span>1
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Now, you will need to setup your HEROKU_URL variable.</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>heroku config:add <span class="nv">HEROKU_URL</span><span class="o">=</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<h4 style="float: right">Work in Progress ... </h4>
<p><br /><br /></p>]]></content><author><name>neeraj</name></author><category term="Ruby" /><category term="Hubot" /><category term="Capistrano" /><category term="ruby" /><category term="rails" /><category term="hubot" /><category term="capistrano" /><category term="basecamp" /><category term="campfire" /><summary type="html"><![CDATA[Smart and Easy Deployment using Hubot]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blog.neerajk.com/assets/images/hubot.jpg" /><media:content medium="image" url="https://blog.neerajk.com/assets/images/hubot.jpg" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">Successful Migration from svn to git with history</title><link href="https://blog.neerajk.com/Migration-from-svn-to-git/" rel="alternate" type="text/html" title="Successful Migration from svn to git with history" /><published>2014-03-15T00:00:00+00:00</published><updated>2014-03-15T00:00:00+00:00</updated><id>https://blog.neerajk.com/Migration-from-svn-to-git</id><content type="html" xml:base="https://blog.neerajk.com/Migration-from-svn-to-git/"><![CDATA[<p>I am a git lover, not svn. But, when I started my programming, svn was so popular and widespread. But the discovery of git pushed the paradigm shift in world of version control. Git is better than svn in lot of senses which could easily be delineated with the fact that most of the open source contributions and repositories are found on <a href="https://github.com">Github</a>. <a href="https://github.com">Github</a> could be recognized as the UI interface for git version control.</p>

<p>The time has changed, but yet there are many projects are still residing on svn. This article will enable you to successfully migrate from your old svn version control to new git version control with the help of few chunck of statements. The astonishing fact is that this will not lose your history too.</p>

<p>Lets say your project name is dummyapp and its residing on svn say <code class="language-plaintext highlighter-rouge">http://svn/Other/RubyOnRails/apps/dummyapp/trunk</code>.</p>

<p>1). Please go to your local svn copy on your local workstation.</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="nb">cd </span>PATH_TO_LOCAL_SVN_COPY
</pre></td></tr></tbody></table></code></pre></div></div>

<p>2). Create a file say authors-transform.txt which will be having list of all users of current svn dummyapp application.</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>svn log <span class="nt">-q</span> | <span class="nb">awk</span> <span class="nt">-F</span> <span class="s1">'|'</span> <span class="s1">'/^r/ {sub("^ ", "", $2); sub(" $", "", $2); print $2" = "$2" &lt;"$2"&gt;"}'</span> | <span class="nb">sort</span> <span class="nt">-u</span> <span class="o">&gt;</span> authors-transform.txt
</pre></td></tr></tbody></table></code></pre></div></div>

<p>This command will fetch all users who have made any commit to dummyapp svn application before, then sort them and store in authors-transform.txt file.</p>

<p>3). Now, install git-svn on your system.</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="nb">sudo </span>apt-get <span class="nb">install </span>git-svn <span class="o">(</span>on linux<span class="o">)</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>I am not sure about windows, but there will be any application to perform this task in windows also (Please help yourself window users).</p>

<p>4). Now, execute the command</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>git svn clone svn://svn/Other/RubyOnRails/apps/dummyapp/trunk <span class="nt">-A</span> authors-transform.txt  ~/temp
</pre></td></tr></tbody></table></code></pre></div></div>

<p>This command will take relatively longer time as it fetches all history of the svn application. For every commit in SVN, it will generate corresponding git commit ID, assign the commiter name and by default create the master branch. As well as, it will copy the whole application as a git repository on your local in temp directory (as in my case the temp directory is there in my home folder)</p>

<p>5). Once step 4 is completed, go to temp directory (cd ~/temp). Create origin remote and assign it to github path.</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>git remote add origin git@github.com:dummyapp.git
</pre></td></tr></tbody></table></code></pre></div></div>

<p>6). Push the code to github.</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre>git push origin master
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Thats it !!!</p>

<p>Congratulation !!! Your application has been successfully moved over github with all of its histories and users etc.</p>]]></content><author><name>neeraj</name></author><category term="Ruby" /><category term="Git" /><category term="Svn" /><category term="ruby" /><category term="git" /><category term="svn" /><summary type="html"><![CDATA[I am a git lover, not svn. But, when I started my programming, svn was so popular and widespread. But the discovery of git pushed the paradigm shift in world of version control.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blog.neerajk.com/assets/images/svn.png" /><media:content medium="image" url="https://blog.neerajk.com/assets/images/svn.png" xmlns:media="http://search.yahoo.com/mrss/" /></entry><entry><title type="html">ruby-oci8 with rails</title><link href="https://blog.neerajk.com/Ruby-oci8_with-rails/" rel="alternate" type="text/html" title="ruby-oci8 with rails" /><published>2013-12-27T00:00:00+00:00</published><updated>2013-12-27T00:00:00+00:00</updated><id>https://blog.neerajk.com/Ruby-oci8_with-rails</id><content type="html" xml:base="https://blog.neerajk.com/Ruby-oci8_with-rails/"><![CDATA[<p><a href="http://rubygems.org/gems/ruby-oci8">Ruby-oci8</a> is a rubygem which we use to build the connection with oracle DB in your rails application. But installation of <a href="http://rubygems.org/gems/ruby-oci8">ruby-oci8</a> on linux or mac is not easy, while its a bit tricky. When I started to configure it on my mac first time, it consumed the entire day to google and configure. Therefore, I am trying to explain you the local configuration and installation here. However this post is only for linux and mac, but not for windows.</p>

<p>Hmmmmm, let’s start with the basics. I am assuming that you are using rails-3.0+ and ruby-1.8.7+. As we do with Rails-3 applications, We maintain a Gemfile and in order to install gems, we run bundle install. But, if you have specified ruby-oci8 gem, then just wait for few moments. This will be failed unless you have proper configuration.</p>

<p>1). You will need to install and configure <a href="http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html">oracle instant client</a> first. Please download and install <a href="http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html">oracle instant client</a>. The directions for installation is already given, but be careful to choose correct instant client version which could differ according to your machine.</p>

<p>2). Next, you will need to set your environment. Set environment variable LD_LIBRARY_PATH (DYLD_LIBRARY_PATH in case of mac). Just run this command on your terminal or console.</p>

<p>In case of Linux</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre> <span class="nb">export </span><span class="nv">LD_LIBRARY_PATH</span><span class="o">=</span><span class="s2">"/usr/lib/oracle/11.2/client64/"</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>In case of Mac</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre> <span class="nb">export </span><span class="nv">DYLD_LIBRARY_PATH</span><span class="o">=</span><span class="s2">"/usr/local/oracle/instantclient_11_2"</span>
</pre></td></tr></tbody></table></code></pre></div></div>

<p>Or, you can add this command in your .bashrc or .zshrc file too and then run the command <code class="language-plaintext highlighter-rouge">source .bashrc</code> or <code class="language-plaintext highlighter-rouge">source .zshrc</code>.</p>

<p>This will set the value of environment variable LD_LIBRARY_PATH as “/usr/lib/oracle/11.2/client64/”.</p>

<p>Also, check that this location does have the client installed. If not, find out where it is and create a soft link to here. ruby-oci8 expects to find it at this location “/usr/lib/oracle/….”.</p>

<p>3). In this last step, you need to make a symlink. Just run this command</p>

<div class="language-shell highlighter-rouge"><div class="highlight"><pre class="highlight"><code><table class="rouge-table"><tbody><tr><td class="rouge-gutter gl"><pre class="lineno">1
</pre></td><td class="rouge-code"><pre><span class="nb">sudo ln</span> <span class="nt">-s</span> /usr/include/linux/ /usr/include/sysclient
</pre></td></tr></tbody></table></code></pre></div></div>

<p>In last, it must work after this. If its not working, then just drop a comment here, may be I could help you more.</p>]]></content><author><name>neeraj</name></author><category term="Ruby" /><summary type="html"><![CDATA[Ruby-oci8 is a rubygem which we use to build the connection with oracle DB in your rails application.]]></summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blog.neerajk.com/assets/images/ruby-oci8.jpeg" /><media:content medium="image" url="https://blog.neerajk.com/assets/images/ruby-oci8.jpeg" xmlns:media="http://search.yahoo.com/mrss/" /></entry></feed>