ery 3.hours do
runner "MyModel.some_process"
rake "my:rake:task"
command "/usr/bin/my_great_command"
end
every 1.day, :at => '4:30 am' do
runner "MyModel.task_to_run_at_four_thirty_in_the_morning"
end
every :hour do # Many shortcuts available: :hour, :day, :month, :year, :reboot
runner "SomeModel.ladeeda"
end
every :sunday, :at => '12pm' do # Use any day of the week or :weekend, :weekday
runner "Task.do_something_great"
end
every '0 0 27-31 * *' do
command "echo 'you can use raw cron syntax too'"
end
# run this task only on servers with the :app role in Capistrano
# see Capistrano roles section below
every :day, :at => '12:20am', :roles => [:app] do
rake "app_server:task"
end
它的 DSL 很简单,直接,暴力易懂!
我个人是推荐使用 Resque 的。
上传组件
Paperclip
gem "paperclip", "~> 4.3"
Paperclip 是老牌产品了,也是几乎绝大多数项目都有在用它,它可以帮你处理上传图片,裁减,定义不同的图片尺寸,几乎很完美。(with the support of ImageMagick)
# Rail4
class User < ActiveRecord::Base
has_attached_file :avatar, styles: { medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :avatar, content_type: /\Aimage\/.*\Z/
end
Carrierwave
gem 'carrierwave'
Carrierwave 是后起之秀,功能和 Paperclip 差不多,但它还可以管理除图片之外的东西,而且灵活性更高。
对比
Paperclip 官方只支持 ActiveRecord 但相对稳定, 而 Carrierwave 比较灵活支持更多的 ORM, 比如 mongoid
If you feel adventurous, you can try refile from the creators of carrierwave.
表单
Rails 为我们带来和一改传统的表单构件方式,但是经过实际的使用,我们渐渐发觉这样依然还是不够“敏捷”,我们需要更加简便并具有更细致规范的表单,所以有了 Formtastic ,它用起来比 Rails 默认的 form 更加简洁,但是却具有更多的功能,你可以为每个字段设定 help-text 放到文本框下面,并可以走 I18n 的方式设置语言。simple_form 和 Formtastic 功能类似,但它的写法还要简单一些。
gem 'simple_form'
SimpleForm 在用法上与 Formtastic 类似,同样很好的支持了I18n、表间关联、嵌套表单和表单验证等常用的功能。SimpleForm 从 2.0 开始,在可定制性上有质的突破(Twitter Bootstrap 在里边起了很关键的作用),现在的它更像是一个 Form Builder 框架,可以很方便的对它进行设置和扩展。
看看 simple_form 的用法 :
= simple_form_for @user do |f|
= f.input :username, label: 'Your username please'
= f.input :password, hint: 'No special characters.'
= f.input :email, placeholder: 'user@domain.com'
= f.input :remember_me, inline_label: 'Yes, remember me'
= f.button :submit
gem 'formtastic', '~> 3.0'
Formtastic 的主要缺点在于对HTML输出的可定制性上不够灵活。目前的系统中,想要满足各种表单的需求,就需要在每个表单页写很多重复的代码进行设置,甚至很多页面都在使用 Rails 原生的 Form Builder,这样做维护量太大。
<%= semantic_form_for @post do |f| %>
<%= f.inputs do %>
<%= f.input :title %>
<%= f.input :body %>
<%= f.input :section, :as => :radio %>
<%= f.input :categories %>
<%= f.input :created_at, :as => :string %>
<% end %>
<%= f.actions do %>
<%= f.action :submit, :as => :button %>
<%= f.action :cancel, :as => :link %>
<% end %>
<% end %>
Grape
gem 'grape'
随着 Mobile App 的增多,很多时候我们在做用 Rails 做 API Base 项目时,rails 自带的 C 和 V 层显得过于繁杂,grape 可以帮助我们快速的构建和 Rails 完美融合的 API 接口。
module Twitter
class API < Grape::API
version 'v1', using: :hea