class User
def before_save
puts 'before save'
end
def after_save
puts 'after_save'
end
def around_save
puts 'in around save'
yield
puts 'out around save'
end
end
User.save
before save
in around save
out around save
after_save
=> true
Tuesday, February 24, 2015
The around_* callback is called around the action and inside the before_* and after_* actions. For example:
jQuery – Select element cheat sheet
Note: elementid refers to the actual id of the select element. elementname refers to the actual name
of the select element.
e.g. <select id = “elementid” name = “elementname”></select>
of the select element.
e.g. <select id = “elementid” name = “elementname”></select>
Selecting an element(s) for further information refer to jQuery API Selectors
Basics:
Select element by id $(“#elementid”)
Select all select elements $(“select”)
Select all select elements by class $(“select.classname”)
Hierarchy:
Select all options from a select element $(“#elementid option”)
Basic Filters:
Select the first option $(“#elementid option:first”)
Select the last option $(“#elementid option:last”)
Select the option at a particular index (variant 1) $(“#elementid option:eq(2)”)
Attribute Filters:
Select all options that have a value attribute set $(“#elementid option[value]”)
Select element by name $(“select[name=elementname]”)
Form Filters:
Select the selected option $(“#elementid option:selected”)
jQuery core functionality, for further information refer to jQuery API Core
jQuery Object Accessors:
Execute a function on each matched element
e.g. $(“#elementid option”).each(function() {
alert(“I am an option of #elementid”);
})
Number of matched select elements
e.g. $(“select”).size() returns number of select elements on a page
Number of options in a particular select element $(“#elementid option”).size()
Select an option at a particular index (variant 2) $(“#elementid option”).eq(1)
Get the index of the selected option (variant 1) $("#elementid option").index($("#elementid option:selected")))
Select attributes, for further information refer to jQuery API Attributes
Attr:
Select an attribute from matched select element e.g. select name attribute $(“#elementid”).attr(“name”)
Set an attribute for matched select element e.g. set title $(“#elementid”).attr(“title”, “myselect”)
Remove an attribute from matched select e.g. remove title element $(“#elementid”).removeAttr(“title”)
Class:
Add a class to matched select element $(“#elementid”).addClass(“inputs”)
Remove a class from matched select element $(“#elementid”).removeClass(“inputs”)
HTML:
Get inner HTML of matched select $(“#elementid”).html()
Remove all options from matched select $(“#elementid”).html(“”)
Set all new options for matched select e.g.
$("#elementid").html("<option value='1'>Some oranges</option><option value='2'>More Oranges</option><option value='3'>Even more oranges</option>")
Text:
Get the text of matched option e.g. $(“#elementid option:first”).text()
Get the selected text of matched select Returns text value of selected option $(“#elementid option:selected”).text()
Remove the text of matched option e.g. $(“#elementid option:last”).text(“”)
Set the text of a matched option e.g. $(“#elementid option:eq(2)”).text(“Purple”)
Value:
Get the value of the matched option item e.g. $(“#elementid option:first”).val()
Get the selected value of matched select Returns value attribute of selected option $(“#elementid”).val()
Set the value of a matched option e.g. $(“#elementid option:eq(2)”).val(“7”)
Set the selected element based upon value i.e. Sets the selected option to option with value 7
e.g. $(“#elementid”).val(“7”)
Set the selected element based upon text e.g.
$("#elementid").val("Oranges").attr("selected", "selected")
Traversing select attributes, for further information refer to jQuery API Traversing
Finding:
Get the index of the selected option (variant 2) $("#elementid option:selected").prevAll().size()
Manipulating select attributes, for further information refer to jQuery API Manipulation
Inserting Inside:
Add options to the end of select element
e.g. $("#elementid").append("<option
value='1'>Apples</option>")
Add options to the start of select element
e.g. $("#elementid").prepend("<option
value='0'>Before Apples</option>")
Inserting Outside:
Add options after selected index
e.g. $("#elementid option:eq(0)").after("<option
value='4'>Some pears</option>"
Add options before selected index
e.g. $("#elementid option:eq(3)").before("<option
value='5'>Some apricots</option>")
Replacing:
Replace items at a certain index
e.g. $("#elementid
option:eq(1)").replaceWith("<option
value='2'>Some apples</option>")
Removing:
Remove option at specified index $("#elementid option:eq(0)").remove()
Remove first option $("#elementid option:first").remove()
Remove last option $("#elementid option:last").remove()
Select element events, for further information refer to jQuery API Events
Event Helpers:
Function to call when an option is selected $("#elementid").change(function() {})
Getting values when an option is selected
$("#elementid").change(function() {
alert($(this).val());
alert($(this).children("option:selected").text());
})
Select form submission, for further information refer to jQuery API AJAX
Event Helpers:
Serialize select element so it can be submitted or $("#elementid").serialize()
passed in URL Returns something like this: "elementname=1"
There are some steps to configure Server, Firewall and Apache .
Steps To Configure Server :
1. Lets know server type : $cat /etc/*-release
2. Update system : $ sudo apt-get update
3. Install git and curl : $sudo apt-get install curl git-core gitosis .
4. Now Install RVM : $ bash < <( curl https://rvm.beginrescueend. com/releases/rvm-install-head )
5. Make changes in /root/.bashrc file as mentioned after installing RVM .
replace : [ -z "$PS1" ] && return
with : if [[ -n "$PS1" ]] ; then
and at the end of file add
[[ -s "/usr/local/rvm/scripts/rvm" ]] && source "/usr/local/rvm/scripts/rvm"
if
After chnages use "su" on prompt .So that .bashrc file can be loaded with changes
6. Install Ruby depandencies for RVM: $ apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake
7. Now RVM is ready to install ruby like : $ rvm install 1.9.2-p180
8. Install database server (say mysql) : $ sudo apt-get install mysql-server
9. Setup application in /var/www ( if www directory not exist then create it.)
Step to Configure firewall :
1. View entries in iptable : $ iptables -L
2. Allow ports : $ iptables -A INPUT -p tcp -dport ssh -j ACCEPT
it can also be done by specifing port number : $ iptables -A INPUT -p tcp -dport 80 -j ACCEPT
3. TO allow ping : $ iptables -I INPUT 2 -p icmp -j ACCEPT
4. TO allow established sessions to receive traffic : $ iptables -I INPUT 2 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
5. Save setting : $ sudo bash -c "iptables-save > /etc/iptables.rules"
Step to Configure apache :
1. Install apache : $ sudo apt-get install apache2
apache's required files for changes will be available in /etc/apache2/ directory .
2. Install passenger gem to support ruby and rails on apache : $ gem install passenger
3. Now run : $ passenger-install-apache2- module
This will suggest, what to do next.
4. Create a file in sites-available directory with any name you want . And copy content suggested in above step
<VirtualHost *:80>......
.....
.....
</VirtualHost>
And make setting as required .
5. Remove the symbolic links for default from site-enabled.
6. Create symbolic links in site-enabled folder from newly created file in site-available.
7. Restart apache :/etc/init.d/apache2 start/stop/restart
8. Restart passanger after chnages in app : touch path_to_app/tmp/restart.txt
Rails 3 Problem with Passenger :
when gem 'paperclip', :git => 'git://github.com/lmumar/ paperclip.git', :branch => 'rails3' is added to your Gemfile
then there will be some problem while you are running app . Try
$ bundle --deployment
Steps To Configure Server :
1. Lets know server type : $cat /etc/*-release
2. Update system : $ sudo apt-get update
3. Install git and curl : $sudo apt-get install curl git-core gitosis .
4. Now Install RVM : $ bash < <( curl https://rvm.beginrescueend.
5. Make changes in /root/.bashrc file as mentioned after installing RVM .
replace : [ -z "$PS1" ] && return
with : if [[ -n "$PS1" ]] ; then
and at the end of file add
[[ -s "/usr/local/rvm/scripts/rvm" ]] && source "/usr/local/rvm/scripts/rvm"
if
After chnages use "su" on prompt .So that .bashrc file can be loaded with changes
6. Install Ruby depandencies for RVM: $ apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake
7. Now RVM is ready to install ruby like : $ rvm install 1.9.2-p180
8. Install database server (say mysql) : $ sudo apt-get install mysql-server
9. Setup application in /var/www ( if www directory not exist then create it.)
Step to Configure firewall :
1. View entries in iptable : $ iptables -L
2. Allow ports : $ iptables -A INPUT -p tcp -dport ssh -j ACCEPT
it can also be done by specifing port number : $ iptables -A INPUT -p tcp -dport 80 -j ACCEPT
3. TO allow ping : $ iptables -I INPUT 2 -p icmp -j ACCEPT
4. TO allow established sessions to receive traffic : $ iptables -I INPUT 2 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
5. Save setting : $ sudo bash -c "iptables-save > /etc/iptables.rules"
Step to Configure apache :
1. Install apache : $ sudo apt-get install apache2
apache's required files for changes will be available in /etc/apache2/ directory .
2. Install passenger gem to support ruby and rails on apache : $ gem install passenger
3. Now run : $ passenger-install-apache2-
This will suggest, what to do next.
4. Create a file in sites-available directory with any name you want . And copy content suggested in above step
<VirtualHost *:80>......
.....
.....
</VirtualHost>
And make setting as required .
5. Remove the symbolic links for default from site-enabled.
6. Create symbolic links in site-enabled folder from newly created file in site-available.
7. Restart apache :/etc/init.d/apache2 start/stop/restart
8. Restart passanger after chnages in app : touch path_to_app/tmp/restart.txt
Rails 3 Problem with Passenger :
when gem 'paperclip', :git => 'git://github.com/lmumar/
then there will be some problem while you are running app . Try
$ bundle --deployment
#~ class Array
#~ def reverse_iterate
#~ current_index = self.size-1
#~ while current_index >= 0
#~ yield self[current_index]
#~ current_index -= 1
#~ end
#~ end
#~ end
#~
#~ [2,4,6,8].reverse_iterate { |i| print "#{i} "}
class Array
def each_anil &block
yield self
end
end
[2,4,6,8].each_anil {|p| puts p}
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”>
</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’
<div id=”fb-root”></div>
<script src=”http://connect.facebook.net/en_US/all.js”>
</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/” for local host or your application url say “http://www.twitterapp.com”
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.
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/” for local host or your application url say “http://www.twitterapp.com”
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/”
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.
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/”
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.
Subscribe to:
Posts (Atom)
FastAPI: The Modern Python Web Framework
Introduction to FastAPI: The Modern Python Web Framework The world of web frameworks has always been competitive — from Django and Flask ...
-
# Object Oriented Programming : OOP is a method of programming.The main purpose of object oriented programming is that it simplify the ...
-
rspec is a full-featured testing framework that will let you write what Rails considers unit tests, functional tests, and integration test...
-
Cucumber – a Behavior Driven Development (BDD) framework which is used with Selenium for performing acceptance testing. Cucumber Introd...