Padrino中文指南

生成一个Padrino应用之后,在应用文件夹内没有一个Rakefile(Rake任务);实际上,我们可以使用很多Padrino内建的Rake任务:

padrino rake
# or for a list of tasks
padrino rake -T

如果需要定制任务,可以将其加入下列文件中:

  • your_project/lib/tasks
  • your_project/tasks
  • your_project/test
  • your_project/spec

Padrino会自动搜索这些目录内的任何 *.rake 文件。

Padrino默认会执行一些很有用的任务。

基本任务

同其他一些框架一样,我们使用一些 :environment 任务加载我们在这些 environment 下的一些应用。例如:

# 这是一个定制的任务
# 文件位置:task/version.rake
task :version => :environment do
  puts Padrino.version
end

路由

我们已经提供一个列出应用内所有具名路由的任务。

padrino rake routes

上面命令将会应用内所有具名路由:

Application: core
                 URL  REQUEST  PATH
  (:guides, :search)    GET    /guides/search
   (:guides, :index)    GET    /guides
    (:guides, :show)    GET    /guides/:id

Application: foo
     (:blog, :index)    GET    /blog
      (:blog, :show)    GET    /blog/:id

测试

当使用Padrino进行测试的时候,可以使用内建的 padrino rake test 或者rspec的 padrino rake spec.

padrino rake test # => for bacon, riot, shoulda
padrino rake spec # => for rspec

可以定制 test/test.rake 或者 spec/spec.rake

国际化(I18n)

您可以通过程序自动生成模型的本地化 yml 文件:

padrino rake locale:models

具体细节查看 本地化章节

Orm

Padrino针对不同的对象关系模型(ORM)都有内建的任务支持,例如 DataMapperActiveRecordSequelMongomapper,和 Mongoid 都有针对性的命令

注意:我们针对不同的ORM使用不同的命名空间,因此Padrino可以在一个应用中挂载不同的程序,每个程序都可以使用自己的ORM而互不影响。例如在一个大型应用内,这个程序使用了DataMapper,而另一个程序却使用了 ActiveRecord/MongoMapper/Couch/Sequel。我们就是使用这种方式防止冲突发生。

ActiveRecord任务:

rake ar:abort_if_pending_migrations    # Raises an error if there are pending migrations.
rake ar:auto:upgrade                   # Uses schema.rb to auto-upgrade.
rake ar:charset                        # Retrieves database charset.
rake ar:collation                      # Retrieves databsae collation.
rake ar:create                         # Creates the database as defined in config/database.yml
rake ar:create:all                     # Creates local databases as defined in config/database.yml
rake ar:drop                           # Drops the database for the current Padrino.env
rake ar:drop:all                       # Drops local databases defined in config/database.yml
rake ar:forward                        # Pushes the schema to the next version.
rake ar:migrate                        # Migrates the database through scripts in db/migrate.
rake ar:migrate:down                   # Runs the "down" for a given migration VERSION.
rake ar:migrate:redo                   # Rollbacks current migration and migrates up to version
rake ar:migrate:reset                  # Resets your database using your migrations.
rake ar:migrate:up                     # Runs the "up" for a given migration VERSION NUMBER
rake ar:reset                          # Drops and recreates the database using db/schema.rb.
rake ar:rollback                       # Rolls back the schema to previous schema version.
rake ar:schema:dump                    # Creates a portable db/schema.rb file.
rake ar:schema:load                    # Loads a schema.rb file into the database.
rake ar:schema:to_migration            # Creates a migration from schema.rb
rake ar:schema:to_migration_with_reset # Creates a migration and resets the migrations log.
rake ar:setup                          # Creates the database, loads the schema, and seeds data.
rake ar:structure:dump                 # Dumps the database structure to a SQL file.
rake ar:version                        # Retrieves the current schema version number.

rake ar:auto:upgrade

这是一个对于不爱使用数据版本迁移(例如我)的人来说是超酷和超有用的工具。这有一个复制版本在 auto_migrations

实际上,不写数据版本迁移,也可以直接编辑 schema.rb 然后运行 padrino rake ar:auto:upgrade 来进行一次无伤大雅的数据迁移。

DataMapper任务:

rake dm:auto:migrate           # Performs an automigration (resets your db data)
rake dm:auto:upgrade           # Performs a non destructive automigration
rake dm:create                 # Creates the database
rake dm:drop                   # Drops the database (postgres and mysql only)
rake dm:migrate                # Migrates the database to the latest version
rake dm:migrate:down[version]  # Migrates down using migrations
rake dm:migrate:up[version]    # Migrates up using migrations
rake dm:reset                  # Drops the database, and migrates from scratch
rake dm:setup                  # Create the database migrate and initialize with the seed data

Sequel任务:

rake sq:migrate:auto           # Perform automigration (reset your db data)
rake sq:migrate:to[version]    # Perform migration up/down to VERSION
rake sq:migrate:up             # Perform migration up to latest migration available
rake sq:migrate:down           # Perform migration down (erase all data)

Mongomapper任务:

rake mm:translate              # Generates .yml files for I18n translations

Mongoid任务:

从0.9.21版本开始提供

rake mi:drop                    # Drops all the collections for the database for the current environment
rake mi:create_indexes          # Create the indexes defined on your mongoid models
rake mi:objectid_convert        # Convert string objectids in mongo database to ObjectID type
rake mi:cleanup_old_collections # Clean up old collections backed up by objectid_convert

Seed:

像Rails一样,我们可议使用 db/seeds.rb 来输入数据到数据库,这是一个示例(来自padrino-admin):

email     = shell.ask "Which email do you want use for loggin into admin?"
password  = shell.ask "Tell me the password to use:"

shell.say ""

account = Account.create(:email => email, :password => password,
                         :password_confirmation => password, :role => "admin")

if account.valid?
  shell.say "Perfect! Your account was created."
  shell.say ""
  shell.say "Now you can start your server with padrino start and then login into /admin with:"
  shell.say "   email: #{email}"
  shell.say "   password: #{password}"
  shell.say ""
  shell.say "That's all!"
else
  shell.say "Sorry but some thing went worng!"
  shell.say ""
  account.errors.full_messages.each { |m| shell.say "   - #{m}" }
end