<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet type="text/xml" href="/http/davidstump.net/feed.xslt.xml"?><feed xmlns="http://www.w3.org/2005/Atom"><generator uri="http://jekyllrb.com" version="3.3.0">Jekyll</generator><link href="/http/davidstump.net/feed.xml" rel="self" type="application/atom+xml" /><link href="/" rel="alternate" type="text/html" /><updated>2017-03-01T17:06:01+00:00</updated><id>//</id><subtitle>Hacking Adventures in Rails, Phoenix, Swift and More</subtitle><entry><title type="html">David Stump - Building Programmers Podcast</title><link href="/http/davidstump.net/2017/02/21/building-programmers-podcast.html" rel="alternate" type="text/html" title="David Stump - Building Programmers Podcast" /><published>2017-02-21T00:00:00+00:00</published><updated>2017-02-21T00:00:00+00:00</updated><id>/2017/02/21/building-programmers-podcast</id><content type="html" xml:base="/2017/02/21/building-programmers-podcast.html">&lt;div&gt;
  &lt;iframe width=&quot;560&quot; height=&quot;80&quot; scrolling=&quot;no&quot; frameborder=&quot;no&quot; src=&quot;https://building.fireside.fm/26/player/iframe&quot;&gt;&lt;/iframe&gt;
&lt;/div&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;about-this-episode&quot;&gt;About this Episode&lt;/h3&gt;
&lt;p&gt;This week, Kevin and David talk about becoming a programmer, fun languages, and the irresistible siren song that emanates from southwestern Ohio. Then they play the hot game “YCombinator Startup or Star Trek: Deep Space Nine Character”. How does David do? Who is Michael Dorn? Tune in to find out!&lt;/p&gt;

&lt;p&gt;Please check out all of the other awesome episodes on &lt;a href=&quot;https://building.fireside.fm&quot;&gt;Building Programmers&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;episode-links&quot;&gt;Episode Links&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://davidstump.net&quot;&gt;David Stump&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://twitter.com/davidstump&quot;&gt;David Stump (@davidstump) - Twitter &lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://littlelines.com&quot;&gt;Ruby on Rails Consulting &amp;amp; Web Design Firm - Littlelines&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://github.com/davidstump&quot;&gt;davidstump (David Stump) @ Github&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;episode-details&quot;&gt;Episode Details&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;February 21st, 2017&lt;/li&gt;
  &lt;li&gt;35 mins 33 secs&lt;/li&gt;
&lt;/ul&gt;</content><summary type="html">I had the pleasure of being a featured guest on the Building Programmers podcast. On Building Programmers, Kevin McGladdery interviews software developers about their careers, their current responsibilities, and how they became programmers. Then he makes them play ill-conceived game show games for worthless points! Delightful! This week, Kevin and David talk about becoming a programmer, fun languages, and the irresistible siren song that emanates from southwestern Ohio. Then they play the hot game 'YCombinator Startup or Star Trek - Deep Space Nine Character'. How does David do? Who is Michael Dorn? Tune in to find out!</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="/assets/images/building-programming.jpg" /></entry><entry><title type="html">Using Phoenix with Legacy Rails Applications</title><link href="/http/davidstump.net/2016/09/27/phoenix-with-rails.html" rel="alternate" type="text/html" title="Using Phoenix with Legacy Rails Applications" /><published>2016-09-27T00:00:00+00:00</published><updated>2016-09-27T00:00:00+00:00</updated><id>/2016/09/27/phoenix-with-rails</id><content type="html" xml:base="/2016/09/27/phoenix-with-rails.html">&lt;p&gt;Here’s the situation: I love writing Elixir and Phoenix, but many of my clients have existing legacy Rails applications that have been running in production for years. Doing a full rewrite of all of their systems at once is time-consuming, expensive and potentially dangerous. The best-case scenario would be to gradually add or update small features on their existing application utilizing Phoenix behind the scenes. As more and more of these features get updated, the app slowly transforms from a “Rails App with a little bit of Phoenix”, to a “Phoenix App with a little bit of legacy Rails”. The nice part of this approach is that it is easy to experiment with and gradually make changes without introducing more bugs and degrading the overall user-experience of your client’s application. This way we can add value for our client by leveraging an ecosystem with improved power and performance while avoiding disrupting the application flow that users are already familiar with.&lt;/p&gt;

&lt;h3 id=&quot;setup&quot;&gt;Setup&lt;/h3&gt;

&lt;p&gt;To get started, we need running Rails and Phoenix apps:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;rails&lt;/span&gt; &lt;span class=&quot;kp&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_app&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;localhost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3000&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;mix&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;phoenix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_new_app&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;localhost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Next, we need an easy way to let our rails app proxy features to our new Phoenix app. For this, I used the gem &lt;code class=&quot;highlighter-rouge&quot;&gt;rails-reverse-proxy&lt;/code&gt;. After bundling, I added a basic helper method to my &lt;code class=&quot;highlighter-rouge&quot;&gt;ApplicationController&lt;/code&gt; like such:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;proxy_to_phoenix&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;reverse_proxy&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;http://localhost:4000&quot;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;config&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;on_missing&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;code&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;redirect_to&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;root_url&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;and&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This method will allow us to proxy an action over to our new Phoenix app, or redirect to our root path (or whatever makes sense for your use case) in the case of a missing path.&lt;/p&gt;

&lt;h3 id=&quot;adding-a-new-feature-in-phoenix&quot;&gt;Adding a new feature in Phoenix&lt;/h3&gt;

&lt;p&gt;Now that we have our setup complete, lets add the ability to create basic Posts in our new Phoenix app. To do that, I am going to use a generator to create a basic crud endpoint for &lt;code class=&quot;highlighter-rouge&quot;&gt;posts&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;mix&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;phoenix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;gen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;json&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Post&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;posts&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;author&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:string&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:text&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;As instructed by the generator, we need to add this line to our Phoenix routes&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;resources&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/posts'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;PostController&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And finally migrate our Phoenix app up to include the new Post feature&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;mix&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ecto&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;migrate&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Great! Now we have a working Post feature that will allow us to perform basic CRUD actions on a Post model and get JSON data back. To have some usable data to demo once we get our Rails app setup to use this new Phoenix feature I am going to add a few dummy posts:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;changeset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;changeset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;title: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Using Phoenix with Rails&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;author: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;David Stump&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;body: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Hello, from Phoenix!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Repo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;changeset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;changeset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;changeset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;title: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Another Blog Post&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;author: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;David Stump&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;body: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;I like turtles.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Repo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;changeset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;changeset&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;changeset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;title: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Even moar Blog Posts&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;author: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;David Stump&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;body: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Ping Pong&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;Repo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;insert&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;changeset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;wiring-up-our-existing-rails-application&quot;&gt;Wiring Up Our Existing Rails Application&lt;/h3&gt;

