Tuesday, 27 August 2013

Git Basics

Today I am going to share some of the basic commands of the git.
Starting with a brief description about git :

In software development, Git is a distributed version control and source code management system with an emphasis on speed. Read More
Source:wikipedia

Now coming to commands,I am not going to explain each and every command in detail as the git documentation is enough for that. Will only give brief introduction about them

1) Create your account in github

Goto https://github.com and create your account.

2) Create a repository

Create an empty repository where you want to push the data from your local machine.

Give a name to the repository
A url will be generated for your respository. This is useful in the later process while adding a remote in your local for pushing and pulling purpose. 



3) git init

This command creates an empty git repository.
Suppose you have a folder named "GitTest" in your local. Now you want to push this folder to github then go inside that folder using command line and type "git init".

3) Now add the git remote you created in github.com in your local so that the local changes are pushed to that url .

 git remote add <short name for the url> <url>
 eg : - git remote add origin https://github.com/nikitasingh/GitTest.git

Here origin is the short name for the url

git remote -v will list all the remotes which are added for your repository.

5) Now create some file like "xyz.txt" inside that folder. (Suppose this is the file you want to push to the github)

6) git add

To add the newly created file type-

git add xyz.txt

To add all the files in your folder to github type:-

git add .

7) git commit

File you just added is not ready to be pushed yet, If you will run the git push command without running commit command this file will not be added to your github repository.

so to commit this file type-

git commit -m "first file added" xyz.txt

Here -m is used to give commit messages, you cant run commit command without giving a commit message

8) git push

The next step is to push the changes to the github repository from your local machine. To do that type:

git push origin master

It will ask your login credentials to push the changes.

9) git pull

git pull is used to fetch the changes from the github repository to your local machine.
When two or more people are working on the same repository, it is must to pull the changes so that you have the updated project in your local.

9) git status

If you will type git status it will show the files with following details:-

  •  unmodified files
  •  modified files
  •  added files
  • deleted files
  • renamed files
  • copied files
  • untracked files

Monday, 12 August 2013

Difference between collect, select, map and each in ruby

This post is not related to rails part but the RUBY part.

To iterate over an array we generally use collect, select, map and each. As I am beginner I never actually thought what is the difference and I kept using EACH for every loop iteration. However, recently I researched a bit more about these loops and thought to share it . It might help some one I guess  :)

1) Map

Map takes the enumerable object and a block like this [1,2,3].map { |n| n*2 } and evaluates the block for each element and then return a new array with the calculated values.

so the outcome of  [1,2,3].map { |n| n*2 } will be [2,4,6]

If you are try to use map to select any specific values like where n >2 then it will evaluate each element and will output only the result which will be either true or false

so the outcome of  [1,2,3].map { |n| n>2 } will be [false, false, true]

2) Collect

Collect is similar to Map which takes the enumerable object and a block like this [1,2,3].collect { |n| n*2 } and evaluates the block for each element and then return a new array with the calculated values.

so the outcome [1,2,3].collect{ |n| n*2 } of will be [2,4,6]

If you are try to use collect to select any specific values like where n >2 then it will evaluate each element and will output only the result which will be either true or false

so the outcome of  [1,2,3].collect { |n| n>2 } will be [false, false, true]

3) Select

Select evaluates the block with each element for which the block returns true.

so the outcome of  [1,2,3].select { |n| n*2 } will be [1,2,3]

If you are try to use select to get any specific values like where n >2 then it will evaluate each element but returns the array that meets the condition

so the outcome of  [1,2,3].select{ |n| n>2 } will be [3]

4) Each

Each will evaluate the block with each array and will return the original array not the calculated one.
so the outcome of  [1,2,3].each{ |n| n*2 } will be [1,2,3]

If you are try to use each to select any specific values like where n >2 then it will evaluate each element but returns the original array

so the outcome of  [1,2,3].each { |n| n>2 } will be [1,2,3]

Monday, 29 April 2013

Using NOKUGIRI gem to parse XML

Nokugiri gem can be used to parse html and xml files. Below is a small tutorial on how to do this in UBUNTU. (Installation procedure for windows is different I gue

1) Add gem "nokogiri" inyour gem file.

