Ubuntu – Display Current GIT Hub Branch Name in Terminal

Copy Below script and paste in .bashrc or .bash_profile file.
You can find these files at /home/

parse_git_branch() {
 git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
if [ "$color_prompt" = yes ]; then
  PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\
   [\033[01;34m\]\w\[\033[01;31m\]$(parse_git_branch)\[\033[00m\]\$'
else
  PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$(parse_git_branch)\$'
fi

Run Rails Development Server with HTTPS

1.   Install thin as web server instead of webrick. Write below line in gemfile and do bundle install.

gem 'thin'

2.  In environments/development.rb file write below lines

config.force_ssl = true
config.ssl_port = 3001 # port will be your choice

3.  Now start server using below command

thin start --ssl -p 3001

Now go to web browser and start your localhost:3000 with https://localhost:3000

Rails and RubyZip – Create zip file and delete it after download using send_data

We are not using send_file method because it will get deleted before rails actually starts sending it

 

filename = “#{Rails.root}/public/invoices.zip”
temp_file = Tempfile.new(filename)
input_filenames = Dir.entries(“#{Rails.root}/public/Invoices_pdfs”).select {|f| !File.directory? f}
folder = “#{Rails.root}/public/Invoices_pdfs”

begin
#This is the tricky part
#Initialize the temp file as a zip file
Zip::OutputStream.open(temp_file) { |zos| }

#Add files to the zip file as usual
Zip::File.open(temp_file.path, Zip::File::CREATE) do |zipfile|
input_filenames.each do |file|
zipfile.add(file,  File.join(folder, file))
end
end

#Read the binary data from the file
zip_data = File.read(temp_file.path)

#Send the data to the browser as an attachment
#We do not send the file directly because it will
#get deleted before rails actually starts sending it
send_data(zip_data, :type => ‘application/zip’, :filename => “#{Time.now.to_date}”)
ensure
#Close and delete the temp file
temp_file.close
temp_file.unlink
FileUtils.rm_rf(Dir.glob(“#{Rails.root}/public/Invoices_pdfs/*”))
end

Rails and RubyZip – Create zip file and download using send_file

def make_zip

# Path where your pdfs are situated (‘my_pdf’ is folder with pdfs)
folder = “#{Rails.root}/public/my_pdfs”
input_filenames = Dir.entries(“#{Rails.root}/public/my_pdfs”).select {|f| !File.directory? f}

zipfile_name = “#{Rails.root}/public/myfirstzipfile.zip”

Zip::File.open(zipfile_name, Zip::File::CREATE) do |zipfile|
input_filenames.each do |filename|
# Two arguments:
# – The name of the file as it will appear in the archive
# – The original file, including the path to find it
zipfile.add(filename,  File.join(folder, filename))
end
zipfile.get_output_stream(“success”) { |os| os.write “All done successfully” }
end
send_file(File.join(“#{Rails.root}/public/”, ‘myfirstzipfile.zip’), :type => ‘application/zip’, :filename => “#{Time.now.to_date}.zip”)
# Remove content from ‘my_pdfs’ folder if you want
FileUtils.rm_rf(Dir.glob(“#{Rails.root}/public/my_pdfs/*”))

end

Prevent submit event of remote => ‘true’ Rails form which open in Modal

We can not directly prevent the form which submit using remote => true

This example show how to prevent submit event of ajax  form which open in modal

$(".modal-form").live("ajax:before",function(evt,data,status,xhr){
   var checked = $(".project-modal-form input:checked").length > 0
   if (!checked){
     $("#warning_notification-project-modal").text("Warning.").slideDown(70         0).delay(7000).slideUp(700);
     $('#warning_notification-project-modal').removeClass('hide')
     return false; 
   }else{
      return true;
     }
});

How to solve conflicts?

When there’s a conflict, Git marks them for you in the files. You’ll see sections like this:

<<<<<<< HEAD
* Daniel Pass <daniel.antony.pass@googlemail.com>
* Kieran Moore
=======
* Kermit the Frog
* Miss Piggy
>>>>>>> upstream/unmergeable-branch

The first section, HEAD is what you have in your version. The second section, upstream/unmergeable-branch is what Git found in the version you were trying to pull in.

Make up to date Development machine For Ruby on Rails

1) Install Ruby On Rails

https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-on-ubuntu-14-04-using-rvm

2) Install Sublime

http://askubuntu.com/questions/172698/how-do-i-install-sublime-text-2-3

3) just cut/paste following line: (RVM IS NOT A FUNCTION ERROR)

http://stackoverflow.com/questions/9336596/rvm-installation-not-working-rvm-is-not-a-function

To permanently resolve this just put following line:

[[ -s “$HOME/.rvm/scripts/rvm” ]] && source “$HOME/.rvm/scripts/rvm”

into: ~/.bash_profile, ~/.bashrc and in ~/.profile file

4) Install MySQL

https://www.digitalocean.com/community/tutorials/how-to-use-mysql-with-your-ruby-on-rails-application-on-ubuntu-14-04

5) Install git & Configuring git

sudo apt-get install git

Configuring git

git config –global user.name “YOUR NAME”
git config –global user.email “YOUR@EMAIL.com”

CREATE SSH KEY

ssh-keygen

6) Install PostgreSQL

Setting Up PostgreSQL

For PostgreSQL, we’re going to add a new repository to easily install a recent version of Postgres 9.3.

sudo sh -c “echo ‘deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main’ > /etc/apt/sources.list.d/pgdg.list”
wget –quiet -O – http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add –
sudo apt-get update
sudo apt-get install postgresql-common
sudo apt-get install postgresql-9.3 libpq-dev

The postgres installation doesn’t setup a user for you, so you’ll need to follow these steps to create a user with permission to create databases. Feel free to replace chris with your username.

sudo -u postgres createuser chris -s

# If you would like to set a password for the user, you can do the following
sudo -u postgres psql
postgres=# \password chris

REF: https://gorails.com/setup/ubuntu/14.04

7) Install Redis Server

https://www.digitalocean.com/community/tutorials/how-to-install-and-use-redis

8) Install ImageMagick

http://askubuntu.com/questions/471045/14-04-doesnt-have-package-imagemagick

9) Set up Sublime for Rails Environment

Package Controll: https://packagecontrol.io/installation#st3

User Pref: https://mattbrictson.com/sublime-text-3-recommendations#packages

For ERB: https://github.com/eddorre/SublimeERB#add-keybinding

For .rb and .erb: https://github.com/CraigWilliams/BeautifyRuby

then: gem install htmlbeautifier

type: which ruby

set that path in preferences > package settings > beautify ruby > settings default

type: “ruby”: “/home/user_name/.rvm/rubies/ruby-1.9.3-p551/bin/ruby”,

10) Install Java

sudo apt-get update

sudo apt-get install default-jdk

11) Install DBeaver – Free Universal SQL Client

http://dbeaver.jkiss.org/download/

12)  Chrome Extension

http://usersnap.com/blog/chrome-extensions-for-developers/

How to Explore Existing Ruby On Rails Project

=========== How to setup Development Environment =========

STEP -1: Install Ruby with RVM. Use ruby version you are going to use in your project

STEP -2: Clone Project from VCS and Place in your directory and cd to your “project” and ask which branch is ahead and checkout to that branch

STEP -3: If gemset already created then if you cd to project it will automatically use that otherwise create gemset

STEP -4: install bundler by typing this command: ‘gem install bundler’ and install other dependances if require

STEP -5: Type command: ‘bundle install’ to install all gems. In between you will meet with many unmet dependancy errors. Solve them.

STEP -6: Check config/database.yml file and fill reqire information if needed

STEP -7: Type these commands one by one:
rake db:create # to create database, Solve errors if any
rake db:migrate # to migrate all migration to database
rake db:seed # if any seed data

STEP -8: Now type command: ‘rails s’ inside your project directory to start server and visit ‘localhost:3000’ into browser

======== ============ How to Explore Existing Rails Project ==================

1) Use The Site. Understand the purpose of the product

