Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
264 views
in Technique[技术] by (71.8m points)

admin - Rails - How to handle requiring different administrator approval depending on the change being made

Apologies for the terrible title, but I'm having trouble expressing succinctly what I'm trying to do.

I have Accounts, and these accounts can do things like create Projects or Credits or Trades. I need to require administrator approval for any changes to these things. The catch is, all of these models are tagged as belonging to a particular State (as in a US state). I need to have administrator Accounts that belong to each state and are required to authorize any changes to anything that happens in their state. So for example, if you create a project that's in Virginia, I need to have functionality that allows a Virginia administrator to come in and approve the project, or approve any trades happening in Virginia.

The best analogy I can think of is how in a web forum you would have users that are moderators over specific forums but not the whole site.

Is there some way that I can "tag" anything that belongs to this model needs to require that admin's approval?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It'd be worth looking at a gem like cancan.

You can define abilities there which would, I believe, allow you to model what you're describing.

So, for example, you could define your abilities:

class Ability
  include CanCan::Ability

  def initialize(user)
    can :manage, Project, :state_id => user.state_id
  end
end

And you can then check abilities to if your user can perform actions on a project in your controller, say:

if can?(:update, @project)
  ...
else
  flash[:notice] = 'You can only manage projects in your state.'
end

Cancan can do a lot more than just this simple example but that gives you some idea of what's possible.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...