&lt;p&gt;Now that we have our setup done and our new feature implemented in Phoenix, lets introduce it into our legacy Rails app. To do this, we are going to use the reverse proxy helper we implemented in our Setup section. We will need a controller on the Rails side to handle requests to our new Posts feature.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PostsController&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ApplicationController&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;include&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ReverseProxy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Controller&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;prepend_before_action&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:proxy_to_phoenix&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;index&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;show&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;In the Rails controller above, there are two unique lines that let this handle our Phoenix feature. First we have the code &lt;code class=&quot;highlighter-rouge&quot;&gt;include ReverseProxy::Controller&lt;/code&gt; which imports the basic actions from our reverse proxy gem. Second, we have the code &lt;code class=&quot;highlighter-rouge&quot;&gt;prepend_before_action :proxy_to_phoenix&lt;/code&gt; which just tells each method in this controller to proxy to phoenix when a request to that action is received. Finally, our controller has all of the normal restful actions of a traditional Rails controller.&lt;/p&gt;

&lt;p&gt;To receive requests to this controller, we are going to need to add it to our Rails routes:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;resources&lt;/span&gt;  &lt;span class=&quot;ss&quot;&gt;:posts&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And we are all set!&lt;/p&gt;

&lt;h3 id=&quot;its-alive&quot;&gt;It’s Alive!&lt;/h3&gt;

&lt;p&gt;If we navigate to our various restful post paths in our Rails app, we will now see the appropriate JSON response coming from our Phoenix code through our existing Rails application. We have successfully introduced a new post feature using Phoenix and Elixir into our legacy Rails application. To an outside viewer, this feature looks just like any other output from our Rails app. As stated in our initial goals, this allows us to gradually introduce Phoenix into existing applications in a much slower and more deliberate fashion. (&lt;em&gt;click to enlarge GIF&lt;/em&gt;)&lt;/p&gt;

&lt;p class=&quot;center&quot;&gt;&lt;a href=&quot;/assets/images/phoenix_with_rails.gif&quot; target=&quot;_blank&quot;&gt;&lt;img src=&quot;/assets/images/phoenix_with_rails.gif&quot; alt=&quot;Phoenix with Rails Demo&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;optional-activerecord&quot;&gt;Optional: ActiveRecord&lt;/h3&gt;

&lt;p&gt;As a followup, I thought I would demonstrate one possible approach for introducing interactions to these new Post records via Rails and ActiveRecord.  To allow both applications to connect to the same data store, I entered the same MyApp credentials into the Rails &lt;code class=&quot;highlighter-rouge&quot;&gt;database.yml&lt;/code&gt; as well as the Phoenix &lt;code class=&quot;highlighter-rouge&quot;&gt;dev.exs&lt;/code&gt;. Once both applications were referencing the same data, I added a generic Rails model like so:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Post&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Base&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Once that model is in place, I can fire up a Rails console and perform the following query:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;first&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#&amp;lt;Post id: 3, title: &quot;Using Phoenix with Rails&quot;, author: &quot;David Stump&quot;, body: &quot;Hello, from Phoenix!&quot;, inserted_at: ..., updated_at: ...&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Awesome! Now we could interact with Phoenix created Posts from our existing Rails code should the need arise.&lt;/p&gt;

&lt;p&gt;But wait, you say, what about having two models with full control over the posts table in the database?? Good Point! What we need to do is to tell Rails that it is only allowed to read the posts table, and is forbidden from making any changes. We want to make sure Phoenix is the owner of that table, and all Rails can do is reference the data if it needs. To do this, we are going to update the Post model we created in Rails to this:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Post&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Base&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;after_initialize&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:readonly!&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now, if we attempt to create Posts from the rails side, we get a stern talking to from the database:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;inserted_at: &lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Date&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;today&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;k&quot;&gt;BEGIN&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ms&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;no&quot;&gt;ROLLBACK&lt;/span&gt;
  &lt;span class=&quot;no&quot;&gt;ActiveRecord&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;ReadOnlyRecord&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Post&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;marked&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;readonly&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;The strategy outlined in this articles is far from the only one, however, it provides us with a clean way to slowly integrate new features with Phoenix into an existing Rails application. This allows us to leverage all of the power, speed, fault tolerance and concurrency of Elixir without first needing to convince a boss or client to undertake a time-consuming, expensive and potentially risky full rewrite of your application. We can now quickly add powerful, fast and concurrent features to our applications today. That’s awesome :)&lt;/p&gt;

&lt;p&gt;As always, I am more than happy to add or update sections of this article as people respond with suggestions, experiences and feedback.&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;</content><summary type="html">Here’s the situation: I love writing Elixir and Phoenix, but many of my clients have existing legacy Rails applications that have been running in production for years. Doing a full rewrite of all of their systems at once is time-consuming, expensive and potentially dangerous. The best-case scenario would be to gradually add or update small features on their existing application utilizing Phoenix behind the scenes. As more and more of these features get updated, the app slowly transforms from a “Rails App with a little bit of Phoenix”, to a “Phoenix App with a little bit of legacy Rails”. The nice part of this approach is that it is easy to experiment with and gradually make changes without introducing more bugs and degrading the overall user-experience of your client’s application. This way we can add value for our client by leveraging an ecosystem with improved power and performance while avoiding disrupting the application flow that users are already familiar with.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="/assets/images/legacy-rails.jpg" /></entry><entry><title type="html">Phoenix Beyond the Browser - ElixirConf 2016</title><link href="/http/davidstump.net/2016/09/02/elixirconf.html" rel="alternate" type="text/html" title="Phoenix Beyond the Browser - ElixirConf 2016" /><published>2016-09-02T00:00:00+00:00</published><updated>2016-09-02T00:00:00+00:00</updated><id>/2016/09/02/elixirconf</id><content type="html" xml:base="/2016/09/02/elixirconf.html">&lt;div class=&quot;youtube&quot;&gt;
  &lt;iframe src=&quot;https://www.youtube.com/embed/7vBc0Q-nwUI&quot; frameborder=&quot;0&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
&lt;/div&gt;

&lt;hr /&gt;

&lt;h3 id=&quot;audience&quot;&gt;Audience&lt;/h3&gt;
&lt;p&gt;Elixir/Phoenix developers with an interest in iOS/Swift development&lt;/p&gt;

&lt;h3 id=&quot;summary&quot;&gt;Summary&lt;/h3&gt;
&lt;p&gt;Leverage the Elixir and Phoenix foundation of concurrency, speed and reliability toward making powerful realtime applications in Swift&lt;/p&gt;

