<?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 Juliana Kang on Medium]]></title>
        <description><![CDATA[Stories by Juliana Kang on Medium]]></description>
        <link>https://medium.com/@julianajlk?source=rss-7c22d28554c1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*mTF66gUBPCeNRx3Xu9XkCA.jpeg</url>
            <title>Stories by Juliana Kang on Medium</title>
            <link>https://medium.com/@julianajlk?source=rss-7c22d28554c1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 01:12:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@julianajlk/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[Managing Dates with Moment.js]]></title>
            <link>https://medium.com/@julianajlk/managing-dates-with-moment-js-874d95132081?source=rss-7c22d28554c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/874d95132081</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[momentjs]]></category>
            <dc:creator><![CDATA[Juliana Kang]]></dc:creator>
            <pubDate>Sat, 20 Apr 2019 03:22:32 GMT</pubDate>
            <atom:updated>2019-04-20T03:22:32.653Z</atom:updated>
            <content:encoded><![CDATA[<p>In building a “to do” web application — <strong>Taskr</strong> — my initial idea was to set the date of each task using the JavaScript <a href="https://www.w3schools.com/js/js_dates.asp">Date Object</a>.</p><p>On <em>componentDidMount()</em> on the Form Component, a Date object is created using the <em>new Date()</em> constructor, formatted to month/day/year format, and set state to date.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rvVrVSmNpEl-hXiZ_woNsQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/896/1*KHnLZQy3bupB78fzKw6mIg.png" /></figure><p>Working with date and times can be tricky in JavaScript. Say hello to <a href="https://momentjs.com/">Moment.js</a> 🙌 a great JavaScript library to manage and format dates.</p><h4>Install Moment.js</h4><pre>yarn add moment || npm install moment --save</pre><p>Get the current date and time, by setting state to moment()</p><pre>date: moment()</pre><p>Make sure your <em>node</em> version is updated before installing.</p><pre>nvm ls //list all node versions <br>nvm use version# or nvm use node //use latest version</pre><p>There are several ways you can <strong>format dates</strong> —</p><pre>moment().format(&#39;MMMM Do YYYY, h:mm:ss a&#39;); // April 2nd 2019, 9:18:35 pm<br>moment().format(&#39;dddd&#39;);                    // Tuesday<br>moment().format(&quot;MMM Do YY&quot;);               // Apr 2nd 19<br>moment().format(&#39;YYYY [escaped] YYYY&#39;);     // 2019 escaped 2019<br>moment().format();                          // 2019-04-02T21:18:35-04:00</pre><p>And <strong>relative time</strong> —</p><pre>moment(&quot;20111031&quot;, &quot;YYYYMMDD&quot;).fromNow(); // 7 years ago moment(&quot;20120620&quot;, &quot;YYYYMMDD&quot;).fromNow(); // 7 years ago moment().startOf(&#39;day&#39;).fromNow();        // 21 hours ago moment().endOf(&#39;day&#39;).fromNow();          // in 3 hours moment().startOf(&#39;hour&#39;).fromNow();       // 19 minutes ago</pre><p>Now, the Task cards display the date it was created, and also the due date in the relative time format!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*8pfH2L2XylAg4JQbKHTvHw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*rdCPfSdlDtJFyyMDziLk0Q.png" /></figure><h4>Day Picker Component</h4><p>For the user experience, it’s not very practical to have to type in a date in case the input date is another date other than today’s date. Which is why I chose to use this <a href="http://react-day-picker.js.org/">day picker</a> reusable component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/790/1*fGTwe7-KRY6D7GOo_FEMWw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XDfxKn7T9OBp-NLu1ifJ6A.png" /></figure><p>The DayPickInput Component has the day in the format — Sat Apr 20 2019 12:00:00 GMT-0400 (Eastern Daylight Time). On handleDayClick, this format is changed the the desired “Month/Day/Year”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IKgrAPND3wPLtzzqupkpvw.png" /></figure><p>Instead of “Click to select a day”, you could potentially have as a placeholder the actual selected date by using the following —</p><pre>placeholder={`${formatDate(new Date())}`</pre><p>And there you have it! Managing dates is not so confusing after all by using Moment.js and a good calendar component 😃 👏 .</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=874d95132081" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript MV Patterns]]></title>
            <link>https://medium.com/@julianajlk/javascript-mv-patterns-c642076bb6aa?source=rss-7c22d28554c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/c642076bb6aa</guid>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Juliana Kang]]></dc:creator>
            <pubDate>Fri, 29 Mar 2019 03:06:41 GMT</pubDate>
            <atom:updated>2019-03-29T03:07:15.565Z</atom:updated>
            <content:encoded><![CDATA[<p>I recently learned about <a href="https://addyosmani.com/resources/essentialjsdesignpatterns/book"><strong>Addy Osmani’s book on Design Patterns</strong></a> from an engineer at a company I interviewed at. While at the <em>Flatiron School’s Software Engineering Immersive</em>, I learned about the <strong>MVC </strong>pattern. But Osmani also talks about the <strong>MVP</strong> and <strong>MVVM</strong> patterns.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/325/1*RRHCAnc1IxLwXrHMFZbIHw.png" /></figure><blockquote>Design patterns are reusable solutions to commonly occurring problems in software design.</blockquote><h4>MVC (Model-View-Controller)</h4><p>The MVC is a way to build frameworks for web applications, by providing a <em>separation of concerns</em>. Different files each have a specific responsibility.</p><blockquote>The pattern was originally designed by <a href="http://en.wikipedia.org/wiki/Trygve_Reenskaug">Trygve Reenskaug</a> during his time working on Smalltalk-80 (1979) where it was initially called Model-View-Controller-Editor.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J_iBiRLURjYTXz5mTwQBzA.png" /></figure><p>My teacher explained the <strong>MVC architectural design pattern</strong> with a <em>restaurant analogy </em>🍽:<em> </em>The cooks (<em>Model</em>) make the food, from the orders they receive from the waiter, placed by the customer. The customers receive plates of food (<em>View</em>). The waiters (<em>Controller</em>) go between the kitchen and the restaurant. They take orders from the customer to the kitchen (<em>from View to Model</em>).</p><p>JavaScript has many frameworks that use the MVC pattern — Backbone, Ember.js and AngularJS to name a few.</p><p><strong>Model</strong>: the logic of a web application, where data is changed and/or saved. A model may have various views observing it’s data updates.</p><blockquote>Persistence allows us to edit and update models with the knowledge that its most recent state will be saved in either: memory, in a user’s localStorage data-store or synchronized with a database.</blockquote><p><strong>View</strong>: the user-facing part, where the user interacts with the application. In JavaScript that means building the DOM element. They get updated when the model changes their state. The responsibility of actually updating the model goes to the Controller, the view is only a visual representation of models.</p><p>It’s recommended to use templating solutions — Handlebars.js and Mustache — so the views are dynamically loaded when needed, as opposed to manually creating all your HTML elements in-memory using string concatenation.</p><p><strong>Controller</strong>: the bridge between the models and the views. It transmits data from the browser to the application and vice-versa. This part in JavaScript frameworks is where it does not follow the classical MVC pattern, often referred as an MV* pattern.</p><blockquote>Backbone contains models and views, however it doesn’t actually have true controllers. Its views and routers act a little similar to a controller, but neither are actually controllers on their own.</blockquote><h4>MVP (Model-View-Presenter)</h4><p>It also has the separation of concerns across components like the MVC pattern, but there are key differences. Osmani refer’s to this pattern as passive architecture. It’s best used for web applications that have complex views with a lots of user interaction, so all logic is decoupled in a presenter.</p><p><strong>Presenter</strong>: component that holds the user-interface business logic. It receives the user requests and sends data back so the View can be updated accordingly when models change. It’s the Presenter’s responsibility to set the data.</p><h4>MVVM (Model-View-ViewModel)</h4><p>This design pattern clearly isolates UI development from business logic in a web application. It was created by Microsoft.</p><blockquote>UI developers write bindings to the ViewModel within their document markup (HTML), where the Model and ViewModel are maintained by developers working on the logic for the application.</blockquote><p>JavaScript frameworks such as KnockoutJS, Kendo MVVM and Knockback.js use this pattern.</p><p><strong>ViewModel</strong>: passes data from View to Model, acts like a Controller. Views and ViewModels talk to each other by two-way data-binding and events. Not required to reference a View because of data-binding.</p><blockquote>The ViewModel doesn’t just expose Model attributes but also access to other methods and features such as validation.</blockquote><p>Osmani notes that this pattern abstracts the View and decreases the business logic in the code, and facilitates unit testing comparing to event driven code. But data-binding is not recommended for simpler UIs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c642076bb6aa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[HTML Inline Frame (aka the <iframe>)]]></title>
            <link>https://medium.com/@julianajlk/html-inline-frame-aka-the-iframe-faaa4ad2e3e8?source=rss-7c22d28554c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/faaa4ad2e3e8</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[html]]></category>
            <dc:creator><![CDATA[Juliana Kang]]></dc:creator>
            <pubDate>Thu, 28 Mar 2019 21:46:14 GMT</pubDate>
            <atom:updated>2019-03-28T21:46:14.657Z</atom:updated>
            <content:encoded><![CDATA[<p>Recently, I learned about using the <strong>Inline Frame </strong>😃 It’s a great alternative to load another HTML page in the current HTML document, without having to refresh the browser or go to that page. This is done by embedding through the <strong>&lt;iframe&gt; </strong>tag. Why is this good? It won’t have any effect on the parent document, and we don’t need to worry about style inheritance.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/734/1*_HviC6MZOKcxk76GEL0T_g.jpeg" /><figcaption>Image <a href="https://blog.mozilla.org/fxtesteng/2013/02/13/part-2-ui-testing-on-firefox-os-working-with-iframes/">source</a>.</figcaption></figure><h4>Attributes 💻</h4><pre>&lt;iframe<br>    title=&quot;Inline Frame&quot;<br>    width=&quot;400&quot;<br>    height=&quot;350&quot;<br>    src=&quot;<a href="https://www.openstreetmap.org/export/embed.html?bbox=-0.004017949104309083%2C51.47612752641776%2C0.00030577182769775396%2C51.478569861898606&amp;layer=mapnik">https://www.urlhere.com/embed.html?options=</a>optionshere&quot;<br>    style=&quot;border:4px solid black&quot;&gt;<br>&lt;/iframe&gt;</pre><p>The <strong>height</strong> and <strong>width</strong> attributes specify the size in pixels by default — percentages can also be utilised in this case.</p><p>The <strong>src</strong> attribute specifies the URL of the embedded document.</p><p>The <strong>style</strong> attribute can be used to specify inline styling, although a separate CSS document is recommended.</p><p>The <strong>title</strong> attribute is good for accessibility issues, especially if there are multiple &lt;iframe&gt; tags in the same document, or the content of the embedded document is interactive.</p><h4>Handling &lt;iframe&gt; in React</h4><p>When working with <strong>React</strong>, I was curious to see how the <strong>Inline Frame</strong> would work with separate components. The embed is of a Youtube video from Bon Appetit 🧀</p><p>The <em>App Component</em> has the form input for the user to type the start time in seconds the video should be started. The <em>Frame Component</em> would then be responsible for rendering the <strong>&lt;iframe&gt;</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/598/1*u7NMXPv01T64KEKFUG6fjQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/470/1*1cArFbJ9etNWgLX5vzD1kg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/674/1*8tS8kMlxwu7nvpIXKu6D4Q.png" /><figcaption>App.js</figcaption></figure><p>On the <em>Frame Component</em>, I used the props passed in from the parent <em>App Component</em> to complete the url with the start in seconds, which was the input by the user in the parent component.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/790/1*x6t4kkh0LfqRZT52rZImWw.png" /><figcaption>Frame.js</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/589/1*5DkUF5_pupcD-B1tL_YtyA.png" /></figure><p>And it does indeed work 🙌</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=faaa4ad2e3e8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hosting a Ruby on Rails App on AWS]]></title>
            <link>https://medium.com/@julianajlk/hosting-a-ruby-on-rails-app-on-aws-e0c45ddcf661?source=rss-7c22d28554c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/e0c45ddcf661</guid>
            <category><![CDATA[ec2]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Juliana Kang]]></dc:creator>
            <pubDate>Wed, 27 Feb 2019 02:21:18 GMT</pubDate>
            <atom:updated>2019-02-27T02:22:39.960Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://aws.amazon.com/">Amazon Web Services</a> gives us on-demand cloud computing platforms — emphasis on the on-demand, very important because you don’t need to pay and guesstimate your usage in advance compared to on-premisses computing, so there is low upfront costs and ability to scale up and down as needed — but it can be a bit tricky for a newbie 😯.</p><p>Thank goodness for the World Wide Web (I looked into <a href="https://www.awsrails.com/rails-ec2/">this</a> and <a href="https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-with-rbenv-on-ubuntu-16-04">this</a> before setting up my Ruby on Rails Game of Thrones Tracker on AWS).</p><p>I will be using an <strong>EC2 instance</strong>, which is a virtual server in Amazon’s Elastic Compute Cloud for running applications on the AWS<strong> </strong>infrastructure. It hosts the bits of things you think of as a computer. An instance is a single virtual machine, backed up by virtual hard-drives (<em>EBS volumes</em>). Protected with firewall (<em>security group</em>).</p><h4><strong>Step 1: create EC2 instance and Security Group </strong>🗝</h4><p>After creating an account, create a new instance and security group. By default my machine is deployed on AWS cloud, so I need to grant permission to specific security groups.</p><pre>SSH port on default port 22 </pre><p><strong>SSH</strong> is a way for you to access the terminal prompt from the host machine to the cloud machine.</p><p>After the instance is created, get the public IP address AWS created of that instance.</p><pre>chmod 400 key-name-here.pem</pre><pre>ls -l //this will show you which users have which permissions </pre><p>Connect hosted machine: SSH into the machine as user to the public IP address.</p><pre>ssh -i &quot;key-name-here.pem&quot; <a href="/service/mailto:ubuntu@ec2-3-95-182-181.compute-1.amazonaws.com">ubuntu@p</a>ublicIPaddressHere<a href="/service/mailto:ubuntu@ec2-3-95-182-181.compute-1.amazonaws.com">.compute-1.amazonaws.com</a></pre><p>Add port 3001 Custom TCP Rule, and allow source: anywhere on security group.</p><h4><strong>Step 2: Set up Rails Development Environment</strong></h4><p>Once you have granted the above permissions, make sure you are on ubuntu@ip-here on your terminal.</p><pre>~$ sudo apt-get update<br>~$ sudo apt-get install autoconf bison build-essential libssl-dev libyaml-dev libreadline6-dev zlib1g-dev libncurses5-dev libffi-dev libgdbm3 libgdbm-dev</pre><p>Install <strong>rbenv</strong></p><pre>~$ git clone <a href="https://github.com/rbenv/rbenv.git">https://github.com/rbenv/rbenv.git</a> ~/.rbenv<br>~$ echo &#39;export PATH=&quot;$HOME/.rbenv/bin:$PATH&quot;&#39; &gt;&gt; ~/.bashrc<br>~$ echo &#39;eval &quot;$(rbenv init -)&quot;&#39; &gt;&gt; ~/.bashrc<br>~$ source ~/.bashrc<br>~$ rbenv //should see rbenv is a function if the above was properly installed </pre><p>Install <strong>Ruby</strong></p><pre>~$ git clone <a href="https://github.com/rbenv/ruby-build.git">https://github.com/rbenv/ruby-build.git</a> ~/.rbenv/plugins/ruby-build<br>~$ rbenv install -l<br>~$ rbenv install 2.3.3 //ruby version of your app<br>~$ rbenv global 2.3.3<br>~$ ruby -v<br>~$ echo &quot;gem: --no-document&quot; &gt; ~/.gemrc //Ruby gems NOT to generate local documentation for each installed gem</pre><p>Install <strong>bundler</strong></p><pre>~$ gem install bundler<br>~$ gem env home //check ruby gems path</pre><p>Install <strong>Rails</strong></p><pre>~$ gem install rails<br>~$ gem search &#39;^rails$&#39; --all //see all versions available<br>~$ gem install rails -v 5.2.1<br>~$ rbenv rehash<br>~$ rails -v //check if correct version was installed</pre><p>Install Git/Clone your repository of the app from GitHub —</p><pre>~$ git init<br>~$ which git<br>~$ git clone <a href="https://github.com/linkhere">https://github.com/repolinkhere</a> </pre><p>Install <strong>SQLite</strong></p><pre>~$ sudo apt-get install libsqlite3-dev</pre><p>Don’t forget to run “bundle install”</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/709/1*MhBzpdDgGhv7U4kcpiEFkw.png" /></figure><p>Install Node.js, a JavaScript runtime, some Rails features depend on JavaScript even though my app was written in Ruby —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/717/1*ESQ_4HRpWez0dc3TNcjfhw.png" /></figure><pre>~$ sudo add-apt-repository ppa:chris-lea/node.js<br>~$ sudo apt-get install nodejs<br>// make sure it&#39;s the latest version, install npm<br>~$ sudo apt-get install npm<br>~$ nodejs --version<br>~$ sudo npm cache clean -f<br>~$ sudo npm install -g n<br>~$ sudo n stable //this will install latest version</pre><p>Migrate and seed your database tables —</p><pre>bin/rails db:migrate RAILS_ENV=development<br>bin/rails db:seed RAILS_ENV=development<br>bundle exec rake db:seed RAILS_ENV=&#39;development&#39;</pre><p>Run rails on port 3001 —</p><pre>rails s -p 3001 -b 0.0.0.0</pre><p>Now you should see you app running on the IP address of the EC2 Instance (on port 3001).</p><p><strong>But wait…</strong></p><p>When your 💻 goes to sleep, the app stops running 😳. One fix is to use screen (<a href="https://www.digitalocean.com/community/tutorials/how-to-install-and-use-screen-on-an-ubuntu-cloud-server">here</a> is more info on that).</p><blockquote>Screen is a console application that allows you to use multiple terminal sessions within one window.</blockquote><pre>sudo apt-get install screen<br>screen rails s<br>ctrl + a  d </pre><p>The last command allows you to detach the existing process and let it run. The screen session is there, but managed in the background.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e0c45ddcf661" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Flatiron School: Final Project, part. 1]]></title>
            <link>https://medium.com/@julianajlk/flatiron-school-final-project-part-1-7e1a388d3560?source=rss-7c22d28554c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e1a388d3560</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[redux]]></category>
            <dc:creator><![CDATA[Juliana Kang]]></dc:creator>
            <pubDate>Mon, 29 Oct 2018 20:00:51 GMT</pubDate>
            <atom:updated>2019-04-23T03:49:09.367Z</atom:updated>
            <content:encoded><![CDATA[<p>They say time flies when you are having fun, and it’s hard to believe the final module in this program has started. Psychology explains that the more tasks you have to finish during a time interval, the faster that perceived time will pass — let’s just say the next 2 weeks of final project mode will fly by!</p><p>For my final project, I am creating an <em>Apparel Industry App</em>, where brands can track their supply chain and better communicate with their suppliers/factories when producing clothing.</p><p>The reason behind this idea is that my first job out of college was working with Product Development at a large U.S. fashion brand, and we relied on a very slow, clumsy, and outdated software.</p><p>I am utilising Ruby on Rails for the backend and React-Redux for the frontend.</p><h4><strong>Day 1: Wednesday, October 10, 2018</strong></h4><blockquote><em>Take away of the day: build vertically!</em></blockquote><p>I spent a good amount of time mapping out what all of my models would be and the relationships between them. But as my instructor Rob said, you should <strong>focus on one model at a time</strong>. I started with my <em>User Model </em>— where I quickly realised that I would need different types of users, depending if you are a <em>brand or a supplier. </em>Each brand/supplier will have many users. To accomplish this, instead of having a single-table inheritance, my User Model will have <strong><em>Polymorphic Associations</em></strong>. You can read more about this in the Rails Docs <a href="https://guides.rubyonrails.org/association_basics.html#polymorphic-associations">here</a>.</p><p>Rails convention for naming a polymorphic association adds “<em>-able</em>” at the end of the class name, but you can actually use any name — I’ve decided to use <em>“Organization”.</em> To tell our database we’re using a polymorphic association, we use special “<em>type</em>” and “<em>id</em>” columns for the polymorphic class (User).</p><p>Below, you can see that by using polymorphic associations, users belong to an “<em>Organization</em>”, which can be of type <em>Brand or Supplier</em>. And Brands/Suppliers can have many users.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/712/1*uCqwlSSrh66cagc7F2aJGg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TYT-rFBYKxtJ1BKY3OIHdg.png" /></figure><p>Tomorrow’s tasks: choose an UI Framework to be used with React.</p><h4>Day 2: Thursday, October 11, 2018</h4><blockquote>Take away of the day: polymorphic associations can be very tricky.</blockquote><p>In this app, I could potentially fetch my users in the <em>UsersContainer</em>. But the fetch should be handled by our <strong>Redux</strong> logic. But you can’t really do asynchronous actions in Redux — say hello to <strong><em>thunk</em></strong>! Thunk is just a <strong>function that dispatches functions </strong>— so you can make your actions dispatch more actions. To be able to use it, you need to install thunk with the command —</p><pre>yarn add redux-thunk</pre><p>Thunk comes in as a <strong><em>middleware</em></strong>, so you need to import {applyMiddleware, compose} from Redux, and use the <em>composeEnhancers()</em> method (we are now an advanced store!), which takes in the <em>applyMiddleware(thunk)</em> method with our thunk middleware.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/723/1*YQokHmaIkLMmeudcBKuHxw.png" /></figure><p>In my actions.js file, I can <em>fetchUsers()</em> — this is called on <em>componentDidMount()</em> on the <em>UsersContainer</em> on load of the app.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/633/1*LD-UrHwhceqkYDCcT0aTTw.png" /><figcaption>actions.js</figcaption></figure><p>Now you can dispatch actions within more actions…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/610/1*MBMmgrQ9-M4YmZwTeC97Vw.png" /><figcaption>reducers</figcaption></figure><p>I also added a “<em>loading</em>” state and reducer, for when the user’s network is slow to display a message while the fetch is working.</p><p>The combineReducers will let state know that inside the <em>loadingReducer()</em>, <strong>state = loading</strong>, and inside the <em>usersReducer(),</em> <strong>state = users</strong>*.</p><p>*see Friday below.</p><p><strong>CREATE USER with POLYMORPHIC ASSOCIATIONS</strong></p><p>I also worked on the CREATE part of my <em>User </em>model, which turned out to be much trickier than I expected because of the <em>polymorphic association</em>.</p><p>I could go about this problem in 2 ways: change the way my data was sent in the backend with Ruby, or change the way my form is capturing users’ input to match my data structure. I’ve decided to go with the second option.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/321/1*3ZzD5zSrp6eYNpB9SvYO-A.png" /><figcaption>This is my data structure for Users</figcaption></figure><p>…</p><p>Tomorrow’s tasks: fix the create User in the correct data structure, fetch Brands and Suppliers.</p><h4>Day 3: Friday, October 12, 2018</h4><blockquote>Takeaway of the day: pay attention to your syntax, especially when using Spread Operators + thunk/combineReducers!</blockquote><p>To create a user and have that user that was submitted on my form be added to my users array, I thought the below code would do the task.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*npdKq0U7JYjt_Oaq9l95aA.png" /></figure><p>This was my store’s state before adding a new user (which only has the 7 users of my seed data).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*Ayr9qc5AGXN8xgIgySU20A.png" /></figure><p>And this is my state after adding the new user “Jessie” —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*KXCaTHMBkajfdUBbxE5JbQ.png" /></figure><p>It added a new user (yay?) but not how it should be added. Here you can see that it added an entire users array inside the existing users array.</p><p>This spread operator syntax only works if I was NOT using <em>combineReducers()</em> like the below modified code —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/380/1*o54hftLyol8KZfRuzadiQA.png" /></figure><p>Because I am using <em>thunk/combineReducers()</em>, inside my <em>usersReducer()</em>, state is just users. So I don’t need to call <em>state.users</em> — I can just call <em>state</em>. But that was not my only problem. Because I am inside the <em>usersReducer()</em>, I don’t need to specify with the key users like in the previous code. This resulted in the below code — I am just making a copy of the users array (state) and adding that newly created user to the new array. Makes sense right?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/467/1*fNvaC7KQbX7E4yH7tFmkUQ.png" /></figure><p>No. This returned the below —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/1*ZtcBXVBLEtmPzYVUky7C7w.png" /></figure><p>It did not add a copy of the entire users array inside the users array like the previous problem, which is great. But it added a user key with my added user inside (why?….). Still not what I want.</p><p>The <strong>spread operator</strong> was not behaving like I expected. So I tried to use something else — the below code makes a new copy (when you don’t specify the arguments inside the <em>slice method</em>, it just makes a new copy of the entire array), then pushes the newly created user into that newly copied array.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/443/1*2B-40RsdLKkV5o_LXKZHCA.png" /></figure><p>The above code changed my state to the below —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/1*xE2X9C-CoAu2pU32tCfDCg.png" /></figure><p>It added a new key newUsers inside the users array. Still not what I want. Turns out, the <strong>curly braces make a difference</strong>!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/461/1*wtpS-ZMCvuNVB5UF0PnE8Q.png" /></figure><p>This resulted in the below state for users —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/631/1*W1DwquYEA5ywh2Fha0a4CA.png" /></figure><p>Great! That’s what I want. But I was still frustrated that I could not accomplish this using the <em>spread operator</em>. Turns out, the <strong>spread operator needs to be inline to work with brackets</strong>. See the very simple example below —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/671/1*gRkouuurCXaaro39z_bhNg.png" /></figure><p>So using the above finding, I refactored my code to the below —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/493/1*drfJkTS9eKTlNBsVqYH7-Q.png" /></figure><p>And this is my updated state, finally!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/632/1*3Vqhwtw2U8TddqbyxXv4AQ.png" /></figure><p>Weekend’s tasks: routes.</p><h4>Day 4 and 5: Weekend</h4><blockquote>Takeaway of the weekend: capitalisation matters!</blockquote><p>Because of the <em>polymorphic association</em> in my User Model, for the permitted params in my <em>UsersController</em> I added <strong><em>:organizationable</em></strong> so that when the form was submitted, it would add all of the key/values in user <em>:organizationable</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/491/1*fpILAuH19FK4RRCiuYNyfw.png" /></figure><p>The below is my <em>UserForm</em>, and the data that I was passing in to <em>createUser() </em>matched exactly the data structure of my user backend.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/543/1*t98FOqWz7WUwAL3DY_PrvQ.png" /><figcaption>UserForm</figcaption></figure><p>However, on submit of the form, I got a rollback. As you can see, it attempted to add a “user” with only “name” and “email”. So the params <em>:organizationable</em> was not working properly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/673/1*hN3Rr9Nt3NlOwkyXRGdtTA.png" /></figure><p>Rails is actually pretty clever, so it turns out that I don’t need <em>:organizationable</em> under params, only need <em>:organizationable_type</em> and <em>:organizationable_id</em> and it will figure out how to save that in the correct data structure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/495/1*7jMPCfM48M1kiPN-hXhkSQ.png" /><figcaption>Updated User params</figcaption></figure><p>With this, <em>onSubmit</em> on my <em>UserForm</em>, I no longer need a condition to specify if the <em>:organizationable</em> &gt; <em>:type</em> is “brand” or “supplier”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/506/1*_A4_HwEBVPa2HmqZQ8uIkw.png" /><figcaption>Updated UserForm</figcaption></figure><p>However, I still got a rollback when trying to submit my form and create a user. Read your error messages! It says “NameError (wrong constant name brand)”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/671/1*OjjIlYRs8KYZkIf3Z_mXQA.png" /></figure><p>It needs “brand” to be capitalized. I thought I should <em>.toUpperCase()</em> my value on the<em> handleOnSubmit()</em> function in my <em>UserForm</em>. But that returned the below, not what I wanted.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/668/1*UX3fIAvtXoU8qIIXC5ZUsA.png" /></figure><p>Since “brand” and “supplier” is being selected in my UserForm, I could just update the value to have it capitalised.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/466/1*Gibd-_BJbu5uFYFEFYAuyw.png" /></figure><p>Great! Success! User is created and saved to the database.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/671/1*Ct_A3S7GP0A8QnGd6WhpvA.png" /></figure><p>Interestingly, the “type” is saved as lowercase “brand”, even though it needs to be “Brand” to be created. Go figure…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/365/1*SzRtQObxtw-zqLpHgy3ryw.png" /></figure><p>Tomorrow’s tasks: Update User, redirects.</p><h4>Day 6: Monday, October 15, 2018</h4><blockquote>Takeaway of the day: forms are taken for granted!</blockquote><p>I worked on my Garment form today. The most difficult part was capturing different types of inputs in the right structure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/869/1*zBvveNf64Sbb4GWS0DiMqw.png" /></figure><p>Because I have select, upload, input, input number, and a slider, I had to write separate handle functions to deal with each individual type of input.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/596/1*dd4p0D1um1izvsVORrdcsA.png" /></figure><p>Tomorrow’s tasks: continue on the Garment Model (form).</p><h4>Day 7: Tuesday, October 16, 2018</h4><blockquote>Takeaway of the day: seed data must reflect real life data (even if it’s dummy/lorem ipsum text).</blockquote><p>Today we presented the skateboard version of our app. After our feedback, I spent a good amount of time creating data. For users, I used the <a href="https://randomuser.me/">Random User</a> API, but did not make a fetch because I don’t need too many users, so I saved them in my seeds file. Now my <em>User</em> has more than <em>:name</em> and <em>:email</em>.</p><p>In the <em>future</em>, I want to create a Company page, so when the user clicks on the name of the company you work at, the user can see all other users that work in the same company.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1002/1*cU5nGbjbh1anLOrLsT_Jjg.png" /></figure><p>Tomorrow’s tasks: image upload via Rails <a href="https://edgeguides.rubyonrails.org/active_storage_overview.html">Active Storage</a>.</p><h4>Day 8: Wednesday, October 17, 2018</h4><blockquote>Takeaway of the day: patience is a highly requested virtue for developers.</blockquote><p>Image upload is a feature I need both on my User form and on my Garment form. I started with the User. On creation, a user should be able to upload their avatar.</p><p>Rails has something called <strong>Active Storage</strong> to deal with uploads. You can read the Rails Docs <a href="https://edgeguides.rubyonrails.org/active_storage_overview.html">here</a>. In a very simplified summary, it creates 2 new tables with a polymorphic association: <strong><em>attachment</em></strong> (active_storage_attachments) and <strong><em>blob </em></strong><em>(</em>active_storage_blob) with the below command —</p><pre>rails active_storage:install<br>rails db:migrate</pre><p>Then, on your config &gt; storage.yml, add the below (amazon if using AWS for storage) —</p><pre>local:</pre><pre>service: Disk</pre><pre>root: &lt;%= Rails.root.join(&quot;storage&quot;) %&gt;</pre><pre>test:</pre><pre>service: Disk</pre><pre>root: &lt;%= Rails.root.join(&quot;tmp/storage&quot;) %&gt;</pre><pre>amazon:</pre><pre>service: S3</pre><pre>access_key_id: &quot;&quot;</pre><pre>secret_access_key: &quot;&quot;</pre><pre>bucket: &quot;&quot;</pre><pre>region: &quot;&quot; # e.g. &#39;us-east-1&#39;</pre><p>For now, I am storing my uploads locally.</p><p>Then add the following to config/environments/development.rb —</p><pre># Store files locally.</pre><pre>config.active_storage.service = <strong>:local</strong></pre><pre># Store files on Amazon S3.</pre><pre>config.active_storage.service = <strong>:amazon</strong></pre><pre># Store uploaded files on the local file system in a temporary directory.</pre><pre>config.active_storage.service = <strong>:test</strong></pre><p><strong>Attaching files to records</strong></p><p>I want my user to be able to upload only one image for their avatar. In this case, I have to use the has_one_attached macro.</p><pre>class User &lt; ApplicationRecord</pre><pre>  has_one_attached <strong>:avatar</strong></pre><pre>end</pre><p>And on my <strong>UsersController</strong>, I added <em>:avatar</em> to my permitted params.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/532/1*dBAk3ds5u27I-KXbCTYD1w.png" /><figcaption>UsersController</figcaption></figure><p>On the <strong>frontend</strong>, I added an &lt;input/&gt; with type “file” with a call to handlePictureUpload() on change, passing in the event.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*zvjCjDA0enRnuJyUio1uLg.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/429/1*aPQmAOlWeJAkSf90tWiyAw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/378/1*2cXjYfiWhE4tL0vTvOv-lA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/466/1*YvHBOmbWSQkPcSYnsEXi4g.png" /><figcaption>console.log</figcaption></figure><p>In the console.log(event.target.files), the <em>File(5836)</em> is what I need to send to my backend, which is why I set state picture: event.target.files[0] on my handle function. The file object is a <strong>special kind of object</strong> used to encode data. This is what Rails is expecting to see in the backend, that is why we need to pass this on upon UserForm submission.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/508/1*-sLtqRY5b0ug8k60ffcgzw.png" /><figcaption>actions.js</figcaption></figure><p>On my action to <em>createUser()</em>, instead of passing in the newUser and stringifying that into JSON data, I need to construct a set of key/value pairs with my form fields using FormData(). **The above code is not DRY and was the way I could make it work — just appending newUser into formData gave me a 500 error and I could not create a new user.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/697/1*hdyKtsaCYXtqbDnGvmatAg.png" /></figure><p><strong>FormData().append()</strong> takes in a name and a value as arguments. You can check the MDN docs <a href="https://developer.mozilla.org/en-US/docs/Web/API/FormData/append">here</a>. Name is the name of the field whose data is contained in value.</p><pre>// formData.append(name, value);<br>// formData.append(name, value, filename);</pre><pre>let formData = new FormData()</pre><pre>formData.append(&#39;username&#39;, &#39;Chris&#39;);<br>formData.append(&#39;userpic&#39;, myFileInput.files[0], &#39;chris.jpg&#39;);</pre><p>Remember the <strong><em>blob and attachment</em></strong>? On my backend, when the user is created, you can see below that a <em>blob</em> is also created —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/847/1*wBpXbau_7oSmWu2iJXl3sw.png" /></figure><p>When I add a byebug on my <em>UsersController</em> in the create method, you can get to the actual blob — with .avatar.attachment.blob.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*gJhdM-FRM-f6qlUj_DOpTQ.png" /></figure><p>And that is what I have to transform into an URL — by way to <strong><em>url_for()</em></strong>.</p><p>p.s. You need to add in your CreateUser migration a column for url (t.string :url), then DON’T forget to add it to your User Serializer. This is where the newly created URL will be saved.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/430/1*79s089ReSvD6BnJFcwD0ig.png" /><figcaption>UsersController</figcaption></figure><p>And this is the User object once a new user is created, which is passed to my <em>addUser()</em> function where it adds the newly created user to my users array in my Redux store.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/779/1*R8FFhXZ4euVOU2f2p32YVQ.png" /></figure><p>As you can see, the <em>url_for()</em> transformed my blob into an actual functioning url!! After this, all I need to do is display that url into an image tag in the frontend in the UserProfile page —</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/379/1*XOKgDE08-rjAeegfbQOaiQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/628/1*vb0TQk-n2DU7tgCrAoNnVQ.png" /></figure><p>Next is being able to edit the User’s avatar, because Jon definitely added the wrong picture!</p><p>Stay tuned for part 2, coming soon.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e1a388d3560" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Grids, Rows, and Columns in React-Bootstrap]]></title>
            <link>https://medium.com/@julianajlk/grids-rows-and-columns-in-react-bootstrap-c36a703c3c45?source=rss-7c22d28554c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/c36a703c3c45</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[bootstrap]]></category>
            <dc:creator><![CDATA[Juliana Kang]]></dc:creator>
            <pubDate>Sun, 30 Sep 2018 21:31:56 GMT</pubDate>
            <atom:updated>2018-09-30T21:31:56.873Z</atom:updated>
            <content:encoded><![CDATA[<p>Much like <a href="https://semantic-ui.com/">Semantic UI</a>, you can use <a href="https://react-bootstrap.github.io/">Bootstrap</a> — a great responsive mobile-first CSS framework — to build an aesthetically pleasing app in much less time than if you would write your own CSS. We want components, grids and pretty buttons after all!</p><p>For our project in Mod 4 (a Travel/Trip/Places Tracker), John and I decided it would be great practice to use React-Bootstrap, since we were both not very familiar with it. They do say it’s</p><blockquote>“The most popular front-end framework. Rebuilt for React”.</blockquote><h4><strong>Getting Started</strong></h4><p>The first step is to install react-dom and react-bootstrap. React-Bootstrap applies all of the Bootstrap 3 components as React components, so you can embed them into your JS files.</p><pre>$ npm install --save react react-dom <br>$ npm install --save react-bootstrap</pre><p><em>*Side note</em>: React-Bootstrap currently targets Bootstrap 3. So make sure you include the CSS for Bootstrap 3 instead of Bootstrap 4. Make sure both versions are the same! Add the below CDN to your <em>index.html</em> file.</p><pre>&lt;!-- Latest compiled and minified CSS --&gt;<br>&lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css&quot; integrity=&quot;sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u&quot; crossorigin=&quot;anonymous&quot;&gt;<br><br>&lt;!-- Optional theme --&gt;<br>&lt;link rel=&quot;stylesheet&quot; href=&quot;https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css&quot; integrity=&quot;sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp&quot; crossorigin=&quot;anonymous&quot;&gt;</pre><p>Now we can use any of the <a href="https://github.com/react-bootstrap/react-bootstrap/tree/master/src">components available</a> through React-Bootstrap.</p><h4><strong>Grids, Rows &amp; Columns</strong></h4><p>To use the responsive and fluid grid system Bootstrap offers, we need to import {Grid, Row, Col} from ‘react-bootstrap’.</p><p>Columns accept props for extra small, small, medium, and large devices, where as an example —</p><p><strong><em>xs={number}</em></strong><em> </em>is<em> </em>“the number of columns you wish to span for Extra small devices Phones (&lt;768px)”</p><p><strong><em>md={number}</em></strong> is “the number of columns you wish to span for Medium devices Desktops (≥992px)”</p><p>We thought the below would render a container separated into 3 columns, where <em>SideMenu</em> would take up 1/3 <em>(md={4})</em> and <em>Map + CardContainer</em> would take up the other 2/3 columns <em>(md={8})</em>, and inside <em>CardContainer</em> we would have 3 <em>PlaceCard (md={3})</em>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/962/1*OengjbOmGldeir-D6k1sYA.png" /></figure><p>The above code rendered the below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1imNi8TDZ0r2TJdc3j7E8Q.png" /></figure><p>But as you can see, it was somewhat close, but definitely not what we wanted. The fourth <em>PlaceCard</em> should be rendered on another row.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FxDQ3Oql1BN54c%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fdog-confused-i-have-no-idea-what-im-doing-xDQ3Oql1BN54c&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FxDQ3Oql1BN54c%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="300" frameborder="0" scrolling="no"><a href="https://medium.com/media/025cebbf3c6e5ce4cc959052a6dc0fb0/href">https://medium.com/media/025cebbf3c6e5ce4cc959052a6dc0fb0/href</a></iframe><p>Turns out, one important piece of information we found out later is that the entire grid is divided into <strong>12 parts only</strong>. Which means, how we had the code on <em>TravelContainer.js</em> (with 1/3 being md={4}) was correct: one column will take up 4 parts out of the 12, and the other column will take up the remaining 8 parts out of 12. <strong>But each container within that is also divided by 12</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/855/1*1onnJ9EbF4ugaRn02GlF2w.png" /></figure><p>After learning that, it meant container “2” also had 12 parts. We changed the <em>md={3}</em> to <em>md={4}</em> on <em>CardContainer.js</em> for each <em>PlaceCard. </em>Following the 12 part logic, we want 3 cards to be rendered in each row, so they should each take up 4 parts out of the 12.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/971/1*y3FPsVXPfEuU-ml5WqJLJw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*uSjrPzmkyfpjI0FuiqKywQ.png" /></figure><p>That’s some progress! We finally have the fourth <em>PlaceCard</em> rendering on another row. But that was still not what we wanted… why was the <em>CardContainer</em> not responsive? Hey Bootstrap, you’re supposed to do that magic…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/959/1*7iJayPfsdMrmPCk40QyaIQ.png" /></figure><p>Turns out, telling <em>CardContainer</em> on <em>TravelContainer.js</em> column should be md={8} is not enough, you also need to specify that in <em>CardContainer.js</em>, otherwise it will think it has the width of the entire 12 parts. <strong>Nested Rows</strong> to the rescue! We were then able to set the width to 8 parts out of the 12.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TlKHVIsfST8KezndWhqi-g.png" /></figure><p>Mission accomplished!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F18pjPEqqIt2k8%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fcomedy-drama-emmys-18pjPEqqIt2k8&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F18pjPEqqIt2k8%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="244" frameborder="0" scrolling="no"><a href="https://medium.com/media/33a1f8167e02d04f8d9f0111791d6b48/href">https://medium.com/media/33a1f8167e02d04f8d9f0111791d6b48/href</a></iframe><h4>Other Props used with Grid: Offset, Push, Pull</h4><p>The <strong><em>Offset</em></strong> prop moves the columns to the right.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/962/1*Jo5puZYzk9fMmtE8EYF9hA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*_P67WcRqc15OsdDcbJmwVg.png" /></figure><p>Note that in the below example, the <em>xsOffset</em> value and the <em>xs</em> value sum up to 12. If they sum up to a number greater than 12, the <em>PlaceCard</em> would be <em>Offset</em> outside of the <em>CardContainer</em> Column, like below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/396/1*N55Ua4s5owH3aQG32fwQtA.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/819/1*EX2B0jGHjrvaPPWGXj7Ldg.png" /></figure><p><strong>Pull</strong> changes the order of grid columns to the left. <strong>Push</strong> changes the order to the right.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/432/1*DQXASgaBmFzPGQcQuPEorQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*yher5fMI1ksFunlRMfzPfg.png" /></figure><p><a href="https://react-bootstrap.github.io/layout/grid/">Here</a> you can see all of the Props available in React-Bootstrap Grid system.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c36a703c3c45" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[REM vs. EM units]]></title>
            <link>https://medium.com/@julianajlk/rem-vs-em-units-93509e0ac759?source=rss-7c22d28554c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/93509e0ac759</guid>
            <category><![CDATA[css]]></category>
            <dc:creator><![CDATA[Juliana Kang]]></dc:creator>
            <pubDate>Thu, 20 Sep 2018 12:20:53 GMT</pubDate>
            <atom:updated>2018-09-20T12:32:58.359Z</atom:updated>
            <content:encoded><![CDATA[<p>How we (humans) read and interpret data has a lot to do with <em>typography</em>. What is typography exactly?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/613/1*nky8KY01t9KB1TukFU-Rgg.png" /><figcaption>dictionary.com</figcaption></figure><p>There are many types of fonts — serif, sans serif, modern, script, calligraphy, bubble, and so on. For designers, typography plays a HUGE role into the look and feel of a design, whether it is in web or print. Use it correctly, it can change the mood of the viewer. Use it incorrectly, it can make your entire design look outdated, or just not readable! Typography plays a very important role in branding as well.</p><p>Choosing the right type of font, however, is only the first step in this process. When displaying text on the web and on print, there is much power in the<em> size</em> of the font. Don’t believe me?</p><p>Believe me, font size matters!</p><p><strong>YES, FONT SIZE MATTERS!</strong></p><h3>DON’T BELIEVE ME YET? FONT SIZE MATTERS!</h3><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FitnJ1gt7pXmLK%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fangry-spongebob-squarepants-scream-itnJ1gt7pXmLK&amp;image=https%3A%2F%2Fmedia1.giphy.com%2Fmedia%2FitnJ1gt7pXmLK%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="324" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a759c660727cb24b33c33ed242c467e/href">https://medium.com/media/1a759c660727cb24b33c33ed242c467e/href</a></iframe><p>Just in this simple example, writing in caps may seem like I’m SCREAMING.</p><p>Font size also helps to determine the <strong>hierarchy of information</strong> to be displayed. We usually write main points/titles in a more stylised font and larger size, while leaving the detailed and longer text information in a clean and smaller sized font.</p><p>Now, let’s get to the way fonts are displayed on the web — when writing CSS, we can determine the size of a font like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/669/1*zt22af84xQfWnrmoGaQzxA.png" /></figure><p>But should we really use points or even pixels here? Points are used in print. Pixels are used in web, but they are absolute — fixed size with many limitations. The truth is, font-size != pixel size on screen. In many cases, different font-families with the same size may actually have a different length/height in pixels for the same letter.</p><h4><strong>EM units</strong></h4><p>An <em>em unit</em> measures the size of a font. It’s a CSS unit, called ‘<em>em</em>’ because it was originally equal to the width of the capital letter M.</p><p>The same CSS above can be written like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/667/1*Vju6Ceytml6SXK8LvgwCrw.png" /></figure><p>What’s the difference you ask? 1em = 12 pt. <strong><em>1em</em></strong> is actually equal to the <em>current font size</em>, and the default text size is provided by the browser, <strong><em>16px</em></strong> or 12pt. However, if there is a div element with font-size: 18px, for that parent element and it’s children, 1em = 18px. So it will only inherit from the browser’s default text size if there are no parents.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/624/1*8uyYeMgXiAcHphWGoV7UNw.png" /></figure><p>The <strong><em>em</em></strong> is special and more preferable than using pt because it’s scalable. It’s relative to the font size of it’s direct or nearest parent as we saw earlier. 2em would be equal to 32px, 3em to 48px and so on. We can also use decimals with <strong><em>em</em></strong>. 0.25em would be equal to 4px. This comes in handy when designing for mobile devices and rendering different mediums, and in today’s world, we think of mobile-first web development.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FFpouo0w0ULhUA%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fmy-s4-Fpouo0w0ULhUA&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FFpouo0w0ULhUA%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="210" frameborder="0" scrolling="no"><a href="https://medium.com/media/1b95ef55539970dffc684e960a30ee96/href">https://medium.com/media/1b95ef55539970dffc684e960a30ee96/href</a></iframe><p>But the em world is not just unicorns and rainbows. Because it’s relative to the parent, this causes a compounding issue.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/693/1*QQXxZYQWDvl2PaoP6DUmYA.png" /></figure><p>Let’s say for the above HTML, the CSS font-size for &lt;div&gt; is set to 2em, and for &lt;h4&gt; to 1.5em. One would think that &lt;h4&gt; would be rendered at 24px (1.5 x 16px default from the browser). However with em units, it compounds to the parent’s font-size, rendering a 48px size (1.5 x 2 x 16px). It get’s even uglier if this &lt;div&gt; was inside of another &lt;div&gt; with another font-size in ems.</p><h4><strong>Thank goodness for REM units!</strong></h4><p>The <em>rem unit</em> is only relative to the root (html) element font-size. We can define a single font size on the html element and have all the other elements’ font-sizes be a <em>percentage</em> of the root font-size. If the root element font-size is not defined, it will take the browser’s font-size by default.</p><p><strong>A tip on calculating %:</strong></p><p>Let’s say you’re using the browser’s default 16px font-size, and you set your root element font-size to be 100%. So 1rem = 16px. What if you want the font-size of a particular element to be equal to 28px, how would you know the equivalent to that in rem units? It can be a little cumbersome to have to do math all the time!</p><p>The solution is to calculate what percentage of the root font-size equals to 10px — decimals make things simpler. In this case, it’s 62.5% (62.5% of 16px = 10px). So now you know that 1rem = 10px. So for 28px, you would set it equal to 2.8rem. In this way, you only need to do the math once!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2F76duN2S3Bfof6%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Ftext-way-easy-76duN2S3Bfof6&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2F76duN2S3Bfof6%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="195" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa6a07b47fb5596293fe0d978a450676/href">https://medium.com/media/fa6a07b47fb5596293fe0d978a450676/href</a></iframe><p>As developers, we don’t create static pages for a single browser for a single medium. We need scale (without having to change the size of every single item once you make one change). Using REM units allows you the flexibility you need not only for font-size, but for margins, padding, letter-spacing, and so forth. It’s all about proportion.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93509e0ac759" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Validations & Helper Methods in Ruby on Rails]]></title>
            <link>https://medium.com/@julianajlk/validations-helper-methods-in-ruby-on-rails-b751e404c830?source=rss-7c22d28554c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/b751e404c830</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <dc:creator><![CDATA[Juliana Kang]]></dc:creator>
            <pubDate>Thu, 23 Aug 2018 13:26:23 GMT</pubDate>
            <atom:updated>2018-08-23T19:11:24.573Z</atom:updated>
            <content:encoded><![CDATA[<p>Anytime an application needs to accept data from the outside world, you need validations. In Rails, they safeguard the database by rejecting bad/invalid data from being saved.</p><p>How many times have you come across a website, tried to create a login username and sadly, your intended username has already been taken by someone else?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ucs8yHCgu-543lOU98sKOw.png" /></figure><p>Or maybe you didn’t remember you had an account, tried to create one with your email address, and saw that you indeed had an account. Now to remember that password… that’s another story, on another blog post!</p><p>All of these verifications can be done thanks to validations class methods. Yay ActiveRecord!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fl2YWCPLrCIaNc9QT6%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2FFilmeditor--halloween-ashley-olsen-l2YWCPLrCIaNc9QT6&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fl2YWCPLrCIaNc9QT6%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="324" frameborder="0" scrolling="no"><a href="https://medium.com/media/b580d6bf44b7eae84e799bc6a3f1ab4b/href">https://medium.com/media/b580d6bf44b7eae84e799bc6a3f1ab4b/href</a></iframe><p>Rails provides many useful methods for commonly used validations, such as <em>presence</em>, <em>confirmation</em>, <em>inclusion/exclusion</em>, <em>numericality</em>, etc.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/453/1*3AEFBMqtC0gx5ura-uz-AA.png" /></figure><p>In the above example, <em>#validates</em> confirms username is typed as an input on the create new user form, and is unique — <em>no evil twin here</em>! It also confirms the password is at least 6–15 characters long.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/447/1*jgSNscbnVLJBoRuUy8Uvgg.png" /></figure><p>But wait! With Rails, we can actually use <em>#has_secure_password </em>for simple authentication, simplifying our validation for password. It adds the basic validations, such as <em>presence</em>, <em>uniqueness</em> and <em>confirmation</em> automatically on :create, so all we need to check in this case is the length of the password.</p><p><strong>EMAIL VALIDATION</strong></p><p>One user with the wrong typed email address may not break your application now, but imagine having thousands and thousands of users with an invalid email — you would never be able to reach them.</p><p>But how would we check if the email was typed correctly? There are great library sources such as <a href="https://github.com/plataformatec/devise/blob/ee01bac8b0b828b3da0d79c46115ba65c433d6c8/lib/devise.rb">Devise</a> that has written expressions already created.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/797/1*P_8i4WSUokwhv84CW0aGhw.png" /></figure><pre>Class User &lt; ApplicationRecord<br>  validates :email, <br>     presence: true, <br>     format: { with: /<strong>\A</strong>[^@<strong>\s</strong>]+@[^@<strong>\s</strong>]+<strong>\z</strong>/ }<br>end </pre><p>The above validation will check for the presence of an email before creating a user, it will confirm the email address does not have any whitespaces or @ in the domain URL and local part, and only has one @ symbol in between them.</p><p><strong>What’s up with all these symbols?</strong></p><ol><li>\A and \Z matches the start and end of the string.</li></ol><p>2. ^ matches the start of a line.</p><p>3. \s whitespace</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1013/1*GxQ-7h6-lgiwF6iUFrczTQ.png" /></figure><p>*Cheat sheet from <a href="http://rubular.com/regexes/4765">Rubular</a>.</p><h4>HELPER METHODS</h4><p>Rails generators have some very handy helper methods to — for a lack of a better word — help us write simpler code. A better way to write the above email validation is using helper method <em>#validates_format_of</em>:</p><pre>Class User &lt; ApplicationRecord<br>  validates_format_of :email,<br>     with<strong>:</strong> /<strong>\A</strong>[^@<strong>\s</strong>]+@[^@<strong>\s</strong>]+<strong>\z</strong>/<br>end</pre><p>Another handy helper method validator is #validates_acceptance_of:</p><pre>Class User &lt; ApplicationRecord <br>   validates_acceptance_of :terms_of_service,<br>       message: &quot;must be accepted&quot; <br>end</pre><p>This will assure the “I accept the Terms of Service” checkbox is checked before creating a new user by default on save and if <em>:terms_of_service</em> is not <em>nil</em>.</p><p>Bet you will never look at a sign up form the same way again!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FS7EBSPPVrigiA%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Foh-really-o-rly-S7EBSPPVrigiA&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FS7EBSPPVrigiA%2F200.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="195" frameborder="0" scrolling="no"><a href="https://medium.com/media/cdc43088af4e7be3ccb35a2a05d004c9/href">https://medium.com/media/cdc43088af4e7be3ccb35a2a05d004c9/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b751e404c830" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Active Record & Relationships]]></title>
            <link>https://medium.com/@julianajlk/active-record-relationships-6b66bb6c0c56?source=rss-7c22d28554c1------2</link>
            <guid isPermaLink="false">https://medium.com/p/6b66bb6c0c56</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[activerecord]]></category>
            <dc:creator><![CDATA[Juliana Kang]]></dc:creator>
            <pubDate>Wed, 08 Aug 2018 17:38:51 GMT</pubDate>
            <atom:updated>2018-08-09T13:13:26.538Z</atom:updated>
            <content:encoded><![CDATA[<p>Relationships are difficult. Can a relationship work when values are different? My mother would say “absolutely not”. The same applies to Object Oriented Ruby. The <em>:primary_key</em> and <em>:foreign_key</em> ID’s must have the same value in order for the associations between models to work. The model with the belongs_to association has the <em>:foreign_key</em> ID.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ej-NSYZqY2xInSG3myvgiQ.jpeg" /></figure><p>But how do we <em>define the relationship</em>?</p><p>That’s something most people have a hard time with IRL — the so called “<em>what are we</em>” conversation has agonised many couples when determining where they stand. When it comes to Ruby and Object Oriented Models, defining the relationship early on is CRUCIAL.</p><p>When we make associations, the association names coincide with the names of the models and tables. In a many-to-many relationship (has_many :through) between guests, listings, and trips, where the class Trip acts as the connector between Guest and Listing classes, we can define the relationships as below:</p><pre>class Guest &lt; ActiveRecord::Base<br>  has_many :trips<br>  has_many :listings, through: :trips</pre><pre>class Trip &lt; ActiveRecord::Base<br>  belongs_to :guest<br>  belongs_to :listing<br>end</pre><pre>class Listing &lt; ActiveRecord::Base<br>  has_many :trips<br>  has_many :guests, through :trips<br>end</pre><h3>Embracing Active Record Associations</h3><p>Active Record is magic. Well, it’s actually an ORM (Object Relational Mapping). It streamlines your code, so you don’t have to spend precious time coding SQL statements and queries to retrieve your data. Your Ruby file will inherit many useful methods from Active Record, such as .all, .find_by, .where, and .create*.</p><p>*.create is a very handy method — instead of using User.new and User.save to save your newly created user in the database, you can just use User.create. It creates a new user, and saves it at the same time. “You’re welcome”, says Active Record.</p><p>In the Guest/Listing/Trip relationship, one could delete a new Trip without Active Record with the following code:</p><pre>@new_trip = Trip.create(:guest_id =&gt; @guest.id, :listing_id =&gt; @listing.id)</pre><pre>@new_trip = Trip.find_by_guest_id(@guest.id) <br>@new_trip.each do |t|   <br>  t.destroy <br>end</pre><p>To delete the trip, we would need to search the trip by the guest’s ID, then iterate over. BUT with Active Record, all you need to code is:</p><pre>@new_trip.destroy</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/120/1*cXne8pbvP1ATST8a4uQx4w.png" /></figure><h4>Mind blowing, right?</h4><p>With Active Record associations, life gets much easier! That’s why defining the relationship early between the models is so important.</p><h4>MOD 1 PROJECT 1 EXAMPLE:</h4><p>On our Project 1 Command Line Interface, where Investor has many Investments (and has many Ideas through Investments), and Inventor has many Ideas (and has many Investments through Ideas), without Active Record, our code to find a user (by the user name input) would consist of:</p><pre>puts &quot;Please enter your name&quot;<br>@user_input = gets.chomp<br>investor_name_arr = Investor.all.map {|n| n.name}.to_s <br>verify = investor_name_arr.include?(@user_input.downcase)                 Investor.find_by(verify) #find exising Object from Investor Class</pre><pre>if verify == false</pre><pre>puts &quot;What company do you work for?&quot;      <br>company_input = gets.chomp          <br>x = Investor.new(name: @user_input, company: company_input)<br>x.save</pre><p>With Active Record, there is no need to — 1) <em>.map</em>, 2) verify if the user already exists using <em>.include?</em> , 3) if it does not, make a <em>.new</em> user, 4) <em>.save</em> it. Less is more!</p><pre>name = Investor.find_or_create_by(name: @user_input.downcase)<br>puts &quot;Welcome #{name}!&quot;</pre><p>This will find an instance of a class that meets the criteria (ie. name that corresponds to the user’s input). If it meets the criteria, the instance will be retrieved. If it does not find any instance with the criteria, it will create one.</p><h3>.find_or_create_by vs. first_or_create</h3><p>Let’s suppose you want to find an Investor named “Bob”, and if there is no “Bob” in the database, create one.</p><pre>Investor.find_or_create_by(name: &quot;Bob&quot;)</pre><p>What if there happens to be another user, who is also an Investor and is named “Bob”? How would <em>.find_or_create_by</em> know that the user input “Bob” is not the same “Bob” that was in the database already?</p><p>That’s when we can use <em>.first_or_create</em>!</p><pre>Investor.where(name: &quot;Bob&quot;).first_or_create(company: &quot;Flatiron&quot;)<br>#=&gt; #&lt;Investor id:1, name: &quot;Bob&quot;, company: &quot;Flatiron&quot;, created_at &quot;2018-08-05 10:44:11&quot;&gt;</pre><p>This allows us to load the first record (find an Investor with name: “Bob”), then if it returns <em>nil</em>, it creates an user with the attribute :company as well as :name from the <em>.where</em> method.</p><p>In this way, it’s clear which arguments are used for what (as opposed to <em>.find_or_create_by</em>).</p><p>Now let’s say that you actually have many more attributes, and want to be able to create an user with these attributes. You can use <em>.first_or_create</em> with a block to loop through the other attributes and include them in the creation (so you don’t get <em>nil</em> if used <em>.find_or_create_by</em>).</p><pre>name = Investor.where(name: &quot;Bob&quot;, company: &quot;Flatiron&quot;).first_or_create do |name|<br>    name.department = &quot;Marketing&quot;<br>    name.email = &quot;bob@flatironschool.com&quot;<br>    name.city = &quot;NYC&quot; <br>end </pre><p>So it will look for the first “Bob” at “Flatiron” in the database, and if it returns <em>nil</em>, it will create a new Investor named “Bob”, loop through and add :department, :email and :city as specified. The block is executed only if a new instance is being created. If the user exists, the block will not be executed.</p><p>So <em>.first_or_create</em> is great for when you know you have a lot of data, and need to import possible duplicate records with multiple attributes.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2Fci0uDcGQtBh0k%2Ftwitter%2Fiframe&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2Fhappy-excited-cool-beans-ci0uDcGQtBh0k&amp;image=https%3A%2F%2Fmedia.giphy.com%2Fmedia%2Fci0uDcGQtBh0k%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="231" frameborder="0" scrolling="no"><a href="https://medium.com/media/c475a2599586800490a30e26bbfcda67/href">https://medium.com/media/c475a2599586800490a30e26bbfcda67/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6b66bb6c0c56" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>