It can be tempting to jump straight in to the code to see how everything works. If the site is already up and hosted somewhere, try and curtail that urge and instead just go use the existing site. Click around and really get a feel for it. Even if you’ve been given an administrator account with which to work on the app, create an account and go through the sign up process. Pay attention to the URLs and paths. This will give you a general idea of the overall flow of the application. Don’t worry about any 500 errors you see, just make a mental note.

2) Read the Gemfile Twice

The Gemfile will contain all of the different external libraries in use by the application. If you’ve been working with Rails apps for a little while, you may be familiar with most of the gems inside. The most subtle surprises that arise when reading a new code base are from external libraries — mystery method definitions and constants that aren’t directly searchable in the application code can be troublesome to locate. You won’t necessarily combat all of those by getting familiar with the library names in the Gemfile but seeing them may jog your memory later.

3) Build a project vocabulary

What is a Player , and what’s a Session ? How about an Account vs a User vs
a Provider ?
Every app uses different terms and differentmetaphors. And if you don’t understand the vocabulary of an app, you’ll waste time browsing app/models every time you see a new class name.Or worse, you’ll make bad assumptions about how features work.So, the first thing you have to do is build that vocabulary. Learn what this app is made of. And if you want to do that quickly, start by reading db/schema.rb . You’ll learn the terms
the app uses, and how all its data fits together. Pay special attention to the *_id columns,
so you can see how the models connect to one another.If you’re more visual, try the rails­erd gem. It generates a diagram from your Rails models,showing all your tables and columns and how they interact. Keep a printout on your desk,and you’ll have an easier time understanding the rest of your app.

