Tuesday, November 25, 2014

Integrating facebook using koala gem

In view:

<div id=”fb-root”></div>
<script src=”http://connect.facebook.net/en_US/all.js”&gt;
</script>
<script>
FB.init({
appId:’204597759582691′, cookie:true,
status:true, xfbml:true
});
</script>
<fb:login-button v=”2″ size=”xlarge” length=”long”
perms=”email” onlogin=’window.location = “/app/code”‘ >
Login with Facebook
</fb:login-button>
<div>
<a href=””>signin facebook</a>
</div>

In Controller:

class AppController < ApplicationController
def code
“set_facebook_client” #to check whether the user already login with the face book
oauth = Koala::Facebook::OAuth.new(204597759582691, “df14fdac2d0ebf6fbf684c68b2009ef8″)
#~ oauth = Koala::Facebook::OAuth.new(appid, “appsecretkey”)
@facebook_client ||= if @facebook_cookies = oauth.get_user_info_from_cookies(cookies)
Koala::Facebook::GraphAPI.new(@facebook_cookies[‘access_token’])
end
profile = @facebook_client.get_object(“me”)
end
end


In gemfile:
gem ‘koala’

Integrating Twitter Login With Rails Application

Follow the below steps to create Twitter application for your rails application:

1. Goto https://dev.twitter.com/
2. Click “Create an App” link.
3. Enter your app name, description, website and call back url.
For Website and call back url enter “http://127.0.0.1:3000/&#8221; for local host or your application url say “http://www.twitterapp.com&#8221;
5. You will get the Consumer key and Consumer secret.

Follow the below steps to integrate the Twitter login:


1. Include the below in your Gemfile.
    gem “omniauth-twitter”  
2. Execute the below command to install the gem
bundle
3. Create a new file called “omniauth.rb” in your initializers folder(app/config/initializers)
4. Inside the “omniauth.rb” copy the below lines
 Rails.application.config.middleware.use OmniAuth::Builder do
   provider :twitter, ‘YOUR APP CONSUMER KEY’, ‘YOUR APP CONSUMER SECRET’
 end
5. Paste the below lines to your routes file
  #calback url from twitter
       match ‘/auth/:provider/callback’ => ‘users#twitter_login’
6. Paste the below lines to your users controller
def twitter_login
 omniauth = request.env[‘omniauth.auth’]   # This contains all the details of the user say Email, Name, Age so that you can store it in your application db.
    redirect_to “Your redirect path after user logged in”
  end
7. Paste the below line to your view file where you want to display the Login with facebook link, Say in your users/new.html.erb
<a href=”/auth/twitter”>Login Via Twitter</a>
8. Restart the Server
9. Go to http://localhost:3000/users/new
10. Click the link  “Login Via Twitter” , page will be redirected to the Twitter site then redirected to your home page.

Integrating Facebook Login With Rails Application

Create Application on Facebook:

1. Goto https://developers.facebook.com/
2. Click “Apss” link.
3. Click “Create App”
4. Enter your app name “test”
5. You will get the App Id and App Secret
6. Click the link “Website with Facebook Login” and enter your site url say “http://localhost:3000/&#8221;
7. Click Save Changes.

Integrate Facebook Login to your application:

1. Include the below in your Gemfile.
gem “omniauth-facebook”
2. Execute the below command in terminal.
bundle
3. Create a new file called “omniauth.rb” in your initializers folder(app/config/initializers)
4. Inside the “omniauth.rb” copy the below lines
 Rails.application.config.middleware.use OmniAuth::Builder do
   provider :facebook, ‘YOUR APP KEY’, ‘YOUR APP SECRET’
 end
5. Paste the below lines to your routes file
    #calback url from facebook
      match ‘/auth/:provider/callback’ => ‘users#facebook_login’
6. Paste the below lines to your users controller
def facebook_login
      omniauth = request.env[‘omniauth.auth’]   # This contains all the details of the user say Email, Name, Age so that you can store it in your application db.
      redirect_to “Your redirect path after user logged in”
    end
7. Paste the below line to your view file where you want to display the Login with facebook link, Say in your users/new.html.erb
<a href=”/auth/facebook”>Login Via facebook</a>
8. Restart the Server
9. Go to http://localhost:3000/users/new

10. Click the link  “Login Via Facebook” , page will be redirected to the Facebook site then redirected to your home page.

Thursday, October 16, 2014

set environment variable

Insert these lines of code to the config/application.rb after lineconfig.assets.version = '1.0':

config.before_configuration do
      env_file = File.join(Rails.root, 'config', 'paypal.yml')
      YAML.load(File.open(env_file)).each do |key, value|
        ENV[key.to_s] = value
      end if File.exists?(env_file)
    end

Git vs. SVN - Basic Commandline Syntax Reference

