首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Rails路由中有没有更好的方法来处理这些约束?

在Rails路由中有没有更好的方法来处理这些约束?
EN

Stack Overflow用户
提问于 2012-05-25 08:09:14
回答 1查看 159关注 0票数 1

我的路由文件的一半以上都被约束(和它们的类)占用了,所以我想知道是否有更好的方法来处理这个问题。如果我添加其他宠物,每个宠物都是一样的-所以文件可能会变得很长。

代码语言:javascript
复制
class AkcConstraint
  TYPES = %w[sporting-group hound-group working-group terrier-group toy-group non-sporting-group herding-group misc-class]
  def self.matches?(request)
    TYPES.include? request.path_parameters[:akc_group]
  end
end

class AnkcConstraint
  TYPES = %w[toy-group terrier-group gundog-group hound-group working-group utility-group non-sporting-group]
  def self.matches?(request)
    TYPES.include? request.path_parameters[:ankc_group]
  end
end

class CkcConstraint
  TYPES = %w[sporting-group hound-group working-group terrier-group toy-group non-sporting-group herding-group]
  def self.matches?(request)
    TYPES.include? request.path_parameters[:ckc_group]
  end
end

class FciConstraint
  TYPES = %w[group-1 group-2 group-3 group-4 group-5 group-6 group-7 group-8 group-9 group-10]
  def self.matches?(request)
    TYPES.include? request.path_parameters[:fci_group]
  end
end

class IkcConstraint
  TYPES = %w[group-1 group-2 group-3 group-4 group-5 group-6 group-7 group-8 group-9 group-10]
  def self.matches?(request)
    TYPES.include? request.path_parameters[:ikc_group]
  end
end

class KcConstraint
  TYPES = %w[hound-group working-group gundog-group terrier-group utility-group pastoral-group toy-group]
  def self.matches?(request)
    TYPES.include? request.path_parameters[:kc_group]
  end
end

class KusaConstraint
  TYPES = %w[hound-group working-group gundog-group terrier-group utility-group pastoral-group toy-group]
  def self.matches?(request)
    TYPES.include? request.path_parameters[:kusa_group]
  end
end

class NzkcConstraint
  TYPES = %w[toy-group terrier-group gundog-group hound-group working-group utility-group non-sporting-group]
  def self.matches?(request)
    TYPES.include? request.path_parameters[:nzkc_group]
  end
end


# For dog groups and types
match 'dogs/akc/:akc_group', :to => "dogs#index", :as => "akc_dogs",
                        :constraints => AkcConstraint
match 'dogs/ankc/:ankc_group', :to => "dogs#index", :as => "ankc_dogs",
                        :constraints => AnkcConstraint
match 'dogs/ckc/:ckc_group', :to => "dogs#index", :as => "ckc_dogs",
                        :constraints => CkcConstraint
match 'dogs/fci/:fci_group', :to => "dogs#index", :as => "fci_dogs",
                        :constraints => FciConstraint
match 'dogs/ikc/:ikc_group', :to => "dogs#index", :as => "ikc_dogs",
                        :constraints => IkcConstraint
match 'dogs/kc/:kc_group', :to => "dogs#index", :as => "kc_dogs",
                        :constraints => KcConstraint
match 'dogs/kusa/:kusa_group', :to => "dogs#index", :as => "kusa_dogs",
                        :constraints => KusaConstraint
match 'dogs/nzkc/:nzkc_group', :to => "dogs#index", :as => "nzkc_dogs",
                        :constraints => NzkcConstraint

我想我最关心的是性能--我应该担心吗?有没有更好的方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-25 08:51:06

假设AkcAnkcCkc等都继承自相同的模型,例如Club (我当然希望它们是这样的),那么您肯定可以使其更简洁,至少,可能更高效。如下所示:

代码语言:javascript
复制
class ClubConstraint
  Types = { # in actuality this should come from your database and not be
            # hard-coded; otherwise it's going to be a big maintenance headache
    :akc  => %w[ sporting-group  hound-group    working-group
                 terrier-group   toy-group      non-sporting-group
                 herding-group   misc-class ],
    # ...
    :nzkc => %w[ toy-group       terrier-group  gundog-group
                 hound-group     working-group  utility-group
                 non-sporting-group ],
  }.freeze

  def matches? request
    pparams = request.path_parameters
    return unless club_groups = Types[ pparams[ :club ] ]

    club_groups.include? pparams[ :group ]
  end
end

# For dog groups and types
match 'dogs/:club/:group', :to => "dogs#index",
      :constraints => ClubConstraint.new

这可能不会像我写的那样工作,因为我还没有测试过它(并且您的:as =>选项需要重新实现),但是您已经明白了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10746932

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档