Skip to content

Instantly share code, notes, and snippets.

@raderj89
Last active December 30, 2015 02:39
Show Gist options
  • Save raderj89/7764357 to your computer and use it in GitHub Desktop.
Save raderj89/7764357 to your computer and use it in GitHub Desktop.
add and remove collaborators solution
<!-- wiki_collaborations/add_or_remove.html.erb -->
<!-- how can this be cleaned up? -->
<div class="row">
<div class="col-sm-6">
<%= form_for wiki, url: wiki_collaborations_path, method: :post do %>
<%= hidden_field_tag :wiki_id, wiki.id %>
<% @users.each do |user| %>
<% next if current_user == user %>
<div class="checkbox">
<%= check_box_tag "user_ids[]", user.id, wiki.users.include?(user) %>
<%= user.username %>
<% if wiki.users.include?(user) %>
<% wiki.wiki_collaborations.each do |wiki_collaboration| %>
<% if user.wiki_collaborations.include?(wiki_collaboration) %>
<%= link_to "| Remove", wiki_collaboration, method: :delete %>
<% end %>
<% end %>
<% end %>
</div>
<% end %>
<%= submit_tag 'Add Collaborators', class: 'btn btn-primary' %>
</div>
<% end %>
</div>
<h1><%= @wiki.title %></h1>
<h4>Published by <%= @wiki.user.username %></h4>
<br>
<%= @wiki.body %>
<hr>
<% if user_signed_in? && @wiki.user == current_user %> <!-- more security needed? -->
<h1>Add Collaborators</h1>
<%= render template: 'wiki_collaborations/add_or_remove', locals: { wiki: @wiki } %>
<% end %>
class WikiCollaborationsController < ApplicationController
before_filter :setup, only: [:create] # Best way to accomplish this?
before_filter :authenticate_user!, only: [:create]
def add_or_remove
end
## TODO: If wiki already has some collaborations and no new values are selected, do not attempt
## a save and flash the correct notification
## TODO: Ajaxify
def create
@user_ids = params[:user_ids]
@user_ids.each do |user_id|
next if @collaborator_ids.include?(user_id.to_i)
@wiki_collaboration = @wiki.wiki_collaborations.new(wiki_id: @wiki, user_id: user_id)
@wiki_collaboration.save
end
flash[:success] = "Collaborators added."
redirect_to @wiki
end
## TODO: Ajaxify
def destroy
@wiki_collaboration = WikiCollaboration.find(params[:id])
if @wiki_collaboration.destroy
flash[:notice] = "User removed."
redirect_to :back
end
end
private
def setup
@wiki = Wiki.find(params[:wiki_id]) # Should this be
# @wiki = current_user.wikis.find(params[:wiki_id]) ?
@wiki_collaborations = @wiki.wiki_collaborations
@wiki_users = @wiki.users
@collaborator_ids = []
@wiki_users.each { |user| @collaborator_ids << user.id }
end
end
class WikisController < ApplicationController
before_filter :authenticate_user!, except: [:show]
def new
@wiki = current_user.wikis.new
end
def create
@wiki = current_user.wikis.build(params[:wiki])
if @wiki.save
flash[:success] = "Wiki created!"
redirect_to @wiki
else
render :new
end
end
def show
@users = User.all # Why do I have to list all users here
# and am unable to do so in
# wiki_collaborations controller?
@wiki = Wiki.find(params[:id])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment