Blog

RSS Feed

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>

VSCode + ElixirLS = đź‘Ťđź‘Ť

<p>I’ve played around with <a href="https://code.visualstudio.com">VSCode</a> here and there, but as a fairly picky Vim user who doesn’t do TypeScript, I never quite understood the hype.</p> <p>Today I started up a new Elixir / Phoenix project (more on that to come) and tried out the <a href="https://github.com/JakeBecker/vscode-elixir-ls">Elixir Language Server Extension</a> and the integration is <strong>very</strong> impression. Code completion, debugger support, automatic inference of Dialyzer <a href="https://hexdocs.pm/elixir/typespecs.html">Typespecs</a>, documentation on hover, and more….</p> <p>I don’t expect to be using VSCode as my standard editor (again, picky Vim user<sup id="fnref1"><a href="#fn1">1</a></sup>), but I think I’ll stick with it on Elixir projects.</p> <div class="footnotes"> <hr> <ol> </ol> </div>

Read More...

Netlify Analytics First Impressions

<p>My new website is hosted on <a href="https://www.netlify.com">Netlify</a>, which is an <strong>awesome</strong> service for hosting static websites. Recently, <a href="https://www.netlify.com/blog/2019/07/10/netlify-analytics---accurate-insights-without-performance-impacts/">they launched an Analytics Platform</a> as an alternative to Google Analytics. My impressions of this new tool isn’t quite as enthusiastic as their service in general, but it does have some nice features.</p> <div style="text-align:center; margin-bottom: 1rem"> <img src="/assets/images/netlify_analytics_screenshot.png" alt="Netlify Analytics Screenshot"> </div> <h3 id="pros">Pros:</h3> <h5 id="server-side">Server Side</h5> <ul> <li>Can’t be blocked by Ad-Blockers</li> <li>Tracks requests of non-web-page assets like RSS feeds, images, etc</li> <li>No impact on your client side performance since there’s no Javascript library</li> </ul> <h5 id="privacy-centric">Privacy Centric</h5> <ul> <li>Isn’t connected to an ad network...</li> </ul>

Read More...

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

Lookup IP Address & Location via the Command Line

<p>With <a href="https://ifconfig.co">ifconfig.co</a>, you can lookup your external IP Address and Location Information using a simple curl command. You can use it like so:</p> <div class="highlight"><pre class="highlight shell"><code><span class="nv">$ </span>curl ifconfig.co/json <span class="o">{</span> <span class="s2">"ip"</span>: <span class="s2">"96.27.202.202"</span>, <span class="s2">"ip_decimal"</span>: 1612409628, <span class="s2">"country"</span>: <span class="s2">"United States"</span>, <span class="s2">"country_iso"</span>: <span class="s2">"US"</span>, <span class="s2">"city"</span>: <span class="s2">"Columbus"</span>, <span class="s2">"hostname"</span>: <span class="s2">"d27-96-101-101.nap.wideopenwest.com"</span> <span class="o">}</span> <span class="nv">$ </span>curl ifconfig.co/city Columbus <span class="nv">$ </span>curl ifconfig.co/country United States </code></pre></div>

Website Revamp! And introduction to my Work Journal.

<p>My website has gone slightly stagnent lately, so I decided that it was time for a revamp. There were a few things I didn’t like about the old website:</p> <ul> <li>As a developer with some design chops, I never liked the fact that I hadn’t designed my website myself. I was using the <a href="http://hyde.getpoole.com">Hyde theme for Jekyll</a>.</li> <li>I felt boxed in by the fact that all my previous posts were longer-form articles.</li> <li>While <a href="https://jekyllrb.com">Jekyll</a> is a great tool, it’s very blogging-centric, and I wanted to add new types of content.</li> </ul> <p>So I re-designed the website from scratch using <a href="http://middlemanapp.com">Middleman</a>. I started with <a href="https://github.com/thoughtbot/middleman-template">Thoughtbot...</a></p>

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

How to Replace the Angular Stack with React plus a few NPM modules

<p>As you can tell from the content of my blog posts, I’ve been practicing and preaching Angular.js for quite some time now. It is an extremely productive web framework that felt like a big step forward from my days doing jQuery “sprinkles” and then Backbone.</p> <p>But then recently, I started playing with Facebook’s <a href="https://facebook.github.io/react/">React</a> framework. And while I’m still not quite as productive as I was with Angular, I absolutely love the code that I’m writing. And on top of that, it has opened my eyes to a whole new paradigm for creating user interfaces.</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...