Learning the git workflow takes a bit of brain retraining, but since I've been using SVN almost entirely via commandline (because Subversive sucks and locks up my Eclipse when I try to use it for anything beyond synching/updating/committing a handful of files), adopting git's commandline syntax is reasonably similar. Consider these simple operations:
Initial checkout from existing repo for a given branchgit clone http://github.com/sonatype/sonatype-tycho.git; cd sonatype-tycho; git checkout origin/tycho-0.10.xsvn checkout http://anonsvn.jboss.org/repos/jbosstools/branches/jbosstools-3.2.0.Beta1/
Update locally checked out files from central repogit pullsvn update
List locally changes files/foldersgit statussvn stat
Diff locally changed filegit diff somefile.txtsvn diff somefile.txt
Revert locally changed file*git checkout somefile.txtsvn revert somefile.txt
Revert ALL local changes (except untracked files)*git reset --hard HEADsvn revert . -R
Add new filegit add file.txtsvn add file.txt
Add new folder recursivelygit add foldersvn add folder
Delete filegit rm file.txtsvn rm file.txt
Delete foldergit rm -r folder (non-recursive by default; use -r to recurse)svn rm folder (recursive by default; use -N to not recurse)
Commit changed file to central repogit commit -m "message" file.txt; git pushsvn ci -m "message" file.txt
Ignore files/folders (in the current folder)echo "target
*.class
bin" > .gitignore; \
git ci -m "gitignore" .gitignore
svn propset svn:ignore "target
*.class
bin" .; \
svn ci -N -m "svn:ignore" .
Obviously you can do a lot more w/ Git than with SVN (like stashing local changes temporarily), but for the sake of simply moving from a VCS to a DVCS and being able to continue to work the same way you already do, the above table should provide a good introduction.

15 Best Ruby on Rails Gem Used for an application

Devise (https://rubygems.org/gems/devise): Since some years ago, it represents the authentication mechanism preferred by all Rails developers. Powerful, flexible, allows to integrate with OAuth authentication systems with a minimal effort.

Haml (https://rubygems.org/gems/haml): Allows you to write valid XHTML concisely. The learning curve is relatively short.

Gritter (https://rubygems.org/gems/gritter): After years of flash messages in the classic div in the page, we moved to Growl like notifications. Thanks to these pop-ups, we can show our flash messages in any page in a completely non-invasive and elegant way.

Cells (https://rubygems.org/gems/cells): Cells can really keep our controllers very skinny. We use it to represent and caching some boxes, like “recommended items”, “top users” and so on. I really like Cells over the use of helpers.

FriendlyId (https://rubygems.org/gems/friendly_id): A perfect gem to make our url seo friendly.

SimpleForm (https://rubygems.org/gems/simple_form): We use it primarily for its excellent integration with Bootstrap and for its ease of use.

Paperclip (https://rubygems.org/gems/paperclip): Despite the years, it still remains the reference point for attachments management .

Kaminari (https://rubygems.org/gems/kaminari): Useful gem to manage paginated collections .

Cancan (https://rubygems.org/gems/cancan): Our choice to manage permissions. We never have had the need to use some other solution .

Resque (https://rubygems.org/gems/resque) or Delayed Job (https://rubygems.org/gems/delayed_job): Both are valuable supports to manage background processes. If you do not have enough resources to set up a Redis server, we recommend Delayed Job.

Sunspot (https://rubygems.org/gems/sunspot): After a brief period with thinking_sphinx, we have moved to this very powerful indexing engine. Thanks to Solr, we can easily implement geolocated full text searches. The only problem is that you need to configure a dedicated Tomcat server. If you do not have these resources, we recommend using pg_search (https://rubygems.org/gems/pg_search) with a Postgres database or the old but still valid meta_search (http://rubygems.org/gems/meta_search) .

ActiveAdmin (https://rubygems.org/gems/activeadmin): When it is necessary to set up a back office administration in a short time, here is the right gem. Powerful, fairly customizable, ideal for simple administration interfaces.

Letter opener (https://rubygems.org/gems/letter_opener): Useful to test sending emails simply by opening them in a browser window .

RSpec (https://rubygems.org/gems/rspec): A perfect gem to test our models in a BDD way.

Capybara (https://rubygems.org/gems/capybara) : In addition to unit test the models, we like to create a suite of acceptance tests. Capybara allows you to test all the application’s user stories relatively quickly.

Wednesday, October 15, 2014

How to migrate from Heroku Shared-database to Dev or Basic Postgres

First, we installed the postgres add-on:
heroku addons:add heroku-postgresql:basic

When you run the command it created a new database:
Attached as HEROKU_POSTGRESQL_SOMECOLOR
You can see it by typing heroku config

Most of our apps were already running pgbackups:monthly (free), on one of them we ran:
heroku addons:add pgbackups

Put the app in maintenance mode:
heroku maintenance:on

Capture a backup of the current database:
heroku pgbackups:capture --expire
(Took under 2 minutes for a 12 MB database)

Now push that data into the new database:
heroku pgbackups:restore HEROKU_POSTGRESQL_SOMECOLOR

Now make the new database the current database:
heroku pg:promote HEROKU_POSTGRESQL_SOMECOLOR
(Replaces the heroku config variable DATABASE_URL with the URL for the new database. The old shared-database and it’s URL are still there, but they are not the default anymore.)

Check the new database is working:
heroku pg:psql HEROKU_POSTGRESQL_SOMECOLOR
=> select count(*) from users;
(To quit the CLI type \q )

Turn off maintenance mode:
heroku maintenance:off


Short steps ::

heroku pgbackups:capture --expire
heroku pgbackups:url
curl -o latest_dump_file.dump DATABASE_URL
heroku maintenance:on
heroku pgbackups:restore HEROKU_POSTGRESQL_WHITE_URL
heroku pg:promote HEROKU_POSTGRESQL_WHITE_URL
heroku pg:psq HEROKU_POSTGRESQL_WHITE_URL
heroku pg:psql HEROKU_POSTGRESQL_WHITE_URL
heroku maintenance:off

New project setup from heroku

New project setup from heroku 

ssh-keygen -t rsa -C YOUR EMAIL ID

ssh-add ~/.ssh/id_rsa

heroku login

heroku keys:add

git clone git@heroku.com:project.git

cd project/

rvm gemset create project

rvm gemset use project

bundle install

Change in database.yml

rake db:create

rake db:migrate

rails s