<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Rails 5 &#8211; redpanthers.co</title>
	<atom:link href="/category/rails-5/feed/" rel="self" type="application/rss+xml" />
	<link>/</link>
	<description>Red Panthers - Experts in Ruby on Rails, System Design and Vue.js</description>
	<lastBuildDate>Mon, 29 Apr 2019 11:28:12 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.2.7</generator>
	<item>
		<title>Rails 5.2.0</title>
		<link>/rails-5-2-0/</link>
				<pubDate>Mon, 29 Apr 2019 11:28:12 +0000</pubDate>
		<dc:creator><![CDATA[levi]]></dc:creator>
				<category><![CDATA[Rails]]></category>
		<category><![CDATA[Rails 5]]></category>

		<guid isPermaLink="false">http://redpanthers.co/?p=16152</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[

<p class="western" align="justify"><span style="font-size: large">Cheers everyone, the new Rails 5.2.0 has released. Let’s have a look at the new features and upgrades in it.</span></p>




<p class="western" align="justify"><span style="font-size: large">It’s been too hard to deal with file uploads in Rails. T</span><span style="font-size: large">here’s been a lot of fine plugins available, but it was overdue that we incorporated something right into the framework. Finally, we have one, in the form of <a href="https://github.com/rails/rails/blob/d3893ec38ec61282c2598b01a298124356d6b35a/activestorage/README.md">Active Storage</a></span><span style="font-size: large">. This release includes Active Storage, a new framework provided by Rails to make it easier to upload and process files. With this new framework, we’ve solved for the modern approach of uploading files straight to the cloud. Out of the box, there’s support for Amazon’s S3, Google’s Cloud Storage, and Microsoft Azure Cloud File Storage. If you’re dealing with images, you can create variants on the fly. If you’re dealing with videos or PDFs, you can create previews on the fly. And regardless of the type, you can analyze uploads for metadata extraction asynchronously. This framework is not only used in production, but it is born for production.</span></p>




<p class="western" align="justify"><span style="font-size: large">There’s a sparkling new <a href="https://github.com/rails/rails/pull/31134">Redis Cache Storage</a> that incorporates general partial, fragment, and other Rails caching jobs into a cohesive unit that anyone can use.</span></p>


<span style="font-size: large">This new Redis Cache Store supports Redis::Distributed, for Memcached-like sharding across Redises. It’s fault tolerant, so will treat failures like misses, rather than kill the request with an exception. It even supports distributed MGETs for that full partial collection caching goodness.</span>


<h3 class="western" align="justify"></h3>




<p class="western" align="justify"><span style="font-size: large">Rails 5.2.0 has Added support for PostgreSQL operator classes to </span><code class="western"><span style="font-size: large">add_index.
</span></code><span style="font-size: large">The operator classes identify database operators to be used by the index for the columns. You can assign the same operator to all columns, or not. It currently only supports PostgreSQL.</span>


<p class="western" align="justify"><span style="font-size: large">Added ability to create PostgreSQL foreign keys without validation. Normally, PostgreSQL verifies that all rows in a table satisfy its foreign keys constraints. With this option, you can create these constraints without the overhead of checking if they are valid.</span></p>




<p class="western" align="justify"><span style="font-size: large">Rails 5.2.0 also has embraced the cherry of HTTP/2 with early hints. This means we can automatically instruct the web server to send required style sheet and JavaScript assets early. This means a faster page delivery.</span></p>




<p class="western" align="justify"><span style="font-size: large">On the topic of performance, Rails now ships with <a href="https://github.com/Shopify/bootsnap">Bootsnap</a> in the default </span><code class="western"><span style="font-size: large">Gemfile</span></code><span style="font-size: large">. This generally reduces application boot times by over 50%.</span></p>




<p class="western" align="justify"><span style="font-size: large"> CSRF and XSS have enhanced that further in Rails 5.2 with the addition of a new DSL that allows you to configure a <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy">Content Security Policy</a> for your application. We can configure a global default policy and then override it on a per-resource basis and even use lambdas to inject per-request values into the header such as account subdomains in a multi-tenant application.</span></p>




<p class="western" align="justify"><span style="font-size: large">In Rails 5.2, we’ve rectified the mess by deprecating the two different kinds of secrets and introduced a new shared concept called <a href="https://github.com/rails/rails/pull/30067">Credentials</a>. Credentials, like AWS access keys and other forms of logins and passwords, were the dominant use case for secrets. </span><span style="font-size: large">Credentials are always encrypted. This means they’re safe to check into revision control, as long as you keep the key out of it. This means atomic deploys and there is no need to mess with environment variables.</span></p>




