如何制作关注路线DRYer

时间:2014-06-02 08:34:01

标签: ruby-on-rails routes dry

有时我需要一个关注路线作为一个集合,有时候作为一个成员(有时我是have_many画廊,有时候只是has_one

concern :single_galleriable do
  resource :gallery, concerns: :photoable do
    member do
      post :make_feature
    end
  end
end

concern :galleriable do
  resources :gallery, concerns: :photoable do
    member do
      post :make_feature
    end
  end
end

然后我会做

resources :somemodel, concerns: :single_galleriable
显然,太湿了。

根据我的需要,我可以将关注点用作资源或资源,而关注点的内容保持不变吗?

1 个答案:

答案 0 :(得分:1)

不知道这是否有用,但您可以使用routes.rb文件中的方法:

 #config/routes.rb (some of our actual code)

 #Methods have to be kept at the top
 def destroy_all
      collection do
        delete :destroy_all
        delete 'destroy(/:id)', action: 'destroy', as: 'multi_destroy'
      end
 end

 #General stuff
 resources :controller do
     destroy_all
 end

-

这意味着您可以执行以下操作:

 #config/routes.rb

 #Methods have to be kept at the top
 def gallery type = true
      method = type ? "s" : ""
      self.send("resource#{method}") :gallery, concerns: :photoable do
          post :make_feature
      end
 end

 #General
 resources :controller do
     gallery false
 end