Blog Entries tagged "rails"

SSH Tunnels into Production Rails Database

<p>Today I needed to access a production database for my Rails app directly using a GUI application on my mac, so I figured out that this can be done by creating an SSH tunnel like so:</p> <div class="highlight"><pre class="highlight shell"><code>ssh <span class="nt">-Ng</span> <span class="nt">-L</span> &lt;local-port&gt;:&lt;remote-host&gt;:&lt;remote-port&gt; &lt;user&gt;@&lt;remote-host&gt; ssh <span class="nt">-Ng</span> <span class="nt">-L</span> 3307:100.64.26.11:3307 adam@100.64.26.11 </code></pre></div> <p>And then, in your Rails app, update the database.yml as if you were connecting to a local database, but specify the proper database name.</p> <div class="highlight"><pre class="highlight yaml"><code><span class="na">development</span><span class="pi">:</span> <span class="na">adapter</span><span class="pi">:</span> <span class="s">mysql2</span> <span class="na">database</span><span class="pi">:</span> <span class="s">myapp_production</span> <span class="na">host</span><span class="pi">:</span> <span class="s">127.0.0.1</span> <span class="na">port</span><span class="pi">:</span> <span class="m">3307</span> <span class="na">username</span><span class="pi">:</span> <span class="s">root</span> <span class="na">password</span></code></pre></div>

Read More...

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...

Authenticating your Angular / Rails App with JSON Web Tokens

<p><em>UPDATE: There have been some changes in the JWT Gem that make some of the below not work exactly right (it’ll still be about 90% the same). Specifically, they added expiration support. See <a href="/2015/07/20/authentication-using-json-web-tokens-using-rails-and-react">my post on the same topic, but using React.js</a>. The server side code in this post will work just as well with Angular.</em></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...