&lt;h3 id=&quot;description&quot;&gt;Description&lt;/h3&gt;
&lt;p&gt;Elixir and Phoenix are built on a foundation of concurrency, speed and reliability. Thinking beyond the browser, these features are ideal for the backbone to any successful mobile application. Together we will explore leveraging the powers of Phoenix and WebSockets to get started building realtime mobile applications in Swift. Along with a basic introduction to Apple’s (relatively) new language Swift, we will explore how to setup a mobile application to communicate with a Phoenix application in realtime using Phoenix Channels. We will also discuss the impacts of some of the newer Phoenix features, like Presence, and how these can be used to enhance your applications. Attendees will get a brief introduction to the basics of iOS development, explore how to easily connect Phoenix and iOS applications through WebSockets, and discover how to use the power of Elixir and Phoenix to create complex realtime applications and games.&lt;/p&gt;</content><summary type="html">Elixir and Phoenix are built on a foundation of concurrency, speed and reliability. Thinking beyond the browser, these features are ideal for the backbone to any successful mobile application. Together we will explore leveraging the powers of Phoenix and WebSockets to get started building realtime mobile applications in Swift. Along with a basic introduction to Apple's (relatively) new language Swift, we will explore how to setup a mobile application to communicate with a Phoenix application in realtime using Phoenix Channels. We will also discuss the impacts of some of the newer Phoenix features, like Presence, and how these can be used to enhance your applications. Attendees will get a brief introduction to the basics of iOS development, explore how to easily connect Phoenix and iOS applications through WebSockets, and discover how to use the power of Elixir and Phoenix to create complex realtime applications and games.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="/assets/images/elixirconf-square.jpg" /></entry><entry><title type="html">How to Send Email From Your Phoenix App in Under 5 Minutes</title><link href="/http/davidstump.net/2015/06/08/phoenix-email.html" rel="alternate" type="text/html" title="How to Send Email From Your Phoenix App in Under 5 Minutes" /><published>2015-06-08T00:00:00+00:00</published><updated>2015-06-08T00:00:00+00:00</updated><id>/2015/06/08/phoenix-email</id><content type="html" xml:base="/2015/06/08/phoenix-email.html">&lt;p&gt;Sending scheduled or automated emails from your application is a very common problem with a number of solutions. In this write-up I am going to demonstrate one particularly easy approach to adding email functionality to your Phoenix App. &lt;a href=&quot;http://phoenixframework.org&quot;&gt;Phoenix&lt;/a&gt; is a web framework written in &lt;a href=&quot;http://elixir-lang.org&quot;&gt;Elixir&lt;/a&gt; that is enjoying a meteoric rise in popularity recently. Many people are jumping onto the Phoenix bandwagon for its explicit, non-magical and functional approach to development.&lt;/p&gt;

&lt;p&gt;For this demonstration, we are going to use an external library that integrates &lt;a href=&quot;http://mailgun.com&quot;&gt;Mailgun&lt;/a&gt; into our Phoenix application. Phoenix includes the Elixir configuration file &lt;code class=&quot;highlighter-rouge&quot;&gt;mix.exs&lt;/code&gt; which we will use to add our new dependency. We will add &lt;code class=&quot;highlighter-rouge&quot;&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;:mailgun,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;~&amp;gt; 0.1.1&quot;&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;/code&gt; to the list of “deps” our project is currently using.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;defp&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;deps&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:phoenix&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;~&amp;gt; 0.13&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
   &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:phoenix_html&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;~&amp;gt; 1.0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
   &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:phoenix_ecto&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;~&amp;gt; 0.3&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
   &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:postgrex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&amp;gt;= 0.0.0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
   &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:phoenix_live_reload&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;~&amp;gt; 0.4&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
   &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:cowboy&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;~&amp;gt; 1.0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
   &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:mailgun&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;~&amp;gt; 0.1.1&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Running &lt;code class=&quot;highlighter-rouge&quot;&gt;mix deps.get&lt;/code&gt; from our terminal will pull in all required files for including our new Mailgun library dependency.&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;gp&quot;&gt;$ &lt;/span&gt;mix deps.get
Running dependency resolution
Dependency resolution completed successfully
  mailgun: v0.1.1
&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt; Getting mailgun &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;Hex package&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
Checking package &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;https://s3.amazonaws.com/s3.hex.pm/tarballs/mailgun-0.1.1.tar&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
Using locally cached package
Unpacked package tarball &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;/Users/david/.hex/packages/mailgun-0.1.1.tar&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now we need to setup our Mailgun configuration in &lt;code class=&quot;highlighter-rouge&quot;&gt;config/config.exs&lt;/code&gt;. First, however, you will need to jump over to &lt;a href=&quot;http://mailgun.com&quot;&gt;http://mailgun.com&lt;/a&gt; and sign up for a free sandbox account. Once you are logged in, you will want to add your first domain (under the Domains tab in the top navigation). Once created, you can click on this new domain to view the &lt;code class=&quot;highlighter-rouge&quot;&gt;API Base URL&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;API Key&lt;/code&gt;. We will need to add both of these values to our config file. In your &lt;code class=&quot;highlighter-rouge&quot;&gt;config.exs&lt;/code&gt; add the following information:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;config&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:my_app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;mailgun_domain: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;YOUR_MAILGUN_DOMAIN&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;ss&quot;&gt;mailgun_key: &lt;/span&gt;&lt;span class=&quot;no&quot;&gt;System&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;MAILGUN_API_KEY&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Next we need to create a Mailer module to use throughout our application. In your &lt;code class=&quot;highlighter-rouge&quot;&gt;lib&lt;/code&gt; directory, create a new file named &lt;code class=&quot;highlighter-rouge&quot;&gt;mailer.ex&lt;/code&gt; (not required - just my preference). Within that new file you will need to define our new Mailer module and reference our Mailgun client and configuration information.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;MyApp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Mailer&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;use&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Mailgun&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Client&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;domain: &lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Application&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:my_app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:mailgun_domain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                      &lt;span class=&quot;ss&quot;&gt;key: &lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Application&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;get_env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:my_app&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:mailgun_key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;You will generally add a module attribute specifying the from address of your application emails within the Mailer module.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;vi&quot;&gt;@from&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;noreply@mydomain.com&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Finally, we add simple functions to handle the html and text versions of our application specific emails.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;send_welcome_text_email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;send_email&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;to: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;ss&quot;&gt;from: &lt;/span&gt;&lt;span class=&quot;vi&quot;&gt;@from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;ss&quot;&gt;subject: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Welcome!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;ss&quot;&gt;text: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Welcome to MyApp!&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;send_welcome_html_email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;send_email&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;to: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;ss&quot;&gt;from: &lt;/span&gt;&lt;span class=&quot;vi&quot;&gt;@from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;ss&quot;&gt;subject: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Welcome!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;ss&quot;&gt;html: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&amp;lt;strong&amp;gt;Welcome to MyApp&amp;lt;/strong&amp;gt;&quot;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Once we have our email functions complete, we are ready to send emails throughout our Phoenix app.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;no&quot;&gt;MyApp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Mailer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;send_welcome_html_email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And thats it!&lt;/p&gt;

