使用嵌套属性形式管理子关系

发布于 2024-07-30 12:53:59 字数 750 浏览 2 评论 0原文

我想做的是:

在任何时候,用户都可以拥有 1 个活动配置文件。 此活动配置文件必须经过管理员授权,以确保其符合站点的规则和规定。 当用户编辑其个人资料时,其公共个人资料不受影响,直到管理员注销其更改为止。 如果他们在审核个人资料时进行编辑,则他们的编辑将应用于待审核的待审核个人资料,并被推到队列的末尾。

我的模型看起来像这样:

class Profile < AR:B
    belongs_to :user
end

class User < AR:B
    has_many :profiles do
        def active
            ...
        end
        def latest
        end
    end
    def profile
        self.profiles.active
    end
end

有一个小问题......用户不应该能够直接编辑配置文件,因为配置文件集合没有公开。 相反,他们编辑用户并显示个人资料字段。

管理这个问题的最佳方法是什么? 目前我正在使用:

accepts_nested_attributes_for :profiles

在用户中,但这看起来很老套。 理想情况下,大部分逻辑都存在于模型中,但我正在考虑的另一件事是使用演示者。

任何想法将不胜感激,如果您需要更多信息作为评论,请告诉我,我将适当更新这篇文章。

What I'm trying to do is the following:

At anyone time a user can have 1 active profile. This active profile must be authorized by an administrator to make sure that it is compliant with the rules and regulations of the site. When a user edits their profile their public profile is unaffected until the administrator signs off their changes. If they make an edit while their profile is in review, their edits are applied to the outstanding profile for review and is pushed to the back of the queue.

My models look something like this:

class Profile < AR:B
    belongs_to :user
end

class User < AR:B
    has_many :profiles do
        def active
            ...
        end
        def latest
        end
    end
    def profile
        self.profiles.active
    end
end

There is one small twist... the user should not be able to edit the profile directly, as the profiles collection is not exposed. Instead they edit their user and the profile fields are shown there.

What's the best way to manage this? Currently I'm using:

accepts_nested_attributes_for :profiles

In the user, but that seems quite hacky. Ideally most of this logic would live in the model, but the other thing I'm flirting with is the use of a presenter.

Any thoughts would be greatly appreciated, let me know if you need more information as a comment and I'll update this post appropriately.

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

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

发布评论

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

评论(2

苍风燃霜 2024-08-06 12:53:59

也许您应该尝试建立从用户到配置文件的两种关系。 一种是他们可以通过您的用户界面进行编辑的,另一种是经过管理员批准的。

它可以像这样工作:

class User < AB:B

has_one :profile #the user-editable one one
has_one :active_profile, :class_name=>"profile" #the one shown

end

通过表单对用户配置文件进行的每个更改都会向管理员显示(使用观察者或者可能只是“after_save”过滤器)。 当它批准后,更改就会转储到 active_profile 中,并显示在某处。

这样,您就可以拥有一个干净的表单界面,并且每当他们再次编辑它时,他们都会看到最新的(但未批准的)个人资料。 您还可以使用 Updated_at 列对队列进行排序,以获得“他们的编辑将应用于未完成的配置文件以供审核,并被推送到队列的后面”功能。

Maybe you should try having two relationships from user to profile. One is the one they can edit through your User interface, and the other is the one that is approved by the administrator.

It could work something like:

class User < AB:B

has_one :profile #the user-editable one one
has_one :active_profile, :class_name=>"profile" #the one shown

end

Each changes on the User profile through the form would then show for the Admin (using and observer or maybe just and "after_save" filter). When it aproves it, the changes are then dumped to the active_profile one, and shown somewhere.

That way, you can have a clean form interface, and whenever they edit it again, they see the latest (but not approved) profile. You can also order the Queue using the updated_at column to gain the "their edits are applied to the outstanding profile for review and is pushed to the back of the queue" funcionality.

血之狂魔 2024-08-06 12:53:59

我将通过让用户模型与上面建议的两个配置文件建立关系来解决此问题。 一份“已批准”配置文件,以及一份进入您的管理队列的编辑配置文件。

但是,为了处理“待处理”配置文件和“已批准”配置文件之间的转换,我建议可能添加状态机来处理转换。 AASM gem 在最近的一个项目中对我很有帮助。 (http://github.com/rubyist/aasm/tree/master),我相信 Edge Rails 也刚刚烘焙了 State Machinage。 (http://github.com/rails/rails/commit/aad5a30bf25d8a3167afd685fc91c99f4f09cc57)

你的模型可能看起来像这样:

class User < AR:B

has_one :active_profile 
has_one :pending_profile

include ActiveRecord:: StateMachine

state_machine do
   state :approved
   state :pending
   state :rejected

   event :update_profile_pending do
    transitions :to => :pending, :from => [:approved], :on_transition => :send_to_admin_queue
  end

   event :update_profile_approved do
    transitions :to => :approved, :from => [:pending], :on_transition => :update_current_profile
   end

   event :update_to_rejected do
    transitions :to => :rejected, :from => [:pending]
  end
end

def send_to_admin_queue
  //method to handlesending pending profiles to admin for approval
end

def update_current_profile
 //method to handle updated profiles with changes
end

end

然后您可以调用 User.update 配置文件挂起! 或 User.update 个人资料已获批准! 在您的配置文件状态之间进行转换,并使用转换回调来处理在活动配置文件和待处理配置文件之间发送编辑数据。

至于将nested_attributes_for与您的实际表单一起使用,我不认为这是一个黑客,我也使用它来更新嵌套属性,并且它工作得很好。 在这种情况下,您可能不需要太多,因为您有 2 个配置文件(一个是公开的,一个是待定的)。

只是一个想法! 在这里大声思考!

I'd go about this by having the user Model have a relationship with two profiles as suggested above as well. One "Approved" profile, and the one for editing that goes into your admin queue.

However, in order to handle transitions between "pending" profiles and "approved" profiles i'd suggest possibly adding in a State Machine to handle the transitions. The AASM gem has been good for me in a recent project. (http://github.com/rubyist/aasm/tree/master), and I believe Edge Rails has just baked State Machinage right in as well. (http://github.com/rails/rails/commit/aad5a30bf25d8a3167afd685fc91c99f4f09cc57)

Your model could look something like this:

class User < AR:B

has_one :active_profile 
has_one :pending_profile

include ActiveRecord:: StateMachine

state_machine do
   state :approved
   state :pending
   state :rejected

   event :update_profile_pending do
    transitions :to => :pending, :from => [:approved], :on_transition => :send_to_admin_queue
  end

   event :update_profile_approved do
    transitions :to => :approved, :from => [:pending], :on_transition => :update_current_profile
   end

   event :update_to_rejected do
    transitions :to => :rejected, :from => [:pending]
  end
end

def send_to_admin_queue
  //method to handlesending pending profiles to admin for approval
end

def update_current_profile
 //method to handle updated profiles with changes
end

end

You could then call User.update profile pending! or User.update profile approved! to transition between your profile states and use the transition callbacks to handle sending the editing data between your active profile and pending profile.

As far as using the nested_attributes_for with your actual form, I don't think it's a hack, I've used it as well to update nested attributes and it'd work fine. In this case though you may not need too since you have 2 profiles (one public, one pending).

Just an idea! Thinking out loud here!

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