<?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>The Wojo Group &#187; Ruby</title>
	<atom:link href="http://www.thewojogroup.com/tag/ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thewojogroup.com</link>
	<description>The musings of a small creative media company.</description>
	<lastBuildDate>Tue, 11 May 2010 00:10:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Simple Pagination with Rails</title>
		<link>http://www.thewojogroup.com/2009/05/simple-pagination-with-rails/</link>
		<comments>http://www.thewojogroup.com/2009/05/simple-pagination-with-rails/#comments</comments>
		<pubDate>Tue, 05 May 2009 14:07:17 +0000</pubDate>
		<dc:creator>Brett Wejrowski</dc:creator>
				<category><![CDATA[Motionspire]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[content]]></category>
		<category><![CDATA[pages]]></category>
		<category><![CDATA[pagination]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.thewojogroup.com/?p=510</guid>
		<description><![CDATA[While I was developing Motionspire, I needed to implement a pagination system for the video content.  I looked through the existing plugins, but found that I really desired something extremely simple and custom to work within my search controller.  With only a few lines of code, I created a very basic system for managing pagination [...]]]></description>
			<content:encoded><![CDATA[<p>While I was developing <a href="http://www.motionspire.com">Motionspire</a>, I needed to implement a pagination system for the video content.  I looked through the existing plugins, but found that I really desired something extremely simple and custom to work within my search controller.  With only a few lines of code, I created a very basic system for managing pagination that can be ported to other sites with ease.</p>
<h4>The Search Controller</h4>
<p>The Motionspire search controller uses one function and view for returning any content, whether it&#8217;s filtered or not, so the pagination only required one copy of the code to be implemented.  I keep a session variable for maintaining the user&#8217;s preference for items per page, and if that parameter has been sent to the search function I set the session variable :</p>
<pre name="code" class="ruby">

if params[:itemsPerPage] then session[:itemsPerPage] = params[:itemsPerPage] end
</pre>
<p>When I run though the search function, I see if a page parameter has been passed:</p>
<pre name="code" class="ruby">

if params[:page] then @page = params[:page].to_i else @page = 1 end
</pre>
<p>After I retrieve the articles pertaining to the search parameters (in this case the results are stored into an array called @videos), I check the :itemsPerPage preference and find the last possible page:</p>
<pre name="code" class="ruby">

if session[:itemsPerPage] then @per = session[:itemsPerPage].to_i else @per = 24 end

@lastpage = @videos.length/(@per) + 1

if @videos.length%(@per) == 0

    @lastpage = @lastpage - 1

end
</pre>
<p>I have a default itemsPerPage value of 24 hardcoded, and you can change this for your search.</p>
<p>From here, we simply return the search results for that page, using the current page and the items per page preference:</p>
<pre name="code" class="ruby">

@videos = @videos[((@page-1)*@per),@per]
</pre>
<p><em>For search queries within a large data set, I would recommend using these parameters with the sql query with the &#8216;ORDER BY&#8217; and &#8216;LIMIT&#8217; in order to avoid returning extremely large results unnecessarily.</em></p>
<h4>The Helper</h4>
<p>First, I created a short helper function that finds the min and max pages, ensuring we don&#8217;t show a negative page or a page that doesn&#8217;t exist:</p>
<pre name="code" class="ruby">

def minmaxpage(current,total,option)
    min = current - 2
    if min &lt; 1
        min = 1
        max = 1 + 4
        if max &gt; total then max = total end
    end
    max = min + 4
    if max &gt; total
        max = total
        min = max - 4
        while min &lt; 1
            min = min + 1
        end
    end
    if option == &#039;min&#039; then return min else return max end
end
</pre>
<h4>The View</h4>
<p>For the content from the view, we use the @videos array to present the content for the page.  The only custom coding necessary is in the links for the pages.  For this site, I provide a first and last page link, along with 2 pages on either side of the current page.  You can manipulate this code how you want, but the basic idea will remain the same.</p>
<p>First we grab the min and max page from the helper:</p>
<pre name="code" class="ruby">

&lt;% min = minmaxpage(@page, @lastpage, &#039;min&#039;) %&gt;

&lt;% max = minmaxpage(@page, @lastpage, &#039;max&#039;) %&gt;
</pre>
<p>For the code for the page links, we simply check to see if the min page is within two of the current:</p>
<pre name="code" class="ruby">

&lt;% if min &lt; @page %&gt;

    &lt;%= link_to( &quot;Prev&quot;,

        :action =&gt; &quot;search&quot;,

        :page =&gt; (@page - 1) ) %&gt;

&lt;% end %&gt;

&lt;% if min &gt; 1 %&gt;

    &lt;%= link_to( &quot;1&quot;,

        :action =&gt; &quot;search&quot;,

        :page =&gt; 1 ) %&gt;

&lt;% end %&gt;

&lt;% x = min %&gt;
</pre>
<p>Then we loop through the remaining pages, ensuring we don&#8217;t go over the max, and make the current page simple text instead of a link:</p>
<pre name="code" class="ruby">

&lt;% while x &lt;= max %&gt;

    &lt;% if x.to_i == @page.to_i %&gt;

        &lt;span class=&quot;current&quot;&gt;&lt;%= x %&gt;&lt;/span&gt;

    &lt;% else %&gt;

        &lt;%= link_to( x.to_s,

            :action =&gt; &quot;search&quot;,

            :page =&gt; x ) %&gt;

    &lt;% end %&gt;

    &lt;% x = x+1 %&gt;

&lt;% end %&gt;

&lt;% if max &lt; @lastpage %&gt;

    &lt;%= link_to( @lastpage.to_s,

        :action =&gt; &quot;search&quot;,

        :page =&gt; @lastpage ) %&gt;

&lt;% end %&gt;

&lt;% if max &gt; @page %&gt;

    &lt;%= link_to( &quot;Next&quot;,

        :action =&gt; &quot;search&quot;,

        :page =&gt; (@page + 1) ) %&gt;

&lt;% end %&gt;
</pre>
<p>The code may look a little long, but the implementation took very little time.  I know exactly what the code is doing and it is very easy to expand this for different situations and code.</p>
<p>There are a lot of solutions for pagination of content, but I found that this simple implementation helped me avoid using plugins and save time.  Let me know if you guys have any questions or comparable solutions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thewojogroup.com/2009/05/simple-pagination-with-rails/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Writing your own code vs. Plugins</title>
		<link>http://www.thewojogroup.com/2008/09/writing-your-own-code-vs-plugins/</link>
		<comments>http://www.thewojogroup.com/2008/09/writing-your-own-code-vs-plugins/#comments</comments>
		<pubDate>Tue, 30 Sep 2008 14:06:16 +0000</pubDate>
		<dc:creator>Brett Wejrowski</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Coding Style]]></category>
		<category><![CDATA[Plugins]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[RoR]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.thewojogroup.com/?p=160</guid>
		<description><![CDATA[Recently I have been discussing with some fellow coders the benefits of plugins, and when you should write your own code.  I basically wanted to throw out a couple of ideas and hope for some discussion on the topic.  Let me know what you guys think and I'll do a follow up soon.]]></description>
			<content:encoded><![CDATA[<p>Recently I have been discussing with some fellow coders the benefits of plugins, and when you should write your own code.  I basically wanted to throw out a couple of ideas and hope for some discussion on the topic.  Let me know what you guys think and I&#8217;ll do a follow up soon.</p>
<h3>Some Background</h3>
<p>I, like many of you out there, started programming back in the day by messing around with computers, writing little calculator programs, making websites, etc.  But I really didn&#8217;t get into it much more until I started college, where I immediately started programming with C++.  </p>
<p>Most of my classes started out with projects where we had to write everything (no STL!), and all of my code was from scratch.  Once I started doing more of the &#8220;development&#8221; in &#8220;web design and development&#8221;, I still practiced the idea that the only code I know is good is the code I write.</p>
<h3>But now I can see&#8230;</h3>
<p>Then one day, the skies parted, trumpets sounded, and my designer Steve said &#8220;Hey, have you heard of Ruby on Rails?&#8221;  </p>
<p>I immediately started developing with nearly pure Rails, using the built-in-just-about-everything and loving the fact that anything I couldn&#8217;t code (or didn&#8217;t want to) was usually readily available in plugins ( I love file_column!).  </p>
<p>I will say this, RoR is good code.  And it&#8217;s constantly being improved.  For the most part, the plugins are also good code.  And many times, a plugin works perfectly into your project to save you time and add functionality.  </p>
<h3>Good Programming</h3>
<p>Ask any real programmer, and the efficiency of code is not only about how it performs, but how long it takes you to create.  So obviously there is a trade-off between using plugins and writing your own code.  </p>
<p>That being said, if you are a web designer who is using rails to help you out, or just trying something new, use the plugins: they are great and it will save you days of coding and headaches.  </p>
<p>If you consider yourself a developer (or hope to), you should still use plugins.  If you code everything, you&#8217;re going to waste time. However, don&#8217;t completely rely on them.  If you want to be a programmer, you need to know how things work, because some day there won&#8217;t be a plugin for you, and you&#8217;re going to need to know what to do.  </p>
<p>Also, plugins are meant to be helpful for a range of situations.  Therefore, there is going to be some overhead.  If you are concerned with performance, and a plugin isn&#8217;t &#8220;perfect&#8221; for what you&#8217;re trying to do; you might need to code from scratch (or at least be prepared to customize the plugin code).</p>
<h3>Here&#8217;s the thing&#8230;</h3>
<p>All things considered, I will say that my natural tendency is to code most everything myself.  I like to know where everything is, and <strong>exactly </strong>what the code is doing.  I also appreciate when the code is only doing exactly what I want it to do.  If it&#8217;s impractical to write something, by all means I will ( and do ) use plugins.  </p>
<p>If you want some good coding experience, and have the time, try writing something yourself that you wouldn&#8217;t normally do.  Even if you don&#8217;t end up using it, you&#8217;ll learn a bit more about RoR (or any other language) and you may just have some good code you can use again.</p>
<p>Agree? Disagree? Don&#8217;t care?</p>
<p>I&#8217;m just one guy.  Let me know what you guys think.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thewojogroup.com/2008/09/writing-your-own-code-vs-plugins/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Remember Me&#8217;s with Rails</title>
		<link>http://www.thewojogroup.com/2008/09/remember-mes-with-rails/</link>
		<comments>http://www.thewojogroup.com/2008/09/remember-mes-with-rails/#comments</comments>
		<pubDate>Fri, 26 Sep 2008 18:51:13 +0000</pubDate>
		<dc:creator>Brett Wejrowski</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[expiration]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[remember me]]></category>
		<category><![CDATA[RoR]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[session]]></category>

		<guid isPermaLink="false">http://www.thewojogroup.com/?p=150</guid>
		<description><![CDATA[I recently had a need for a login system that needed a 'remember me' function.  After hours of looking through countless blogs, I came to the conclusion that either (1) people don't use a remember me function with Rails logins, or (2) they don't write about it.  In this article, I outline a simple remember me system using the cookie variable in Rails that will tack on to most custom authentication systems.  ]]></description>
			<content:encoded><![CDATA[<p>I recently had a need for a login system that <em>needed </em>a &#8216;remember me&#8217; function.  After hours of looking through countless blogs, I came to the conclusion that either (1) people don&#8217;t use a remember me function with custom authentication systems for Rails, or (2) they don&#8217;t talk about it.  In this article, I outline a simple remember me system using the <span style="color: #008000;">cookie</span> variable in Rails that will tack on to most custom authentication systems.</p>
<p>Using the <span style="color: #008000;">cookie</span> functions in Rails is pretty <a href="http:/http://api.rubyonrails.org/classes/ActionController/Cookies.html" target="_blank">straightforward.</a> It&#8217;s used with the ActionController and is quite simple to use.  Most people use <span style="color: #008000;">sessions </span>for authentication, which is a good idea.  <span style="color: #008000;">Sessions</span>, unlike cookies, automatically save your content as encrypted strings using the browsers cookies. ActionController#<span style="color: #008000;">cookie</span> provides a method for saving information in the browser, but you need to hash the content yourself if need be.</p>
<p>However, if a user selects the remember me option when logging in, we would like to have the <span style="color: #008000;">session</span> expiration set to be a longer period, like 30 days.  Unfortunately, this is quite difficult to do if you don&#8217;t want to change the expiration of ALL sessions.</p>
<p>My site already uses <span style="color: #008000;">sessions</span> for authentication, and I&#8217;m going to leave that be.  In fact, I&#8217;m not going to change anything about the <span style="color: #008000;">session</span> variable <em>at all. </em>This way, I can add this remember function to almost any authentication system I use in the future very easily.</p>
<p>When a user is authenticated and has selected the &#8220;remember me&#8221; option, I do two things:</p>
<p>- create a cookie that stores (<strong>plain text</strong>) the user&#8217;s <span style="color: #ff0000;">id</span><span style="color: #ff0000;"> </span>(you can use name, email, etc. but I prefer the id because it says nothing about the user to anyone trying to get information)</p>
<p>- create a second cookie with an <strong>hashed string</strong> of some other information about the user( name, email, address )</p>
<pre name="code" class="ruby">
if params[:rememberMe]
userId = (@user.id).to_s
cookies[:remember_me_id] = { :value =&gt; userId, :expires =&gt; 30.days.from_now }
userCode = Digest::SHA1.hexdigest( @user.email )[4,18]
cookies[:remember_me_code] = { :value =&gt; userCode, :expires =&gt; 30.days.from_now }
end
</pre>
<p>For the hashing of the second piece of information, use a hash such as SHA1 or MD5.  We can use these two cookies to authenticate a user when they return after a session has expired.</p>
<pre name="code" class="ruby">
if ( cookies[:remember_me] and cookies[:remember_me] and User.find( cookies[:remember_me]) and Digest::SHA1.hexdigest( User.find( cookies[:remember_me] ).email )[4,18] == cookies[:remember_me_code]  )
@u = User.find( cookies[:remember_me_id] )
session[&#039;user&#039;] = @u.id
end
</pre>
<p>Just work that into your <span style="color: #ff9900;">:before_filter</span> for your authentication system, and you&#8217;re all set.  Make sure you delete the variables when someone logs out:</p>
<pre name="code" class="ruby">
if cookies[:remember_me_id] then cookies.delete :remember_me_id end
if cookies[:remember_me_code] then cookies.delete :remember_me_code end
</pre>
<p>****Edit: make sure to have &#8220;require &#8216;digest/sha1&#8242;&#8221; at the top of any page where you are using the SHA1 hash.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.thewojogroup.com/2008/09/remember-mes-with-rails/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