&lt;h2 id=&quot;helpful-tips&quot;&gt;Helpful Tips:&lt;/h2&gt;
&lt;hr /&gt;

&lt;p&gt;&lt;strong&gt;Attachments&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Should you want or need to send attachments along with your email it is equally simple. All we need to do is add an &lt;code class=&quot;highlighter-rouge&quot;&gt;attachments&lt;/code&gt; key to our email and provide it with a list of maps that respond to &lt;code class=&quot;highlighter-rouge&quot;&gt;path&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;filename&lt;/code&gt;. The Mailgun library we are using handles reading the actual file so long as we give it a valid path.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;send_welcome_html_email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;send_email&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;to: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;email&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;ss&quot;&gt;from: &lt;/span&gt;&lt;span class=&quot;vi&quot;&gt;@from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;ss&quot;&gt;subject: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Welcome!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;ss&quot;&gt;html: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&amp;lt;strong&amp;gt;Welcome to MyApp&amp;lt;/strong&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
             &lt;span class=&quot;ss&quot;&gt;attachments: &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;sx&quot;&gt;%{path: &quot;priv/my_file.csv&quot;, filename: &quot;my_file.csv&quot;}&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Templates&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You will most likely want to write your markup for an HTML email outside of the Mailer module. This is extremely easy to accomplish. You will want to update the &lt;code class=&quot;highlighter-rouge&quot;&gt;text&lt;/code&gt; of your email to point at an external view. This would look like:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;ss&quot;&gt;html: &lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Phoenix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;View&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;render_to_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;MyApp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;EmailView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;welcome.html&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;some_var: &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;some_var&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Congratulations. You can now send emails throughout your Phoenix application. For more information please check out the following sources:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/chrismccord/mailgun/&quot;&gt;Mailgun Library Repo&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.phoenixframework.org/v0.14.0/docs/overview&quot;&gt;Phoenix Guides&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://elixir-lang.org/getting-started/introduction.html&quot;&gt;Elixir Guides&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cheers!&lt;/p&gt;</content><summary type="html">Sending scheduled or automated emails from your application is a very common problem with a number of solutions. In this write-up I am going to demonstrate one particularly easy approach to adding email functionality to your Phoenix App. Phoenix is a web framework written in Elixir that is enjoying a meteoric rise in popularity recently. Many people are jumping onto the Phoenix bandwagon for its explicit, non-magical and functional approach to development.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="/assets/images/phoenix-email.jpg" /></entry><entry><title type="html">Swift Extensions</title><link href="/http/davidstump.net/2014/06/06/swift-extensions.html" rel="alternate" type="text/html" title="Swift Extensions" /><published>2014-06-06T00:00:00+00:00</published><updated>2014-06-06T00:00:00+00:00</updated><id>/2014/06/06/swift-extensions</id><content type="html" xml:base="/2014/06/06/swift-extensions.html">&lt;p&gt;This past Monday, Apple made a shocking announcement. They are moving forward
without Objective-C. After the murmuring lessened, Apple went on to demonstrate
an attractive, clean and expressive new language called Swift. Once the Taylor
Swift jokes had subsided, the developer community seemed genuinely excited about
this new look for iOS development. More information about the new language can be
found on &lt;a href=&quot;https://developer.apple.com/swift/&quot;&gt;Apple’s site&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Over the past year I had begun digging into iOS development and learning Objective-C.
While the thrill of seeing something you built on your iPhone was enticing, every
time I started playing with it again, I was reminded how little enjoyment I got
out of writing Objective-C. As someone not intimately familiar with the language,
Objective-C proved hard to digest, write and understand each time I hopped back
into Xcode. Sadly, without any other option for iOS apps, your only option was to
keep trudging on. Monday changed that.&lt;/p&gt;

&lt;p&gt;During the announcement on Monday, my eyes lit up with the prospect of a clean
and easy-to-grasp language to ship applications on iOS and OSX. With that, I dove
into the &lt;a href=&quot;https://developer.apple.com/videos/wwdc/2014/&quot;&gt;WWDC Videos&lt;/a&gt; and full
length book &lt;a href=&quot;https://itunes.apple.com/us/book/the-swift-programming-language/id881256329?mt=11&quot;&gt;available for free on iBooks&lt;/a&gt;
to learn as much as I could about this new and attractive language. Coming from
Ruby, I loved what I saw: optional parenthesis, no semicolons and concise, expressive
code. To give you a sense of the improvement I saw my first steps into Swift, here
is a basic “Hello, World!” app written in both Objective-C and Swift:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Objective-C&lt;/em&gt;&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;c1&quot;&gt;#import &amp;lt;Foundation/Foundation.h&amp;gt;&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;main&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;no&quot;&gt;NSLog&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;Hello, world!&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;em&gt;Swift&lt;/em&gt;&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;n&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Foundation&lt;/span&gt;

  &lt;span class=&quot;n&quot;&gt;println&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Hello, World!&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Like the first time I experienced Perl after being a Java developer years ago,
I was hooked.&lt;/p&gt;

&lt;p&gt;One of the first powerful features in Swift I started to enjoy playing with were
&lt;a href=&quot;https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/Extensions.html&quot;&gt;Extensions&lt;/a&gt;.
Extensions, similar to monkey patching, allow you to add new functionality to
existing structures such as classes, structs, or enumerations. As a rubyist, my
first experiments have been focused on bringing some of the joy I get from using
ActiveSupport helpers to my iOS development space. An example of a basic extension
to add the ability to uppercase a string with &lt;code class=&quot;highlighter-rouge&quot;&gt;uppercase&lt;/code&gt; instead of the more verbose
&lt;code class=&quot;highlighter-rouge&quot;&gt;uppercaseString&lt;/code&gt; is as follows:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;n&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;uppercase: &lt;/span&gt;&lt;span class=&quot;no&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;uppercaseString&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This makes the following valid Swift syntax and instantly adds this method to
Xcode’s autocomplete and syntax highlighting.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;David&quot;&lt;/span&gt;
  &lt;span class=&quot;nb&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;uppercase&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# &quot;DAVID&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;To try my hand at a somewhat more technical extension I decided to add the helper
