Applied  Ruby on Rails & AJAX An insight into the technology and principles behind a social networking community
Who am I? Farhan Mashraqi (Frank Mash) Developer for 7+ years MySQL DBA Community Member A Recovering PHP Developer Contact Information: Business E-mail:   [email_address] Personal E-mail:   [email_address]   Personal Blog   http://mashraqi.com
What we’ll cover (aka the essentials) Why ROR? MVC Crash Course (Scaffolding etc) URLs in ROR Adoppt AJAXifying Forms Relationships / Validations
The essentials continued Transactions Blog Claims Tagging In place editing Learn more Q&A
Why would I use Ruby On Rails? RAD DRY: “Do not Repeat Yourself” Disciplined Normalization Relationships Web Services Everything is an Object
MVC
Crash Course [root@srv31 docs]# rails ror create create  app/controllers create  app/helpers create  app/models create  app/views/layouts create  config/environments create  components create  db create  doc create  lib create  lib/tasks create  log create  public/images ... Creating a bare application on Rails [root@srv31 docs]# rails ror
Do it with Scaffolding [root@srv31 ror]# ruby script/generate scaffold user exists  app/controllers/ exists  app/helpers/ create  app/views/users exists  test/functional/ dependency  model exists  app/models/ exists  test/unit/ exists  test/fixtures/ create  app/models/user.rb ruby script/generate scaffold user CRUD = ‘C’reate, ‘R’ead, ‘U’pdate, ‘D’elete
Experience the magic http://localhost:3000/users http://localhost:3000/users/new
Find functions find_by_[field_name] (field_value) find_by_[field_name]_and_[field_name] find_first find_all_by find() find_by_sql(‘SELECT ….’)
URLs in ROR <%= link_to “Reply”, :controller=>”posts”, :action=>“reply”, :id=>4  %>
What is Adoppt A replicatable social networking platform Create and participate in communities Blogs, Forums, Favorites and more
 
 
Ajaxifying the login  form Ajaxified version <%=  link_to  “Login”, :controller=>”member”, :action=>”login” %> Typical form in ROR <%=  link_to_remote  “Login”,  :url=>{ :controller=>”member”, :action=>”login” }, :update => “loginform”, :loading => “Element.toggle($(‘loading’))”, :complete => “Element.toggle($(‘loading’))” %> Same form ‘AJAXified’
 