<p class="western" align="justify"><span style="font-size: large">Since Rails 5.1, we’ve also made great strides with <a href="https://github.com/rails/webpacker">Webpacker</a>. So Rails 5.2 is meant to pair beautifully with the new Webpacker 3.0 release. Rails have fully embraced modern JavaScript with a pre-configured build pipeline run by Webpack. We keep strengthening that relationship.</span></p>




<p class="western" align="justify"><span style="font-size: large">Thanks again to everyone who continue to pour their love and support into Ruby on Rails! Happy Coding.</span></p>

]]&gt;		</p>
]]></content:encoded>
										</item>
		<item>
		<title>Make unit tests great again &#8211; Integrate Jasmine into Rails</title>
		<link>/write-beautiful-jasmine-tests-rails/</link>
				<pubDate>Tue, 22 Aug 2017 07:28:43 +0000</pubDate>
		<dc:creator><![CDATA[alan]]></dc:creator>
				<category><![CDATA[Jasmine]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Rails 5]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby 2.4]]></category>
		<category><![CDATA[better tests]]></category>
		<category><![CDATA[jasmine]]></category>
		<category><![CDATA[karma]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">https://redpanthers.co/?p=3149</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[Jasmine is a framework to write tests for the Javascript code in the Behaviour Driven Development (BDD) style. In this article, you will learn how to integrate Jasmine into your existing rails application and how to write clean and beautiful unit tests. Let us make tests great again!
&nbsp;


<h2>Install Jasmine</h2>


To make Jasmine available to your Rails app, you just have to place the jasmine-gem (<a href="https://github.com/jasmine/jasmine-gem">link</a>) in your Gemfile. That will do the magic. Just make sure you have it under Development and Test group in the Gemfile as follows:


<pre class="theme:orange-code lang:ruby decode:true">group :development, :test do
  gem "jasmine"
end</pre>


Then run this to install the gem:


<pre class="theme:orange-code lang:default decode:true">bundle install</pre>


After all the gems are installed, run this code to generate necessary files for Jasmine to run:


<pre class="theme:orange-code lang:sh decode:true">rails generate jasmine:install</pre>


This will create the jasmine helper file and the yml file where you configure how it should run the tests.


<h2>Run tests</h2>


You can use Jasmine right after it&#8217;s installed. It can be run in several ways, the most important ones being,


<ol>
 	

<li>In your browser</li>


 	

<li>Continuous Integration Mode (CI)</li>


</ol>


The CI mode is usually used when you have to integrate it into your build system.


<h2>Browser Mode</h2>


You have to start the Jasmine server to run it in a browser. This server runs all the tests and serves the results to a webpage. Run this to start the Jasmine Server:


<pre class="theme:orange-code lang:default decode:true">rake jasmine</pre>


With the default settings, you can view the output in:


<pre class="theme:orange-code lang:default decode:true">http://localhost:8888/</pre>


But this page would be pretty empty since you don&#8217;t have any tests written for your Javascript code. There is a method provided to generate sample tests. Try running this:


<pre class="theme:orange-code lang:default decode:true">rails generate jasmine:examples</pre>


Now refresh the webpage and you can see something similar to this:
<a href="https://redpanthers.co/wp-content/uploads/2017/08/Screen-Shot-2017-08-21-at-3.47.22-PM.png"><img class="aligncenter wp-image-3152" src="https://redpanthers.co/wp-content/uploads/2017/08/Screen-Shot-2017-08-21-at-3.47.22-PM-300x91.png" alt="Jasmine test page" width="768" height="233" /></a>


<h3>Configurable Settings</h3>


Clicking on the options button in the top right corner will display a list of options that change how Jasmine runs the tests. Let&#8217;s get into each one of them:


<h4>Raise Exceptions</h4>


This option disables the error catching mechanism of Jasmine in the JavaScript source code and in the test file. The default setting is to wrap all the errors in a catch block.


<h4>Stop Spec on Expectation Failure</h4>


With this option turned on, Jasmine will stop the test at the first occurrence of an error. The default setting is to run the full test suit and then display all the tests which fail.


<h4>Run Tests in Random Order</h4>


This option enables the test to be run in a random sequence every time the test runs. The benefit of enabling this option is to reveal dependencies between tests, therefore, you can reduce test dependencies and each test will have good isolation.


<h2>Continous Integration Mode</h2>


A headless browser is used to integrate Jasmine into your continuous integration workflow. To make our lives easier, this gem that we are using supports integration with a headless browser out of the box. The default headless browser is <a href="http://phantomjs.org/">Phantom JS</a>. So it will download automatically if not installed when you try to run in CI mode. Run this code to run in CI mode:


<pre class="theme:orange-code lang:default decode:true ">rake jasmine:ci</pre>


By default, Jasmine will attempt to find a random open port. To set a default port manually, just add this to the <strong>jasmine_helper.rb</strong>


<pre class="theme:orange-code lang:default decode:true ">Jasmine.configure do |config|
   config.ci_port = 1234
end</pre>




<h2>Configuration</h2>


The two files which you should be looking into, if you need to alter the behavior of tests are:


<ul>
 	

<li>jasmine.yml</li>


 	

<li>jasmine_helper.rb</li>


</ul>


Jasmine reads the jasmine.yml first and then overrides it with the settings mentioned in jasmine_helper.rb


<h4>Sample configuration:</h4>




<pre class="theme:orange-code lang:default decode:true "># spec/javascripts/support/jasmine.yml
random: true
show_console_log: false
stop_spec_on_expectation_failure: true</pre>




<pre class="theme:orange-code lang:default decode:true "># spec/javascripts/support/jasmine_helper.rb
Jasmine.configure do |config|
  config.random = false
  config.show_console_log = false
  config.stop_spec_on_expectation_failure: false
  config.show_full_stack_trace = false
  config.prevent_phantom_js_auto_install = false
end</pre>




<h2>Testing</h2>


Writing tests for Javascript in a Rails app should be fairly straightforward as it uses same standards as Jasmine in general. But there are things that need to be considered specific to a Jasmine installation in Rails.


<h3>Testing JavaScript</h3>


Test files for JavaScript in a rails application reside in the <strong>spec/javascripts</strong> folder. For each javascript file, you need to put the test file in the same path as the file. For example, if you have the following javascript file in your app:
<strong>app/assets/javascripts/jasmine_examples/Calculator.js</strong>
You place the spec file in the following path:
<strong>spec/javascripts/jasmine_examples/CalculatorSpec.js</strong>
Jasmine will include the test on the next test run. There is no configuration to have your test run.


<h2>Plugins worth considering</h2>




<ul>
 	

<li><strong>Jasmine-Jquery</strong> &#8211; this plugin provides a lot of jquery related matchers. Download it <a href="https://github.com/velesin/jasmine-jquery/releases">here</a></li>


 	

<li><strong>Jasmine-Matchers</strong> &#8211; a tool to provide additional matchers. Download it <a href="https://github.com/JamieMason/Jasmine-Matchers/releases">here</a></li>


 	

<li><strong>Jasmine-Fixture &#8211; </strong>a plugin that provides DOM creation using CSS selectors, therefore you can interact with the DOM much easier. Download it <a href="https://github.com/searls/jasmine-fixture/releases">here</a></li>


</ul>




<h1>Write Beautiful Unit tests</h1>


95% of the developers I know write unit tests in order to prevent defects from being deployed to production. But the essential ingredients to a great unit test is unknown to most of them. There have been countless times that I&#8217;ve seen a test fails, only to investigate and discover that I have no idea what feature the developer was trying to test, let alone how it went wrong or why it matters.


<h2>Importance of Test Discipline</h2>


Your tests are the best set of weapons to defend your code from bugs. They are more important that linting and static analysis. A few reasons why tests are your secret weapon:


<ul>
 	

<li>Writing tests first gives you a clearer perspective on the ideal API design.</li>


 	

<li>Does the developer understand the problem enough to articulate in code all critical component requirements?</li>


 	

<li>Manual QA is error-prone. In my experience, it’s impossible for a developer to remember all features that need testing after making a change to refactor, add new features, or remove features.</li>


 	

<li>Continous Integration prevents failed builds from getting deployed to production.</li>


</ul>




<h2>Bug Report vs plain Unit Test</h2>


The test fail report comes to save your life when a test fails. So you better make it loud and clear. I came up with a list of must-have info in your bug report.


<ul>
 	

<li>What are you trying to test?</li>


 	

<li>What should it do?</li>


 	

<li>What is the real-time output (actual behavior)?</li>


 	

<li>What is the expected output (expected behavior)?</li>


</ul>


Here is a sample test with all of these info:


<pre class="theme:orange-code lang:default decode:true">describe("CalculatorAddMethod", function() {
  var calculator = new Calculator();
  it("should return number", function() {
    const actual = typeof Calculator.add(5,10);
    const expected = 'number'
    expect(actual).toEqual(expected);
  });
});
</pre>


This test suit answers all the questions above. Let&#8217;s go through each one of them.


<ul>
 	

<li>What are you trying to test?


<ul>
 	

<li>-&gt; Go for the test description. It is testing for the return type of the add method of Calculator().</li>


</ul>


</li>


 	

<li>What should it do?


<ul>
 	

<li>-&gt; Again, look at the test description. It clearly says that it is testing for the return type of add method.</li>


</ul>


</li>


 	

<li>What is the actual output?


<ul>
 	

<li>-&gt; There is a dedicated variable which holds the actual result and how you got the actual result. TWO FOR ONE!</li>


</ul>


</li>


 	

<li>What is the expected output?


<ul>
 	

<li>-&gt; Again, there is a dedicated variable which holds the actual result. Straight as a ruler!</li>


</ul>


</li>


</ul>




<h2>Make your tests even better</h2>


This is from my experiences and the knowledge I gained from good articles. This worked for me in the long run even if I find it a little difficult to implement when I started. <strong>Write every single test using toEqual()</strong>. Don&#8217;t worry about the quality impact on your test suit. It will get better with exercise.


<h2>Easter Egg</h2>


This method I suggested would answer one more question, which is by far the most important question I guess. <strong>How can you reproduce the test?</strong> The <strong>const actual </strong>holds the answer to this question. Please go take a look at the variable in my sample test suit above and get delighted.


<h1>Conclusion</h1>


Integrating Jasmine into your Rails app is done by the <strong>jasmine-gem. </strong>This gem gives you the ability to run tests in a browser or as Continous Integration mode. The usage of right plugins will improve your productivity and helps you write tests faster and better.
Next time you write a test, remember to see if your test answers the following questions:


<ul>
 	

<li>What are you trying to test?</li>


 	

<li>What should it do?</li>


 	

<li>What is the real-time output (actual behavior)?</li>


 	

<li>What is the expected output (expected behavior)?</li>


 	

<li>How can be the test reproduced?</li>


</ul>

]]&gt;		</p>
]]></content:encoded>
										</item>
		<item>
		<title>application_record.rb available since rails 5</title>
		<link>/application_record-rb-available-since-rails-5/</link>
				<pubDate>Tue, 13 Sep 2016 06:27:31 +0000</pubDate>
		<dc:creator><![CDATA[coderhs]]></dc:creator>
				<category><![CDATA[Beginners]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Rails 5]]></category>
		<category><![CDATA[active_record]]></category>
		<category><![CDATA[application_record]]></category>
		<category><![CDATA[base]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[models]]></category>
		<category><![CDATA[pollute]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[universal]]></category>

		<guid isPermaLink="false">https://redpanthers.co/?p=509</guid>
				<description><![CDATA[
				<![CDATA[]]>		]]></description>
								<content:encoded><![CDATA[<p>				<![CDATA[Those who have been starting with Rails 5, must have noticed the new <strong>application_record.rb</strong> file present in your model folder. And all new models seems to be inheriting the <strong>application_record.rb</strong> instead of the <strong>ActiveRecord::Base. </strong>This is done so that we don&#8217;t pollute the <strong>ActiveRecord::Base</strong> namespace with our own extensions.  Before when we require something, say an extension to the ActiveRecord itself we used to have it included using the following code.


<pre class="lang:ruby decode:true">module NewFeature
  def to_do_something
    puts "I am doing something new which Active Record coudln't do before"
  end
end
ActiveRecord::Base.include(NewFeature)</pre>


Now the problem with this approach is that when we use rails engines this NewFeature gets added in there as well, and it could end up doing things that we didn&#8217;t expect.
With the new application_record.rb, which would be inherited by all the models, we need to include the new module at the <strong>ApplicationRecord</strong> and it would be available as the new feature of <strong>ActiveRecord</strong>. Every new engine generated using <strong>rails plugin new</strong> would also be having their own <strong>application_reocord.rb</strong>
One more point to note is that we can place application wide hooks in this file. So if you were to do


<pre class="lang:ruby decode:true ">after_create do
  Rails.logger.info "[DataEntry] A new record has been created"
end</pre>


it would be triggered when a new record is created in any of the models of the rails application.]]&gt;		</p>
]]></content:encoded>
										</item>
	</channel>
</rss>
