Introducción

  1. Darío Javier Cravero says 'The best way to get started is to have a look at the guides'
  2. Padrino Blog Tutorial
  3. Gist Template para seguir el tutorial del blog en Padrino: achiu / blog_template.rb

[~/srcpadrino/padrino-book(master)]$ padrino --version
Padrino v. 0.11.4

[~/srcpadrino/padrino-book(master)]$ padrino --help
Tasks:
  padrino console   # Boots up the Padrino application irb console (alternatively use 'c').
  padrino generate  # Executes the Padrino generator with given options (alternatively use 'gen' or 'g').
  padrino help      # Describe available tasks or one specific task
  padrino rake      # Execute rake tasks.
  padrino runner    # Run a piece of code in the Padrino application environment (alternatively use 'run' or 'r').
  padrino start     # Starts the Padrino application (alternatively use 's').
  padrino stop      # Stops the Padrino application (alternatively use 'st').
  padrino version   # Show current Padrino version.

Options:
  -c, [--chdir=CHDIR]            # Change to dir before starting.
  -e, --environment=ENVIRONMENT  # Padrino Environment.
                                 # Default: development
      [--help]                   # Show help usage

[~/srcpadrino/padrino-book(master)]$ padrino help generate
Usage:
  padrino generate

Options:
  -c, [--chdir=CHDIR]            # Change to dir before starting.
  -e, --environment=ENVIRONMENT  # Padrino Environment.
                                 # Default: development
      [--help]                   # Show help usage

Executes the Padrino generator with given options (alternatively use 'gen' or 'g').

 gem uninstall activesupport -v 4.0.0

To create a Padrino application, the best place to start is using the convenient Padrino generator. Similar to Rails, Padrino has a project generator which will create a skeleton application with all the files you need to being development of your new idea. Padrino is an agnostic framework and supports using a variety of different template, testing, JavaScript and database components. You can learn more by reading the generators guide.

For this sample application, we will use the ActiveRecord ORM, the Haml templating language, the Shoulda testing framework and the jQuery JavaScript library. With that in mind, let us generate our new project:

[~/srcpadrino]$ padrino g project sample_blog -t shoulda -e haml -c sass -s jquery -d activerecord -b
      create  
      create  .gitignore
      create  config.ru
      create  config/apps.rb
      create  config/boot.rb
      create  public/favicon.ico
      create  public/images
      create  public/javascripts
      create  public/stylesheets
      create  tmp
      create  .components
      create  app
      create  app/app.rb
      create  app/controllers
      create  app/helpers
      create  app/views
      create  app/views/layouts
      create  Gemfile
      create  Rakefile
    applying  activerecord (orm)...
       apply  orms/activerecord
      insert  Gemfile
      insert  Gemfile
      insert  app/app.rb
      create  config/database.rb
    applying  shoulda (test)...
       apply  tests/shoulda
      insert  Gemfile
      insert  Gemfile
      create  test/test_config.rb
      create  test/test.rake
    skipping  mock component...
    applying  jquery (script)...
       apply  scripts/jquery
      create  public/javascripts/jquery.js
      create  public/javascripts/jquery-ujs.js
      create  public/javascripts/application.js
    applying  haml (renderer)...
       apply  renderers/haml
      insert  Gemfile
    applying  sass (stylesheet)...
       apply  stylesheets/sass
      insert  Gemfile
      insert  app/app.rb
      create  lib/sass_initializer.rb
      create  app/stylesheets
   identical  .components
       force  .components
       force  .components
Bundling application dependencies using bundler...
         run  bundle install from "."
Fetching gem metadata from https://rubygems.org/...........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Using rake (10.1.1) 
Using i18n (0.6.9) 
Using multi_json (1.8.2) 
Using activesupport (3.2.16) 
Using builder (3.0.4) 
Installing activemodel (3.2.16) 
Installing arel (3.0.3) 
Using tzinfo (0.3.38) 
Installing activerecord (3.2.16) 
Using bundler (1.3.5) 
Using tilt (1.4.1) 
Using haml (4.0.4) 
Using rack (1.5.2) 
Using url_mount (0.2.1) 
Using http_router (0.11.0) 
Using mime-types (1.25.1) 
Using polyglot (0.3.3) 
Using treetop (1.4.15) 
Using mail (2.5.4) 
Using rack-protection (1.5.1) 
Using sinatra (1.4.4) 
Using thor (0.17.0) 
Using padrino-core (0.11.4) 
Using padrino-helpers (0.11.4) 
Using padrino-admin (0.11.4) 
Using padrino-cache (0.11.4) 
Using padrino-gen (0.11.4) 
Using padrino-mailer (0.11.4) 
Using padrino (0.11.4) 
Using rack-test (0.6.2) 
Using sass (3.2.13) 
Installing shoulda-context (1.1.6) 
Installing shoulda-matchers (2.4.0) 
Installing shoulda (3.5.0) 
Using sqlite3 (1.3.8) 
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.

