<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: bc-kelly</title>
    <description>The latest articles on DEV Community by bc-kelly (@bc_kelly).</description>
    <link>https://dev.to/bc_kelly</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F806960%2F033099fc-de15-4db8-9179-478c7148fe7b.jpg</url>
      <title>DEV Community: bc-kelly</title>
      <link>https://dev.to/bc_kelly</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bc_kelly"/>
    <language>en</language>
    <item>
      <title>Using SweetAlerts2</title>
      <dc:creator>bc-kelly</dc:creator>
      <pubDate>Thu, 12 May 2022 17:58:05 +0000</pubDate>
      <link>https://dev.to/bc_kelly/using-sweetalerts2-2a09</link>
      <guid>https://dev.to/bc_kelly/using-sweetalerts2-2a09</guid>
      <description>&lt;p&gt;While working on my most recent project, I decided to incorporate a few pop ups for different features. A quick pop up for signing in or adding a new item to a list is a good way to let users know their clicks are working, but the standard alert is not very appealing. Enter SweetAlerts2. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3eQlBTv---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxozwjdtaaeusbjov0xh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3eQlBTv---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fxozwjdtaaeusbjov0xh.png" alt="Image description" width="728" height="334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;vs&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1tG2NDc9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vgyccmv9eb973chz7i4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1tG2NDc9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5vgyccmv9eb973chz7i4.png" alt="Image description" width="880" height="259"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The SweetAlerts2 documentation is very helpful(&lt;a href="https://sweetalert2.github.io/"&gt;https://sweetalert2.github.io/&lt;/a&gt;) for further details. Here is a quick setup guide. My project is built using React, so these steps are assuming you have the component built where you want to show your alert. &lt;/p&gt;

&lt;p&gt;1) install sweetalerts2. &lt;br&gt;
$ npm install sweetalerts2&lt;br&gt;
2) Create a variable using sweetalerts in your function.&lt;br&gt;
 &lt;code&gt;const Swal = require('sweetalert2')&lt;/code&gt;&lt;br&gt;
3) Call your constant in the function where you want it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Swal.fire({
          title: 'Added!',
          // text: 'Do you want to continue',
          icon: 'success',
          timer: 1500,
          // confirmButtonText: 'Cool',
          showConfirmButton: false
        }).then(
          function () {},
          // handling the promise rejection
          function (dismiss) {
            if (dismiss === 'timer') {
            }
          }
        )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I did not want any text added to my alert so I commented that out.&lt;br&gt;
I also did not want any click button for users to close out the alert, I wanted it to disappear after a few seconds. To do this, I added in the timer line, shown in milliseconds. And showConfirmationButton needs to be set to: false. I also have the confirmationButtonText commented out since I am not using it. &lt;/p&gt;

&lt;p&gt;In addition to the added alert, I also included a "success" alert when the user logged in. The steps are the same as listed above but with some minor adjustments. &lt;/p&gt;

&lt;p&gt;1) set your constant:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Swal = require('sweetalert2')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) set the Toast constant for the popup:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Toast = Swal.mixin({
        toast: true,
        position: 'top-end',
        showConfirmButton: false,
        timer: 3000,
        timerProgressBar: true,
        didOpen: (toast) =&amp;gt; {
          toast.addEventListener('mouseenter', Swal.stopTimer)
          toast.addEventListener('mouseleave', Swal.resumeTimer)
        }
      })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3) Include it in your function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Toast.fire({
                icon: 'success',
                title: 'Signed in successfully'
              })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--j8qLFt7P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5cxw1yrxt1hn7bq57435.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--j8qLFt7P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5cxw1yrxt1hn7bq57435.png" alt="Image description" width="492" height="114"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are 5 different alert icons included in the SweetAlert2 docs: success, error, info, warning, and question. As you can see, I used the success icon for both of my alerts. &lt;/p&gt;

&lt;p&gt;I hope this article was helpful in getting started with SweetAlerts2. Please feel free to leave your thoughts or questions below.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>Conditional Rails Serializers</title>
      <dc:creator>bc-kelly</dc:creator>
      <pubDate>Thu, 14 Apr 2022 18:13:26 +0000</pubDate>
      <link>https://dev.to/bc_kelly/conditional-rails-serializers-og9</link>
      <guid>https://dev.to/bc_kelly/conditional-rails-serializers-og9</guid>
      <description>&lt;p&gt;Over the last few weeks while learning Ruby on Rails, one of my most common searches was serializer. The issue I came across most frequently was trying to use serializer on only one action in a model, while not using it on the other. &lt;/p&gt;

&lt;p&gt;Here is an example to show the issue I came across and how I solved it. The model I was working on was Episodes, and the two actions I was working on were show and index. I was looking to display all of my episodes along with their attributes with the &lt;em&gt;index&lt;/em&gt; action. And I was looking to display only one episode with the &lt;em&gt;show&lt;/em&gt; action, but also with the nested Guest information associated with that episode. When I tried to utilize a serializer in the show action, it affected my index action as well. &lt;/p&gt;

&lt;p&gt;In order to get around this, I used a flag in my serializer. Using a flag in the serializer with a condition allows you to restrict the serializer from applying to the action where you do not want it to apply. Below is the code in my serializer and my controller.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class EpisodeSerializer &amp;lt; ActiveModel::Serializer
  attributes :id, :date, :number

  has_many :guests, if: :condition

  def condition
    @instance_options[:flag] != "restrict"
  end

end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class EpisodesController &amp;lt; ApplicationController

    def index 
        @episode = Episode.all
        render json: @episode, flag: "restrict", status: :ok
    end


    def show 
        episode = Episode.find(params[:id])
        render json: episode, serializers: EpisodeSerializer, status: :ok
    end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is the response for the index action, showing only the episode data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
    {
        "id": 1,
        "date": "1/11/99",
        "number": 1
    },
    {
        "id": 2,
        "date": "1/12/99",
        "number": 2
    },
......
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is the response for the show action for one episode with the nested guest data as a result of the episode serializer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "id": 1,
    "date": "1/11/99",
    "number": 1,
    "guests": [
        {
            "id": 1,
            "name": "Michael J. Fox",
            "occupation": "actor"
        }
    ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is my first blog post and I am new to Rails so if there are any suggestions, please feel free to leave a comment below! &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>rails</category>
    </item>
  </channel>
</rss>
