<?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: Luis Vasconcellos</title>
    <description>The latest articles on DEV Community by Luis Vasconcellos (@lfv89).</description>
    <link>https://dev.to/lfv89</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%2F65934%2F36c9bcb2-b993-4ee0-b86d-71c2b68100fa.jpeg</url>
      <title>DEV Community: Luis Vasconcellos</title>
      <link>https://dev.to/lfv89</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lfv89"/>
    <language>en</language>
    <item>
      <title>Rails Console Magic Tricks</title>
      <dc:creator>Luis Vasconcellos</dc:creator>
      <pubDate>Sun, 26 Aug 2018 16:59:41 +0000</pubDate>
      <link>https://dev.to/lfv89/rails-console-magic-tricks-1032</link>
      <guid>https://dev.to/lfv89/rails-console-magic-tricks-1032</guid>
      <description>

&lt;h1&gt;Rails Console Magic Tricks&lt;/h1&gt;

&lt;p&gt;A few techniques to increase the developer experience during a Rails Console session. Please note that some of them will come from Ruby itself, and thus won’t be necessarily restricted to the console.&lt;/p&gt;

&lt;h2&gt;Rolling back after the end of a session&lt;/h2&gt;

&lt;p&gt;It is possible to run the console in a mode called sandbox. In that mode, every change made to the database will be automatically rolled back when the session terminates:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rails console --sandbox
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;Retrieving the previous execution value&lt;/h2&gt;

&lt;p&gt;The result of the immediately previous console execution can be retrieved and, as the example suggests, assigned to a local variable by calling the_:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt; Game.all.map(&amp;amp;:name)
=&amp;gt; ["zelda", "mario", "gta"]
&amp;gt;&amp;gt; names = _
=&amp;gt; ["zelda", "mario", "gta"]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;Searching for methods with grep&lt;/h2&gt;

&lt;p&gt;It is possible to find out the complete name of a method having only a part of it. By calling grep from Arrayone can execute a handy search over the methods of a given object:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt; Game.first.methods.grep(/lumn/)
Game Load (0.8ms)  SELECT  "games".* FROM "games" ORDER BY "games"."id" ASC LIMIT $1  [["LIMIT", 1]]
=&amp;gt; [:column_for_attribute, :update_column, :update_columns]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;Finding out a method location&lt;/h2&gt;

&lt;p&gt;The source_location method from the Object class returns the full path of a method's file definition, including the line where it was defined. It can be specially useful when exploring third party libraries:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt; 'Luis Vasconcellos'.method(:inquiry).source_location
=&amp;gt; ["/usr/local/bundle/gems/activesupport-5.2.1/lib/active_support/core_ext/string/inquiry.rb", 12]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;Returning the source code of a method&lt;/h2&gt;

&lt;p&gt;While it is interesting to know the exactly location of a method, there are cases where might be even better to output its source code directly to the console. This can be achieved by using the source method:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt; 'Luis Vasconcellos'.method(:inquiry).source.display
def inquiry
    ActiveSupport::StringInquirer.new(self)
  end
=&amp;gt; nil
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;The helper object&lt;/h2&gt;

&lt;p&gt;The console provides an object called helper, which can be used to directly access any view helper from a Rails application:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt; helper.truncate('Luis Vasconcellos', length: 9)
=&amp;gt; "Luis V..."
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;The app object&lt;/h2&gt;

&lt;p&gt;The console also provides an interesting object called app, which is basically an instance of your application. It is possible with this object to achieve such things as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Access to &lt;code&gt;GET&lt;/code&gt; endpoints&lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt; app.get('/')
Started GET "/" for 127.0.0.1 at 2018-08-25 22:46:52 +0000
   (0.5ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by HomeController#show as HTML
  Rendering home/show.html.erb within layouts/application
  Rendered home/show.html.erb within layouts/application (11417.2ms)
  Rendered shared/_menu.html.erb (3.6ms)
  Rendered shared/header/_autocomplete.html.erb (292.2ms)
  Rendered shared/_header.html.erb (312.9ms)
  Rendered shared/_footer.html.erb (3.7ms)
Completed 200 OK in 11957ms (Views: 11945.5ms | ActiveRecord: 0.0ms)
=&amp;gt; 200
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Access to &lt;code&gt;POST&lt;/code&gt; endpoints&lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt; app.post('/games/zelda/wishlist_placements.js')
Started POST "/games/zelda/wishlist_placements.js" for 127.0.0.1 at 2018-08-25 23:03:21 +0000
Processing by OwnlistPlacementsController#create as JS
  Parameters: {"game_slug"=&amp;gt;"zelda"}
  Game Load (0.6ms)  SELECT  "games".* FROM "games" WHERE "games"."slug" = $1 LIMIT $2  [["slug", "zelda"], ["LIMIT", 1]]
  Rendering wishlist_placements/create.js.erb
  Rendered wishlist_placements/create.js.erb (194.8ms)
Completed 200 OK in 261ms (Views: 252.9ms | ActiveRecord: 0.6ms)
=&amp;gt; 200
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Search for a &lt;code&gt;_path&lt;/code&gt; helper from a &lt;code&gt;Game&lt;/code&gt; route:&lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt; app.methods.grep(/_path/).grep(/game/)
=&amp;gt; [:search_games_path, :game_ownlist_placements_path, :game_ownlist_placement_path, :game_wishlist_placements_path, :game_wishlist_placement_path, :game_path]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Combining the previous tricks in a more useful way:&lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt; app.get(app.root_path)
Started GET "/" for 127.0.0.1 at 2018-08-26 02:27:40 +0000
Processing by HomeController#show as HTML
  Rendering home/show.html.erb within layouts/application
  Rendered home/show.html.erb within layouts/application (12550.2ms)
  Rendered shared/_menu.html.erb (3.8ms)
  Rendered shared/header/_autocomplete.html.erb (1.2ms)
  Rendered shared/_header.html.erb (28.0ms)
  Rendered shared/_footer.html.erb (3.8ms)
Completed 200 OK in 12835ms (Views: 12810.0ms | ActiveRecord: 0.0ms)
=&amp;gt; 200
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight html"&gt;&lt;code&gt;&amp;gt;&amp;gt; app.body.response
=&amp;gt; "\n&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;\n&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;\n  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;\n    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&lt;/span&gt; ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt; app.cookies
=&amp;gt; #&amp;lt;Rack::Test::CookieJar:0x0000556ee95c33e0 @default_host="www.example.com", @cookies=[#&amp;lt;Rack::Test::Cookie:0x0000556eeb72b2d0 @default_host="www.example.com", ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;




</description>
      <category>rubyrailsrubyonrails</category>
    </item>
  </channel>
</rss>
