AASM:验证哪个对象正在推动状态前进

发布于 2024-08-02 21:04:19 字数 1084 浏览 5 评论 0原文

我正在使用 http://elitists.textdriven.com/svn/plugins/ 的 AASM acts_as_state_machine/trunk

在我的示例中,我有一个空手道道场 Rails 站点。在该网站上,教师可以管理他们所指导的课程,并将学生转移到下一个逻辑带。

我的“学生”模型使用 AASM 进行腰带级数,其定义如下:

class Student < ActiveRecord::Base
  acts_as_state_machine :initial => :White_Belt
  state :White_Belt
  state :Yellow_Belt
  state :Green_Belt
  state :Purple_Belt
  state :Brown_Belt
  state :Black_Belt

  event :Graduate do
    transitions :from => :White_Belt, :to => :Yellow_Belt
    ...
    transitions :from => :Brown_Belt, :to => :Black_Belt
  end
end

...教师模型定义如下...

class Teacher < ActiveRecord::Base
   def Promote_Student(pupil)
      pupil.Graduate!
   end
end

有没有办法确保只有教师才能调用“Student.Graduate!”?我见过“:guard”命令,但似乎我只能具有检查当前对象(学生)的函数,而不能具有检查调用该函数的对象(教师)的函数。

看来我无法向我的事件添加参数,例如...

event :Gradate(teacher_id) do
   ...
end

...这将是理想的。

I'm using AASM from http://elitists.textdriven.com/svn/plugins/acts_as_state_machine/trunk

In my example, I have a Karate dojo rails site. On the site, Teachers can manage the classes they instruct and move their students to the next logical belt.

My "Student" model use AASM for belt progression and it's defined like this:

class Student < ActiveRecord::Base
  acts_as_state_machine :initial => :White_Belt
  state :White_Belt
  state :Yellow_Belt
  state :Green_Belt
  state :Purple_Belt
  state :Brown_Belt
  state :Black_Belt

  event :Graduate do
    transitions :from => :White_Belt, :to => :Yellow_Belt
    ...
    transitions :from => :Brown_Belt, :to => :Black_Belt
  end
end

...and the Teacher model is defined like this...

class Teacher < ActiveRecord::Base
   def Promote_Student(pupil)
      pupil.Graduate!
   end
end

Is there a way ensure that only Teachers can call "Student.Graduate!"? I've seen ":guard" command, but that seems that I can only have functions that check the current object (the Student) and not the object that called the function (the Teacher).

It also appears that I can't add a param to my event like...

event :Gradate(teacher_id) do
   ...
end

...which would be ideal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

格子衫的從容 2024-08-09 21:04:19

我认为只要您编码正确,就不需要这种强制措施。您可以在代码中自行强制执行此操作。

您可以编写一个函数,例如 Teacher#promote(student),然后在毕业生事件上写一些注释,说明不要直接调用它,而是调用 Teacher#promote。

在方法和符号上也首选不使用首字母大写的驼峰命名法。

I think this kind of enforcement is not needed provided that you code correctly. You enforce this by yourself in the code.

You can write a function such as Teacher#promote(student), and then write some comments on the graduate event stating not to call it directly, but calling Teacher#promote instead.

also camel_case whitout first letter capitalization is preferred on methods and symbols.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文