method &lt;code class=&quot;highlighter-rouge&quot;&gt;times&lt;/code&gt; to the Int class in Swift. To do this, I needed to define a function
&lt;code class=&quot;highlighter-rouge&quot;&gt;times&lt;/code&gt; that accepted a function as a parameter and executed it as many times as
defined by the initial Int value.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;n&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Int&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;times&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;task: &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;())&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;_&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;task&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Using this extension my Swift code begins to look very familiar…&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;times&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;println&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Hello, World!&quot;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;# Hello, World!&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Hello, World!&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Hello, World!&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Hello, World!&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;# Hello, World!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Finally, before admitting defeat and going to sleep, I added a few helpers
onto the Array class to bring some rubyisms to Swift.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;n&quot;&gt;extension&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Array&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;first: &lt;/span&gt;&lt;span class=&quot;no&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;second: &lt;/span&gt;&lt;span class=&quot;no&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;forty_two: &lt;/span&gt;&lt;span class=&quot;no&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;42&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;last: &lt;/span&gt;&lt;span class=&quot;no&quot;&gt;T&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;count&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  &lt;span class=&quot;n&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;things&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;things&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;first&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 1&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;things&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;second&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 2&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;things&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;last&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# 3&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;things&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;forty_two&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;# Answer to The Ultimate Question of Life, the Universe, and Everything&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Cheers!&lt;/p&gt;</content><summary type="html">This past Monday, Apple made a shocking announcement. They are moving forward
without Objective-C. After the murmuring lessened, Apple went on to demonstrate
an attractive, clean and expressive new language called Swift. Once the Taylor
Swift jokes had subsided, the developer community seemed genuinely excited about
this new look for iOS development. More information about the new language can be
found on Apple’s site.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="/assets/images/swift.jpg" /></entry><entry><title type="html">Fun with Variants and Enums in Rails 4.1</title><link href="/http/davidstump.net/2014/04/21/enums-and-variants.html" rel="alternate" type="text/html" title="Fun with Variants and Enums in Rails 4.1" /><published>2014-04-21T00:00:00+00:00</published><updated>2014-04-21T00:00:00+00:00</updated><id>/2014/04/21/enums-and-variants</id><content type="html" xml:base="/2014/04/21/enums-and-variants.html">&lt;p&gt;The release of Rails 4.1 this past week included some &lt;a href=&quot;http://weblog.rubyonrails.org/2014/4/8/Rails-4-1/&quot;&gt;cool new features&lt;/a&gt; two of which are the introduction of enums and variants. Enums give us some syntatic sugar for handling attributes like status, role, etc. Variants allow us to handle separate types of templates from within a respond_to block on a given action. I setup a &lt;a href=&quot;https://github.com/davidstump/enums_and_variants&quot;&gt;tiny Rails 4.1 application&lt;/a&gt; to demonstrate one possible use for these two new features.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Enum&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This application has a basic user model with a name string and an email string. The users controller contains basic index and show actions to show a list to users and their respective details. In the user model, I included an enum attribute &lt;code class=&quot;highlighter-rouge&quot;&gt;role&lt;/code&gt; to handle differentiating between users of different types (student, admin or staff). The basic way of defining an enum attribute is as follows:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;enum role: [:student, :admin, :staff]
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;However, with enums used in this fashion, order must always be maintained or you risk recasting your users with a new role unintentionally. Suppose, months later, a new developer comes along and adjusts the code above like so:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;enum role: [:golfer, :student, :admin, :staff]
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;We have now, inadvertanly, cast every user previously assigned with the role 
&lt;code class=&quot;highlighter-rouge&quot;&gt;:student&lt;/code&gt; to &lt;code class=&quot;highlighter-rouge&quot;&gt;:golfer&lt;/code&gt;. Oops.&lt;/p&gt;

&lt;p&gt;For this reason I would most likely use explicitly defined enums to avoid any potential future mishaps:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;enum role: {student: 0, admin: 1, staff: 2}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;In this example, even if my new developer friend stumbles across this and updates it, they would have to explicitly overwrite the role associated with a given integer to cause any harm.&lt;/p&gt;

&lt;p&gt;Once I have my enum defined, I now have access to all kinds of goodies to use throughout my application. I can now easily determine a given users role:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;user.role # =&amp;gt; &quot;student&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;I can easily determine if a user is a member of a particular role:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;user.student? # =&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;I can quickly assign/update a user’s role:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;user.staff! # =&amp;gt; [sql] true
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Or see all of the users scoped to a particular role:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;User.staff # =&amp;gt; #&amp;lt;ActiveRecord::Relation []&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Variants&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Variants can be used within a respond_to block to handle the same action in separate ways. Using the &lt;code class=&quot;highlighter-rouge&quot;&gt;User&lt;/code&gt; model from above, we can render a show page unique for a particular user’s role.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;def show
    @user = User.find(params[:id])
    request.variant = @user.role.to_sym
    respond_to do |format|
        format.html do |html|
        html.student
        html.staff
        html.admin
        end
    end
end
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;In this example, the variants of the show action such as &lt;code class=&quot;highlighter-rouge&quot;&gt;html.student&lt;/code&gt; will render a view template with the name matching: &lt;code class=&quot;highlighter-rouge&quot;&gt;show.html+student.haml&lt;/code&gt; (or erb if you prefer). This allows us to use the same logic from our show action but render different content/layouts. This pattern could also be used to present different templates for mobile devices, webviews inside native mobile apps, etc.&lt;/p&gt;

&lt;p&gt;With very little additional code, enums and variants allowed me to add a very basic role engine to my application and render content unique to a user’s role. These are just two of the fun new updates released in Rails 4.1. Check out the &lt;a href=&quot;http://edgeguides.rubyonrails.org/4_1_release_notes.html&quot;&gt;Release Notes&lt;/a&gt; for some additional reading.&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;</content><summary type="html">The release of Rails 4.1 this past week included some cool new features two of which are the introduction of enums and variants. Enums give us some syntatic sugar for handling attributes like status, role, etc. Variants allow us to handle separate types of templates from within a respond_to block on a given action. I setup a tiny Rails 4.1 application to demonstrate one possible use for these two new features.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="/assets/images/code.jpeg" /></entry><entry><title type="html">How to Use Angular and Keep Loving Rails</title><link href="/http/davidstump.net/2014/02/21/angular-with-rails.html" rel="alternate" type="text/html" title="How to Use Angular and Keep Loving Rails" /><published>2014-02-21T00:00:00+00:00</published><updated>2014-02-21T00:00:00+00:00</updated><id>/2014/02/21/angular-with-rails</id><content type="html" xml:base="/2014/02/21/angular-with-rails.html">&lt;p&gt;I love writing Ruby and I love using Rails. So when I started looking for a client-side framework to help organize my front-end process and code, I was cautious of frameworks that demanded I abandon my “Rails Way” and adopt their process, style, and approach. I was looking for a framework that added value to my “Rails App”, not a solution that wanted Rails to instead act as a compliment to my client-side app.&lt;/p&gt;

