English 中文(简体)
铁路:对于确定影响nes取资源/模型的方法,是否有干的方法?
原标题:Rails: Is there a dry way to define methods that affect nested resources/models?

有理由怀疑,是否有更好的方式来做以下工作:

用户有男性项目

项目清单

清单

某些用户只能获得某些项目。 但是,如果用户能够进入一个项目,那么用户可以查阅该项目的所有清单和项目。

这里,我想清理的方法是:

def check_for_user_access(resource, resource_class, user)
  case resource_class
    when  Project 
      if resource.user == user
        return true
      end
    when  List 
      if resource.project.user == user
        return true
      end
    when  Item 
      if resource.list.project.user == user
        return true
      end
    else
      return false
  end
end

我不想在清单和项目上使用用户——即归属,因为准入实际上与项目挂钩。

这样做的最好办法莫过于在稍有不同之处进行总结的开关说明?

提前感谢!

最佳回答

或许更简单的办法是确定允许准入? 每种资源类别的方法——那么所有控制者都必须问资源,如果用户允许的话。 例如:

class Project < ActiveRecord::Base #I assume
  def allow_access?(user)
    return self.user == user
  end
end

class List < ActiveRecord::Base
  def allow_access?(user)
    return self.project.user == user
  end
end

class Item < ActiveRecord::Base
  def allow_access?(user)
    return self.list.project.user == user
  end
end

并且,选择性地说,在你身上打上了记分。 用户模式:

class User < ActiveRecord::Base
  def can_access?(resource)
    resource.allow_access?(self)
  end
end

那么,你的方法可以替换为

user.can_access?(resource)

如果你想把事情紧缩起来,你会 用户模式可以检查,看看资源是否适合进入? ......

问题回答

两点:

(1) 鉴于你的情况,我考虑在清单和项目上添加“密码”:用户

在您的名单上和项目模式中重复这样做,不会伤害任何东西,而且会给你一个更加简单的方法,看看你在座的任何物体属于谁。

(2) 应检查CanCan。 它是一个真正简单、轻重的授予图书馆,旨在规范与你一样的获取。





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签