RailsTip 3 – Configure sign_out method in Rails Devise Gem


Devise gem is one of the most popular gem for creating user login and signup.

The /logout action is by default set to :DELETE inside devise.rb . Most of the browsers only support :GET and :POST.

To change the method of /logout to :GET make following changes in devise.rb

config.sign_out_via = :get

Cheers !!

Ignore transitive dependency in Maven


Maven helps to manage the dependency of JARS . Most of the time it may happen that same JAR file with different versions is being loaded because some other jar files have it as dependency and thus causing the conflicts .

Typical example is have log4j or slf4j jar with different version in spring-xx.jar and junit-xxx.jar . So when you load you classes an conflict may arise and because of incompatible version of same jar file . For e.g.

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>3.2.1.RELEASE</version>
</dependency>

The above jar uses log4j 1.2.17 (you can check the dependency using mvn dependency:tree).

Consider this log4j jar file is conflicting with some other higher version of log4j which is used by other jar. To resolve this issue we would have to remove the conflicting log4j jar from org.springframework.
it can be done as follows :

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>3.2.1.RELEASE</version>
         <exclusions>
                <exclusion>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                </exclusion>
            </exclusions>
</dependency>

Cheers..

Convert JSON to Map or Map to Json in Java


One of the common uses cases while working with json is to convert it to a java object or convert java object to json. There is an open source library available in java which helps in processing json objects.

Add the Jackson dependency in pom.xml file

<repositories>
	<repository>
		<id>codehaus</id>
		<url>http://repository.codehaus.org/org/codehaus</url>
	</repository>
  </repositories>

  <dependencies>
	<dependency>
		<groupId>org.codehaus.jackson</groupId>
		<artifactId>jackson-mapper-asl</artifactId>
		<version>1.9.12</version>
	</dependency>
  </dependencies>

The Java class :

public static void main(String args[]) {
        ObjectMapper mapper = new ObjectMapper();
        Map m = new HashMap();
        Map personMap = new HashMap<>();
        Map personDetail = new HashMap();
        personDetail.put("firstname", "Bob");
        personDetail.put("lastname", "jackson");
        personDetail.put("age", "12");
        personDetail.put("city", "Berlin");

        personMap.put("person", personDetail);
        //convert Map to json string
        try {
            System.out.println(mapper.writeValueAsString(personMap));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // convert json to Map
        String json = "{\"person\":{\"age\":\"12\",\"lastname\":\"jackson\""
                + ",\"firstname\":\"Bob\",\"city\":\"Berlin\"}}"
        try {
            Map map = mapper.readValue(json, Map.class);
            System.out.println("Map is " + map);
        } catch (Exception e) {
            e.printStackTrace();
        }
 }

Output is

Output: {“person”:{“age”:”12″,”lastname”:”jackson”,”firstname”:”Bob”,”city”:”Berlin”}}

Map is {person={age=12, lastname=jackson, firstname=Bob, city=Berlin}}

ObjectMapper mapper = new ObjectMapper();

Creating an object of ObjectMapper is a expensive operation and as best practice it should be created as a singleton object in a class.

Rails tip #1 – Set a custom layout for a web page


Its may require that you dont want to use application.html layout for some pages . For e.g display different header on page2 which than page 1 and the header is rendered in application.html.erb.

One way is to create a new layout for e,g headerless.html.erb inside a views->layout folder and in the particular controller which serves the request set the new layout .


 class SomeController < ApplicationController

    layout :headless , :only=>[:your_action1,:your_action2]

end

With the layout keyword you can use options such as :only, :except.