Blog Entries tagged "ruby"

Ruby Symbol and String Array Shorthand

<p>Ruby has some handy Array shorthands, but I always forget which is which, so I thought I should write them down.</p> <h3 id="symbol-array-shorthand">Symbol Array Shorthand:</h3> <div class="highlight"><pre class="highlight ruby"><code><span class="sx">%i{foo bar}</span> <span class="c1"># =&gt; [:foo, :bar]</span> </code></pre></div> <h3 id="string-array-shorthand">String Array Shorthand:</h3> <div class="highlight"><pre class="highlight ruby"><code><span class="sx">%w{foo bar}</span> <span class="c1"># =&gt; ["foo", "bar"]</span> </code></pre></div> <p>Both also work with square brackets <code>%w[a b c]</code> or parenthesis <code>%i(a b c)</code></p>

Rails presence method

<p>I use Rails&rsquo; <code>present?</code> method constantly, but recently I stumbled across the <code>presence</code> method.</p> <p>In the past, I have often found myself doing the following because params[:foo] could be nil or a blank string.</p> <div class="highlight"><pre class="highlight ruby"><code><span class="n">foo</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="ss">:foo</span><span class="p">].</span><span class="nf">present?</span> <span class="p">?</span> <span class="n">params</span><span class="p">[</span><span class="ss">:foo</span><span class="p">]</span> <span class="p">:</span> <span class="s2">"something else"</span> </code></pre></div> <p>I hate that i have to write <code>params[:foo]</code> twice. But the <code>presence</code> method is handy for this case. It returns nil if the item is nil or a blank string, but otherwise returns the value.</p> <div class="highlight"><pre class="highlight ruby"><code><span class="n">foo</span> <span class="o">=</span> <span class="n">params</span><span class="p">[</span><span class="ss">:foo</span><span class="p">].</span><span class="nf">presence</span> <span class="o">||</span> <span class="s2">"something-else"</span> </code></pre></div>

TIL: Pass in an ActiveRecord Object to a where string

<p>I’m surprised I didn’t already know this, but if you have an ActiveRecord <code>where</code> statement that references an id column, you can just pass in an object and it will pass in the object’s id, not the string representation of the object.</p> <div class="highlight"><pre class="highlight ruby"><code><span class="n">company</span> <span class="o">=</span> <span class="no">Company</span><span class="p">.</span><span class="nf">find</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="no">User</span><span class="p">.</span><span class="nf">where</span><span class="p">(</span><span class="s2">"users.company_id = ?"</span><span class="p">,</span> <span class="n">company</span><span class="p">)</span> <span class="c1"># is equal to</span> <span class="no">User</span><span class="p">.</span><span class="nf">where</span><span class="p">(</span><span class="s2">"users.company_id = ?"</span><span class="p">,</span> <span class="n">company</span><span class="p">.</span><span class="nf">id</span><span class="p">)</span> </code></pre></div> <p>So when you create named scopes, you don’t have to do the song-and-dance where you check to see if the object is a model or an integer, as shown below.</p> <div class="highlight"><pre class="highlight ruby"><code><span class="n">scope</span> <span class="ss">:by_company</span><span class="p">,</span> <span class="nb">lambda</span> <span class="p">{</span> <span class="o">|</span><span class="n">company</span><span class="o">|</span> ...</code></pre></div>

Read More...

Run your Ruby tests quickly and easily using Vim and Tmux

<p>In order for testing to become part of your development workflow, it needs to become a habit. And like any habit, its biggest enemy is neglect. Too often I’ll be in a rush and not add tests to my code for a day, and that turns into a week and then a month, and suddenly I have an app where half of my codebase is untested and the other half has breaking tests. There are many things you can do to help keep this habit (hooking up a CI server immediately comes to mind), but an important one is to make running your tests as quick and easy as possible.</p> <p>One way I do this is by making my ruby tests (either Rspec or Minitest) extremely easy to run while I’m using <a href="http://www.vim.org">Vim</a> (and <a href="https://tmux.github.io">Tmux</a>). With one quick keystroke, I can run the current test file or individual test in a new Tmux pane.</p> <p></p>

Read More...

Authentication with JSON Web Tokens using Rails and React / Flux

<p>In a <a href="/2014/12/04/add-json-web-token-authentication-to-your-angular-rails-app">previous post</a>, I went over how to add authentication to your Rails + Angular app using JSON Web Tokens (JWT). This time, I’ll do the same, but using the <a href="2015/07/17/how-to-replace-the-angular-stack-with-the-react-ecosystem">React ecosystem</a>. But even if you’re using another front-end framework (Angular, Ember, Backbone), this post will be helpful because it fixes some issues with the previous server-side code that broke due to a change in the <a href="https://github.com/progrium/ruby-jwt">jwt gem</a>.</p> <p></p>

Read More...

Pre-Filling PDF Form Templates in Ruby-on-Rails with PDFtk

<p>In <a href="/2014/01/14/generate-clean-testable-pdf-reports-in-rails-with-prawn">a recent post</a>, I talked about how to generate PDF reports in Rails using Prawn. This approach is great for generating PDF’s with lots of data tables and other variable-length content. But an alternative situation is when you already have a template authored in an application such as Adobe Acrobat and you want to populate it with data from your database. This makes it more difficult to insert variable-length content, but on the plus side, you no longer need to worry about the layout of the document.</p> <p></p>

Read More...

Generate Clean, Testable PDF Reports in Ruby/Rails with Prawn

<p>I generally hate PDF’s. The file format is complex and designed to mimic physical paper documents, which really has little to do with the web. But unfortunately, PDF’s are still very common and often expected, particularly when working on businesses applications. I have a legacy ruby-on-rails application with a number of PDF reports and I recently took the time to refactor them in a clean and testable manner. Here’s how I went about that process:</p> <p></p>

Read More...