&lt;p&gt;Enter Angular stage left.&lt;/p&gt;

&lt;p&gt;But wait, you say, tons of Angular apps use Rails (or any other framework) as a backend api for their purely client-side application. Yes, you would be very correct, however, unlike numerous other client-side frameworks, Angular allows me to easily and quickly sprinkle it on top of my traditional Rails app if that’s my preference.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CAVEAT&lt;/strong&gt; - This article is only meant to demonstrate how I stayed a happy, healthy Rails developer while still enjoying all the advantages Angular provides. If, in your heart, you want nothing more than to make purely client-side apps every day, then more power to you for being focused. Thanks for reading this far - and please accept my &lt;strong&gt;&lt;a href=&quot;http://f.cl.ly/items/3c340s340q3d3q3X0I0h/corgis.gif&quot;&gt;token of thanks&lt;/a&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;My happy-place with Angular and Rails is to let each framework do what it’s good at and then move aside. For Rails, it is well suited to handle routing, helpers and primary application logic. For Angular, it is best suited to handle client-side interactions, dynamic bindings, ajax requests and presentation logic. Using this approach I can stay away from so many headaches caused by Angular routing, Rails remote linking and endless arbitrary jQuery listeners.&lt;/p&gt;

&lt;p&gt;Once I had settled on an approach to co-existence of Angular and Rails, my next obstacle was the somewhat verbose and obscure Angular syntax.&lt;/p&gt;

&lt;h3 id=&quot;coffeescript-sanity&quot;&gt;CoffeeScript Sanity&lt;/h3&gt;

&lt;p&gt;Personally, I love using CoffeeScript (through that is in no way required to enjoy Angular on Rails) and my first step in incorporating Angular into my daily Rails flow was to get a handle on some of the more obtuse Angular syntax in favor of the CoffeeScript class approach.&lt;/p&gt;

&lt;p&gt;The first step was to create a base class (NGObject) to handle basic dependency injection. This class iterates through the array of dependencies passed via the class @$inject variable and creates an instance variable with the same name. This gives us the ability to easily reference scope, services, etc passed into our angular object by friendly means (@scope, @Thing, @ThingSvc, etc). Once the dependencies are handled, the NGObject class will check for an init() function and call it if one exists.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class @NGObject
    constructor: (dependencies...) -&amp;gt;
        for dependency, index in @constructor.$inject
            @[dependency.replace('$', '')] = dependencies[index]
            @init?()
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Next, I use that NGObject base class to build a variety of other classes to handle specific angular object types such as NGController, NGService, etc. Each of these classes exposes a register function that wires up our object to the correct angular application. During object construction, we take the attributes of a given object (controller, service) and apply them to the proper scope. This allows us access to the attributes and functions of any object in the view layer.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class @NGController extends @NGObject
    @register: (app) -&amp;gt;
        app.controller &quot;#{@name}&quot;, this

    constructor: (@scope) -&amp;gt;
        @scope[key] = value for key, value of this when !@scope[key]?
        super
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Specifically for service objects, we setup an NGService object that implements event notifications allowing controllers, directives, etc to register callbacks for specific service events. In the service, whenever desired, you can call the &lt;/p&gt;

&lt;p&gt;notify function and inform all registered observers that the specific event occurred.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class @NGService extends @NGObject
    @register: (app) -&amp;gt; app.service &quot;#{@name}&quot;, this

    observableCallbacks: {}

    on: (event_name, callback) -&amp;gt;
        @observableCallbacks[event_name] ?= []
        @observableCallbacks[event_name].push callback

    notify: (event_name, data = {}) -&amp;gt;
        angular.forEach @observableCallbacks[event_name], (callback) -&amp;gt;
            callback(data)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;We use an event_name for each event type which adds the ability to sanely register and call multiple events explicitly as so:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;handleEvent: -&amp;gt; notify 'my:event'

MyService.on 'my:event', -&amp;gt; doStuff()
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Included in my Rails projects, this helper allow me to write concise and legible angular code in my preferred CoffeeScript class syntax. Here is a short example of a rewritten Angular controller with the new syntax and format:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class CalendarCtrl extends @NGController
    @register window.App

    @$inject: [
        '$scope',
        'Calendar',
        'Panel',
    ]

    init: -&amp;gt;
        @config = @Calendar.config()
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Is there anything explicitly problematic or wrong about the verbose traditional Angular syntax? Absolutely not! Does this syntax make me a happier developer and allow me to get my code out the door quicker? Without a doubt. Setting up this syntax was a fun endeavor and my hope is it makes at least one other developer’s life happier!&lt;/p&gt;

&lt;h3 id=&quot;rails-resource-to-the-rescue&quot;&gt;Rails Resource To The Rescue&lt;/h3&gt;

&lt;p&gt;One of the last remaining hurdles to quick and easy angular development with rails is wiring up Rails resources with Angular. One source of frustration is Angular’s handling (or lack thereof) of nested resource parameters. Here are a few recent GitHub issues as examples of that frustration &lt;strong&gt;&lt;a href=&quot;https://github.com/angular/angular.js/pull/1640&quot;&gt;fix(ngResource): Add support for nested params&lt;/a&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;a href=&quot;https://github.com/angular/angular.js/issues/3740&quot;&gt;Query params not serializes as expected&lt;/a&gt;&lt;/strong&gt;. Thankfully, Angular uses a modular approach to resource handling, so swapping ngResource with a different library is a breeze.&lt;/p&gt;

&lt;p&gt;After months of massaging the flat params from Angular in my Rails controllers, I stumbled across &lt;strong&gt;&lt;a href=&quot;https://github.com/FineLinePrototyping/angularjs-rails-resource&quot;&gt;Rails Resource&lt;/a&gt;&lt;/strong&gt;. Rails Resource is a promise based resource module that handles Angular resources exactly as a default Rails application would expect. With very little code, we can wire up Angular with our Rails resource and be on our way.&lt;/p&gt;

&lt;p&gt;As an example, I have a basic Rails app with a model Beer that &lt;/p&gt;

&lt;p&gt;belongs_to a Brewery. To wire up Angular with these resources using Rails Resource I could write an Angular factory such as:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;App.factory &quot;Beer&quot;, ['railsResourceFactory', 'railsSerializer', (railsResourceFactory, railsSerializer) -&amp;gt;
    resource = railsResourceFactory
        url: &quot;/beers&quot;
        name: &quot;beer&quot;
        serializer: railsSerializer -&amp;gt;
            @nestedAttribute 'brewery'
    ]
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This code creates an Angular resource ‘Beer’ and maps it to the appropriate endpoints in our Rails application. Our next step is to setup the association with the Brewery model. You can see this done above with the following lines:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;serializer: railsSerializer -&amp;gt;
    @nestedAttribute 'brewery'
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The serializer in Rails Resource looks to be quite powerful in allowing you to both specify relationships and alter the json coming and going to resource endpoints. The factory we have created above sets up a Beer resource in Angular that&lt;/p&gt;

