Sunday 23 July 2017

Polymorphic routing to get the path of different resource

Suppose that we have three types of resources and we have three model as Employee, Employer and Manager.

Now we also have separate dashboard action in controller and After sign in we have to redirect to dashboard path according to the resource.

employees controller -

 
 class EmployeesController < ApplicationController
  
   def dashboard
         # some_code
   end 
 end


employers controller -


  
 class EmployersController < ApplicationController
  
   def dashboard
         # some_code
   end 
 end


managers controller -


  
 class ManagersController < ApplicationController
  
   def dashboard
         # some_code
   end 
 end


And routes would look like this:


 resources :employees do
   member do
     get 'dashboard'
   end
 end

 resources :employers do
   member do
     get 'dashboard'
   end
 end

 resources :managers do
   member do
     get 'dashboard'
   end
 end


In this scenario we can use switch case:


 case current_user
 when Manager
   dashboard_manager_path
 when Employer
   dashboard_employer_path
 when Employee
   dashboard_employee_path
 end


Don't you think it can be re-factor!

Rails provides polymorphic_path that makes polymorphic url generation much easier and simpler. There is also a method named polymorphic_url which is the same as the polymorphic_path except that polymorphic_url generate the full url including the host name.


These methods are useful when you want to generate the correct URL or path to a RESTful resource without having to know the exact type of the record in question.

It also provides support for nested resources.

So, we can re-factor our code using ploymorphic_path helper method:


 polymorphic_path([:dashboard, current_user])

Sunday 16 July 2017

Remove particular XML tag with Nokogiri

Suppose that we have XML -


xml_content = <?xml version="1.0"?>
     <book>
        <author>Gambardella, Matthew</author>
        <title>XML Developer's Guide</title>
        <genre>Computer</genre>
        <price>44.95</price>
        <publish_date>2000-10-01</publish_date>
        <description>An in-depth look at creating applications 
        with XML.</description>
     </book>


And we need to remove <genre> tag but keep the inside that tag -


 <?xml version="1.0"?>
    <book>
       <author>Gambardella, Matthew</author>
       <title>XML Developer's Guide</title>
       Computer
       <price>44.95</price>
       <publish_date>2000-10-01</publish_date>
       <description>An in-depth look at creating applications 
       with XML.</description>
    </book>


We can achieve this with Nokogiri -


 
require 'nokogiri'
doc = Nokogiri::XML 'xml_content'
doc.xpath('//genre').each do |tag|
  tag.replace(Nokogiri::XML::Text.new(tag.text, tag.document))
end
doc.inner_html
 

Sunday 9 July 2017

Get synonyms and antonyms of a word in Rails.

In this blog, we will integrate Big Huge Labs Thesaurus API in our Rails app for getting synonyms and antonyms of a word. For this we will use dinosaurus gem.

So, in Gemfile.

 gem 'dinosaurus'

Run bundle.

Before initializing this we have to get an api key for configuration. We can get it from here.

For initializing, create a file dinosaurus.rb in config/initializer and put below code in this file.


 
 Dinosaurus.configure do |config|
   config.api_key = 'your_api_key'
 end 
 

Now, we can use it as

 > Dinosaurus.synonyms_of('word')

 => ["news", "intelligence", "tidings", "discussion", "give-and-take", "parole", "word of honor", "Son", "Word", "Logos", "password",   "watchword", "countersign", "Bible", "Christian Bible", "Book", "Good Book", "Holy Scripture", "Holy Writ", "Scripture", "Word of God",  "arcanum", "computer memory unit", "hypostasis", "hypostasis of Christ", "info", "information", "language", "language unit",   "linguistic unit", "oral communication", "order", "positive identification", "promise", "religious text", "religious writing", "sacred   text", "sacred writing", "secret", "speech", "speech communication", "spoken communication", "spoken language", "statement", "voice   communication", "give voice", "formulate", "phrase", "articulate", "evince", "express", "show"]


  > Dinosaurus.antonyms_of('synonyms')

  => ["antonym"]


You can get synonyms and antonyms together with lookup method. It will return a hash like this


 > Dinosaurus.lookup('word')

  => {"noun"=>{"syn"=>["news", "intelligence", "tidings", "discussion", "give-and-take", "parole", "word of honor", "Son", "Word",   "Logos", "password", "watchword", "countersign", "Bible", "Christian Bible", "Book", "Good Book", "Holy Scripture", "Holy Writ",   "Scripture", "Word of God", "arcanum", "computer memory unit", "hypostasis", "hypostasis of Christ", "info", "information", "language",  "language unit", "linguistic unit", "oral communication", "order", "positive identification", "promise", "religious text", "religious   writing", "sacred text", "sacred writing", "secret", "speech", "speech communication", "spoken communication", "spoken language",   "statement", "voice communication"]}, "verb"=>{"syn"=>["give voice", "formulate", "phrase", "articulate", "evince", "express", "show"]}}

Sunday 2 July 2017

Debugging Rails application in Docker with byebug

During development of any application we need debugging in our application. For this rails provides many tools like byebug. Byebug stop the application inside the code.
But when you are using it with docker, we have to do some simple configuration in our docker-compose.yml file.
As we know that base requirement is to run the whole application within a container. Using docker-compose this is fairly simpleWe need a compose file which covers a web container that runs the rails server command.
When using docker-compose in combination with byebug this needs to be added to make it work properly. 
So, add below code in your docker-compose.yml

 
web: ...
  stdin_open: true
  tty: true
 


Now, we have to run docker-compose in daemon mode and attach to the web container.


  
  
  $ docker-compose up -d

  $ docker attach myapp_web_1