Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Padrino Introduction

Padrino Introduction

Sumeet Singh

April 24, 2013
Tweet

Other Decks in Programming

Transcript

  1. About Me • Name: Sumeet Singh • @ortuna • github.com/ortuna

    • Ruby/Rails Developer 2 Wednesday, April 24, 13
  2. What is Padrino(quick version) Sinatra Helpers Generators Rake Tasks Console

    Mailer Admin Cache Multi-apps Sub Apps 3 Wednesday, April 24, 13
  3. What is Sinatra? • Micro-framework • DSL for quickly creating

    web applications • Does not include: • Database interaction • Helpers • Luxuries 4 Wednesday, April 24, 13
  4. Still Packs a Punch • Render • Templates (haml, erb,

    liquid) • Layouts • URL-matching patterns • before/after filters 5 Wednesday, April 24, 13
  5. #hi.rb require 'sinatra' get '/hi' do "Hello World!" end $

    gem install sinatra $ ruby hi.rb $ curl http://localhost:4567/hi 7 Wednesday, April 24, 13
  6. #hi.rb require 'sinatra' get '/hi' do "Hello World!" end $

    gem install sinatra $ ruby hi.rb $ curl http://localhost:4567/hi 7 Wednesday, April 24, 13
  7. #hi.rb require 'sinatra' get '/hi' do "Hello World!" end $

    gem install sinatra $ ruby hi.rb $ curl http://localhost:4567/hi 7 Wednesday, April 24, 13
  8. #hi.rb require 'sinatra' get '/hi' do "Hello World!" end $

    gem install sinatra HTTP Verb $ ruby hi.rb $ curl http://localhost:4567/hi 7 Wednesday, April 24, 13
  9. #hi.rb require 'sinatra' get '/hi' do "Hello World!" end $

    gem install sinatra HTTP Verb Path $ ruby hi.rb $ curl http://localhost:4567/hi 7 Wednesday, April 24, 13
  10. #hi.rb require 'sinatra' get '/hi' do "Hello World!" end $

    gem install sinatra HTTP Verb Path Return Value $ ruby hi.rb $ curl http://localhost:4567/hi 7 Wednesday, April 24, 13
  11. #hi.rb require 'sinatra' get '/hi' do "Hello World!" end $

    gem install sinatra HTTP Verb Path Return Value $ ruby hi.rb $ curl http://localhost:4567/hi 7 Wednesday, April 24, 13
  12. What is Padrino? • Web framework built on top of

    Sinatra • Adds: Routing, Helpers, Mailer, Caching, Admin interface, Localization, and more... • Has a standard library of tools for Sinatra • “Structured Sinatra” 8 Wednesday, April 24, 13
  13. #hi.rb require 'sinatra' .... get '/hi' do .... "Hello World!"

    end Many different ways to append features 9 Wednesday, April 24, 13
  14. #hi.rb require 'sinatra' .... get '/hi' do .... "Hello World!"

    end Many different ways to append features many approaches many approaches 9 Wednesday, April 24, 13
  15. Padrino is Mix and Match • mongoid • activerecord •

    datamapper • sequel • ohm • more... Persistence 10 Wednesday, April 24, 13
  16. Padrino is Mix and Match • mongoid • activerecord •

    datamapper • sequel • ohm • more... Persistence • minitest • bacon • rspec • shoulda • cucumber • more... Testing 10 Wednesday, April 24, 13
  17. Padrino is Mix and Match • mongoid • activerecord •

    datamapper • sequel • ohm • more... Persistence • minitest • bacon • rspec • shoulda • cucumber • more... Testing Templating • erb • haml • slim • liquid • more... 10 Wednesday, April 24, 13
  18. Padrino is Mix and Match • mongoid • activerecord •

    datamapper • sequel • ohm • more... Persistence • minitest • bacon • rspec • shoulda • cucumber • more... Testing Templating • erb • haml • slim • liquid • more... css javascript mocking 10 Wednesday, April 24, 13
  19. Installing Padrino • # gem install padrino • # padrino

    g project myapp -d datamapper 11 Wednesday, April 24, 13
  20. Installing Padrino • # gem install padrino • # padrino

    g project myapp -d datamapper • # cd myapp 11 Wednesday, April 24, 13
  21. Installing Padrino • # gem install padrino • # padrino

    g project myapp -d datamapper • # cd myapp • # padrino start 11 Wednesday, April 24, 13
  22. Installing Padrino • # gem install padrino • # padrino

    g project myapp -d datamapper • # cd myapp • # padrino start => Padrino/0.11.0 has taken the stage development at http://127.0.0.1:3000 [2013-04-21 20:48:07] INFO WEBrick 1.3.1 [2013-04-21 20:48:07] INFO ruby 2.0.0 (2013-02-24) [x86_64-darwin12.2.1] [2013-04-21 20:48:07] INFO WEBrick::HTTPServer#start: pid=12867 port=3000 11 Wednesday, April 24, 13
  23. App Structure !"" app # !"" controllers # !"" helpers

    # $"" views # $"" layouts !"" config !"" public # !"" images # !"" javascripts # $"" stylesheets $"" tmp 13 Wednesday, April 24, 13
  24. App Structure !"" app # !"" controllers # !"" helpers

    # $"" views # $"" layouts !"" config !"" public # !"" images # !"" javascripts # $"" stylesheets $"" tmp 13 Wednesday, April 24, 13
  25. App Structure !"" app # !"" controllers # !"" helpers

    # $"" views # $"" layouts !"" config !"" public # !"" images # !"" javascripts # $"" stylesheets $"" tmp 13 Wednesday, April 24, 13
  26. Auto Loaded Paths • ./lib • ./models • ./shared/lib •

    ./shared/models 16 Wednesday, April 24, 13
  27. Auto Loaded Paths • ./lib • ./models • ./shared/lib •

    ./shared/models • ./#{app}/models (multi-apps) 16 Wednesday, April 24, 13
  28. Auto Loaded Paths • ./lib • ./models • ./shared/lib •

    ./shared/models • ./#{app}/models (multi-apps) • ... 16 Wednesday, April 24, 13
  29. config/boot.rb • Usually contains: • Debug logging on/off • before_load

    / after_load • load any prerequisites 17 Wednesday, April 24, 13
  30. config/boot.rb • Usually contains: • Debug logging on/off • before_load

    / after_load • load any prerequisites • Padrino.require_dependencies("path/one/**/*.rb") 17 Wednesday, April 24, 13
  31. config/boot.rb • Usually contains: • Debug logging on/off • before_load

    / after_load • load any prerequisites • Padrino.require_dependencies("path/one/**/*.rb") • ... 17 Wednesday, April 24, 13
  32. config/apps.rb • Usually contains: • Site wide configs • session_secret,

    csrf, etc... • mounts each app 18 Wednesday, April 24, 13
  33. config/apps.rb • Usually contains: • Site wide configs • session_secret,

    csrf, etc... • mounts each app • ... 18 Wednesday, April 24, 13
  34. ./tmp • Used for: • Temp use with tests •

    Cache 20 Wednesday, April 24, 13
  35. ./tmp • Used for: • Temp use with tests •

    Cache • Anything with: 20 Wednesday, April 24, 13
  36. ./tmp • Used for: • Temp use with tests •

    Cache • Anything with: • tmp_location = Padrino.root(‘tmp’) 20 Wednesday, April 24, 13
  37. ./tmp • Used for: • Temp use with tests •

    Cache • Anything with: • tmp_location = Padrino.root(‘tmp’) • ... 20 Wednesday, April 24, 13
  38. ./tmp • Used for: • Temp use with tests •

    Cache • Anything with: • tmp_location = Padrino.root(‘tmp’) • ... 20 Wednesday, April 24, 13
  39. ./public • Used for assets • Can use for packaging:

    • sinatra-assetpack 21 Wednesday, April 24, 13
  40. ./public • Used for assets • Can use for packaging:

    • sinatra-assetpack • sprockets 21 Wednesday, April 24, 13
  41. ./public • Used for assets • Can use for packaging:

    • sinatra-assetpack • sprockets • ... 21 Wednesday, April 24, 13
  42. App Structure !"" app # !"" controllers # !"" helpers

    # $"" views # $"" layouts !"" config !"" public # !"" images # !"" javascripts # $"" stylesheets $"" tmp 22 Wednesday, April 24, 13
  43. ./app/app.rb ./#{app_name}/app.rb • Usually contains: • Overall app configuration •

    enable/disable options • register helpers 23 Wednesday, April 24, 13
  44. ./app/app.rb ./#{app_name}/app.rb • Usually contains: • Overall app configuration •

    enable/disable options • register helpers • ... 23 Wednesday, April 24, 13
  45. Myapp::App.controllers do get :index do ... end get ‘/about’ do

    ... end end #app/controllers/example.rb route to /about 25 Wednesday, April 24, 13
  46. Myapp::App.controllers do get :index do ... end get ‘/about’ do

    ... end end #app/controllers/example.rb Context route to /about 25 Wednesday, April 24, 13
  47. Myapp::App.controllers do get :index do ... end get ‘/about’ do

    ... end end #app/controllers/example.rb Context route to /about route to / 25 Wednesday, April 24, 13
  48. get :index, :with => :id do /5 /xyz Named Routes

    & Params 26 Wednesday, April 24, 13
  49. get :index, :with => :id do /5 /xyz get ‘/:id’

    do # /my_id # params[:id] => my_id Named Routes & Params 26 Wednesday, April 24, 13
  50. get :index, :with => :id do /5 /xyz get ‘/:id’

    do # /my_id # params[:id] => my_id Named Routes & Params #url_for(:index, 5) => /5 #url_for(:admin, :show) => /admin/show In views: 26 Wednesday, April 24, 13
  51. Myapp::App.controllers :admin do get :index do ... end get :index,

    :with => :id do “request id #{params[:id]}” end end #app/controllers/admin.rb Named Routes 27 Wednesday, April 24, 13
  52. Mounted Apps A Simple way to have multiple applications within

    the same project. 28 Wednesday, April 24, 13
  53. Mounted Apps • More testable • More readable • Changable

    A Simple way to have multiple applications within the same project. 28 Wednesday, April 24, 13
  54. !"" app # !"" controllers # !"" helpers # $""

    views # $"" layouts !"" blog # !"" controllers # !"" models !"" shared # !"" models # !"" lib !"" config !"" public # !"" images # !"" javascripts # $"" stylesheets $"" tmp 30 Wednesday, April 24, 13
  55. blog_path = Padrino.root('blog/app.rb') Padrino.mount('Myapp::Blog', :app_file => blog_path).to('/blog') Config/apps.rb Easy way:

    padrino-gen app blog create blog create blog/app.rb create blog/controllers create blog/helpers create blog/views create blog/views/layouts create public/blog append config/apps.rb 31 Wednesday, April 24, 13