&lt;p&gt;accepts_nested_attributes_for a Brewery resource. With only the code demonstrated above, the parameters sent to the logs on a create action (for example) are just what Rails expects and is handled easily by a traditional Rails create action.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Started POST &quot;/beers&quot; for 127.0.0.1
Processing by BeersController#create as JSON
Parameters: {&quot;beer&quot;=&amp;gt;{&quot;name&quot;=&amp;gt;&quot;Goose Island IPA&quot;, &quot;description&quot;=&amp;gt;&quot;Hoppy Goodness&quot;,
            &quot;brewery_attributes&quot;=&amp;gt;{&quot;name&quot;=&amp;gt;&quot;Goose Island&quot;}}}
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h3 id=&quot;the-result&quot;&gt;The Result&lt;/h3&gt;

&lt;p&gt;During this article I have described three tools I use to make me a happier Rails developer leveraging the power of AngularJS in my applications:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Letting each framework handle what it’s good at&lt;/li&gt;
  &lt;li&gt;The CoffeeScript helper that allows me to write more concise code in a syntax I enjoy&lt;/li&gt;
  &lt;li&gt;A resource module that wires up Rails resources with ease&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Using these methods I setup a very basic CRUD Rails application on Github&lt;strong&gt;&lt;a href=&quot;http://github.com/davidstump/beers&quot;&gt;davidstump/beers&lt;/a&gt;&lt;/strong&gt;. With the exception of the factory shown above, the only Javascript required was a short and sweet Angular controller:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class BeersCtrl extends @NGController
    @register window.App

    @$inject: [
        '$scope',
        'Beer'
    ]

    init: -&amp;gt;
        @loadBeers()

    loadBeers: -&amp;gt;
        @Beer.query().then (results) =&amp;gt;
            @all = results

    create: -&amp;gt;
        new @Beer(@newBeer).create().then (results) =&amp;gt;
            @loadBeers()

    destroy: (beer_id) -&amp;gt;
        @Beer.$delete(@Beer.resourceUrl(beer_id)).then (results) =&amp;gt;
            @loadBeers()
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This style of Angular fits seamlessly into my familiar mental model formed from my time developing Rails applications and ultimately makes me a quicker, happier and healthier developer.&lt;/p&gt;

&lt;p&gt;I would love any and all feedback (and Pull Requests) on both these approaches as well as the NGHelper library itself. Thank you for taking the time to read through my thoughts and happy coding!&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;</content><summary type="html">I love writing Ruby and I love using Rails. So when I started looking for a client-side framework to help organize my front-end process and code, I was cautious of frameworks that demanded I abandon my “Rails Way” and adopt their process, style, and approach. I was looking for a framework that added value to my “Rails App”, not a solution that wanted Rails to instead act as a compliment to my client-side app.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="/assets/images/angular.jpg" /></entry><entry><title type="html">Simple Touch Events in CoffeeScript</title><link href="/http/davidstump.net/2013/05/06/touch-events.html" rel="alternate" type="text/html" title="Simple Touch Events in CoffeeScript" /><published>2013-05-06T00:00:00+00:00</published><updated>2013-05-06T00:00:00+00:00</updated><id>/2013/05/06/touch-events</id><content type="html" xml:base="/2013/05/06/touch-events.html">&lt;p&gt;I am an avid fan of simple, clean and elegant CoffeeScript (and thus JavaScript of course). On top of your code being more approachable, I love the sense of accomplishment when I find a clean way to solve a problem without immediately jumping to an external library or plugin. Thus, when I was faced with the problem of handling touch events on handheld and tablet devices for our new site, I started with a blank page in my favorite editor.&lt;/p&gt;

&lt;p&gt;Touch.coffee is a simple and straightforward CoffeeScript class written with pure JavaScript that emits touch events to your application. The only dependency this class utilizes is &lt;a href=&quot;https://github.com/Wolfy87/EventEmitter&quot;&gt;EventEmitter&lt;/a&gt; to aid in handling the event triggers. The full source for this class is available for browsing on &lt;a href=&quot;https://gist.github.com/davidstump/5499239&quot;&gt;GitHub&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To get our class started, we will setup a few variables that will help us along the way.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;@Touch=
    horizontalSensitivity: 22
    verticalSensitivity: 6
    touchDX: 0
    touchDY: 0
    touchStartX: 0
    touchStartY: 0
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The sensitivity variables allow us to specify how long a touch event should last before we go ahead and emit our event to the rest of the application. This helps us differentiate between a swipe style gesture versus a simple tap. The next two variables will help us gauge the distance travelled during our touch event. Finally, the last two will let us store the starting point of our event.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;bind: (elements...) -&amp;gt;
    for elem in elements
        elem.addEventListener &quot;touchstart&quot;, (event) =&amp;gt;
            @handleStart(event, elem)
        elem.addEventListener &quot;touchmove&quot;, (event) =&amp;gt;
            @handleMove(event, elem)
        elem.addEventListener &quot;touchend&quot;, (event) =&amp;gt;
            @handleEnd(event, elem)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;First we want to add a bind method to our class to attach the appropriate touch events to whichever DOM element (or elements) specified. This method handles one or more elements and attaches the touchstart, touchmove and touchend events and delegates the appropriate handler methods for each type.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;handleStart: (event, elem) -&amp;gt;
    if event.touches.length is 1
        @touchDX = 0
        @touchDY = 0
        @touchStartX = event.touches[0].pageX
        @touchStartY = event.touches[0].pageY
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Once we have the events attached to the correct elements, we need to tell our class how to handle the various event types along the way. Here we store our initial values once the touch event is initially registered.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;handleMove: (event, elem) -&amp;gt;
    if event.touches.length &amp;gt; 1
        @cancelTouch(elem)
        return false

    @touchDX = event.touches[0].pageX - @touchStartX
    @touchDY = event.touches[0].pageY - @touchStartY
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Next we need a method fired during the portion of the touch event where our finger is moving across the screen. During this event we want to detect if another touch event is registered and reset our listeners to avoid complications. We also want to update our two variables tasked with telling us the distance our touch event has travelled.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;handleEnd: (event,elem) -&amp;gt;
    dx = Math.abs(@touchDX)
    dy = Math.abs(@touchDY)

    if (dx &amp;gt; @horizontalSensitivity) and (dy &amp;lt; (dx * 2 / 3))
        if @touchDX &amp;gt; 0 then @emitSlideRight() else @emitSlideLeft()

    if (dy &amp;gt; @verticalSensitivity) and (dx &amp;lt; (dy * 2 / 3))
        if @touchDY &amp;gt; 0 then @emitSlideDown() else @emitSlideUp()

    @cancelTouch(event, elem)
    false
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Once a touchend event is triggered and the gesture is complete, we need to gather the total distance travelled both horizontally and vertically. Once we have these values, we can use our sensitivity variables to determine if the distance of the event is great enough to warrant emitting to the rest of our application. We can also use the positive or negative value of our difference/distance variable to indicate the specific direction of our event and emit the correct notification.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;emitSlideLeft: -&amp;gt; @emit 'swipe:left'
emitSlideRight: -&amp;gt; @emit 'swipe:right'
emitSlideUp: -&amp;gt; @emit 'swipe:up'
emitSlideDown: -&amp;gt; @emit 'swipe:down'
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Finally, once we have successfully registered a touch event and determined that it passes our sensitivity threshold set initially, we need to emit the correct event to our application. Using &lt;a href=&quot;https://github.com/Wolfy87/EventEmitter&quot;&gt;EventEmitter&lt;/a&gt; we can easily send out the proper event notification with very little code.&lt;/p&gt;

