Friday, February 27, 2015

RESTful architecture

RESTful interface means clean URLs, less code, CRUD interface.

CRUD means Create-READ-UPDATE-DESTROY.

You might heard about HTTP verbs, GET, POST. In REST, they add 2 new verbs, i.e, PUT, DELETE.

There are 7 default actions, those are – index, show, new, create, edit, update, destroy


GET is used when you retrieve data from database.
POST is used when you create new record in database.
PUT is used when you are updating any existing record in database.
DELETE is used when you are destroying any record in database.

Following table may clear the concept.

Action VERB

index                        GET

show                        GET

new                          GET

create                       POST

edit                          GET

update                      PUT


destroy                    DELETE

Tuesday, February 24, 2015

The around_* callback is called around the action and inside the before_* and after_* actions. For example:

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

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>

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

#~ 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}