2) Run bundle install.

3) To open the xml file use this:-
  doc = Nokogiri::XML(open(filename/URL))

4)Suppose you want to read the content from the folowing xml:-
<first_tag>
<second_tag>Hi XYZ</second_tag>
</first_tag>
To read the content do the following:-
doc.css("first_tag second_tag").map {|node|
                 
        p  tag<<node.children.text
        }

This will output HI XYZ.

5) There are also some other methods which can be used to read the xml like:-

data=doc.xpatch("//first_tag").map do |item|
  [
    item.search("second_tag").first.content
  ]
end

Friday, 5 April 2013

Nice twitter bootstrap popup box(modal)

Recently I came across a requirement to have some content on the pop up box when a user clicks on the link. The content can be static or dynamic from the database itself. There are several options that are available like jqbox. However I found twitter bootstrap modal quite useful and attractive.

Refer to the link to know how to use it Twitter Bootstrap modal

To make it dynamic I simply made an ajax call and used the returned JSON object to append the content in the modal. Refer the following to get a brief idea about it.

Code for Twitterbootstrap modal:-



<div id="example" class="modal hide fade in" style="display: none; ">
<div class="modal-header" style="background-color:#CCB647"><h3>  <center><font color="white">Identify the Person!!</font><a class="close" data-dismiss="modal">×</a></center> </h3>
</div>
<div class="modal-body" style="background-color:#D6DEE4">
<div id="game">
<img id="photo" src="" style="width:200px;height:200px;"><br/>
 <div id="options">
 </div>
</div>
</div>
<div class="modal-footer" style="background-color:#CCB647">

Link to open the modal:

<a data-toggle='modal' href='#example' data-backdrop="static" data-keyboard="false" onclick="updatemodal();">
</a>

Script for ajax call:

function updatemodal{
  $.ajax({
   url: '../play/play_game',
   error: function (XMLHttpRequest,textStatus,errorThrown) {
     alert("Something went wrong!! Please try again later. Unable to create options");

   },
   success: function(data){

// Here data is the json object returned by the server
      var jsonObj = $.parseJSON(data);

      $.each(jsonObj,function(key,value){
 
  $('#options').append(value.content);
 
    });

   },
   type: "get"
 });



}


Multiple tab excel sheet using spreadsheet gem


To create multiple sheets in a single excel sheet , its good to use spreadsheet gem. In your controller (suppose card is your controller) add the following:

def export
 
    book = Spreadsheet::Workbook.new
    sheet1 = book.create_worksheet :name => 'Sheet1'
    sheet2 = book.create_worksheet :name => 'Sheet2'

     sheet1.row(0).push "some content in Column1" //to push content in 1st row 1st column of 1st spreadsheet

     //keep pushing on row(0) if you want to push contents other columns of 1st row

      spreadsheet = StringIO.new
      book.write spreadsheet
      file = "Excelsheet"
      send_data spreadsheet.string, :filename => "#{file}", :type =>  "application/vnd.ms-excel"
  end


In you view
<%=link_to("Export Database to Excel", cards_url + "/export")  %>

Wednesday, 20 February 2013

Giving name to excel sheet while exporting it- rails 3

You may want to give file name to your excelsheet when you are exporting it. Suppose you want to give the todays date and time as its name. Then do the following:

Suppose export is the method in which you have written the code for exporting excel sheet then do the following:


    def export
       time = Time.now
       time.strftime("%Y-%m-%d %H:%M:%S")
 
      respond_to do |format|
      format.html

      format.xls { headers["Content-Disposition"] = "attachment; filename=\"#{time}"+".xls"}
    end

Tuesday, 19 February 2013

Disable browser cache while using devise gem -rails 3

While using devise gem for authentication, I found that even after a user log out, he can still see the previous page he visited if he clicks on the back button of browser. To disable this add the following code in your application_controller.rb.


before_filter :set_no_cache
def set_no_cache


   response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
   response.headers["Pragma"] = "no-cache"
   response.headers["Expires"] = "0"


end