&lt;p&gt;With our new touch class, we can now bind touch events to any elements in our application.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Touch.bind document.getElementById('menu')
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Once we have our bindings set, performing actions based our touch events is as easy as calling a method when a touch event is detected.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Touch.on 'swipe:left', =&amp;gt; @toggleMenu()
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;While this class certainly doesn’t handle every possible interaction on a handheld or tablet device, it does provide you with a great base for handling basic touch events throughout your application with very little code. I welcome and look forward to any and all feedback, suggestions and improvements and hope this little class proves helpful!&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;</content><summary type="html">I am an avid fan of simple, clean and elegant CoffeeScript (and thus JavaScript of course). On top of your code being more approachable, I love the sense of accomplishment when I find a clean way to solve a problem without immediately jumping to an external library or plugin. Thus, when I was faced with the problem of handling touch events on handheld and tablet devices for our new site, I started with a blank page in my favorite editor.</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="/assets/images/touch.jpg" /></entry><entry><title type="html">Use Git to Manage Your Time</title><link href="/http/davidstump.net/2013/02/10/mytime.html" rel="alternate" type="text/html" title="Use Git to Manage Your Time" /><published>2013-02-10T00:00:00+00:00</published><updated>2013-02-10T00:00:00+00:00</updated><id>/2013/02/10/mytime</id><content type="html" xml:base="/2013/02/10/mytime.html">&lt;p&gt;Donec eget ex magna. Interdum et malesuada fames ac ante ipsum primis in faucibus. Pellentesque venenatis dolor imperdiet dolor mattis sagittis.
&lt;strong&gt;A gem that uses your git commits to manage a Freshbooks timesheet&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As an extension to my git-based timesheet I &lt;a href=&quot;http://davidstump.net/blog/2012/12/31/add-a-simple-git-timesheet/&quot;&gt;wrote about at the beginning of the year&lt;/a&gt;, I went ahead and setup a self contained gem with some additional features. The gem, mytime, can be found at: &lt;a href=&quot;http://rubygems.org/gems/mytime&quot;&gt;rubygems.org/gems/mytime&lt;/a&gt; as well as &lt;a href=&quot;https://github.com/davidstump/mytime&quot;&gt;github.com/davidstump/mytime&lt;/a&gt; for your browsing pleasure.&lt;/p&gt;

&lt;p&gt;The mytime gem uses your git commit history, in the same manner as the timesheet alias did, to pull a log of your daily activities. Once you have this daily history, you can tell mytime to ‘push’ all of your commits as a timesheet log to Freshbooks using their API. Additionally, you can ‘submit’ custom timesheet entries to Freshbooks as needed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MyTime Documentation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use your git commit history to track your time. Uses Freshbooks API.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Installation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Add this line to your application’s Gemfile:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gem 'mytime'
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And then execute:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ bundle
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Or install it yourself as:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ gem install mytime
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Usage&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To link to your Freshbooks account (you will need your Freshbooks username and API token):&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ mytime setup
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;To initialize mytime in a project directory:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ mytime init
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;To see project details:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ mytime project
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;To list log:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ mytime status
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;To submit custom time entry:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ mytime commit [hrs] [custom_message]
$ mytime commit  1.5 &quot;Add an additional note if desired&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;To push your git logs from today for a given project:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;$ mytime push [hrs]
$ mytime push 4.5
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;I would love some feedback and/or pull requests to extend this little gem to include other timesheets, features, etc. I made this to help myself keep track of my daily commits per project, but hopefully it helps out a few others along the way. Thanks for reading!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cheers!&lt;/strong&gt;&lt;/p&gt;</content><summary type="html">Donec eget ex magna. Interdum et malesuada fames ac ante ipsum primis in faucibus. Pellentesque venenatis dolor imperdiet dolor mattis sagittis.
A gem that uses your git commits to manage a Freshbooks timesheet</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="/assets/images/timesheet.jpeg" /></entry><entry><title type="html">A Better Fullscreen Experience in Sublime</title><link href="/http/davidstump.net/2013/01/15/fullscreen-sublime.html" rel="alternate" type="text/html" title="A Better Fullscreen Experience in Sublime" /><published>2013-01-15T00:00:00+00:00</published><updated>2013-01-15T00:00:00+00:00</updated><id>/2013/01/15/fullscreen-sublime</id><content type="html" xml:base="/2013/01/15/fullscreen-sublime.html">&lt;p&gt;While I love coding with my editor in fullscreen mode, I also enjoy having apps such as Campfire or Rdio accessible on another monitor. One complaint I have had with the default OSX fullscreen mode is that it causes a second monitor to show nothing but the grey textured spaced background. I was thrilled the other day when I discovered a feature in Sublime that allowed me to use a “simple” fullscreen mode which behaves differently than the standard OSX mode. If you add the following to your Sublime preferences:&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&quot;use_simple_full_screen&quot;: true
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Using the Enter Full Screen shortcut will allow you to have nothing but your code on one monitor and still use your second monitor for whatever apps your heart desires. Simple preference edit that has made my work flow that much better.&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;</content><summary type="html">While I love coding with my editor in fullscreen mode, I also enjoy having apps such as Campfire or Rdio accessible on another monitor. One complaint I have had with the default OSX fullscreen mode is that it causes a second monitor to show nothing but the grey textured spaced background. I was thrilled the other day when I discovered a feature in Sublime that allowed me to use a “simple” fullscreen mode which behaves differently than the standard OSX mode. If you add the following to your Sublime preferences:</summary><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="/assets/images/sublime.jpg" /></entry></feed>
