Ruby on rails 使用Grape时,模仿“环绕过滤器”的最佳方式是什么?
我想为我的应用程序接收到的每个请求设置时区,使用Ruby on rails 使用Grape时,模仿“环绕过滤器”的最佳方式是什么?,ruby-on-rails,ruby,grape,Ruby On Rails,Ruby,Grape,我想为我的应用程序接收到的每个请求设置时区,使用application\u controller.rb我这样做了: around_filter :user_time_zone, :if => :current_user def user_time_zone(&block) Time.use_zone(current_user.time_zone, &block) end 但是我很难找到一个与我当前使用Grapegem的应用程序相匹配的应用程序 我唯一发现的似乎是:
application\u controller.rb
我这样做了:
around_filter :user_time_zone, :if => :current_user
def user_time_zone(&block)
Time.use_zone(current_user.time_zone, &block)
end
但是我很难找到一个与我当前使用Grape
gem的应用程序相匹配的应用程序
我唯一发现的似乎是:
但是我想知道是否有更干净的东西,你可以把这种常见的行为放在一个模块中 我是这样做的:
module API
module V1
module Defaults
extend ActiveSupport::Concern
included do
helpers do
def current_user
# ...
end
end
before do
Time.zone = current_user.timezone
end
after do
Time.zone = Rails.configuration.time_zone
end
end
end
end
end
module API
module V1
class Things < Grape::API
include API::V1::Defaults
end
end
end
模块API
模块V1
模块默认值
扩展ActiveSupport::关注点
包括做
帮手
def当前用户
# ...
结束
结束
在做之前
Time.zone=当前用户时区
结束
事后
Time.zone=Rails.configuration.Time\u zone
结束
结束
结束
结束
结束
模块API
模块V1
类事物
module API
module V1
module Defaults
extend ActiveSupport::Concern
included do
helpers do
def current_user
# ...
end
end
before do
Time.zone = current_user.timezone
end
after do
Time.zone = Rails.configuration.time_zone
end
end
end
end
end
module API
module V1
class Things < Grape::API
include API::V1::Defaults
end
end
end