Relationships / Validations A community Has many members subscribed Members can post favorite web sites Belongs to a member or system  class Portal < ActiveRecord::Base has_many  :subscriptions has_many  :favorites belongs_to  :member validates_presence_of  :portal_name validates_presence_of  :portal_description validates_presence_of  :portal_url validates_uniqueness_of  :portal_url … end
HABTM using Subscriptions class  Member has_many :subscriptions end  class  Portal has_many :subscriptions end class  Subscription   belongs_to :member  belongs_to :portal end
Self referential relationships A member has many friends (member) Self referential HABTM relationship class Member < ActiveRecord::Base has_and_belongs_to_many  :friends, : class_name  => &quot;Member&quot;, : join_table  => &quot;friends_members&quot;, : association_foreign_key  => &quot;friend_id&quot;, : foreign_key  => &quot;member_id&quot;, : after_add  => :become_friend_with_friend, : after_remove  => :end_friendship end
Using relationships 1. Which favorites were posted to a portal @portal = Portal.new @favorites = @portal.favorites 2. Which members are subscribed? @portal = Portal.new @members = @portal.subscriptions @member = Member.new @member_portals = @member.portals 3. Which portals a member is subscribed to? 4. Which members are friends of a member? @member = Member.new @friends = @member.friends
Transactions (InnoDB) Member.transaction(@member, @friend) do @member.friends << @friend unless  @member.friends.include?(@friend) @friend.friends << @member unless  @friend.friends.include?(@member) end
Blog Claims def claim_verify @wb= Weblog.find_by_url(@params[:url]) require 'open-uri‘ @found =  open(@wb.url).read.include?(@wb.v_key) end
Tagging acts_as_taggable gem  (http://taggable.rubyforge.org/)
Tagging class  Article  < ActiveRecord::Base acts_as_taggable  :join_class_name => ' TagArticle ‘ class  TagArticle belongs_to :article, :class_name => ' Article ', :foreign_key => 'article_id‘ @tagged = Article.tags_count(:limit => 10) Tagging an article @article = Article.new @article.tag “mysql conference ror” Tags for an article @tags = @article.tags() Tag Cloud (Tom Fakes)
In place editing class BlogsController < ApplicationController  in_place_edit_for  :weblog, :description <%=  in_place_editor_field  :weblog, :description, :value=>''  %> In your views file In your model
Recap ROR: Valuable to You
Where to learn more Books Agile Web Development with Rails by Dave Thomas and David Heinemeier Hansson   Pro Rails by me ROR Recipes by Chad Fowler Websites http://www.ruby-doc.org/ http://www.rubygarden.org/ruby/ http://www.ruby-lang.org/ http://www.ruby-doc.org/stdlib/libdoc/rdoc/rdoc/index.html http://script.aculo.us http://rubyonrails.adoppt.com/blog/frank
Q&A

mashraqi_farhan

  • 1.
    Applied Rubyon Rails & AJAX An insight into the technology and principles behind a social networking community
  • 2.
    Who am I?Farhan Mashraqi (Frank Mash) Developer for 7+ years MySQL DBA Community Member A Recovering PHP Developer Contact Information: Business E-mail: [email_address] Personal E-mail: [email_address] Personal Blog http://mashraqi.com
  • 3.
    What we’ll cover(aka the essentials) Why ROR? MVC Crash Course (Scaffolding etc) URLs in ROR Adoppt AJAXifying Forms Relationships / Validations
  • 4.
    The essentials continuedTransactions Blog Claims Tagging In place editing Learn more Q&A
  • 5.
    Why would Iuse Ruby On Rails? RAD DRY: “Do not Repeat Yourself” Disciplined Normalization Relationships Web Services Everything is an Object
  • 6.
  • 7.
    Crash Course [root@srv31docs]# rails ror create create app/controllers create app/helpers create app/models create app/views/layouts create config/environments create components create db create doc create lib create lib/tasks create log create public/images ... Creating a bare application on Rails [root@srv31 docs]# rails ror
  • 8.
    Do it withScaffolding [root@srv31 ror]# ruby script/generate scaffold user exists app/controllers/ exists app/helpers/ create app/views/users exists test/functional/ dependency model exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/user.rb ruby script/generate scaffold user CRUD = ‘C’reate, ‘R’ead, ‘U’pdate, ‘D’elete
  • 9.
    Experience the magichttp://localhost:3000/users http://localhost:3000/users/new
  • 10.
    Find functions find_by_[field_name](field_value) find_by_[field_name]_and_[field_name] find_first find_all_by find() find_by_sql(‘SELECT ….’)
  • 11.
    URLs in ROR<%= link_to “Reply”, :controller=>”posts”, :action=>“reply”, :id=>4 %>
  • 12.
    What is AdopptA replicatable social networking platform Create and participate in communities Blogs, Forums, Favorites and more
  • 13.
  • 14.
  • 15.
    Ajaxifying the login form Ajaxified version <%= link_to “Login”, :controller=>”member”, :action=>”login” %> Typical form in ROR <%= link_to_remote “Login”, :url=>{ :controller=>”member”, :action=>”login” }, :update => “loginform”, :loading => “Element.toggle($(‘loading’))”, :complete => “Element.toggle($(‘loading’))” %> Same form ‘AJAXified’
  • 16.
  • 17.
    Relationships / ValidationsA community Has many members subscribed Members can post favorite web sites Belongs to a member or system class Portal < ActiveRecord::Base has_many :subscriptions has_many :favorites belongs_to :member validates_presence_of :portal_name validates_presence_of :portal_description validates_presence_of :portal_url validates_uniqueness_of :portal_url … end
  • 18.
    HABTM using Subscriptionsclass Member has_many :subscriptions end class Portal has_many :subscriptions end class Subscription belongs_to :member belongs_to :portal end
  • 19.
    Self referential relationshipsA member has many friends (member) Self referential HABTM relationship class Member < ActiveRecord::Base has_and_belongs_to_many :friends, : class_name => &quot;Member&quot;, : join_table => &quot;friends_members&quot;, : association_foreign_key => &quot;friend_id&quot;, : foreign_key => &quot;member_id&quot;, : after_add => :become_friend_with_friend, : after_remove => :end_friendship end
  • 20.
    Using relationships 1.Which favorites were posted to a portal @portal = Portal.new @favorites = @portal.favorites 2. Which members are subscribed? @portal = Portal.new @members = @portal.subscriptions @member = Member.new @member_portals = @member.portals 3. Which portals a member is subscribed to? 4. Which members are friends of a member? @member = Member.new @friends = @member.friends
  • 21.
    Transactions (InnoDB) Member.transaction(@member,@friend) do @member.friends << @friend unless @member.friends.include?(@friend) @friend.friends << @member unless @friend.friends.include?(@member) end
  • 22.
    Blog Claims defclaim_verify @wb= Weblog.find_by_url(@params[:url]) require 'open-uri‘ @found = open(@wb.url).read.include?(@wb.v_key) end
  • 23.
    Tagging acts_as_taggable gem (http://taggable.rubyforge.org/)
  • 24.
    Tagging class Article < ActiveRecord::Base acts_as_taggable :join_class_name => ' TagArticle ‘ class TagArticle belongs_to :article, :class_name => ' Article ', :foreign_key => 'article_id‘ @tagged = Article.tags_count(:limit => 10) Tagging an article @article = Article.new @article.tag “mysql conference ror” Tags for an article @tags = @article.tags() Tag Cloud (Tom Fakes)
  • 25.
    In place editingclass BlogsController < ApplicationController in_place_edit_for :weblog, :description <%= in_place_editor_field :weblog, :description, :value=>'' %> In your views file In your model
  • 26.
  • 27.
    Where to learnmore Books Agile Web Development with Rails by Dave Thomas and David Heinemeier Hansson Pro Rails by me ROR Recipes by Chad Fowler Websites http://www.ruby-doc.org/ http://www.rubygarden.org/ruby/ http://www.ruby-lang.org/ http://www.ruby-doc.org/stdlib/libdoc/rdoc/rdoc/index.html http://script.aculo.us http://rubyonrails.adoppt.com/blog/frank
  • 28.