Reputation: 191
I have a User model, a group model (Cliq), and a group_membership model (Cliq_Membership). Everything seems to be working fine so far. I currently have it so that when a User creates a group they "own" it and when an "owner" leaves the group (destroys their group membership) the entire group is destroyed. A group has one owner and many members. I want to make it so that a User has to request to be a "member". I want the "owner" to be the only one to see the requests and accept/deny the requests.
For Clarity:
How do you accomplish this?
Here is my code so far:
Models:
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
Controllers:
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
Upvotes: 0
Views: 304
Reputation: 909
You can create another model and controller for handling user requests
create request.rb
model
class Request < ActiveRecord::Base
belongs_to :user
belongs_to :cliq
end
create 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
your routes.rb
resources :groups do
resources :requests, only: [:index, :destroy] do
member do
get 'approv'
end
end
end
Upvotes: 1
Reputation: 394
The following solution should work without creating a new model for requests. Adding a new boolean field to CliqMembership model to store whether a particular cliq_memberhip is confirmed or not is sufficient. (Let's call that field 'confirmed', for example)
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
With this, you can set the value of confirmed
field to false
by default when a new cliq_membership is created by a user. Until the owner update
's that particular cliq_membership to change the value of confirmed
to true
.
Assuming user
& cliq
are instances of User model & Cliq model respectively, you can now use user.confirmed_cliqs
and cliq.confirmed_members
.
Edit:
In order to restrict the edit & update actions on cliq_membership to only the cliq owner, you can use a before filter.
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
Hope it works for you.
Upvotes: 0