<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Max Ivak Personal Site</title>
	<atom:link href="http://maxivak.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://maxivak.com</link>
	<description>Entrepreneur, Business Angel, Software Architect. Startups. SaaS, Business Intelligence</description>
	<lastBuildDate>Wed, 10 Apr 2013 18:10:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>How to create your own Ruby gem</title>
		<link>http://maxivak.com/how-to-create-your-own-gem-for-rails-3/</link>
		<comments>http://maxivak.com/how-to-create-your-own-gem-for-rails-3/#comments</comments>
		<pubDate>Sat, 06 Apr 2013 22:40:16 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[gems]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=999</guid>
		<description><![CDATA[This post is the guide to creating your own gem for Rails. What is a Ruby gem? Gems vs. Plugins vs. Engines vs. Realties Create your first gem Use gem in Rails application Test gem Build a new version of gem Gems to build your own gem References What is a Ruby gem? A gem [...]]]></description>
			<content:encoded><![CDATA[<p>This post is the guide to creating your own gem for Rails.</p>
<p><span id="more-999"></span></p>
<ul>
<li><a href="#def">What is a Ruby gem?</a></li>
<li><a href="#cmp">Gems vs. Plugins vs. Engines vs. Realties</a></li>
<li><a href="#create">Create your first gem</a></li>
<li><a href="#use">Use gem in Rails application</a></li>
<li><a href="#test">Test gem</a></li>
<li><a href="#newversion">Build a new version of gem</a></li>
<li><a href="#gems">Gems to build your own gem</a></li>
<li><a href="#ref">References</a></li>
</ul>
<p><a name="def"></a></p>
<h2>What is a Ruby gem?</h2>
<p>A gem is a ruby library in a self-contained format.</p>
<p>The gems are handled by RubyGems, which is a package manager for Ruby that provides a standard format for distributing the gems. As of ruby 1.9 RubyGems comes with Ruby.</p>
<p>Someone can write their own library and then package it as a gem file so that it can be used in any Rails application. Another programmer can install and access its functionality as any other known gems (gem install mygem).</p>
<p>It means that a gem is a generic library, which can be easily installed, which are version-managed, have dependencies.<br />
<a name="cmp"></a></p>
<h2>Gems vs. Plugins vs. Engines vs. Realties</h2>
<p><strong>Gems and Plugins:</strong></p>
<p>- Gems and Plugins are identical in terms of the code and function you actually write, therefore creating a gem is often referred to as &#8220;packaging&#8221; something as a gem.</p>
<p>- Gems are version-managed, have dependencies. Gems can be easily installed.</p>
<p>- Gems are platform independent, which means you can use them in any Ruby environment (such as Rails or Sinatra)</p>
<p>- Plugins are Rails-only and you can’t manage or version them.</p>
<p>&nbsp;</p>
<p>Rails has a plugin system where you type &#8220;script/plugin install &lt;plugin-name&gt;&#8221; but packaging it up as a gem is better because its cleaner and allows for better dependency resolution.</p>
<p>&nbsp;</p>
<p><strong>Realtie</strong></p>
<p>// see <a href="http://edgeapi.rubyonrails.org/classes/Rails/Railtie.html">http://edgeapi.rubyonrails.org/classes/Rails/Railtie.html</a></p>
<p>Railtie is the core of the Rails framework and provides several hooks to extend Rails and/or modify the initialization process.<br />
Every major component of Rails (Action Mailer, Action Controller, Action View and Active Record) is a Railtie. Each of them is responsible for their own initialization. This makes Rails itself absent of any component hooks, allowing other components to be used in place of any of the Rails defaults.</p>
<p>&nbsp;</p>
<p><strong>Engine</strong></p>
<p>An Engine is a sub-application of a Rails application.</p>
<p>Engines are miniature applications that live inside your application and they have structure that you would normally find in a typical Rails application.</p>
<p>If you have used <a title="Devise gem" href="https://github.com/plataformatec/devise">Devise gem</a>, which itself is an engine, you know the benefits of being able to add functionality to your application with just a few lines of code.</p>
<p>These are not full applications, but pages/views/controllers/models that can be added to any rails application.</p>
<p>A <a href="http://edgeapi.rubyonrails.org/classes/Rails/Engine.html">Rails::Engine</a> is nothing more than a <a href="http://edgeapi.rubyonrails.org/classes/Rails/Railtie.html">Railtie</a> with some initializers already set.</p>
<p>&nbsp;</p>
<p>Engines are already available prior to Rails 3 but it is not a core feature of the framework. As such, engine developers resorted to monkey-patching which, oftentimes, lead to engines breaking when Rails gets updated.</p>
<p>Since Rails 3 an engine can be packaged and distributed as a <code>gem</code>.</p>
<p>In Rails 3.1, engines are now supported by the framework and there is now a clearly defined place where to hook your engines into Rails.</p>
<p>&nbsp;</p>
<p>Read this blog about creating your own Rails 3 Engine:</p>
<p><a href="http://gregmoreno.wordpress.com/2012/05/29/create-your-own-rails-3-engine/">http://gregmoreno.wordpress.com/2012/05/29/create-your-own-rails-3-engine/</a></p>
<p><a name="create"></a></p>
<h2>Create your first gem</h2>
<p>Let&#8217;s name our first gem as &#8216;myfirst&#8217;.</p>
<p>- Create a new directory which will contain our new gem</p>
<p>- Create gemspec file<br />
The first thing to do when creating a new gem is to create a file called gemspec that contains information about the gem. The gemspec file must be named &#8216;mygemname.gemspec&#8217;.</p>
<p>File &#8216;myfirst.gemspec&#8217;:</p>
<div id="wpshdo_1" class="wp-synhighlighter-outer"><div id="wpshdt_1" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_1"></a><a id="wpshat_1" class="wp-synhighlighter-title" href="#codesyntax_1"  onClick="javascript:wpsh_toggleBlock(1)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_1" onClick="javascript:wpsh_code(1)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_1" onClick="javascript:wpsh_print(1)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_1" class="wp-synhighlighter-inner" style="display: block;"><pre class="ruby" style="font-family:monospace;"><span class="re2">Gem::Specification</span>.<span class="me1">new</span> <span class="kw1">do</span> <span class="sy0">|</span>s<span class="sy0">|</span>
s.<span class="me1">name</span> = <span class="st0">&quot;myfirst&quot;</span>
s.<span class="me1">version</span> = <span class="st0">'0.0.1'</span>
s.<span class="me1">date</span> = <span class="st0">'2013-04-06'</span>
s.<span class="me1">authors</span> = <span class="br0">&#91;</span><span class="st0">&quot;Max Ivak &quot;</span><span class="br0">&#93;</span>
s.<span class="me1">email</span> = <span class="br0">&#91;</span><span class="st0">&quot;maxivak@gmail.com&quot;</span><span class="br0">&#93;</span>
s.<span class="me1">summary</span> = <span class="st0">&quot;My very first&quot;</span>
s.<span class="me1">description</span> = <span class="st0">&quot;Simple Hello World&quot;</span>
s.<span class="me1">homepage</span> = <span class="st0">&quot;http://github.com/maxivak/myfirst&quot;</span>
<span class="co1">#s.files = [&quot;lib/mygem3.rb&quot;]</span>
<span class="co1"># or</span>
s.<span class="me1">files</span> = <span class="kw4">Dir</span><span class="br0">&#91;</span><span class="st0">&quot;{lib}/**/*.rb&quot;</span>, <span class="st0">&quot;bin/*&quot;</span>, <span class="st0">&quot;LICENSE&quot;</span>, <span class="st0">&quot;*.md&quot;</span><span class="br0">&#93;</span>
<span class="kw1">end</span></pre></div></div>
<p>Find all options here &#8211; <a href="http://guides.rubygems.org/specification-reference/">http://guides.rubygems.org/specification-reference/</a>.<br />
Notice this line:<br />
s.files       = Dir["{lib}/**/*.rb", "bin/*", "LICENSE", "*.md"]</p>
<p>It will include all rb files in lib directory.</p>
<p>&nbsp;</p>
<p>- code for library</p>
<p>Let&#8217;s create ruby code for our library in file lib/myfirst.rb.</p>
<p>We create a simple class with a simple class method.</p>
<p>File &#8216;lib/myfirst.rb&#8217;:</p>
<div id="wpshdo_2" class="wp-synhighlighter-outer"><div id="wpshdt_2" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_2"></a><a id="wpshat_2" class="wp-synhighlighter-title" href="#codesyntax_2"  onClick="javascript:wpsh_toggleBlock(2)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_2" onClick="javascript:wpsh_code(2)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_2" onClick="javascript:wpsh_print(2)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_2" class="wp-synhighlighter-inner" style="display: block;"><pre class="ruby" style="font-family:monospace;"><span class="kw1">class</span> Myfirst
<span class="kw1">def</span> <span class="kw2">self</span>.<span class="me1">hi</span>
<span class="kw2">return</span> <span class="st0">&quot;Hello world&quot;</span>
<span class="kw1">end</span>
<span class="kw1">end</span></pre></div></div>
<p>&nbsp;</p>
<p>- build gem</p>
<div id="wpshdo_3" class="wp-synhighlighter-outer"><div id="wpshdt_3" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_3"></a><a id="wpshat_3" class="wp-synhighlighter-title" href="#codesyntax_3"  onClick="javascript:wpsh_toggleBlock(3)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_3" onClick="javascript:wpsh_code(3)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_3" onClick="javascript:wpsh_print(3)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_3" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash" style="font-family:monospace;">gem build myfirst.gemspec</pre></div></div>
<p>You should see the following result:</p>
<div id="wpshdo_4" class="wp-synhighlighter-outer"><div id="wpshdt_4" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_4"></a><a id="wpshat_4" class="wp-synhighlighter-title" href="#codesyntax_4"  onClick="javascript:wpsh_toggleBlock(4)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_4" onClick="javascript:wpsh_code(4)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_4" onClick="javascript:wpsh_print(4)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_4" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash" style="font-family:monospace;">WARNING: licenses is empty
Successfully built RubyGem
Name: myfirst
Version: 0.0.1
File: myfirst-0.0.1.gem</pre></div></div><br />
It will package your gem project into a gem file that can be used to install the gem. This file has name like gemname-version.gem where with the gemname and version are specified in the gemspec file.<br />
In our case it will create a file myfirst-0.0.1.gem.<br />
- check if your gem is available:</p>
<p><div id="wpshdo_5" class="wp-synhighlighter-outer"><div id="wpshdt_5" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_5"></a><a id="wpshat_5" class="wp-synhighlighter-title" href="#codesyntax_5"  onClick="javascript:wpsh_toggleBlock(5)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_5" onClick="javascript:wpsh_code(5)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_5" onClick="javascript:wpsh_print(5)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_5" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash" style="font-family:monospace;">gem list <span class="sy0">|</span> <span class="kw2">grep</span> myfirst</pre></div></div><br />
<a name="use"></a></p>
<h2>Use gem in an application</h2>
<p><strong>Use gem locally:</strong><br />
Install it locally<br />
<div id="wpshdo_6" class="wp-synhighlighter-outer"><div id="wpshdt_6" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_6"></a><a id="wpshat_6" class="wp-synhighlighter-title" href="#codesyntax_6"  onClick="javascript:wpsh_toggleBlock(6)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_6" onClick="javascript:wpsh_code(6)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_6" onClick="javascript:wpsh_print(6)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_6" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash" style="font-family:monospace;">gem <span class="kw2">install</span> .<span class="sy0">/</span>myfirst-0.0.1.gem</pre></div></div><br />
Now you can use your gem in your local Rails application.<br />
Just include it in Gemfile:<br />
<div id="wpshdo_7" class="wp-synhighlighter-outer"><div id="wpshdt_7" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_7"></a><a id="wpshat_7" class="wp-synhighlighter-title" href="#codesyntax_7"  onClick="javascript:wpsh_toggleBlock(7)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_7" onClick="javascript:wpsh_code(7)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_7" onClick="javascript:wpsh_print(7)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_7" class="wp-synhighlighter-inner" style="display: block;"><pre class="ruby" style="font-family:monospace;">gem <span class="st0">'myfirst'</span></pre></div></div>
<p>Then you can access gem&#8217;s functionality:<br />
<div id="wpshdo_8" class="wp-synhighlighter-outer"><div id="wpshdt_8" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_8"></a><a id="wpshat_8" class="wp-synhighlighter-title" href="#codesyntax_8"  onClick="javascript:wpsh_toggleBlock(8)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_8" onClick="javascript:wpsh_code(8)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_8" onClick="javascript:wpsh_print(8)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_8" class="wp-synhighlighter-inner" style="display: block;"><pre class="ruby" style="font-family:monospace;">s = Myfirst.<span class="me1">hi</span></pre></div></div><br />
Also you can test it in IRB console:<br />
<div id="wpshdo_9" class="wp-synhighlighter-outer"><div id="wpshdt_9" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_9"></a><a id="wpshat_9" class="wp-synhighlighter-title" href="#codesyntax_9"  onClick="javascript:wpsh_toggleBlock(9)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_9" onClick="javascript:wpsh_code(9)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_9" onClick="javascript:wpsh_print(9)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_9" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash" style="font-family:monospace;">irb
irb<span class="br0">&#40;</span>main<span class="br0">&#41;</span>:001:<span class="nu0">0</span><span class="sy0">&gt;</span> require <span class="st_h">'myfirst'</span>
=<span class="sy0">&gt;</span> <span class="kw2">true</span>
irb<span class="br0">&#40;</span>main<span class="br0">&#41;</span>:002:<span class="nu0">0</span><span class="sy0">&gt;</span> Myfirst.hi
=<span class="sy0">&gt;</span> <span class="st0">&quot;Hello world&quot;</span>
irb<span class="br0">&#40;</span>main<span class="br0">&#41;</span>:003:<span class="nu0">0</span><span class="sy0">&gt;</span> puts Myfirst.hi
Hello world
=<span class="sy0">&gt;</span> nil
irb<span class="br0">&#40;</span>main<span class="br0">&#41;</span>:004:<span class="nu0">0</span><span class="sy0">&gt;</span> quit</pre></div></div></p>
<h3>Publish to RubyGems</h3>
<p>You can share your gem to Ruby community by publishing it to RubyGems.org.<br />
- Setup your RubyGems account.<br />
Go to rubygems.org and create a new account.</p>
<p>- Deploy gem to RubyGems:<br />
<div id="wpshdo_10" class="wp-synhighlighter-outer"><div id="wpshdt_10" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_10"></a><a id="wpshat_10" class="wp-synhighlighter-title" href="#codesyntax_10"  onClick="javascript:wpsh_toggleBlock(10)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_10" onClick="javascript:wpsh_code(10)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_10" onClick="javascript:wpsh_print(10)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_10" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash" style="font-family:monospace;">gem push myfirst-0.0.1.gem</pre></div></div></p>
<p>It will ask to enter your credentials (email and password).<br />
After that the gem is published:</p>
<div id="wpshdo_11" class="wp-synhighlighter-outer"><div id="wpshdt_11" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_11"></a><a id="wpshat_11" class="wp-synhighlighter-title" href="#codesyntax_11"  onClick="javascript:wpsh_toggleBlock(11)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_11" onClick="javascript:wpsh_code(11)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_11" onClick="javascript:wpsh_print(11)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_11" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash" style="font-family:monospace;">Pushing gem to https:<span class="sy0">//</span>rubygems.org...
Successfully registered gem: myfirst <span class="br0">&#40;</span>0.0.1<span class="br0">&#41;</span></pre></div></div>
<p>Note: If you don&#8217;t want to enter credentials all time to publish your gem, read  the tutorial &#8211; http://guides.rubygems.org/make-your-own-gem/</p>
<p>You can see your gem on RubyGems.org site in your account. The link will be like this:</p>
<p>https://rubygems.org/gems/myfirst</p>
<p>- Check that your gem is available:<br />
<div id="wpshdo_12" class="wp-synhighlighter-outer"><div id="wpshdt_12" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_12"></a><a id="wpshat_12" class="wp-synhighlighter-title" href="#codesyntax_12"  onClick="javascript:wpsh_toggleBlock(12)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_12" onClick="javascript:wpsh_code(12)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_12" onClick="javascript:wpsh_print(12)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_12" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash" style="font-family:monospace;">gem list <span class="re5">-r</span> myfirst
<span class="sy0">***</span> REMOTE GEMS <span class="sy0">***</span>
myfirst <span class="br0">&#40;</span>0.0.1<span class="br0">&#41;</span></pre></div></div></p>
<p>Other developers can now install your gem:<br />
<div id="wpshdo_13" class="wp-synhighlighter-outer"><div id="wpshdt_13" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_13"></a><a id="wpshat_13" class="wp-synhighlighter-title" href="#codesyntax_13"  onClick="javascript:wpsh_toggleBlock(13)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_13" onClick="javascript:wpsh_code(13)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_13" onClick="javascript:wpsh_print(13)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_13" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash" style="font-family:monospace;">gem <span class="kw2">install</span> myfirst</pre></div></div><br />
<a name="test"></a></p>
<h2>Test gem</h2>
<p>Gems support adding test files into the package itself so tests can be run when a gem is downloaded.</p>
<p>Below we will create a simple test using Ruby&#8217;s built-in test framework Test::Unit.</p>
<p>Create a new file &#8216;Rakefile&#8217; and &#8216;test&#8217; directory.</p>
<p>- Rakefile:</p>
<div id="wpshdo_14" class="wp-synhighlighter-outer"><div id="wpshdt_14" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_14"></a><a id="wpshat_14" class="wp-synhighlighter-title" href="#codesyntax_14"  onClick="javascript:wpsh_toggleBlock(14)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_14" onClick="javascript:wpsh_code(14)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_14" onClick="javascript:wpsh_print(14)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_14" class="wp-synhighlighter-inner" style="display: block;"><pre class="ruby" style="font-family:monospace;"><span class="kw3">require</span> <span class="st0">'rake/testtask'</span>
<span class="re2">Rake::TestTask</span>.<span class="me1">new</span> <span class="kw1">do</span> <span class="sy0">|</span>t<span class="sy0">|</span>
t.<span class="me1">libs</span> <span class="sy0">&lt;&lt;</span> <span class="st0">'test'</span>
<span class="kw1">end</span>
desc <span class="st0">&quot;Run tests&quot;</span>
task <span class="re3">:default</span> <span class="sy0">=&gt;</span> <span class="re3">:test</span></pre></div></div>
<p>- Basic test for our class:</p>
<p>test/test_myfirst.rb:</p>
<div id="wpshdo_15" class="wp-synhighlighter-outer"><div id="wpshdt_15" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_15"></a><a id="wpshat_15" class="wp-synhighlighter-title" href="#codesyntax_15"  onClick="javascript:wpsh_toggleBlock(15)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_15" onClick="javascript:wpsh_code(15)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_15" onClick="javascript:wpsh_print(15)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_15" class="wp-synhighlighter-inner" style="display: block;"><pre class="ruby" style="font-family:monospace;"><span class="kw3">require</span> <span class="st0">'test/unit'</span>
<span class="kw3">require</span> <span class="st0">'myfirst'</span>
<span class="kw1">class</span> MyfirstTest <span class="sy0">&lt;</span> <span class="kw4"><span class="re2">Test::Unit::TestCase</span></span>
<span class="kw1">def</span> test_hi
assert_equal <span class="st0">&quot;Hello world&quot;</span>,
Myfirst.<span class="me1">hi</span>
<span class="kw1">end</span>
<span class="kw1">end</span></pre></div></div>
<p>&nbsp;</p>
<p>- run the tests:</p>
<div id="wpshdo_16" class="wp-synhighlighter-outer"><div id="wpshdt_16" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_16"></a><a id="wpshat_16" class="wp-synhighlighter-title" href="#codesyntax_16"  onClick="javascript:wpsh_toggleBlock(16)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_16" onClick="javascript:wpsh_code(16)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_16" onClick="javascript:wpsh_print(16)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_16" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash" style="font-family:monospace;">rake <span class="kw3">test</span>
<span class="co0"># Running tests:</span>
.
Finished tests <span class="kw1">in</span> 0.001000s, <span class="nu0">1000.0000</span> tests<span class="sy0">/</span>s, <span class="nu0">1000.0000</span> assertions<span class="sy0">/</span>s.
<span class="nu0">1</span> tests, <span class="nu0">1</span> assertions, <span class="nu0">0</span> failures, <span class="nu0">0</span> errors, <span class="nu0">0</span> skips</pre></div></div>
<p>You should see that all the tests passed.<br />
<a name="newversion"></a></p>
<h2>Build a new version of gem</h2>
<p>Let&#8217;s modify our gem by adding a new method.</p>
<p>lib/myfirst.rb<br />
<div id="wpshdo_17" class="wp-synhighlighter-outer"><div id="wpshdt_17" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_17"></a><a id="wpshat_17" class="wp-synhighlighter-title" href="#codesyntax_17"  onClick="javascript:wpsh_toggleBlock(17)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_17" onClick="javascript:wpsh_code(17)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_17" onClick="javascript:wpsh_print(17)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_17" class="wp-synhighlighter-inner" style="display: block;"><pre class="ruby" style="font-family:monospace;"><span class="kw1">class</span> Myfirst
<span class="kw1">def</span> <span class="kw2">self</span>.<span class="me1">hi</span>
<span class="kw2">return</span> <span class="st0">&quot;Hello world&quot;</span>
<span class="kw1">end</span>
<span class="kw1">def</span> <span class="kw2">self</span>.<span class="me1">sum</span><span class="br0">&#40;</span>x,y<span class="br0">&#41;</span>
<span class="kw2">return</span> x<span class="sy0">+</span>y
<span class="kw1">end</span>
<span class="kw1">end</span></pre></div></div></p>
<p>- gemspec<br />
Modify the specification of the gem by changing the version to &#8217;0.0.2&#8242;:<br />
<div id="wpshdo_18" class="wp-synhighlighter-outer"><div id="wpshdt_18" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_18"></a><a id="wpshat_18" class="wp-synhighlighter-title" href="#codesyntax_18"  onClick="javascript:wpsh_toggleBlock(18)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_18" onClick="javascript:wpsh_code(18)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_18" onClick="javascript:wpsh_print(18)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_18" class="wp-synhighlighter-inner" style="display: block;"><pre class="ruby" style="font-family:monospace;"><span class="re2">Gem::Specification</span>.<span class="me1">new</span> <span class="kw1">do</span> <span class="sy0">|</span>s<span class="sy0">|</span>
s.<span class="me1">name</span>        = <span class="st0">&quot;myfirst&quot;</span>
s.<span class="me1">version</span>     = <span class="st0">'0.0.2'</span>
s.<span class="me1">date</span>        = <span class="st0">'2013-04-07'</span>
s.<span class="me1">authors</span>     = <span class="br0">&#91;</span><span class="st0">&quot;Max Ivak &quot;</span><span class="br0">&#93;</span>
s.<span class="me1">email</span>       = <span class="br0">&#91;</span><span class="st0">&quot;maxivak@gmail.com&quot;</span><span class="br0">&#93;</span>
s.<span class="me1">summary</span>     = <span class="st0">&quot;My very first&quot;</span>
s.<span class="me1">description</span> = <span class="st0">&quot;Simple Hello World&quot;</span>
s.<span class="me1">homepage</span>    = <span class="st0">&quot;http://github.com/maxivak/myfirst&quot;</span>
s.<span class="me1">files</span>       = <span class="kw4">Dir</span><span class="br0">&#91;</span><span class="st0">&quot;{lib}/**/*.rb&quot;</span>, <span class="st0">&quot;bin/*&quot;</span>, <span class="st0">&quot;LICENSE&quot;</span>, <span class="st0">&quot;*.md&quot;</span><span class="br0">&#93;</span>
<span class="kw1">end</span></pre></div></div></p>
<p>- Build<br />
<div id="wpshdo_19" class="wp-synhighlighter-outer"><div id="wpshdt_19" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_19"></a><a id="wpshat_19" class="wp-synhighlighter-title" href="#codesyntax_19"  onClick="javascript:wpsh_toggleBlock(19)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_19" onClick="javascript:wpsh_code(19)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_19" onClick="javascript:wpsh_print(19)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_19" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash" style="font-family:monospace;">gem build myfirst.gemspec</pre></div></div></p>
<p>After this you should see a new file myfirst-0.0.2.gem.</p>
<p>- publish<br />
Now publish a new version to RubyGems<br />
<div id="wpshdo_20" class="wp-synhighlighter-outer"><div id="wpshdt_20" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_20"></a><a id="wpshat_20" class="wp-synhighlighter-title" href="#codesyntax_20"  onClick="javascript:wpsh_toggleBlock(20)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_20" onClick="javascript:wpsh_code(20)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_20" onClick="javascript:wpsh_print(20)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_20" class="wp-synhighlighter-inner" style="display: block;"><pre class="bash" style="font-family:monospace;">gem push myfirst-0.0.2.gem</pre></div></div></p>
<p>- Check<br />
Finally, check that a new version is available at RubyGems &#8211; https://rubygems.org/gems/myfirst.<br />
You should see two versions of your gem: &#8217;0.0.1&#8242; and &#8217;0.0.2&#8242;.<br />
<a name="gems"></a></p>
<h2>Gems to build your own gem</h2>
<p>You use one of the gems that help you build a gem, including</p>
<p><a href="http://github.com/lazyatom/gem-this">gem-this</a>, <a href="http://seattlerb.rubyforge.org/hoe/">hoe</a>, <a href="http://github.com/drnic/newgem">newgem</a>, <a href="http://github.com/parfait/bones">bones</a>, <a href="http://github.com/dcrec1/gemhub">gemhub</a>, and <a href="http://blog.evanweaver.com/files/doc/fauna/echoe/files/README.html">echoe</a>.<br />
<a name="ref"></a></p>
<h2>References</h2>
<p>Read more details about creating a gem at official guide &#8211; <a href="http://guides.rubygems.org/make-your-own-gem/">http://guides.rubygems.org/make-your-own-gem/</a></p>
<p>* The Basics of Creating Rails Plugins &#8211; <a href="http://guides.rubyonrails.org/plugins.html">http://guides.rubyonrails.org/plugins.html</a></p>
<p>&nbsp;</p>
<p><strong>Realties</strong></p>
<p>* Creating your Railtie &#8211; <a href="http://edgeapi.rubyonrails.org/classes/Rails/Railtie.html">http://edgeapi.rubyonrails.org/classes/Rails/Railtie.html</a></p>
<p>&nbsp;</p>
<p><strong>Engines:</strong></p>
<p>* Getting Started with Engines &#8211; <a href="http://edgeguides.rubyonrails.org/engines.html">http://edgeguides.rubyonrails.org/engines.html</a></p>
<p>* Create your own Rails 3 Engine &#8211; <a href="http://gregmoreno.wordpress.com/2012/05/29/create-your-own-rails-3-engine/">http://gregmoreno.wordpress.com/2012/05/29/create-your-own-rails-3-engine/</a></p>
<p>&nbsp;</p>
<p><strong>Create your own gem using jeweler</strong></p>
<p>* Jeweler gem &#8211; <a href="https://github.com/technicalpickles/jeweler">https://github.com/technicalpickles/jeweler</a><br />
* <a href="http://blog.galk.me/post/7443618295/getting-started-with-gems-how-to-create-a-gem-for?9da3bc78?94bd6898">http://blog.galk.me/post/7443618295/getting-started-with-gems-how-to-create-a-gem-for</a></p>
<p>* <a href="http://kashirevanna.blogspot.com/2011/12/ruby-gem-jeweler.html">http://kashirevanna.blogspot.com/2011/12/ruby-gem-jeweler.html</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/how-to-create-your-own-gem-for-rails-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using CDN with Rails assets</title>
		<link>http://maxivak.com/using-cdn-with-rails-assets-on-rails/</link>
		<comments>http://maxivak.com/using-cdn-with-rails-assets-on-rails/#comments</comments>
		<pubDate>Mon, 25 Mar 2013 18:00:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[assets]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=993</guid>
		<description><![CDATA[Using CDN (Content Delivery Network) server to serve assets (images and other static files like css, js) on Rails is very easy with assets pipeline. &#160; Change config in production.rb: If you have an image at http://mydomain.com/assets/myimage.jpg then it will be replaced on your HTML  page with URL http://mydomain.com/myimage.jpg (or http://mydomain.com/myfolder/myimage.jpg for the second example). [...]]]></description>
			<content:encoded><![CDATA[<p><span id="more-993"></span>Using CDN (Content Delivery Network) server to serve assets (images and other static files like css, js) on Rails is very easy with assets pipeline.</p>
<p>&nbsp;</p>
<p>Change config in production.rb:</p>
<div id="wpshdo_21" class="wp-synhighlighter-outer"><div id="wpshdt_21" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_21"></a><a id="wpshat_21" class="wp-synhighlighter-title" href="#codesyntax_21"  onClick="javascript:wpsh_toggleBlock(21)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_21" onClick="javascript:wpsh_code(21)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_21" onClick="javascript:wpsh_print(21)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_21" class="wp-synhighlighter-inner" style="display: block;"><pre class="rails" style="font-family:monospace;"><span class="co1"># serve all assets from CDN server</span>
config.<span class="me1">action_controller</span>.<span class="me1">asset_host</span> = <span class="st0">'http://cdn.mydomain.com'</span>
&nbsp;
<span class="co1">#or if your files in a folder on CDN server</span>
config.<span class="me1">action_controller</span>.<span class="me1">asset_host</span> = <span class="st0">'http://cdn.mydomain.com/myfolder'</span></pre></div></div>
<div id="file-rails-assets-cdn-LC6">If you have an image at http://mydomain.com/assets/myimage.jpg then it will be replaced on your HTML  page with URL http://mydomain.com/myimage.jpg (or http://mydomain.com/myfolder/myimage.jpg for the second example).</div>
<p>To use CDN server only for images (but not for stylesheets or javascripts):</p>
<div id="wpshdo_22" class="wp-synhighlighter-outer"><div id="wpshdt_22" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_22"></a><a id="wpshat_22" class="wp-synhighlighter-title" href="#codesyntax_22"  onClick="javascript:wpsh_toggleBlock(22)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_22" onClick="javascript:wpsh_code(22)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_22" onClick="javascript:wpsh_print(22)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_22" class="wp-synhighlighter-inner" style="display: block;"><pre class="rails" style="font-family:monospace;"><span class="co1"># serve only images</span>
config.<span class="me1">action_controller</span>.<span class="me1">asset_host</span> = <span class="kw4">Proc</span>.<span class="kw5">new</span> <span class="br0">&#123;</span> <span class="sy0">|</span>source<span class="sy0">|</span>
<span class="kw1">if</span> source =~ <span class="sy0">/</span>\b<span class="br0">&#40;</span>.<span class="me1">png</span><span class="sy0">|</span>.<span class="me1">jpg</span><span class="sy0">|</span>.<span class="me1">gif</span><span class="br0">&#41;</span>\b<span class="sy0">/</span>i
<span class="st0">&quot;http://cdn.mydomain.com&quot;</span>
<span class="kw1">end</span>
<span class="br0">&#125;</span></pre></div></div>
<p>Examples on github:</p>
<p><a href="https://gist.github.com/maxivak/5239159">https://gist.github.com/maxivak/5239159</a></p>
<p>&nbsp;</p>
<p>For more flexible settings you can use this gem: <a href="https://github.com/cmer/rails-assets-cdn">https://github.com/cmer/rails-assets-cdn</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/using-cdn-with-rails-assets-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rails disable logging of asset pipeline messages</title>
		<link>http://maxivak.com/rails-disable-logging-of-asset-pipeline-messages/</link>
		<comments>http://maxivak.com/rails-disable-logging-of-asset-pipeline-messages/#comments</comments>
		<pubDate>Fri, 22 Mar 2013 21:27:58 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=989</guid>
		<description><![CDATA[There are many assets related log messages in log file. It causes Rack to use the Rails logger very  frequently. To hide the messages, add this line to your config (config/application.rb or config/environments/production.rb or config/environment.rb): &#160; Solution 2. Place the following code in config/initializers/quiet_assets.rb The code was found here. &#160; &#160; Discussions: * http://stackoverflow.com/questions/6312448/how-to-disable-logging-of-asset-pipeline-sprockets-messages-in-rails-3-1 * [...]]]></description>
			<content:encoded><![CDATA[<p>There are many assets related log messages in log file. It causes Rack to use the Rails logger very  frequently.</p>
<p><span id="more-989"></span></p>
<p>To hide the messages, add this line to your config (<em>config/application.rb</em> or <em>config/environments/production.rb</em> or <em>config/environment.rb<em>):</em></em></p>
<div id="wpshdo_23" class="wp-synhighlighter-outer"><div id="wpshdt_23" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_23"></a><a id="wpshat_23" class="wp-synhighlighter-title" href="#codesyntax_23"  onClick="javascript:wpsh_toggleBlock(23)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_23" onClick="javascript:wpsh_code(23)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_23" onClick="javascript:wpsh_print(23)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_23" class="wp-synhighlighter-inner" style="display: block;"><pre class="rails" style="font-family:monospace;">1. <span class="me1">config</span>.<span class="me1">assets</span>.<span class="me1">logger</span> = <span class="kw2">false</span>
config.<span class="me1">assets</span>.<span class="me1">debug</span> = <span class="kw2">false</span> <span class="co1"># optional</span>
2. <span class="me1">rake</span> assets:precompile RAILS_ENV=production</pre></div></div>
<p>&nbsp;</p>
<p>Solution 2.</p>
<p>Place the following code in config/initializers/quiet_assets.rb</p>
<div id="wpshdo_24" class="wp-synhighlighter-outer"><div id="wpshdt_24" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_24"></a><a id="wpshat_24" class="wp-synhighlighter-title" href="#codesyntax_24"  onClick="javascript:wpsh_toggleBlock(24)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_24" onClick="javascript:wpsh_code(24)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_24" onClick="javascript:wpsh_print(24)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_24" class="wp-synhighlighter-inner" style="display: block;"><pre class="rails" style="font-family:monospace;"><span class="kw1">if</span> Rails.<span class="me1">env</span>.<span class="me1">development</span>?
Rails.<span class="me1">application</span>.<span class="me1">assets</span>.<span class="me1">logger</span> = <span class="kw4">Logger</span>.<span class="kw5">new</span><span class="br0">&#40;</span><span class="st0">'/dev/null'</span><span class="br0">&#41;</span>
<span class="re2">Rails::Rack</span>::<span class="kw4">Logger</span>.<span class="me1">class_eval</span> <span class="kw1">do</span>
<span class="kw1">def</span> call_with_quiet_assets<span class="br0">&#40;</span>env<span class="br0">&#41;</span>
previous_level = Rails.<span class="me1">logger</span>.<span class="me1">level</span>
Rails.<span class="me1">logger</span>.<span class="me1">level</span> = <span class="kw4">Logger</span>::ERROR <span class="kw1">if</span> env<span class="br0">&#91;</span><span class="st0">'PATH_INFO'</span><span class="br0">&#93;</span> =~ <span class="sy0">%</span>r<span class="br0">&#123;</span>^<span class="sy0">/</span>assets<span class="sy0">/</span><span class="br0">&#125;</span>
call_without_quiet_assets<span class="br0">&#40;</span>env<span class="br0">&#41;</span>
<span class="kw1">ensure</span>
Rails.<span class="me1">logger</span>.<span class="me1">level</span> = previous_level
<span class="kw1">end</span>
alias_method_chain :<span class="kw5">call</span>, <span class="re3">:quiet_assets</span>
<span class="kw1">end</span>
<span class="kw1">end</span></pre></div></div>
<p>The code was found <a href="http://stackoverflow.com/questions/6312448/how-to-disable-logging-of-asset-pipeline-sprockets-messages-in-rails-3-1">here</a>.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><span style="text-decoration: underline;">Discussions:</span></p>
<p>* <a href="http://stackoverflow.com/questions/6312448/how-to-disable-logging-of-asset-pipeline-sprockets-messages-in-rails-3-1">http://stackoverflow.com/questions/6312448/how-to-disable-logging-of-asset-pipeline-sprockets-messages-in-rails-3-1</a></p>
<p>* Benchmarking of performance &#8211; <a href="https://github.com/rails/rails/pull/3795">https://github.com/rails/rails/pull/3795</a></p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/rails-disable-logging-of-asset-pipeline-messages/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Password protecting pages of Rails App</title>
		<link>http://maxivak.com/password-protecting-pages-of-rails-app/</link>
		<comments>http://maxivak.com/password-protecting-pages-of-rails-app/#comments</comments>
		<pubDate>Mon, 18 Mar 2013 06:20:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=982</guid>
		<description><![CDATA[Often it is needed to close a whole app or some pages of a Rails app in Beta mode. A simple and quick way is using http authentication. So users (beta testers, admin) can access a Rails app by entering a single username and password. This example shows how to password protect the whole app [...]]]></description>
			<content:encoded><![CDATA[<p>Often it is needed to close a whole app or some pages of a Rails app in Beta mode. A simple and quick way is using http authentication. So users (beta testers, admin) can access a Rails app by entering a single username and password.<br />
<span id="more-982"></span></p>
<p>This example shows how to password protect the whole app in beta environment using a single login/password.</p>
<p>Modify ApplicationController. Change username and password to your own.</p>
<div id="wpshdo_25" class="wp-synhighlighter-outer"><div id="wpshdt_25" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_25"></a><a id="wpshat_25" class="wp-synhighlighter-title" href="#codesyntax_25"  onClick="javascript:wpsh_toggleBlock(25)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_25" onClick="javascript:wpsh_code(25)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_25" onClick="javascript:wpsh_print(25)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_25" class="wp-synhighlighter-inner" style="display: block;"><pre class="rails" style="font-family:monospace;"><span class="kw1">class</span> ApplicationController <span class="sy0">&lt;</span> <span class="kw4"><span class="re2">ActionController::Base</span></span>
<span class="kw5">before_filter</span> <span class="re3">:authenticate</span> <span class="kw1">if</span> Rails.<span class="me1">env</span>.<span class="me1">beta</span>?
&nbsp;
<span class="kw1">def</span> authenticate
authenticate_or_request_with_http_basic<span class="br0">&#40;</span><span class="st0">'Administration'</span><span class="br0">&#41;</span> <span class="kw1">do</span> <span class="sy0">|</span>username, password<span class="sy0">|</span>
username == <span class="st0">'admin'</span> <span class="sy0">&amp;&amp;</span> password == <span class="st0">'password'</span>
<span class="kw1">end</span>
<span class="kw1">end</span>
<span class="kw1">end</span></pre></div></div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>References:</strong></p>
<p>* <a href="http://apidock.com/rails/v3.2.3/ActionController/HttpAuthentication/Basic/ControllerMethods/authenticate_or_request_with_http_basic">authenticate_or_request_with_http_basic</a><br />
* <a href="http://www.cowboycoded.com/2011/04/11/lock-down-a-rails-3-app-with-a-single-password-using-lock/">Lock down a Rails 3 app on Nginx </a></p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/password-protecting-pages-of-rails-app/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rails Best Practices</title>
		<link>http://maxivak.com/rails-best-practices/</link>
		<comments>http://maxivak.com/rails-best-practices/#comments</comments>
		<pubDate>Sun, 06 Jan 2013 05:03:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=972</guid>
		<description><![CDATA[A collection of tips and tricks to make Rails applications better. Split out Fat Models Split out your Fat Models into Services, Decorators and Presenters The approach is to put: - Business Logic into Services - Human Data into Decorators - Complex Data Manipulations into Presenters This will follow the single responsibility principle. So our [...]]]></description>
			<content:encoded><![CDATA[<p>A collection of tips and tricks to make Rails applications better.<br />
<span id="more-972"></span></p>
<h2>Split out Fat Models</h2>
<p>Split out your Fat Models into <strong>Services</strong>, <strong>Decorators</strong> and <strong>Presenters</strong><br />
The approach is to put:</p>
<p>- Business Logic into Services</p>
<p>- Human Data into Decorators</p>
<p>- Complex Data Manipulations into Presenters</p>
<p>This will follow the single responsibility principle.</p>
<p>So our Models, Controllers and Helpers really slim.</p>
<p>Ref.</p>
<p><a href="http://reverbhq.com/blog/2012/08/building-web-apps-with-activeadmin/">http://reverbhq.com/blog/2012/08/building-web-apps-with-activeadmin/</a></p>
<p>&nbsp;</p>
<p><strong>Models</strong></p>
<p>Models are used for data access, persistency and consistency.<br />
Models are slim. They only take care of associations, validations, scopes and a few request &amp; mutating methods.</p>
<p><strong>Views</strong></p>
<p>Views either display a Model, a Decorator or a Presenter. There is almost no logic in them. Logic should live in the Decorator or Presenter.</p>
<p><strong>Controller Actions</strong></p>
<p>The CRUD actions are managed by Active Admin through InheritedResources. Custom actions are delegated to a Service.</p>
<p><strong>Services: Business Logic</strong><br />
A Service implements the Business Logic of the application. It can be tested via Unit or Integration Tests. A Service is likely to rely on ActiveRecord models or other Services.</p>
<p>&nbsp;</p>
<p><strong>Decorators: One Model Data for Human-Readable Data</strong></p>
<p>A Decorator adds methods for reading by humans to a model.</p>
<div id="wpshdo_26" class="wp-synhighlighter-outer"><div id="wpshdt_26" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_26"></a><a id="wpshat_26" class="wp-synhighlighter-title" href="#codesyntax_26"  onClick="javascript:wpsh_toggleBlock(26)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_26" onClick="javascript:wpsh_code(26)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_26" onClick="javascript:wpsh_print(26)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_26" class="wp-synhighlighter-inner" style="display: block;"><pre class="rails" style="font-family:monospace;"><span class="kw1">class</span> UserDecorator <span class="sy0">&lt;</span> <span class="re2">Draper::Base</span>
  decorates <span class="re3">:user</span>
&nbsp;
  <span class="kw1">def</span> full_name
    <span class="br0">&#91;</span>user.<span class="me1">first_name</span>, user.<span class="me1">last_name</span><span class="br0">&#93;</span>.<span class="me1">compact</span>.<span class="me1">join</span> <span class="st0">' '</span>
  <span class="kw1">end</span>
<span class="kw1">end</span></pre></div></div>
<p><strong>Presenters</strong></p>
<p>Presenters gather data from multiple models and process it into an understandable data structure.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3>Gems</h3>
<p>Creating your own gems &#8211; <a href="http://maxivak.com/how-to-create-your-own-gem-for-rails-3/">http://maxivak.com/how-to-create-your-own-gem-for-rails-3/</a></p>
<p>&nbsp;</p>
<h2>Code examples</h2>
<p>&nbsp;</p>
<p>* <a href="http://maxivak.com/password-protecting-pages-of-rails-app/">Password protecting pages of Rails App</a></p>
<p><strong>ActiveRecord, Models:</strong></p>
<p>- <a href="https://gist.github.com/4465497">Get a list of IDs from database</a></p>
<p>&nbsp;</p>
<p><strong>Countries, Cities</strong></p>
<p>- <a href="http://maxivak.com/rails-3-select-tag-with-country-list/">Select Tag with List of Countries</a></p>
<p>&nbsp;</p>
<p><strong>Read more:</strong></p>
<p>- <a href="http://rails-bestpractices.com/posts/archive">Rails Best Practices</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/rails-best-practices/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Link to download an image as attachment in Rails (send_file and remote files)</title>
		<link>http://maxivak.com/rails-link_to-to-download-an-image-immediately-instead-of-opening-it-in-the-browser-send_file-and-remote-files/</link>
		<comments>http://maxivak.com/rails-link_to-to-download-an-image-immediately-instead-of-opening-it-in-the-browser-send_file-and-remote-files/#comments</comments>
		<pubDate>Tue, 01 Jan 2013 23:29:18 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=963</guid>
		<description><![CDATA[Build a link_to to download an image. send_file send_file method can be used in a controller to download the file in browser: &#160; But in case when you have an url for the remote file (for example, an image stored in amazon cloud storage like Amazon S3) then this code will give an error &#8220;Cannot [...]]]></description>
			<content:encoded><![CDATA[<p>Build a link_to to download an image.</p>
<p><span id="more-963"></span></p>
<p><strong>send_file</strong></p>
<p>send_file method can be used in a controller to download the file in browser:</p>
<div id="wpshdo_27" class="wp-synhighlighter-outer"><div id="wpshdt_27" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_27"></a><a id="wpshat_27" class="wp-synhighlighter-title" href="#codesyntax_27"  onClick="javascript:wpsh_toggleBlock(27)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_27" onClick="javascript:wpsh_code(27)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_27" onClick="javascript:wpsh_print(27)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_27" class="wp-synhighlighter-inner" style="display: block;"><pre class="rails" style="font-family:monospace;"><span class="kw5">send_file</span> <span class="st0">'/path/to/file'</span>, <span class="re3">:type</span> <span class="sy0">=&gt;</span> <span class="st0">'image/jpeg'</span>, <span class="re3">:disposition</span> <span class="sy0">=&gt;</span> <span class="st0">'attachment'</span></pre></div></div>
<p>&nbsp;</p>
<p>But in case when you have an url for the remote file (for example, an image stored in amazon cloud storage like Amazon S3) then this code</p>
<div id="wpshdo_28" class="wp-synhighlighter-outer"><div id="wpshdt_28" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_28"></a><a id="wpshat_28" class="wp-synhighlighter-title" href="#codesyntax_28"  onClick="javascript:wpsh_toggleBlock(28)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_28" onClick="javascript:wpsh_code(28)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_28" onClick="javascript:wpsh_print(28)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_28" class="wp-synhighlighter-inner" style="display: block;"><pre class="rails" style="font-family:monospace;"><span class="kw5">send_file</span> <span class="st0">'http://someserver.com/path/to/file'</span>, <span class="re3">:type</span> <span class="sy0">=&gt;</span> <span class="st0">'image/jpeg'</span>, <span class="re3">:disposition</span> <span class="sy0">=&gt;</span> <span class="st0">'attachment'</span></pre></div></div>
<p>will give an error &#8220;Cannot read file http://..&#8221;.</p>
<p>According to this <a href="http://apidock.com/rails/ActionController/Streaming/send_file">documentation</a> send_file expects the first parameter to be the path to your file, i.e. the file should be stored locally on this server.</p>
<p>send_file(path, options = {})</p>
<p>&nbsp;</p>
<p><strong>send_data</strong></p>
<p>Instead of send_file you can use <strong>send_data</strong> method as follows:</p>
<div id="wpshdo_29" class="wp-synhighlighter-outer"><div id="wpshdt_29" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_29"></a><a id="wpshat_29" class="wp-synhighlighter-title" href="#codesyntax_29"  onClick="javascript:wpsh_toggleBlock(29)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_29" onClick="javascript:wpsh_code(29)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_29" onClick="javascript:wpsh_print(29)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_29" class="wp-synhighlighter-inner" style="display: block;"><pre class="rails" style="font-family:monospace;"><span class="kw3">require</span> <span class="st0">'open-uri'</span>
url = <span class="st0">'http://someserver.com/path/../filename.jpg'</span>
data = <span class="kw3">open</span><span class="br0">&#40;</span>url<span class="br0">&#41;</span>.<span class="me1">read</span>
<span class="kw5">send_data</span> data, <span class="re3">:disposition</span> <span class="sy0">=&gt;</span> <span class="st0">'attachment'</span>, <span class="re3">:filename</span><span class="sy0">=&gt;</span><span class="st0">&quot;photo.jpg&quot;</span></pre></div></div>
<p>&nbsp;</p>
<p>All code examples are avaiable here on github:</p>
<p><a href="https://gist.github.com/4430975">https://gist.github.com/4430975</a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/rails-link_to-to-download-an-image-immediately-instead-of-opening-it-in-the-browser-send_file-and-remote-files/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python vs Ruby and Django vs. Rails</title>
		<link>http://maxivak.com/python-django-vs-ruby-on-rails/</link>
		<comments>http://maxivak.com/python-django-vs-ruby-on-rails/#comments</comments>
		<pubDate>Sat, 29 Dec 2012 23:51:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=950</guid>
		<description><![CDATA[In this post I am collecting opinions about pros and cons of using two popular web frameworks Python&#8217;s Django and Ruby on Rails in web development. I tried to include all factors that matter to choose between two frameworks. Python vs Ruby General Both languages are good, modern, well-designed, flexible, good for your productivity. &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I am collecting opinions about pros and cons of using two popular web frameworks Python&#8217;s Django and Ruby on Rails in web development. I tried to include all factors that matter to choose between two frameworks.</p>
<p><span id="more-950"></span></p>
<h2>Python vs Ruby</h2>
<p><strong>General</strong></p>
<p>Both languages are good, modern, well-designed, flexible, good for your productivity.</p>
<p>&nbsp;</p>
<p><strong>Philosophy</strong></p>
<p>- <strong>Python</strong> really believes that code readability is the most important thing. Hence there is one-true way of writing code.<br />
Python likes things to be structured, consistent, and simple</p>
<p>-<strong> Ruby</strong> believes in giving programmer the flexibility, freedom and power . It provides a better way to write concise and compact code.</p>
<p>More into the expressiveness of the code and writing code that is clever.</p>
<p>&nbsp;</p>
<p>- Both Ruby and Rails have more &#8216;magic&#8217; involved. This makes development very fast when you understand the magic, but frustrating when you don&#8217;t.</p>
<p>- Django is easier to learn and has less &#8220;magic&#8221;.</p>
<p>&nbsp;</p>
<p>Python people like libraries to be transparent and obvious how they work, while Ruby people tend to provide clean and pretty interfaces with &#8220;magic&#8221; behind the scenes.</p>
<p>&nbsp;</p>
<p><strong>Convention over configuration</strong></p>
<p>- The centerpiece of Rails’s philosophy is called convention over configuration(CoC). Rails provides you with more defaults.</p>
<p>- Django on the other hand let’s you specify most configuration details yourself.</p>
<p>&nbsp;</p>
<p><strong>Application</strong></p>
<p>- Python is more mature, general purpose nature, vs Ruby&#8217;s more niche (Rails) usage.</p>
<p>- Ruby+Rails holds a slight edge over Python+Django for web development, and has more mindshare.</p>
<p>- Python is stronger for things like data manipulation, analytics, system administration, scientific programming,  etc.</p>
<p>&nbsp;</p>
<p><strong>Community</strong></p>
<p>- There are two great communities behind both frameworks, which bring you plugins, extensions and so on.</p>
<p>Some people believe that Python has a more developed community. Python community has developed tons of libraries suited for data analysis, machine learning, natural language processing, scientific libraries.</p>
<p>But the number of gems and extenstions to Rails is growing fast.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>It&#8217;s more a choice you&#8217;ll made between their philosophies than their (similar) features set.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2>Django vs. Rails (with ActiveAdmin or RailsAdmin)</h2>
<p>Ruby on Rails and Python (Django) are two leading frameworks.</p>
<p>Both are great MVC frameworks on top of a great language.</p>
<p>&nbsp;</p>
<p><strong>General</strong></p>
<p>Django is more declarative you&#8217;ll have a clearer understanding of what&#8217;s actually going on.<br />
Rails prefers convention over configuration.</p>
<p>&nbsp;</p>
<p><strong>First application</strong></p>
<p>Compared to Rails, Django creates a much simpler project structure.</p>
<p>&nbsp;</p>
<h2>Rails features</h2>
<p><strong>Convention over configuration (CoC)</strong><br />
CoC in Rails basically means that a Rails project has a predefined layout. All components (models, views, controllers, layouts, css, javascript, etc) have standard places where you should drop them and the application picks them up without any additional effort on your part.</p>
<p><strong>DRY</strong></p>
<p>Another centerpiece in Rails is the “Don’t Repeat Yourself”(DRY) principle.</p>
<p>&nbsp;</p>
<p><strong>MVC</strong></p>
<p>Rails is a classical Model-View-Controller full stack web framework.<br />
What separates it from most of the MVC frameworks around is the heavy emphasis on REST.</p>
<p><strong>Model</strong> contains the application’s business logic, <strong>Controller</strong> invokes functionality from the model layer and feeds the resulting data to <strong>View</strong>, which display the data in a readable way to the user.</p>
<p><strong>ORM</strong><br />
The model layer is the home of your domain objects and the business logic surrounding them.<br />
Domain objects (a.k.a. entities) are mapped to database tables (at least in RDBMS).<br />
Unlike most object relational mappers Rails’s ActiveRecord (the default ORM; could be substituted with something else if you wish) doesn’t require you to explicitly declare the structure of the objects, but rather extracts it automatically from your DB table definitions. A simple class, modeling an application user might look like this:</p>
<p>- Finders &#8211; Rails generates automatically for us “finders” that we can use to make queries about objects in pure Ruby.</p>
<p>- DSL to express relationships between various model classes (belongs_to, has_many, etc)</p>
<p>&nbsp;</p>
<p><strong>Views and HTML Templates</strong></p>
<p>In the view layer Rails relies on HTML templates with Ruby code embedded in them (html.erb).</p>
<p>There are a lot of community contributed alternatives, however, with <strong>HAML</strong> being the most prominent one.</p>
<p>Rails allows you to use SASS as a replacement for traditional CSS.</p>
<p>Rails provides the following tools to be used in views to follow its philosophy (DRY and CoC): partial templats, layout, helpers.</p>
<p>&nbsp;</p>
<p><strong>Ajax and Rails</strong></p>
<p>Rails features tight integration with JavaScript and AJAX. jQuery is the default JavaScript library.</p>
<p>Making and handling AJAX requests in Rails is generally very easy.</p>
<p>&nbsp;</p>
<p><strong>Testing </strong></p>
<p>Rails heavily promotes testing your code. All of the Rails generators would generate test stubs, that you’ll do good to fill in.</p>
<p>Rails supports all major Ruby test frameworks &#8211; Test::Unit, RSpec, Cucumber.</p>
<p>Features:</p>
<p>- fixtures support,</p>
<p>- test database support,</p>
<p>- the ability to run unit and functional test separately.</p>
<p>Rails really pays much attention to having tests.</p>
<p>&nbsp;</p>
<p><strong>Deployment</strong></p>
<p>The most common deployment options for Rails are currently Apache HTTPD or NginX and Phusion Passenger (mod_rails).</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2>Django features</h2>
<p>&nbsp;</p>
<p><strong>MVC</strong></p>
<p>Django calls its separation of domains MTV (model-template-view) instead of the traditional MVC. It uses templates instead of Rail’s views.</p>
<p>While Django is a MVC controller framework as well, it’s built around a different mindset.</p>
<p>Django is totally configurable and minimalistic framework that empowers the developers to tailor it to their needs.</p>
<p>A Django project is comprised of autonomous and reusable apps. Apps on the other hand are comprised of models, views and templates.</p>
<p>&nbsp;</p>
<p><strong>ORM</strong></p>
<p>Django’s default ORM is somewhat reminiscent of frameworks like Hibernate. Entity classes declare explicitly the all the attributes. So you have to write more code than you do in Rails.</p>
<p>Like in Rails we get useful &#8220;finders&#8221; that can be used to query for model objects in pure python (as opposed to using sql).</p>
<p>&nbsp;</p>
<p><strong>View (template layer)</strong></p>
<p>While ERB is basically HTML with Ruby embedded in it, Django features a custom templating language with it’s own (extendable) tag library. This generally means that Django templates tend to be a bit cleaner than Rails’s templates, since you’re not allowed to abuse them very much.</p>
<p>On the other hand you can do virtually anything by embedding code directly in a template, so as usual &#8211; each design decision has its pros and cons.<br />
Django supports alternative templating libraries, so you’re covered in case you don’t like the default one.</p>
<p>&nbsp;</p>
<p><strong>Form generation</strong></p>
<p>Django features a powerful form generation/handling facilities that integrate seamlessly with the templating layer. For instance &#8211; it very easy to generate a form matching the structure of a domain model object with automatic property validation.</p>
<p>Rails also has gems for helping generating forms (<a href="https://github.com/justinfrench/formtastic">Formtastic gem</a>) and client-side validations.</p>
<p>&nbsp;</p>
<p><strong>Javascript and Ajax</strong></p>
<p>Unlike Rails, Django doesn’t bundle any JavaScript libraries. You have to pick a JavaScript framework yourself (which a trivial process).</p>
<p>The AJAX support in Django is both basic and extremely powerful in the same time &#8211; request.is_ajax(). What this means is that you can check if a request was regular or an AJAX request and respond accordingly.</p>
<p>&nbsp;</p>
<p><strong>Testing</strong></p>
<p>- Environments<br />
This is a feature that comes out of the box in Rails and doesn’t in Django. Django is explicit, and you can make your environment system.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>Deployment</strong></p>
<p>Python is quite mature technology and offers you many deployment options &#8211; Apache, Nginx and Google App Engine.<br />
The also the possibility to deploy Django apps on Java or .Net infrastructure using Jython or IronPython.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2>Admin area</h2>
<p>Django comes with authentication and extensible admin built in. It makes it easy to build standard pages for admin to manage your data.</p>
<p>It makes development of simple CMSs really easy.</p>
<p>It is one of the Django advantages &#8211; the automatic admin interface you get for free out-of-the-box.</p>
<p>Rails doesn&#8217;thave a built-in support of admin. But you have a choice to use several available extenstions (gems). Currently the two most populars admin UI &#8216;frameworks’&#8217; are <a href="https://github.com/sferik/rails_admin">RailsAdmin</a> and <a href="http://activeadmin.info/">ActiveAdmin</a>.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>Read more about comparison of RailsAdmin with ActiveAdmin:</p>
<p>- <a href="http://batsov.com/articles/2011/11/20/admin-interfaces-for-rails-apps-railsadmin-vs-activeadmin/">http://batsov.com/articles/2011/11/20/admin-interfaces-for-rails-apps-railsadmin-vs-activeadmin/</a></p>
<p>- <a href="http://stackoverflow.com/questions/6542075/rails-admin-vs-activeadmin">http://stackoverflow.com/questions/6542075/rails-admin-vs-activeadmin</a></p>
<p>&nbsp;</p>
<p><strong>References:</strong></p>
<p>- Discussion on stackoverflow: <a href="http://stackoverflow.com/questions/91846/rails-or-django-or-something-else">http://stackoverflow.com/questions/91846/rails-or-django-or-something-else</a></p>
<p>- <a href="http://batsov.com/articles/2011/06/19/django-vs-rails/">http://batsov.com/articles/2011/06/19/django-vs-rails/</a></p>
<p>- <a href="http://wiki.alcidesfonseca.com/rails-vs-django/">RAILS VS DJANGO</a></p>
<p>- <a href="http://www.quora.com/Ruby-vs-Python/Which-should-I-learn-Django-or-Rails">Which should I learn: Django or Rails? (Discussion on Quora)</a></p>
<p>- <a href="http://gilesbowkett.blogspot.com/2009/07/do-you-believe-in-magic.html">Do You Believe In Magic?</a>, written in 2009</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/python-django-vs-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sessions do not work in Internet Explorer in Rails</title>
		<link>http://maxivak.com/sessions-do-not-work-in-internet-explorer-in-rails/</link>
		<comments>http://maxivak.com/sessions-do-not-work-in-internet-explorer-in-rails/#comments</comments>
		<pubDate>Sat, 29 Dec 2012 17:35:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[sessions]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=943</guid>
		<description><![CDATA[Recently I had a strange problem when sessions didn&#8217;t work on my site on Rails in production environment. The problem was in Internet Explorer while it worked in Firefox and Chrome. I found out that Internet Explorer doesn&#8217;t set cookies for some reason. I&#8217;ve tried many settings for session_store in my config like and many [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had a strange problem when sessions didn&#8217;t work on my site on Rails in production environment. The problem was in Internet Explorer while it worked in Firefox and Chrome.</p>
<p><span id="more-943"></span></p>
<p>I found out that Internet Explorer doesn&#8217;t set cookies for some reason.</p>
<p>I&#8217;ve tried many settings for session_store in my config like</p>
<div id="wpshdo_30" class="wp-synhighlighter-outer"><div id="wpshdt_30" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_30"></a><a id="wpshat_30" class="wp-synhighlighter-title" href="#codesyntax_30"  onClick="javascript:wpsh_toggleBlock(30)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_30" onClick="javascript:wpsh_code(30)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_30" onClick="javascript:wpsh_print(30)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_30" class="wp-synhighlighter-inner" style="display: block;"><pre class="rails" style="font-family:monospace;">config.<span class="me1">session_store</span> <span class="re3">:cookie_store</span>, key: <span class="st0">'_sess'</span>, <span class="re3">:domain</span> <span class="sy0">=&gt;</span> <span class="re3">:all</span>
&nbsp;
config.<span class="me1">session_store</span> <span class="re3">:cookie_store</span>, key: <span class="st0">'_sess'</span>, <span class="re3">:domain</span> <span class="sy0">=&gt;</span> <span class="st0">'.mysite.com'</span>
&nbsp;
config.<span class="me1">session_store</span> <span class="re3">:cookie_store</span>, key: <span class="st0">'_sess'</span>, <span class="re3">:domain</span> <span class="sy0">=&gt;</span> <span class="st0">'.mysite.com'</span>, <span class="re3">:secure</span> <span class="sy0">=&gt;</span> <span class="kw2">false</span></pre></div></div>
<p>and many many others. But it didn&#8217;t help.</p>
<p>The problem was in the name of my subdomain &#8211; it had an underscore ( &#8220;_&#8221; ), for example, dev_rails.mysite.com.</p>
<p><strong>IE has problems accepting cookies from subdomains that contain underscore.</strong></p>
<p>In order to work in IE subdomains must meet the requirements of the <a href="http://www.ietf.org/rfc/rfc2396.txt">URI RFC</a>.</p>
<p>&nbsp;</p>
<p><strong><span style="text-decoration: underline;">Read more:</span></strong></p>
<p><a href="http://stackoverflow.com/questions/794243/internet-explorer-ignores-cookies-on-some-domains-cannot-read-or-set-cookies">http://stackoverflow.com/questions/794243/internet-explorer-ignores-cookies-on-some-domains-cannot-read-or-set-cookies</a></p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/sessions-do-not-work-in-internet-explorer-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>US Government: You Don&#8217;t Own Your Cloud Data So We Can Access It At Any Time</title>
		<link>http://maxivak.com/us-government-you-dont-own-your-cloud-data-so-we-can-access-it-at-any-time/</link>
		<comments>http://maxivak.com/us-government-you-dont-own-your-cloud-data-so-we-can-access-it-at-any-time/#comments</comments>
		<pubDate>Sun, 04 Nov 2012 11:34:55 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[cloud]]></category>
		<category><![CDATA[us]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=938</guid>
		<description><![CDATA[The US government&#8217;s argument that you lose all your property rights by storing your data on the cloud could apply to Amazon&#8217;s S3 or Google Apps or Apple iCloud services as well. &#8220;On Tuesday the EFF filed a brief proposing a process for the Court in the Megaupload case to hold the government accountable for [...]]]></description>
			<content:encoded><![CDATA[<p>The US government&#8217;s argument that <strong>you lose all your property rights by storing your data on the cloud</strong> could apply to Amazon&#8217;s S3 or Google Apps or Apple iCloud services as well.</p>
<p><span id="more-938"></span>&#8220;On Tuesday the EFF filed a brief proposing a process for the Court in the Megaupload case to hold the government accountable for the actions it took (and failed to take) when it shut down Megaupload&#8217;s service and denied third parties access to their property. Many businesses used Megaupload&#8217;s cloud service to store and share files not related to piracy. The government is calling for a long, drawn-out process that would require individuals or small companies to travel to courts far away and engage in multiple hearings just to get their own property back.</p>
<p>Would this stop you from storing your data on the cloud?</p>
<p>&nbsp;</p>
<p>The full news story &#8211; <a href="https://www.eff.org/deeplinks/2012/10/governments-attack-cloud-computing" target="_blank">Megaupload and the Government&#8217;s Attack on Cloud Computing</a></p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/us-government-you-dont-own-your-cloud-data-so-we-can-access-it-at-any-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to block showing your site in IFrame (Anti-iframe)</title>
		<link>http://maxivak.com/how-to-block-showing-your-site-in-iframe-anti-iframe/</link>
		<comments>http://maxivak.com/how-to-block-showing-your-site-in-iframe-anti-iframe/#comments</comments>
		<pubDate>Tue, 30 Oct 2012 21:41:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[iframe]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://maxivak.com/?p=934</guid>
		<description><![CDATA[Recently, I found that one of my popular sites is shown as part of other site. So visitors of that not-mine site, being on that site, can access all the services of my sites. They used my site content and services via iframe and took benefits from my site. Below is the solution how to [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I found that one of my popular sites is shown as part of other site. So visitors of that not-mine site, being on that site, can access all the services of my sites. They used my site content and services via <strong>iframe</strong> and took benefits from my site.</p>
<p>Below is the solution how to prevent this, i.e. block showing your site content via iframe. The solution is pretty simple.<br />
<span id="more-934"></span></p>
<p>Put this javascript code on all your pages:</p>
<div id="wpshdo_31" class="wp-synhighlighter-outer"><div id="wpshdt_31" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_31"></a><a id="wpshat_31" class="wp-synhighlighter-title" href="#codesyntax_31"  onClick="javascript:wpsh_toggleBlock(31)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_31" onClick="javascript:wpsh_code(31)" title="Show code only"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_31" onClick="javascript:wpsh_print(31)" title="Print code"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="http://maxivak.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img border="0" style="border: 0 none" src="http://maxivak.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_31" class="wp-synhighlighter-inner" style="display: block;"><pre class="javascript" style="font-family:monospace;"><span class="sy0">&lt;</span>script<span class="sy0">&gt;</span>if<span class="br0">&#40;</span>self <span class="sy0">!=</span> top<span class="br0">&#41;</span> <span class="br0">&#123;</span> top.<span class="me1">location</span> <span class="sy0">=</span> self.<span class="me1">location</span><span class="sy0">;</span> <span class="br0">&#125;</span><span class="sy0">&lt;/</span>script<span class="sy0">&gt;</span></pre></div></div>
<p>If one site will show your site in iframe, it will redirect the browser to your site directlty, so that user will will your site in browser as it is.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://maxivak.com/how-to-block-showing-your-site-in-iframe-anti-iframe/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