=================================================================
sample_blog is ready for development!
=================================================================
$ cd ./sample_blog
=================================================================

This command will generate our basic Padrino project and the print out a nice report of the files generated.

Notice the -b flag in the previous command which automatically instructs bundler to install all dependencies. All we need to do now is cd into our brand new application:

[~/srcpadrino]$ cd sample_blog/
[~/srcpadrino/sample_blog]$ tree
.
|--- Gemfile
|--- Gemfile.lock
|--- Rakefile
|--- app
|   |--- app.rb
|   |--- controllers
|   |--- helpers
|   |--- stylesheets
|   `--- views
|       `--- layouts
|--- config
|   |--- apps.rb
|   |--- boot.rb
|   `--- database.rb
|--- config.ru
|--- lib
|   `--- sass_initializer.rb
|--- public
|   |--- favicon.ico
|   |--- images
|   |--- javascripts
|   |   |--- application.js
|   |   |--- jquery-ujs.js
|   |   `--- jquery.js
|   `--- stylesheets
|--- test
|   |--- test.rake
|   `--- test_config.rb
`--- tmp

14 directories, 15 files

Padrino generators do not lock you into using any particular database, ORM, testing framework, templating engine or javascript library. In fact, when generating an application you can actually tell Padrino which components you would like to use!

[~/srcpadrino/myproject]$ bundle install
Fetching gem metadata from https://rubygems.org/.........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Installing rake (10.1.1) 
Installing i18n (0.6.9) 
Using multi_json (1.8.2) 
Installing activesupport (3.2.16) 
Using addressable (2.3.5) 
Using bcrypt-ruby (3.1.2) 
Using bundler (1.3.5) 
Using data_objects (0.10.13) 
Using dm-core (1.2.1) 
Using dm-aggregates (1.2.0) 
Using dm-constraints (1.2.0) 
Using dm-do-adapter (1.2.0) 
Using dm-migrations (1.2.0) 
Using do_sqlite3 (0.10.13) 
Using dm-sqlite-adapter (1.2.0) 
Using dm-timestamps (1.2.0) 
Using fastercsv (1.5.5) 
Using json (1.8.1) 
Using stringex (1.5.1) 
Using uuidtools (2.1.4) 
Using dm-types (1.2.2) 
Using dm-validations (1.2.0) 
Using tilt (1.4.1) 
Using haml (4.0.4) 
Using rack (1.5.2) 
Using url_mount (0.2.1) 
Using http_router (0.11.0) 
Installing mime-types (1.25.1) 
Using polyglot (0.3.3) 
Using treetop (1.4.15) 
Using mail (2.5.4) 
Installing metaclass (0.0.1) 
Installing mocha (0.14.0) 
Using rack-protection (1.5.1) 
Using sinatra (1.4.4) 
Using thor (0.17.0) 
Using padrino-core (0.11.4) 
Using padrino-helpers (0.11.4) 
Using padrino-admin (0.11.4) 
Using padrino-cache (0.11.4) 
Using padrino-gen (0.11.4) 
Using padrino-mailer (0.11.4) 
Using padrino (0.11.4) 
Using rack-test (0.6.2) 
Installing rr (1.1.2) 
Installing riot (0.12.7) 
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
[~/srcpadrino/myproject]$

Now, the terminal should be inside the root of our newly generated application with all necessary gem dependencies installed. Let us take a closer look at the particularly important generated files before we continue on with development.

  1. Gemfile – Be sure to include any necessary gem dependencies for your app in this file!
  2. app/app.rb – This is the primary configuration file for your core application.
  3. config/apps.rb – This defines which applications are mounted in your project.
  4. config/database.rb – This defines the connection details for your chosen database adapter.