After this checkout seed.rb file

4) Check out the models

The app/models directory is the next place to check when going through an app for the first time. I won’t necessarily even open every single model when looking through the code the first time — it’s just good to get vaguely familiar with the models in the app.

In a well­designed Rails app, not everything will be backed by a database table. To continue to build your project vocabulary, look inside app/models for extra classes and attributes that you didn’t see inside db/schema.rb.Don’t spend too much time here yet. It’s easy to get lost in how things work. You’re just here
to fill out your project vocabulary.

 As you learn more about the parts your app is built from, you might wonder why certain
objects exist, or why they fit together the way they do. And that’s the point of the next stage.

5) Helpers and Routes

This is the last stop on the app tour before getting in to the front-end code. The reason for checking the helpers directory first is to get the idea of potential helper routes that may not be covered in the routes.rb file. A lot of apps will create custom helpers for URLs and I’ve noticed it’s beneficial to keep those in mind early. With any luck, common layout code will be extracted in to helpers as well which is also good to know up front.

6) Look at The Controllers

I’ve found it helpful to glance at the ApplicationController first to see what methods are used in every controller in the application. It’s usually a great source of information and can be filled with rescue_from directives which will save you head-aches later. After the ApplicationController, take a look at other controllers in the app and look for similarities:

Do they follow Rails conventions?
Does the app follow a skinny controller/fat model conventions?
Is the app RESTful?
Controllers can be a pain point in Rails applications and you may find some less than ideal code here.

7) Check the lib directory

The lib directory has been, in my experience, the source of most surprises when working on an existing Rails application. Unless the team has had a rigid structure in place, code seems to be dumped in here when it doesn’t fit anywhere else. Random POROs, miscellaneous modules, and more wind up finding a home in the lib directory. You’re not trying to read every file in here but it’s still the first stop I make after the test directory. It’s also where the rake tasks live, so check those out while you’re in here.

8) Asset Pipeline and Related Directories

A quick glance at th app/assets and related directories can give you a bunch of information with a relatively quick glance:

What JavaScript frameworks are being used?
What CSS frameworks are being used?
Are there any other external front-end libraries in use?

9) Try the app, and find out “why”

Once you feel comfortable with the objects the app uses, start the app up. Click around and explore. If there’s documentation, it can help guide you. Otherwise, just see those terms you just learned come to life.
As you use the app, you’ll start to see it from a higher level. You’ll notice that there are reasons why models have the associations they do. Why they’re grouped together. Why those class names were chosen. Maybe they all appear on pages together, or are built from
the same form.And when you start to understand the parts the app is built from and why they exist, you’re
ready to figure out how it all works.

10) Figure out the “how”

By now, you know enough about the app to learn it in detail. You won’t get distracted by new terms and new interactions. You can pick one thing you want to learn, and follow it all the way through.
There are lots of ways you can go from here. These are a few of my favorites:
Browse the UI, and think: How could this feature be built, knowing what you know about the pieces? Can you guess? Use a debugger to confirm your guess, or discover how it actually works.
Pick a test case and follow it all the way through the app. Either read through the code,or explore it with a debugger.Pick a route or controller action. Can you figure out how to reach that controller action from the UI?
This is also a good time to go back through your models and plain Ruby objects. And this time, you cango into the details of how the models work.Become curious about the app. Find gaps in your knowledge and close them with a debugger and the code.

11) Why not start with the tests?

But absolutely, make sure the tests run. If they don’t, you’re working with a broken system. And you can’t rely on the things you discover from a broken system.

================ Conclusion ================

What, to why, to how

To understand a new Rails app, go through these three phases, in this order:

What parts make up this app?

Why do those parts exist? Why are they put together that way?

How does each piece work?

That will give you both the broad point of view and the narrow knowledge you need in order to make that new app yours.

How to Install Sublime in Ubuntu 14.04

There are two version’s of Sublime Text is available to install. Install any one version as per your requirements using following options on Ubuntu and Debian Systems.

Install Sublime Text 2:

$ sudo add-apt-repository ppa:webupd8team/sublime-text-2
$ sudo apt-get update
$ sudo apt-get install sublime-text

Install Sublime Text 3:

$ sudo add-apt-repository ppa:webupd8team/sublime-text-3
$ sudo apt-get update
$ sudo apt-get install sublime-text-installer