<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Technical on Tim Loughrist</title><link>https://timloughrist.com/category/technical/</link><description>Recent content in Technical on Tim Loughrist</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Tue, 10 Sep 2024 19:06:40 +0000</lastBuildDate><atom:link href="https://timloughrist.com/category/technical/index.xml" rel="self" type="application/rss+xml"/><item><title>Stopping a click event without breaking your turbostream</title><link>https://timloughrist.com/2024/09/10/stopping-a-click-event-without-breaking-your-turbostream/</link><pubDate>Tue, 10 Sep 2024 19:06:40 +0000</pubDate><guid>https://timloughrist.com/2024/09/10/stopping-a-click-event-without-breaking-your-turbostream/</guid><description>&lt;p&gt;In a Rails application I have a link that has various elements within it. Some of those elements are actionable on their own while others are such that I want them to open the parent link when clicked. The elements that are actionable should not open the parent link when clicked. The link, among other things, renders a turbostream.&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;%=&lt;/span&gt; link_to edit_task_path(task.id), data&lt;span style="color:#ff7b72;font-weight:bold"&gt;:&lt;/span&gt; {action&lt;span style="color:#ff7b72;font-weight:bold"&gt;:&lt;/span&gt; &lt;span style="color:#a5d6ff"&gt;&amp;#34;cli&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a5d6ff"&gt;ck-&amp;gt;drawer#showDrawer&amp;#34;&lt;/span&gt;, &lt;span style="color:#a5d6ff"&gt;&amp;#34;turbo-stream&amp;#34;&lt;/span&gt;&lt;span style="color:#ff7b72;font-weight:bold"&gt;:&lt;/span&gt; &lt;span style="color:#79c0ff"&gt;true&lt;/span&gt;}) &lt;span style="color:#ff7b72"&gt;do&lt;/span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;%&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;&lt;/span&gt;div&lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;%=&lt;/span&gt; render ButtonComponent.&lt;span style="color:#ff7b72"&gt;new&lt;/span&gt;(data&lt;span style="color:#ff7b72;font-weight:bold"&gt;:&lt;/span&gt; {&lt;span style="color:#a5d6ff"&gt;&amp;#34;action&amp;#34;&lt;/span&gt;&lt;span style="color:#ff7b72;font-weight:bold"&gt;:&lt;/span&gt; &lt;span style="color:#a5d6ff"&gt;&amp;#34;cli&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#a5d6ff"&gt; ck-&amp;gt;selectable-table#select&amp;#34;&lt;/span&gt;}) &lt;span style="color:#ff7b72"&gt;do&lt;/span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;%&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;%&lt;/span&gt; render IconComponent.&lt;span style="color:#ff7b72"&gt;new&lt;/span&gt;(name&lt;span style="color:#ff7b72;font-weight:bold"&gt;:&lt;/span&gt; &lt;span style="color:#a5d6ff"&gt;&amp;#34;check_box&amp;#34;&lt;/span&gt;) &lt;span style="color:#ff7b72;font-weight:bold"&gt;%&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;%&lt;/span&gt; end &lt;span style="color:#ff7b72;font-weight:bold"&gt;%&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#f85149"&gt;/div&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;&lt;/span&gt;div&lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;gt;&lt;/span&gt; &#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;&lt;/span&gt;span&lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;gt;&amp;lt;%=&lt;/span&gt; task.title &lt;span style="color:#ff7b72;font-weight:bold"&gt;%&amp;gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#f85149"&gt;/span&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:#f85149"&gt;/div&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;%&lt;/span&gt; end &lt;span style="color:#ff7b72;font-weight:bold"&gt;%&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;As this code currently stands, clicking on the task title will open the link to the edit task path. Good. Clicking on the check box will fire the select method from the selectable-table stimulus controller. Also good.&lt;/p&gt;</description></item><item><title>Action Cable: simple implementation</title><link>https://timloughrist.com/2023/04/04/action-cable-simple-implementation/</link><pubDate>Tue, 04 Apr 2023 17:48:43 +0000</pubDate><guid>https://timloughrist.com/2023/04/04/action-cable-simple-implementation/</guid><description>&lt;p&gt;&lt;a href="https://guides.rubyonrails.org/action_cable_overview.html"&gt;Action Cable&lt;/a&gt; is a framework to allow multiple clients to receive seamless, simultaneous updates from a Rails server. If you have an application where changes submitted by one user should be seen immediately by all users without a refresh, Action Cable might be the solution you’re looking for.&lt;/p&gt;&#10;&lt;p&gt;In my case, I was developing a &lt;a href="https://github.com/tloughrist/capstone-frontend"&gt;two to four player variant of Chess&lt;/a&gt; where all players needed to be able to see every move made by the other players. Since all players are being served by the same server, they could see the new state of the board by refreshing their page, but that is clearly not an ideal way to handle the issue.&lt;/p&gt;</description></item><item><title>Friendship associations with rails</title><link>https://timloughrist.com/2023/01/26/friendship-associations-with-rails/</link><pubDate>Thu, 26 Jan 2023 16:13:44 +0000</pubDate><guid>https://timloughrist.com/2023/01/26/friendship-associations-with-rails/</guid><description>&lt;p&gt;Let’s say we’re building an event scheduling application. We want our users to be able to form friendships with one another, create events, and invite friends to those events. Implementing these functions poses some interesting challenges when using Rails and Active Record. In this post I’ll work through those challenges.&lt;/p&gt;&#10;&lt;p&gt;It is worth noting up front that we don’t need to rebuild the wheel here. There are plenty of gems that will allow you to easily implement friendship relationships, including:&lt;/p&gt;</description></item><item><title>Streamlining git commands with bash scripts</title><link>https://timloughrist.com/2022/11/15/streamlining-git-commands-with-bash-scripts/</link><pubDate>Tue, 15 Nov 2022 17:42:58 +0000</pubDate><guid>https://timloughrist.com/2022/11/15/streamlining-git-commands-with-bash-scripts/</guid><description>&lt;p&gt;Over the last several months, I’ve cloned nearly two-hundred Github repos and pushed hundreds of commits. Every time I’ve had to clone a repo and open the resulting directory in two steps, every time I’ve had to add, commit, and push, I’ve thought, “There’s got to be a better way!”&lt;/p&gt;&#10;&lt;p&gt;And of course there is. By writing custom Bash scripts, you can clone a repo into a directory and open that directory with one command and run through the add-commit-push cycle with another.&lt;/p&gt;</description></item><item><title>Building login functionality with React</title><link>https://timloughrist.com/2022/10/06/building-login-functionality-with-react/</link><pubDate>Thu, 06 Oct 2022 20:04:33 +0000</pubDate><guid>https://timloughrist.com/2022/10/06/building-login-functionality-with-react/</guid><description>&lt;p&gt;&lt;em&gt;Disclaimer: the information below should be used as an illustration only. For secure login functionality, you are best served by using a third-party authentication service, like &lt;a href="https://www.okta.com/"&gt;Okta&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;&#10;&lt;p&gt;Saving user data in order to customize user experience is a common feature in web apps. Implementing this feature requires the ability to:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;Allow users to input a username and password combination&lt;/li&gt;&#10;&lt;li&gt;Check that combination against the combinations associated with user data in a database&lt;/li&gt;&#10;&lt;li&gt;If the combination is found, the user data is stored in a state and used to customize user experience&lt;/li&gt;&#10;&lt;li&gt;If the combination was not found, the user would be alerted that the combination was incorrect and the user would not be able view user data&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;I’m going walk through two different ways to build out this feature using React. The first method only makes use of state while the second uses the browser’s session storage to hold a login token.&lt;/p&gt;</description></item><item><title>Persistent path-specific styling in React</title><link>https://timloughrist.com/2022/10/06/persistent-conditional-styling-in-react/</link><pubDate>Thu, 06 Oct 2022 12:52:58 +0000</pubDate><guid>https://timloughrist.com/2022/10/06/persistent-conditional-styling-in-react/</guid><description>&lt;p&gt;&lt;strong&gt;The situation&lt;/strong&gt;: you’re building a single-page application in React using client-side routing.&lt;/p&gt;&#10;&lt;p&gt;&lt;strong&gt;The goal&lt;/strong&gt;: to apply styling to an element as long as a user is on a particular “page”.&lt;/p&gt;&#10;&lt;p&gt;Imagine that you have a navigation bar across the top of your app with navlinks to the app’s various “pages”. You want the navlink for the current page to stand out so users can quickly see where they are in the app.&lt;/p&gt;</description></item><item><title>Promise chains and Promise.all</title><link>https://timloughrist.com/2022/09/03/promises-chains-and-promise-all/</link><pubDate>Sat, 03 Sep 2022 11:17:46 +0000</pubDate><guid>https://timloughrist.com/2022/09/03/promises-chains-and-promise-all/</guid><description>&lt;p&gt;Promises are a central component of &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Introducing"&gt;asynchronous JavaScript&lt;/a&gt;. While the promise provides instructions for what to do when a given task completes, the rest of your code can chug merrily along without interruption or delay.&lt;/p&gt;&#10;&lt;p&gt;For example, I might want to fetch a large chunk of data from some &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API"&gt;API&lt;/a&gt; when my code first initiates. This might take a bit of time and I don’t want that to get in the way of the rest of my code, e.g., assigning event listeners to elements on a web page and the like. So, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/fetch"&gt;&lt;code&gt;fetch()&lt;/code&gt;&lt;/a&gt; returns a promise that, if nothing goes wrong, is fulfilled. When it’s fulfilled, it returns a response from the API that is then available for further use.&lt;/p&gt;</description></item><item><title>Passing functions as arguments v. invoking functions in arguments</title><link>https://timloughrist.com/2022/08/10/callback-functions-and-immediately-invoked-function-expressions/</link><pubDate>Wed, 10 Aug 2022 12:23:38 +0000</pubDate><guid>https://timloughrist.com/2022/08/10/callback-functions-and-immediately-invoked-function-expressions/</guid><description>&lt;p&gt;JavaScript functions can take all sorts of things as arguments: numbers, strings, arrays, objects…even other functions. A function passed to another function as an argument is a &lt;em&gt;callback function&lt;/em&gt;. This is, however, distinct from invoking a function &lt;em&gt;in&lt;/em&gt; an argument.&lt;/p&gt;&#10;&lt;p&gt;Let’s say we have a function, &lt;code&gt;roll&lt;/code&gt;, which simulates rolling a &lt;code&gt;sides&lt;/code&gt;-sided die &lt;code&gt;throws&lt;/code&gt;-number of times and summing the result:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;&lt;span style="color:#ff7b72"&gt;function&lt;/span&gt; roll(&lt;span style="color:#ff7b72"&gt;throws&lt;/span&gt;, sides) {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72"&gt;let&lt;/span&gt; result &lt;span style="color:#ff7b72;font-weight:bold"&gt;=&lt;/span&gt; &lt;span style="color:#a5d6ff"&gt;0&lt;/span&gt;;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72"&gt;for&lt;/span&gt; (&lt;span style="color:#ff7b72"&gt;let&lt;/span&gt; i &lt;span style="color:#ff7b72;font-weight:bold"&gt;=&lt;/span&gt; &lt;span style="color:#a5d6ff"&gt;0&lt;/span&gt;; i &lt;span style="color:#ff7b72;font-weight:bold"&gt;&amp;lt;&lt;/span&gt; &lt;span style="color:#ff7b72"&gt;throws&lt;/span&gt;; i&lt;span style="color:#ff7b72;font-weight:bold"&gt;++&lt;/span&gt;) {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72"&gt;let&lt;/span&gt; seed &lt;span style="color:#ff7b72;font-weight:bold"&gt;=&lt;/span&gt; Math.random();&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72"&gt;while&lt;/span&gt; (seed &lt;span style="color:#ff7b72;font-weight:bold"&gt;===&lt;/span&gt; &lt;span style="color:#a5d6ff"&gt;0&lt;/span&gt;) {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; seed &lt;span style="color:#ff7b72;font-weight:bold"&gt;=&lt;/span&gt; Math.random();&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; result &lt;span style="color:#ff7b72;font-weight:bold"&gt;=&lt;/span&gt; result &lt;span style="color:#ff7b72;font-weight:bold"&gt;+&lt;/span&gt; Math.ceil(seed &lt;span style="color:#ff7b72;font-weight:bold"&gt;*&lt;/span&gt; sides); &#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; &lt;span style="color:#ff7b72"&gt;return&lt;/span&gt; result;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now let’s say we have an object, &lt;code&gt;RATHGAR&lt;/code&gt; . We want to assign &lt;code&gt;RATHGAR&lt;/code&gt; a property with a key of &lt;code&gt;str&lt;/code&gt; and a value determined by simulating rolling a six-sided die three times and summing the results. We might do so in the following way:&lt;/p&gt;</description></item><item><title>Reflection filter</title><link>https://timloughrist.com/2022/06/18/reflection-filter/</link><pubDate>Sat, 18 Jun 2022 12:13:09 +0000</pubDate><guid>https://timloughrist.com/2022/06/18/reflection-filter/</guid><description>&lt;p&gt;&lt;strong&gt;A warning: do not read this post if you’re currently working on problem set 4 for cs50. Doing so would violate the course’s academic dishonesty policy.&lt;/strong&gt;&lt;/p&gt;&#10;&lt;p&gt;I ran into an interesting bug while working on a c++ problem set for cs50. The function is supposed to flip a .bmp image horizontally. Here’s my first attempt:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" style="color:#e6edf3;background-color:#0d1117;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"&gt;&lt;code class="language-text" data-lang="text"&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;void reflect(int height, int width, RGBTRIPLE image[height][width])&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;{&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; RGBTRIPLE *buffer = malloc(sizeof(buffer));&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; for (int i = 0; i &amp;lt;= height; i++)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; for (int j = 0; j &amp;lt; width - j; j++)&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; {&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; *buffer = image[i][j];&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; image[i][j] = image[i][width - j];&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; image[i][width - j] = *buffer;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; }&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; free(buffer);&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt; return;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span style="display:flex;"&gt;&lt;span&gt;}&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This worked…almost. Every column of pixels was replaced by its partner from the opposite side of the image except the first column. That one was simply moved up one pixel.&lt;/p&gt;</description></item></channel></rss>