基于项目创建使用Enum设置默认角色

时间:2016-03-21 22:57:04

标签: ruby-on-rails ruby-on-rails-4 enums roles pundit

我有一个项目和关系模型来建立一个'以下'用户和项目之间的关系。我为“关系”建立了三个角色。在关系模型中使用枚举...他们是管理员,协作者和访客。但是,我需要根据用户和项目之间的关系建立方式设置默认角色。需要以下简单方案:

(a)创建项目的用户自动关注项目......关系角色应设置为“管理员”。在创建项目时

(b)如果网站访问者只是导航到项目个人资料页面,他们可以点击“关注”按钮。用于建立以下关系的按钮...但是,这应该将关系角色设置为“访问者”

关系模型:

class Relationship < ActiveRecord::Base
  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "Project"
  validates :follower_id, presence: true
  validates :followed_id, presence: true

  enum role: [:admin, :collaborator, :visitor]
  after_initialize :set_default_role, :if => :new_record?

  def set_default_role
    self.role ||= :admin
  end
end

关系控制器:

class RelationshipsController < ApplicationController
  before_filter :authenticate_user!

  def create
    @project = Project.find(params[:relationship][:followed_id])
    current_user.follow!(@project)
    # relationship.visitor! View railsapps documentation for devise pundit#roles
    redirect_to @project
  end

  def destroy
    @project = Project.find(params[:id]).followed
    current_user.unfollow!(@project)
    redirect_to @project
  end

  private

  def relationship_params
    params.require(:relationship).permit(:followed_id, :follower_id)
  end

项目控制器

class ProjectsController < ApplicationController
  before_filter :authenticate_user!, only: [:create, :new, :edit, :update, :delete, :followers]

def create
    @project = current_user.own_projects.build(project_params)
    if @project.save
      if params[:project][:projectimage].present?
        render :crop
      else
        flash[:success] = "You've successfully created a project..."
        redirect_to @project
      end
    else
      render 'new'
    end
  end

  def update
    @project = Project.find(params[:id])
    if @project.update_attributes(project_params)
      if params[:project][:projectimage].present?
        render :crop
      else
        flash[:success] = "Project Created"
        redirect_to @project
        @project.followers << current_user #this establishes the following relationship as soon as a new project is created between user/project
      end
    else
      render 'edit'
    end
  end

end

用户模型:

class User < ActiveRecord::Base
  has_many :own_projects, :class_name=>'Project'

  has_many :projects
  has_many :relationships, foreign_key: "follower_id", dependent: :destroy

  has_many :followed_projects, through: :relationships, source: :followed
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  def following?(some_project)
   relationships.find_by_followed_id(some_project.id)
  end

  def follow!(some_project)
   self.relationships.create!(followed_id: some_project.id)
   relationship.visitor!
  end

end

我需要更改代码才能将默认角色设置为“管理员”。或者访客&#39;根据上面提到的两种情况?

1 个答案:

答案 0 :(得分:1)

正如上面评论中所讨论的,我认为您在创建role时应明确说明Relationship ,因为角色在不固有的条件上有所不同到Relationship班。

现在,由于Relationshiphas many :throughUser模型之间Project关联的中间人,您不能简单地使用标准方式将用户连接到项目使用<<但必须明确构建中间人Relationship ,包括您的自定义参数(例如角色)。

您的解决方案应该类似于posted here

class Project < ActiveRecord::Base
  has_many :relationships, foreign_key: :followed_id, dependent: :destroy
  has_many :users, through: :relationships
end

class User < ActiveRecord::Base
  has_many :relationships, foreign_key: :follower_id, dependent: :destroy
  has_many :projects, through: :relationships

  # this will create the relationship association with the 'visitor' role
  def follow_project!(some_project)
    self.relationships.create!(followed_id: some_project.id, role: :visitor)
    # I think you can even omit the ids here and work with objects:
    # self.relationships.create!(followed: some_project, role: :visitor)
  end

  # this will create the relationship association with the 'admin' role
  def administrate_project!(some_project)
    self.relationships.create!(followed: some_project, role: :admin)
  end

  # this will create the relationship association with the 'collaborator' role
  def collaborate_on_project!(some_project)
    self.relationships.create!(followed: some_project, role: :collaborator)
  end
end

class Relationship < ActiveRecord::Base
  belongs_to :follower, class_name: "User"
  belongs_to :followed, class_name: "Project"

  enum role: [:admin, :collaborator, :visitor]
end

follow_administrate_collaborate_on_project!方法的工作方式相同,但每个方法在关系中设置不同的角色。然后,您只需从控制器中调用适当的一个,例如在创建项目时设置“admin”关系:

class ProjectsController < ApplicationController

  def create        
    # ideally you should wrap multiple separate saves in a transaction so that
    # either they all succeed or all fail
    Project.transaction do
      # this creates a barebone project, without any association
      @project = Project.create!(project_params)
      # this associates the project to the current user with admin role
      current_user.administrate_project!(@project)
      # ...                 
    end
  end

end

请务必仔细阅读rails guides:通过协会。

相关问题