<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Jelani Woods on Medium]]></title>
        <description><![CDATA[Stories by Jelani Woods on Medium]]></description>
        <link>https://medium.com/@jelaniwoods?source=rss-cbccbe91645d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*-4266mqSo_iAsBQu</url>
            <title>Stories by Jelani Woods on Medium</title>
            <link>https://medium.com/@jelaniwoods?source=rss-cbccbe91645d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 22:16:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@jelaniwoods/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Docker Basics]]></title>
            <link>https://medium.com/@jelaniwoods/docker-basics-5963448ebd9c?source=rss-cbccbe91645d------2</link>
            <guid isPermaLink="false">https://medium.com/p/5963448ebd9c</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[cheatsheet]]></category>
            <dc:creator><![CDATA[Jelani Woods]]></dc:creator>
            <pubDate>Fri, 13 Mar 2020 18:41:01 GMT</pubDate>
            <atom:updated>2020-03-30T04:48:00.906Z</atom:updated>
            <content:encoded><![CDATA[<p>A small cheatsheet for creating and updating basic docker images.</p><h4>See list of available images on your computer</h4><pre>docker images</pre><h4>See running Containers</h4><pre>docker ps</pre><h4>Run an Image</h4><pre>docker run -it &lt;image-name&gt;</pre><p>The -it runs the image (creates a container) interactively and attaches it to your terminal.</p><h4>Remove an Image</h4><p><strong>Find the images ID:</strong></p><pre>docker images -a</pre><p><strong>Remove:</strong></p><pre>docker rmi Image Image</pre><h4>Tag an Image</h4><pre>docker tag username/app-name username/app-name:v1</pre><h4>Create an Image from a Dockerfile</h4><p>In the directory of the Dockerfile run</p><pre><em>docker build -t &lt;image-name&gt; .</em></pre><h4>Update Image</h4><ol><li>Create a Container</li></ol><pre>docker run -it &lt;image-name&gt;</pre><p>2. Find Container Id</p><pre>docker ps</pre><p>Make sure you copy the Container ID</p><p>3. Commit change</p><p><strong>Exit container </strong>and then</p><pre>docker commit -m &quot;message&quot; container_id</pre><p>4. Push to Dockerhub</p><pre>docker push username/app-name</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5963448ebd9c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ruby Shovel vs Rails Shovel]]></title>
            <link>https://medium.com/@jelaniwoods/ruby-shovel-vs-rails-shovel-3f179aa5a08d?source=rss-cbccbe91645d------2</link>
            <guid isPermaLink="false">https://medium.com/p/3f179aa5a08d</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Jelani Woods]]></dc:creator>
            <pubDate>Wed, 25 Sep 2019 18:06:01 GMT</pubDate>
            <atom:updated>2019-09-25T18:06:01.272Z</atom:updated>
            <content:encoded><![CDATA[<p>The Shovel Operator is &lt;&lt;.</p><h3>In Ruby</h3><p>Array#&lt;&lt; acts similar to Array#push. Shovel will add an element to the end of an Array.</p><pre>a = [21, 43, 108] &lt;&lt; 99<br>=&gt; [21, 43, 108, 99]</pre><p>easy peasy</p><h3>In Rails</h3><p>Rails enhances the <strong>HECK</strong> out of the Shovel operator when using ActiveRecord::Collections. When using the Shovel operator to add to a Collection, it will populate foreign key columns or even create a record in the join table with the two foreign keys for you.</p><p><strong>Rails automatically creates and saves the associated object.</strong></p><p>The behavior of the shovel operator depends on what the relationship is between the ActiveRecord::Relation and the ActiveRecord object.</p><h4>One-to-Many</h4><p>The shovel populates the foreign key field of the ActiveRecord Object with the foreign key that is common in the Collection.</p><p>For example, say you have two models, Director and Movie.</p><ul><li>a Director has many Movies.</li><li>a Movie belongs to one Director.</li></ul><p>If you have a collection of Movie records you can use the shovel operator like so:</p><pre>d = Director.create(name: &quot;Christopher Nolan&quot;)<br>m = Movie.create(title: &quot;Space Jam&quot;)<br>d.movies &lt;&lt; m</pre><p>Now m will have a director_id that matches the id column of d.</p><h4>Many-to-Many</h4><p>The shovel creates a record in the join table that populates the two foreign keys and whatever else is required and saves it.</p><p>So now say we have three models, Movie, Actor, and Character.</p><ul><li>A Movie has many Characters.</li><li>A Movie has many Actors through Characters.</li><li>A Character belongs to a Movie and belongs to an Actor.</li></ul><pre>new_actor = Actor.create(first_name: &quot;Aubrey&quot;, last_name: &quot;Plaza&quot;)<br>m.actors &lt;&lt; new_actor</pre><p>Now, the shovel operator will create a record in the Character table with the appropriate actor_id from the new_actor and the movie_id from m.</p><p>Check out the documentation <a href="https://api.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html#method-i-3C-3C">here</a>.</p><figure><img alt="Minecraft Shovel" src="https://cdn-images-1.medium.com/max/800/0*ZGiVTrWSiQMBG4C-" /></figure><p>[<a href="http://www.turbosquid.com/3d-models/3d-shovel-minecraft-model/706670">image source</a>]</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f179aa5a08d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rails 6: Subscribing to Multiple Channels in Action Cable]]></title>
            <link>https://medium.com/@jelaniwoods/rails-6-subscribing-to-multiple-channels-in-action-cable-c61e0f2b1e96?source=rss-cbccbe91645d------2</link>
            <guid isPermaLink="false">https://medium.com/p/c61e0f2b1e96</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[actioncable]]></category>
            <category><![CDATA[rails-6]]></category>
            <dc:creator><![CDATA[Jelani Woods]]></dc:creator>
            <pubDate>Tue, 09 Jul 2019 19:01:01 GMT</pubDate>
            <atom:updated>2020-01-15T22:57:56.228Z</atom:updated>
            <content:encoded><![CDATA[<p>There are a lot of tutorials about making a basic chat application with Action Cable, and <em>most</em> of them create an app with one general Channel that acts as one general chat room that all users subscribe to automatically. Maybe you have a model like Room or Topic and users only need to be subscribed for an <em>instance</em> of the that model.</p><p>So the question is, how can stream updates to specific channel that’s based off a database model and how can users subscribe to these specific channels?</p><h3>Streaming from a specific resource</h3><p>This part is easy!</p><p>When there was only one Channel you would set the subscription process like so:</p><pre># In room_channel.rb</pre><pre>def subscribed<br>  stream_from &quot;room_channel&quot;<br>end</pre><p>broadcast to it like</p><pre># In messages_controller#create<br>ActionCable.server.broadcast &quot;room_channel&quot;,<br>                                        body: <a href="http://twitter.com/message">@message</a>.body</pre><p>Now when you broadcast your message, you can broadcast from a specific Channel, the RoomChannel. What’s even better, you can broadcast to a specific <em>instance</em> of a Room.</p><pre># In messages_controller#create<br>room = @message.room</pre><pre>RoomChannel.broadcast_to room,<br>                     body: <a href="http://twitter.com/message">@message</a>.body,<br>                     user: <a href="http://twitter.com/message">@message</a>.user.name</pre><p>Next we need to modify our room_channel.rb to stream for specific Room objects.</p><pre>stream_for room</pre><pre>def room<br>  Room.find(params[:room_id])<br>end</pre><p>stream_for works similarly as stream_from with the difference that stream_for accepts an ActiveRecord and will automatically name the Channel based on the record’s grid param id.</p><p>But what <em>is </em>params here? and how can we add keys and values to it?</p><p>Most importantly — How do we set the room_id?</p><h3>Subscribing to a Specific Resource</h3><p>Now we just need to subscribe to a specific Room, but how do can we find out which Room to subscribe to?</p><p>We can rely on some JavaScript to get the job done.</p><p>In app/javascripts/channels/room_channel.js , we have something like:</p><pre>consumer.subscriptions.create(&quot;RoomChannel&quot;, {<br>  connected() {<br>   ...<br>  },<br>  disconnected() {<br>    ...<br>  },<br>  received(data) {<br>    ...<br>  }<br>});</pre><p>Here ,where the subscription is created, we can specify params for the RoomChannel ruby class.</p><p>So just to make sure this works, let’s say we have a Room with id of 1 and the route looks something like /rooms/1. Let’s create a subscription to that specific Room.</p><p>We can modify our JavaScript to pass a room_id like so:</p><pre>consumer.subscriptions.create({<br>    channel: &quot;RoomChannel&quot;,<br>    room_id: 1<br>  }, {<br>  ...</pre><p>Now if you start your server and visit a page, we should see something in the server log like</p><pre>RoomChannel is streaming from room:Z2lkOi8vYWN0aW9uLWNhYmxlLXJhaWxzNi9SZXNvdXJjZS8x</pre><p>Great! We got our clients to connect to a Channel&#39;s specific Room.</p><p>This seems kinda dumb though, because it’s functionally the <strong>exact </strong>same as what we were doing before. It’s actually <em>worse</em>, because all clients are only subscribed to &quot;RoomChannel&quot; with room_id: 1, while they could potentially send messages to &quot;RoomChannel&quot; with any room_id. If they were to do that, they would need to refresh the page to see any new messages.</p><p>So, how do we make this room_id dynamic?</p><p>Ya got two options:</p><ol><li>Look for an id in the url</li><li>Look for an id embedded in the page</li></ol><p>I prefer the embedding the id in the page. So I modified my Room show page by adding a data-room-id attribute to the containing div and set the value to the id of the current Room.</p><pre>&lt;div class=&quot;col-md-8&quot; <br>  id=&quot;room_messages&quot; <br>  data-channel-subscribe=&quot;room&quot; <br>  data-room-id=&quot;&lt;%= <a href="http://twitter.com/resource">@r</a>oom.id %&gt;&quot;&gt;<br>    ...<br>&lt;/div&gt;</pre><p>Now in room_channel.js I can modify the subscription process to grab the id from the page</p><pre>consumer.subscriptions.create({<br>    channel: &quot;RoomChannel&quot;,<br>    room_id: $(&#39;#room_messages).attr(&#39;data-room-id&#39;)<br>  }, {</pre><p><strong>BUT WAIT!</strong></p><p>This won’t actually work yet because this JavaScript happens <em>before </em>the html finishes loading. This means data-room-id isn’t set yet and won’t have the id value that we’re expecting!</p><p>How do we fix this? Wait for turbolinks to load first.</p><pre>$(document).on(&#39;turbolinks:load&#39;, function () {<br>  consumer.subscriptions.create({<br>    ...<br>})</pre><p>— And just like that we made it work! Depending on what Room show page we’re on, the client will subscribe to a specific Room channel and send messages <em>only </em>to that channel. The client will also see updates to that channel without having to refresh the page.</p><p>I made <a href="https://github.com/jelaniwoods/ActionCableRails6/tree/jw-multiple-channels">an example repo</a> with working code on branch jw-multiple-channels if you want to check it out!</p><ul><li><a href="https://stackoverflow.com/questions/39002150/what-is-the-difference-between-stream-from-and-stream-for-in-actioncable">What is the difference between `stream_from` and `stream_for` in ActionCable?</a></li><li><a href="https://stackoverflow.com/questions/36791071/how-do-you-create-multiple-channels-with-actioncable-how-does-one-pass-an-in-do">How do you create multiple channels with Actioncable; how does one pass an in-document variable to the javascript and ruby channels and jobs?</a></li><li><a href="https://stackoverflow.com/questions/36046155/rails-5-actioncable-establish-stream-from-url-parameters">Rails 5 ActionCable establish stream from URL parameters</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c61e0f2b1e96" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Get Started with Action Cable in Rails 6]]></title>
            <link>https://medium.com/@jelaniwoods/get-started-with-action-cable-in-rails-6-4c605f93c9b8?source=rss-cbccbe91645d------2</link>
            <guid isPermaLink="false">https://medium.com/p/4c605f93c9b8</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[actioncable]]></category>
            <category><![CDATA[rails-6]]></category>
            <dc:creator><![CDATA[Jelani Woods]]></dc:creator>
            <pubDate>Fri, 21 Jun 2019 18:09:24 GMT</pubDate>
            <atom:updated>2021-04-20T15:05:06.656Z</atom:updated>
            <content:encoded><![CDATA[<p>There are some important changes regarding how you use Action Cable in a Rails 6 application. I hope to cover the basics in this post.</p><p>I’m using rails 6.0.0.rc1 and ruby &quot;2.6.3&quot;.</p><p>rails new ChatInRails6</p><h4>Including JQuery</h4><p>First note: No more CoffeeScript, (thank the lord), you need to use ES6. Before that let’s install jquery. cd ChatInRails6, then</p><p>yarn install jquery</p><p>Our JavaScript is no longer inside the assets folder, the folder lives directly in the app folder. application.js is inside the packs folder</p><p>To actually include jquery, open config/webpack/environment.js and require it like</p><pre>const webpack = require(&#39;webpack&#39;)<br>environment.plugins.prepend(&#39;Provide&#39;,<br>  new webpack.ProvidePlugin({<br>    $: &#39;jquery/src/jquery&#39;,<br>    jQuery: &#39;jquery/src/jquery&#39;<br>  })<br>)<br>module.exports = environment</pre><p>This is setting $ and jQuery as variable constants you can use to invoke jquery. You could also have something like,</p><pre>hello_js: &#39;jquery/src/jquery&#39;</pre><p>that would make hello_js point to jquery.</p><p>In application.js we can now require jquery like</p><p>require(&quot;jquery&quot;) near the top of the file with the other requires.</p><p>To make sure we’ve required it correctly we can do a little test at the bottom of application.js:</p><pre>$(document).on(&#39;turbolinks:load&#39;, function () {<br>  alert(&quot;We made it!&quot;);<br>})</pre><p>Before this can work we need to add a controller and a view!</p><pre>rails g controller landing index</pre><p>Modify our routes.rb with root &quot;landing#index&quot; and start the server with rails s, we can visit localhost:3000 and if all goes well we’ll see a JavaScript alert!</p><h4>Database Setup</h4><p>Now let’s get into some more interesting data.</p><pre>rails g model message content:text<br>rails db:migrate<br>rails g controller messages</pre><h4>Channels Setup</h4><p>Notice inside the app/javascript/channels/consumer.js file</p><pre>// You can generate new channels where WebSocket features live using the `rails generate channel` command.</pre><p>So let’s just do that.</p><pre>rails g channel room</pre><p>Inside room_channel.js</p><pre>  connected() {<br>    // Called when the subscription is ready for use on the server<br>    console.log(&quot;Connected to the room!&quot;);<br>  },</pre><p>Inside room_channel.rb</p><pre>def subscribed<br>  stream_from &quot;room_channel&quot;<br>end</pre><p>And if we rails s and go to our root page we should see &quot;Connected to the room!&quot; in the browser console.</p><h4>Sending Data in Channels</h4><pre>class MessagesController &lt; ApplicationController</pre><pre>  def new<br>    <a href="http://twitter.com/message">@message</a> = Message.new<br>  end</pre><pre>  def create<br>    <a href="http://twitter.com/message">@message</a> = Message.create(msg_params)<br>    if <a href="http://twitter.com/message">@message</a>.save<br>      ActionCable.server.broadcast &quot;room_channel&quot;,<br>                                      content: <a href="http://twitter.com/message">@message</a>.content      <br>    else<br>      <br>    end<br>  end</pre><pre>  private</pre><pre>  def msg_params<br>    params.require(:message).permit(:content)<br>  end<br>end</pre><p>Add routes for these actions</p><pre># config/routes.rb<br>Rails.application.routes.draw do<br>  # ...<br>  resources :messages, only: [:new, :create]<br>end</pre><p>In room_channel.js</p><pre>received(data) {<br>    // Called when there&#39;s incoming data on the websocket for this channel<br>    console.log(&quot;Recieving:&quot;)<br>    console.log(data.content)<br>  }</pre><p>Modify our root page</p><pre>&lt;%= form_for <a href="http://twitter.com/message">@message</a>, remote: true do |f| %&gt;<br>  &lt;%= f.text_field :content %&gt;<br>  &lt;%= f.submit %&gt;<br>&lt;% end %&gt;</pre><p>Also Modify landing#index</p><pre>def index<br>  <a href="http://twitter.com/message">@message</a> = Message.new<br>end</pre><p>Now we have a form we can create records and send their content and see it in the console! Cool, but we can do better.</p><h4>Rendering Action Cable Data Live</h4><p>In our index page, we need to add a div that we can put all the Messages in.</p><pre>&lt;div id=&quot;msg&quot;&gt;</pre><pre>&lt;/div&gt;</pre><p>Then in our room_channel.js we can grab that div by its id and append content to it <em>instead</em> of just logging it to the console.</p><pre>$(&#39;#msg&#39;).append(&#39;&lt;div class=&quot;message&quot;&gt; &#39; + data.content + &#39;&lt;/div&gt;&#39;)</pre><h4>Clearing the Form Input</h4><p>In our room_channel.js we can do some more stuff</p><pre>let submit_messages;</pre><pre>$(document).on(&#39;turbolinks:load&#39;, function () {<br>  submit_messages()<br>})</pre><pre>submit_messages = function () {<br>  $(&#39;#message_content&#39;).on(&#39;keydown&#39;, function (event) {<br>    if (event.keyCode == 13) {<br>      $(&#39;input&#39;).click()<br>      event.target.value = &#39;&#39;<br>      event.preventDefault()<br>    }<br>  })<br>}</pre><p>Not the most readable right now, but I’m rusty on my JS and Dom events.</p><p>— Aaaand that’s just about it!</p><p><a href="https://www.youtube.com/watch?v=nRP91C1uX-w">This is really great video tutorial</a> on Action Cable in Rails 6 that I followed closely. Here’s a <a href="https://github.com/jelaniwoods/ActionCableRails6">link to the repo </a>I made.</p><p>If you’re going further and are interested in how to stream to multiple channels in Action Cable, check out another post I made about that called <strong>Rails 6: Subscribing to Multiple Channels in Action Cable </strong><a href="https://medium.com/@jelaniwoods/rails-6-subscribing-to-multiple-channels-in-action-cable-c61e0f2b1e96">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4c605f93c9b8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Action Cable Notes]]></title>
            <link>https://medium.com/@jelaniwoods/action-cable-notes-639e6b1d7d7e?source=rss-cbccbe91645d------2</link>
            <guid isPermaLink="false">https://medium.com/p/639e6b1d7d7e</guid>
            <category><![CDATA[actioncable]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Jelani Woods]]></dc:creator>
            <pubDate>Sun, 09 Jun 2019 23:07:06 GMT</pubDate>
            <atom:updated>2019-06-09T23:07:06.495Z</atom:updated>
            <content:encoded><![CDATA[<h3>Server-Side</h3><p>Main components are Connections and Channels</p><h4>Connections</h4><p>Each time a WebSocket is accepted by the server a connection object is created. This connection, is the parent of <em>all </em>of the channel subscriptions that will be created. That’s all it does. The client is called the connection <em>consumer</em>. Each user will create one consumer-connection (client-server) pair per window they have open.</p><p>Connections are instances of ApplicationCable::Connection. In <a href="https://guides.rubyonrails.org/action_cable_overview.html#connection-setup">this class</a>, you authorize the incoming connection, and proceed to establish it if the user can be identified.</p><h4>Channels</h4><p>A channel is home to any unit of work. It’s like the controller of MVC app. Similar to controller, Rails creates a parent ApplicationCable::Channel class for encapsulating shared logic between your channels. You can inherit from this class when you create your own channels.</p><pre># app/channels/chat_channel.rb<br>class ChatChannel &lt; ApplicationCable::Channel<br>end<br> <br># app/channels/appearance_channel.rb<br>class AppearanceChannel &lt; ApplicationCable::Channel<br>end</pre><p>A consumer could be subscribed to both or neither of these channels.</p><h3>Client-Side</h3><h4>Consumers</h4><p>By default, Rails will generate the following JS</p><pre>// app/assets/javascripts/cable.js<br>//= require action_cable<br>//= require_self<br>//= require_tree ./channels<br> <br>(function() {<br>  this.App || (this.App = {});<br> <br>  App.cable = ActionCable.createConsumer();<br>}).call(this);</pre><p>This will ready a consumer that’ll connect against /cable on your server by default. The connection won&#39;t be established until you&#39;ve also specified at least one subscription you&#39;re interested in having.</p><h4>Subscriber</h4><p>A consumer becomes a subscriber by creating a subscription to a specific channel.</p><pre>App.cable.subscriptions.create(&quot;ChatChannel&quot;, ... )</pre><h3>Client-Server Communication</h3><h4>Streams</h4><p>Streams are the way channels route broadcasts (how they publish) to their subscribers.</p><pre>class ChatChannel &lt; ApplicationCable::Channel<br>  def subscribed<br>    # stream_from &quot;some_channel&quot;<br>    stream_from &quot;chat&quot;<br>  end<br>  ...<br>end</pre><p>You can subscribe to a broadcast based on a specific model and channel? Not sure what’s that’s about yet**</p><h4>Broadcasting</h4><p>A <em>broadcasting </em>is a client-server (publisher/subscriber) link , where anything transmitted by a publisher (anyone who sends a message, could be client or server) is routed to all subscibers who are streaming the named channel. A channel can have 0+ broadcastings.</p><p>Broadcastings are purely an online queue and time-dependent. If a consumer is not streaming (subscribed to a given channel), they’ll not get the broadcast should they connect later.</p><pre>WebNotificationsChannel.broadcast_to(<br>  current_user,<br>  title: &#39;New things!&#39;,<br>  body: &#39;All the news fit to print&#39;<br>)</pre><p>This method will send a message to the queue of online messages under a separate broadcasting name for each user, like web_notifications:&lt;User.id&gt;.</p><p>The channel will stream everything that arrives at web_notifications:1 directly to the client by invoking the received callback.</p><h4>Subscriptions</h4><p>A subscription is when a consumer connects to a channel. Incoming messages are routed to these channel subscriptions based on an identifier sent by the cable consumer.</p><h4>Passing Parameters to Channels</h4><pre># app/channels/chat_channel.rb<br>class ChatChannel &lt; ApplicationCable::Channel<br>  def subscribed<br>    stream_from &quot;chat_#{params[:room]}&quot;<br>  end<br>end</pre><p>An object passed as the first argument to subscriptions.create becomes the params hash in the cable channel. The keyword channel is <strong>required</strong>:</p><pre># app/assets/javascripts/cable/subscriptions/chat.coffee*<br>App.cable.subscriptions.create { channel: &quot;ChatChannel&quot;, room: &quot;Best Room&quot; },<br>  received: (data) -&gt;<br>    <a href="http://twitter.com/appendLine">@appendLine</a>(data)<br> <br>  appendLine: (data) -&gt;<br>    html = <a href="http://twitter.com/createLine">@createLine</a>(data)<br>    $(&quot;[data-chat-room=&#39;Best Room&#39;]&quot;).append(html)<br> <br>  createLine: (data) -&gt;<br>    &quot;&quot;&quot;<br>    &lt;article class=&quot;chat-line&quot;&gt;<br>      &lt;span class=&quot;speaker&quot;&gt;#{data[&quot;sent_by&quot;]}&lt;/span&gt;<br>      &lt;span class=&quot;body&quot;&gt;#{data[&quot;body&quot;]}&lt;/span&gt;<br>    &lt;/article&gt;<br>    &quot;&quot;&quot;</pre><p>*don’t use coffee, use js</p><pre># Somewhere in your app this is called, perhaps<br># from a NewCommentJob.<br>ActionCable.server.broadcast(<br>  &quot;chat_#{room}&quot;,<br>  sent_by: &#39;Paul&#39;,<br>  body: &#39;This is a cool chat app.&#39;<br>)</pre><p><a href="https://guides.rubyonrails.org/action_cable_overview.html#streams">Action Cable Overview - Ruby on Rails Guides</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=639e6b1d7d7e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[WebSockets vs HTTP]]></title>
            <link>https://medium.com/@jelaniwoods/websockets-vs-http-8e095427f89?source=rss-cbccbe91645d------2</link>
            <guid isPermaLink="false">https://medium.com/p/8e095427f89</guid>
            <category><![CDATA[websocket]]></category>
            <category><![CDATA[http-request]]></category>
            <dc:creator><![CDATA[Jelani Woods]]></dc:creator>
            <pubDate>Fri, 31 May 2019 15:39:11 GMT</pubDate>
            <atom:updated>2019-05-31T15:39:11.937Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/604/0*qhyGYftNoRz9DNS8" /></figure><p>WebSocket is a protocol that works on top of HTTP that creates a persistent connection between client and server. This allows <strong>two-way</strong> communication between them.</p><p>HTTP’s design is referred to as “half-duplex”, meaning only the client <em>or </em>the server can send data at one time. This is like the digital version of walkie-talkies. Instead of saying, “over”, to mark the end of one persons communications, HTTP has status codes, like 200 OK or 404 Not Found.</p><p>WebSocket is “full-duplex”, meaning after an initial one-way request (using a header called Upgrade). the client and server can send data simultaneously and persistently. The evolution of walkie-talkies to cell phones.</p><p>A pure HTTP chat app <strong>requires</strong> a user to refresh the page in order to recieve any new messages. One solution is to use JavaScript to poll every few seconds and refresh any changes that might of happened.</p><p>This is inefficient because the polling happens <em>regardless </em>of whether there’s a new message or not, and it scales really bad as more clients are added, because each client has to hit the server every time it polls</p><h4>Why Are WebSockets Better Than Polling?</h4><p>I just told you why.</p><ul><li>Bi-directional connection between server and client meaning any messages sent from Alice are immediately received by Bob.</li><li>The connection is persistent. Alice and Bob can send messages at the same time and still receive them immediately.</li><li>WebSockets scale much better with multiple clients compared to polling. Only one socket per client is needed and the socket only updates when required.</li></ul><p><a href="https://www.learnenough.com/action-cable-tutorial#fig-http_protocol">Learn Enough Action Cable to Be Dangerous</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8e095427f89" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Launch a Deep Learning App with Render]]></title>
            <link>https://medium.com/@jelaniwoods/launch-a-deep-learning-app-with-render-678e4dfdeb57?source=rss-cbccbe91645d------2</link>
            <guid isPermaLink="false">https://medium.com/p/678e4dfdeb57</guid>
            <category><![CDATA[render]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[fastai]]></category>
            <dc:creator><![CDATA[Jelani Woods]]></dc:creator>
            <pubDate>Fri, 24 May 2019 22:41:00 GMT</pubDate>
            <atom:updated>2019-05-24T22:41:00.755Z</atom:updated>
            <content:encoded><![CDATA[<h4>Set up your environment</h4><p>Fastai provides a <a href="https://github.com/render-examples/fastai-v3">starter repo</a> that uses the Bear Image Classification model. You can Fork it to get started. Look <strong>very closely</strong>, at the <a href="https://github.com/render-examples/fastai-v3/blob/master/requirements.txt">requirements.txt</a> to make sure you have the right version of pytorch and other libraries installed on whatever machine you’re testing. There are more libraries required than the ones that were used in the fastai course. Next, create a <a href="https://render.com/i/fastai-v3">Render</a> account and sync to your GitHub account.</p><h4>Upload your trained model file</h4><p>Take the file created with learner.export and upload it to a cloud platform like Dropbox or Google Drive. Once it’s uploaded, copy the download link (<strong>not</strong> the share link) for the file.</p><ul><li>Google Drive: Use <a href="https://www.wonderplugin.com/online-tools/google-drive-direct-link-generator/">this link generator</a>.</li><li>Dropbox: Use <a href="https://syncwithtech.blogspot.com/p/direct-download-link-generator.html">this link generator</a></li></ul><h4>Customize the app to fit your model</h4><ol><li>Edit server.py inside the app directory and update the model_file_url variable with the URL copied above.</li><li>In the same file, update the line classes = [&#39;black&#39;, &#39;grizzly&#39;, &#39;teddys&#39;] with the classes you expect from your model.</li></ol><h4>Locally test your app</h4><p>To run the app locally, run this command in your terminal:</p><pre>python app/server.py serve</pre><p>Go to <a href="http://localhost:5042/">http://localhost:5042/</a> to test your app.</p><h4>Commit and push your changes to GitHub</h4><p>Keep your GitHub repo updated with the latest changes. Render integrates with your repo and builds and delpoys the app automatically after every change.</p><h4>Deploy to production</h4><ol><li>Create a new <strong>Web Service</strong> on Render and use the repo you created above. You will need to grant Render permission to access your repo in this step.</li><li>On the deployment screen, pick a name for your service and use Docker for the Environment. The URL will be created using this service name. The service name can be changed if necessary, but the URL initially created can’t be edited.</li><li>Click <strong>Save Web Service</strong>. That’s it! Your service will begin building and should be live in a few minutes at the URL displayed in your Render dashboard. You can follow its progress in the deploy logs.</li></ol><p>Any issues about deployment are discussed here:</p><ul><li><a href="https://forums.fast.ai/t/deployment-platform-render/33953/168">Deployment Platform: Render ✅</a></li><li><a href="https://forums.fast.ai/t/error-on-learn-predict-after-fastai-update-conv2d-object-has-no-attribute-padding-mode/43649/2">Error on learn.predict() after fastai update: &#39;Conv2d&#39; object has no attribute &#39;padding_mode&#39;</a></li><li><a href="https://course.fast.ai/deployment_render.html">Deploying on Render</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=678e4dfdeb57" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Alias a Built-In Rails Method]]></title>
            <link>https://medium.com/@jelaniwoods/how-to-alias-a-built-in-rails-method-60f94fe85368?source=rss-cbccbe91645d------2</link>
            <guid isPermaLink="false">https://medium.com/p/60f94fe85368</guid>
            <category><![CDATA[activerecord]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Jelani Woods]]></dc:creator>
            <pubDate>Wed, 22 May 2019 16:49:27 GMT</pubDate>
            <atom:updated>2019-05-22T16:49:27.072Z</atom:updated>
            <content:encoded><![CDATA[<p>Let’s say, I wanted to alias the :[] ActiveRecord instance method , Article[:id] # =&gt; 1, to look like the :fetch method for a Hash, I’d first need to find where that method lives. From <a href="https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods.html#method-i-5B-5D">here</a>, I can tell that the method lives in ActiveRecord::AttributeMethods module. Now, I can create an initializer and name it whatever. I’ll call it, attribute_methods_patch.rb.</p><pre># in config/initializers/attribute_methods_patch.rb</pre><pre>module ActiveRecord<br>  module AttributeMethods<br>    # What to do next?<br>  end<br>end</pre><p>Since I know the Rails method lives in ActiveRecord::AttributeMethods, I can affect the AttributeMethods module by re-defining* it here.</p><p>Rails has an easy method to alias methods, called alias_method (wow, who would’ve guessed?). So now I update my initializer:</p><pre># in config/initializers/attribute_methods_patch.rb</pre><pre>module ActiveRecord<br>  module AttributeMethods<br>    alias_method :fetch, :[]<br>  end<br>end</pre><p>— and ta-da! You did it!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=60f94fe85368" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ruby on Rails: Generators]]></title>
            <link>https://medium.com/@jelaniwoods/ruby-on-rails-generators-91bdebc4ca6d?source=rss-cbccbe91645d------2</link>
            <guid isPermaLink="false">https://medium.com/p/91bdebc4ca6d</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Jelani Woods]]></dc:creator>
            <pubDate>Sun, 19 May 2019 01:01:00 GMT</pubDate>
            <atom:updated>2019-05-19T01:01:00.771Z</atom:updated>
            <content:encoded><![CDATA[<p>Generators can inherit from Rails::Generators::Base. When a generator is invoked, each public method in the generator is executed sequentially in the order that it is defined. Generators can use the some of the same methods that are a part of the Rails Application Templates API. Generators inherit methods like create_file that will create a file at the given destination with the given content.</p><h4>Creating Generators</h4><p>Generators themselves have a generator:</p><pre>rails generate generator initializer<br>      create  lib/generators/initializer<br>      create  lib/generators/initializer/initializer_generator.rb<br>      create  lib/generators/initializer/USAGE<br>      create  lib/generators/initializer/templates<br>      invoke  test_unit<br>      create    test/lib/generators/initializer_generator_test.rb</pre><p><strong>Notice: </strong>we are now inheriting from Rails::Generators::NamedBase <em>instead </em>of Rails::Generators::Base. This means that our generator expects at least one argument, which will be the name of the initializer, and will be available in our code in the variable name.</p><p>We can also see that our new generator has a class method called source_root. This method points to where our generator templates will be placed, if any, and by default it points to the created directory lib/generators/initializer/templates.</p><h4>Templates</h4><p>To learn about templates, create lib/generators/initializer/templates/initializer.rb with the following content:</p><pre>class InitializerGenerator &lt; Rails::Generators::NamedBase<br>  source_root File.expand_path(&#39;templates&#39;, __dir__)<br> <br>  def copy_initializer_file<br>    copy_file &quot;initializer.rb&quot;, &quot;config/initializers/#{file_name}.rb&quot;<br>  end<br>end</pre><p>Now we can run this generator and pass it a name like, core_extensions</p><pre>rails generate initializer core_extensions</pre><p>We can see that now an initializer was created at config/initializers/core_extensions.rb with the contents of our template. The copy_file method copied a file in our source root to the destination path we gave. The method file_name is automatically created when we inherit from Rails::Generators::NamedBase.</p><h4>Generators with Templates</h4><p>When you run rails generate initializer core_extensions Rails requires these files in order until one is found:</p><pre>rails/generators/initializer/initializer_generator.rb<br>generators/initializer/initializer_generator.rb<br>rails/generators/initializer_generator.rb<br>generators/initializer_generator.rb</pre><h4>Customizing All Generators</h4><p>They can be configured in config/application.rb</p><pre>config.generators do |g|<br>  g.orm             :active_record<br>  g.template_engine :erb<br>  g.test_framework  :test_unit, fixture: true<br>end</pre><p>The scaffold generator <strong>doesn’t actually generate anything</strong>, it just invokes other generators that do the work. This allows us to add/replace/remove any of those invocations.</p><p>For example, the scaffold generator invokes the scaffold_controller generator, which invokes erb, test_unit and helper generators. Since each generator has a single responsibility, they are easy to reuse, avoiding code duplication.</p><p>If we want to avoid generating the default app/assets/stylesheets/scaffolds.scss file when scaffolding a new resource we can disable scaffold_stylesheet:</p><pre>config.generators do |g|<br>  g.scaffold_stylesheet false<br>end</pre><p>The next customization on the workflow will be to stop generating stylesheet, JavaScript and test fixture files for scaffolds altogether. We can achieve that by changing our configuration to the following:</p><pre>config.generators do |g|<br>  g.orm             :active_record<br>  g.template_engine :erb<br>  g.test_framework  :test_unit, fixture: false<br>  g.stylesheets     false<br>  g.javascripts     false<br>end</pre><p>If we generate another resource with the scaffold generator, we can see that stylesheet, JavaScript and fixture files are not created anymore. If you want to customize it further, for example to use DataMapper and RSpec instead of Active Record and TestUnit, it’s just a matter of adding their gems to your application and configuring your generators.</p><h4>Helper Generator</h4><p>Let’s create a new helper generator that simply adds some instance variable readers. First, create a generator within the <strong>rails </strong>namespace, since Rails searches there for generators used as <strong>hooks</strong>:</p><pre>rails generate generator rails/my_helper</pre><p>We’re <em>not </em>going to use source_root or the templates folder.</p><pre># lib/generators/rails/my_helper/my_helper_generator.rb<br>class Rails::MyHelperGenerator &lt; Rails::Generators::NamedBase<br>  def create_helper_file<br>    create_file &quot;app/helpers/#{file_name}_helper.rb&quot;, &lt;&lt;-FILE<br>module #{class_name}Helper<br>  attr_reader :#{plural_name}, :#{plural_name.singularize}<br>end<br>    FILE<br>  end<br>end</pre><p>We can try out our new generator by creating a helper for products:</p><pre>rails generate my_helper products<br>      create  app/helpers/products_helper.rb</pre><p>And you’ll get a file in app/helpers</p><pre>module ProductsHelper<br>  attr_reader :products, :product<br>end</pre><p>You can even configure the scaffold generator to use that helper</p><pre>config.generators do |g|<br>  g.orm             :active_record<br>  ...<br>  g.helper          :my_helper<br>end</pre><h4>Hooks</h4><p>This works sort of. It doesn’t make the test files for the helper, which maybe we want. We can make a quick change to our helper by providing a hook, then all a test framework needs to implement this hook in order for them to be compatible.</p><p>We can update our helper like so</p><pre># lib/generators/rails/my_helper/my_helper_generator.rb<br>class Rails::MyHelperGenerator &lt; Rails::Generators::NamedBase<br>  def create_helper_file<br>    create_file &quot;app/helpers/#{file_name}_helper.rb&quot;, &lt;&lt;-FILE<br>module #{class_name}Helper<br>  attr_reader :#{plural_name}, :#{plural_name.singularize}<br>end<br>    FILE<br>  end<br> <br>  hook_for :test_framework<br>end</pre><p>Now when we call the helper generator <em>and </em>TestUnit is configured as the test framework, Rails will try to invoke Rails::TestUnitGenerator <em>and </em>TestUnit::MyHelperGenerator. These don’t actually exist, but we can make a tiny adjustment to our hook to instead invoke TestUnit::Generators::HelperGenerator, which <em>does</em> exist.</p><pre>hook_for :test_framework, as: :helper</pre><h4>Customizing Generator Templates</h4><p>In the previous step we added a line to the helper that allowed tests to be generated for the generator. The better way to do that is to replace the templates of generators that already exist, like Rails::Generators::HelperGenerator.</p><p>Generators don’t just look in the source_root for templates. One of the other paths they look in is lib/templates. Since we want to customize Rails::Generators::HelperGenerator , we can do that by making a template copy <em>inside</em> /lib/templates/rails/helper with the name helper.rb. We’ll add the content:</p><pre>#  lib/templates/rails/helper/helper.rb<br>module &lt;%= class_name %&gt;Helper<br>  attr_reader :&lt;%= plural_name %&gt;, :&lt;%= plural_name.singularize %&gt;<br>end</pre><p>and update our application.rb</p><pre>config.generators do |g|<br>  g.orm             :active_record<br>  ...<br>  # g.helper          :my_helper<br>end</pre><p>And we still are generating our helper! This is useful, since we can customize our scaffold templates and/or layouts by just creating edit.html.erb inside lib.templates/erb/scaffold.</p><p>Note: ERB tags need to be escaped with an extra % in templates.</p><pre>&lt;%%= stylesheet_include_tag :application %&gt;</pre><h4>Generator Fallbacks</h4><p>Imagine if you wanted to add a feature on top of TestUnit like <a href="https://github.com/thoughtbot/shoulda">shoulda </a>does. Since TestUnit already implements all generators required by Rails and shoulda just wants to overwrite part of it. We don’t need to make shoulda <em>reimplement </em>some generators again. It can simply tell Rails to use a TestUnit generator if none was found under the Shoulda namespace.</p><p>If we once again modify our application.rb we can make this behavior happen.</p><pre>config.generators do |g|<br>  g.test_framework  :shoulda, fixture: false<br>  ...<br>  # Add a fallback!<br>  g.fallbacks[:shoulda] = :test_unit<br>end</pre><p>Now creating a scaffold will invoke shoulda generators but fallback to the TestUnit generators.</p><pre>rails generate scaffold Comment body:text<br>      invoke  active_record<br>      create    db/migrate/20130924143118_create_comments.rb<br>      create    app/models/comment.rb<br>      invoke    shoulda<br>      create      test/models/comment_test.rb<br>      create      test/fixtures/comments.yml<br>      ...<br>      invoke  test_unit<br>      create    test/application_system_test_case.rb<br>      create    test/system/comments_test.rb<br>      invoke  assets<br>      invoke    coffee<br>      create      app/assets/javascripts/comments.coffee<br>      invoke    scss</pre><p><strong>Fallbacks </strong>allow your generators to have a single responsibility, increasing code reuse and reducing the amount of duplication.</p><h4>Command Line Arguments</h4><p>Rails generators can be easily modded to get custom command line arguments. This functionality comes from <a href="http://www.rubydoc.info/github/erikhuda/thor/master/Thor/Base/ClassMethods#class_option-instance_method">Thor</a>:</p><p>class_option :scope, type: :string, default: &#39;read_products&#39;</p><p>Now our generator can be invoked as follows:</p><p>rails generate initializer --scope write_products</p><p>The command line arguments are accessed through the options method inside the generator class, like:</p><p><strong>@scope</strong> = options[<em>&#39;scope&#39;</em>]</p><p><a href="https://guides.rubyonrails.org/generators.html#first-contact">Creating and Customizing Rails Generators &amp; Templates - Ruby on Rails Guides</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=91bdebc4ca6d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Undo a Git Commit that You Already Pushed]]></title>
            <link>https://medium.com/@jelaniwoods/undo-a-git-commit-that-you-already-pushed-3fabc5edc9bb?source=rss-cbccbe91645d------2</link>
            <guid isPermaLink="false">https://medium.com/p/3fabc5edc9bb</guid>
            <category><![CDATA[git]]></category>
            <dc:creator><![CDATA[Jelani Woods]]></dc:creator>
            <pubDate>Tue, 07 May 2019 21:08:55 GMT</pubDate>
            <atom:updated>2019-05-07T21:08:55.173Z</atom:updated>
            <content:encoded><![CDATA[<p>git revert simply creates a new commit that is the opposite of an existing commit.</p><p>It leaves the files in the same state as if the commit that has been reverted never existed.</p><p>You can undo the last commit with :</p><pre>git revert HEAD</pre><p>A git revert is <em>just</em> another commit, so e.g. push to the remote so that other users can pull/fetch/merge the changes and you&#39;re done.</p><p><a href="https://stackoverflow.com/questions/19032296/how-to-use-git-revert">How to use Git Revert</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3fabc5edc9bb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>