The following important directories are also generated:

  1. app/controllers – This is where the Padrino route definitions should be defined.
  2. app/helpers – This is where helper methods should be defined for your application.
  3. app/views – This should contain your template views to be rendered in a controller.
  4. lib – This should contain any extensions, libraries or other code to be used in your project.
  5. public – This is where images, style sheets and JavaScript files should be stored.
  6. test – This is where your model and controller tests should be stored.
Now, let us examine the config/database.rb file to make sure the database connection settings are correct. For now, the defaults are OK for this tutorial. A sqlite3 database will be used that is stored inside db/sample_blog_development.db.

Let us also setup a few simple routes in our application to demonstrate the Padrino routing system. Let’s go into the app/app.rb file and enter the following routes:

<= Padrino leaves the gun, takes the cannoli
[~/srcpadrino/sample_blog]$ cat Gemfile
source 'https://rubygems.org'

# Distribute your app as a gem
# gemspec

# Server requirements
# gem 'thin' # or mongrel
# gem 'trinidad', :platform => 'jruby'

# Optional JSON codec (faster performance)
# gem 'oj'

# Project requirements
gem 'rake'

# Component requirements
gem 'bcrypt-ruby', :require => 'bcrypt'
gem 'sass'
gem 'haml'
gem 'activerecord', '>= 3.1', :require => 'active_record'
gem 'sqlite3'

# Test requirements
gem 'shoulda', :group => 'test'
gem 'rack-test', :require => 'rack/test', :group => 'test'

# Padrino Stable Gem
gem 'padrino', '0.11.4'

# Or Padrino Edge
# gem 'padrino', :github => 'padrino/padrino-framework'

# Or Individual Gems
# %w(core gen helpers cache mailer admin).each do |g|
#   gem 'padrino-' + g, '0.11.4'
# end

[~/srcpadrino]$ padrino help start
Usage:
  padrino start

Options:
  -a, [--server=SERVER]          # Rack Handler (default: autodetect)
  -h, --host=HOST                # Bind to HOST address.
                                 # Default: 127.0.0.1
  -p, --port=N                   # Use PORT.
                                 # Default: 3000
  -d, [--daemonize]              # Run daemonized in the background.
  -i, [--pid=PID]                # File to store pid.
      [--debug]                  # Set debugging flags.
  -c, [--chdir=CHDIR]            # Change to dir before starting.
  -e, --environment=ENVIRONMENT  # Padrino Environment.
                                 # Default: development
      [--help]                   # Show help usage

Starts the Padrino application (alternatively use 's').
[~/srcpadrino/sample_blog]$ bundle exec  padrino rake -T
=> Executing Rake -T ...
rake ar:abort_if_pending_migrations  # Raises an error if there are pending migrations
rake ar:charset                      # Retrieves the charset for the current environment's database
rake ar:collation                    # Retrieves the collation for the current environment's database
rake ar:create                       # Create the database defined in config/database.yml for the current Padrino.env
rake ar:create:all                   # Create all the local databases defined in config/database.yml
rake ar:drop                         # Drops the database for the current Padrino.env
rake ar:drop:all                     # Drops all the local databases defined in config/database.yml
rake ar:forward                      # Pushes the schema to the next version
rake ar:migrate                      # Migrate the database through scripts in db/migrate and update db/schema.rb by invoking...
rake ar:migrate:down                 # Runs the "down" for a given migration VERSION
rake ar:migrate:redo                 # Rollbacks the database one migration and re migrate up
rake ar:migrate:reset                # Resets your database using your migrations for the current environment
rake ar:migrate:up                   # Runs the "up" for a given migration VERSION
rake ar:reset                        # Drops and recreates the database from db/schema.rb for the current environment and loa...
rake ar:rollback                     # Rolls the schema back to the previous version
rake ar:schema:dump                  # Create a db/schema.rb file that can be portably used against any DB supported by AR
rake ar:schema:load                  # Load a schema.rb file into the database
rake ar:setup                        # Create the database, load the schema, and initialize with the seed data
rake ar:structure:dump               # Dump the database structure to a SQL file
rake ar:translate                    # Generates .yml files for I18n translations
rake ar:version                      # Retrieves the current schema version number
rake db:seed                         # Load the seed data from db/seeds.rb
rake routes[query]                   # Displays a listing of the named routes within a project, optionally only those matched...
rake routes:app[app]                 # Displays a listing of the named routes a given app [app]
rake secret                          # Generate a secret key
rake test                            # Run application test suite
rake test:models                     # Run tests for test:models

Casiano Rodriguez León 2015-01-07