如何要求用户请求群组成员身份?

时间:2016-03-20 18:03:03

标签: ruby-on-rails ruby-on-rails-4 activerecord associations

我有一个用户模型,一个组模型(Cliq)和一个group_membership模型(Cliq_Membership)。到目前为止,一切似乎都很好。我目前拥有它,以便当用户创建一个组时,他们拥有"它和#34;所有者"离开小组(破坏他们的小组成员资格)整个小组被摧毁。一个组有一个所有者和许多成员。我想这样做,以便用户必须要求成为"成员"。我想要"所有者"是唯一一个看到请求并接受/拒绝请求的人。

清晰度:

  • 我希望用户必须要求成为小组成员
  • 我希望Cliqs能够要求用户成为会员
  • 只有所有者才能看到/接受/拒绝朋友请求
  • 我希望这种关系是"双向/自我指导&#34 ;;也就是说,我希望用户/成员显示为包含在组中,而组显示为拥有其他成员
  • Cliqs = Groups

你是如何做到这一点的?

到目前为止,这是我的代码:

型号:

class User < ActiveRecord::Base
 has_many :uploads
 has_one :owned_cliq, foreign_key: 'owner_id', class_name: 'Cliq', dependent: :destroy

 has_many :cliq_memberships
 has_many :cliqs, through: :cliq_memberships
end

class CliqMembership < ActiveRecord::Base
 belongs_to :cliq
 belongs_to :user
end

class Cliq < ActiveRecord::Base
 belongs_to :owner, class_name: 'User'

 has_many :cliq_memberships, dependent: :destroy
 has_many :members, through: :cliq_memberships, source: :user

end

控制器:

class CliqMembershipsController < ApplicationController

def create
    #Cliq or Cliq_ID?
    @cliq = Cliq.find(params[:cliq])

    @cliq_membership = current_user.cliq_memberships.build(cliq: @cliq)
    @cliq.members << current_user

    if @cliq_membership.save
        flash[:notice] = "Joined #{@cliq.name}"
    else
        flash[:notice] = "Not able to join Cliq."
    end
    redirect_to cliq_url
end

def destroy
    @cliq_membership = current_user.cliq_memberships.find(params[:id])
    @cliq = @cliq_membership.cliq

    if @cliq.owner == current_user
       @cliq.destroy
       flash[:notice] = "Cliq has been deleted."
       redirect_to current_user
    else
    @cliq_membership.destroy
       flash[:notice] = "You left the Cliq."
    redirect_to current_user
    end
end
end

class CliqsController < ApplicationController

def show
    @cliq = Cliq.find(params[:id])
end

def new
    @cliq = Cliq.new(params[:id])
end

def create
    @cliq = current_user.build_owned_cliq(cliq_params)
    @cliq.members << current_user

    if @cliq.save
        redirect_to current_user
    else
        redirect_to new_cliq_path
    end
end

def destroy
    #@cliq = current_user.owned_cliq.find(params[:id])
    #lash[:alert] = "Are you sure you want to delete your Cliq? Your Cliq and all of its associations will be permanently deleted."
    #@cliq.destroy

    #if @cliq.destroy
       #redirect_to current_user
       #flash[:notice] = "You deleted the Cliq."
    #else
        #redirect_to current_user
        #set up error handler
        #flash[:notice] = "Failed to delete Cliq."
    #end
end


def cliq_params
    params.require(:cliq).permit(:name, :cliq_id)
end
end

2 个答案:

答案 0 :(得分:1)

您可以创建另一个模型和控制器来处理用户请求

创建request.rb模型

class Request < ActiveRecord::Base
 belongs_to :user
 belongs_to :cliq
end

创建requests_controller.rb

class RequestsController < ApplicationController

  before_action :set_group  
  before_action :auth_group_owner 
  before_action :find_request, except: [:index, :create]

  def index
  end

  def create
    @grp.requests.where(user_id: current_user.id).first_or_create
    # redirect the user
  end

  def approv
    # add the user to the group
    @request.destroy
    # redirect
  end

  def destroy
    #delete the request
  end

  private

  def set_group
    #find group @grp
  end

  def auth_group_owner
   if current_user != @grp.owner
    redirect
   end
  end

  def find_request
    #find request
  end

end

您的routes.rb

resources :groups do
  resources :requests, only: [:index, :destroy] do
    member do
      get 'approv'
    end   
  end
end

答案 1 :(得分:0)

以下解决方案应该无需为请求创建新模型。向CliqMembership模型添加新的布尔字段以存储是否确认特定的cliq_memberhip就足够了。 (我们称这个字段为'确认',例如)

class User < ActiveRecord::Base
  has_many :cliq_memberships
  has_many :cliqs, through: :cliq_memberships
  has_many :confirmed_memberships, -> { confirmed }, class_name: "CliqMembership"
  has_many :confirmed_cliqs, through: :confirmed_memberships, source: :cliq
end

class CliqMembership < ActiveRecord::Base
  belongs_to :cliq
  belongs_to :user

  scope :confirmed, -> { where(confirmed: true) }
end

class Cliq < ActiveRecord::Base
  has_many :cliq_memberships, dependent: :destroy
  has_many :members, through: :cliq_memberships, source: :user
  has_many :confirmed_memberships, { confirmed }, class_name: "CliqMembership"
  has_many :confirmed_members, through: :confirmed_memberships, source: :user
end

使用此功能,您可以在用户创建新的cliq_membership时默认将confirmed字段的值设置为false。在所有者update的特定cliq_membership将confirmed的值更改为true之前。

假设user&amp; cliq是用户模型&amp;分别是Cliq模型,您现在可以使用user.confirmed_cliqscliq.confirmed_members

编辑:

为了限制编辑&amp;将cliq_membership上的操作更新为仅限cliq所有者,您可以在过滤器之前使用

class CliqMembershipsController < ApplicationController
  before_action :cliq_owner, only: [:edit, :update]

  def edit
    @cliq_membership = CliqMembership.find(params[:id])
  end

  def update
    @cliq_membership = CliqMembership.find(params[:id])
    @cliq_membership.update_attributes(cliq_membership_params)
  end

  private
    def cliq_membership_params
      params.require(:cliq_membership).permit(:cliq_id, :user_id, :confirmed)
    end

    def cliq_owner
      @cliq = CliqMembership.find(params[:id]).cliq
      redirect_to root_url unless @cliq.owner == current_user
    end
end

希望它适合你。