adding and removing users to/from groups works now

This commit is contained in:
nilsding 2015-01-17 17:50:25 +01:00
parent 2c19ae8aea
commit dd6af3f651
6 changed files with 79 additions and 5 deletions

View File

@ -0,0 +1,29 @@
($ document).on "click", "input[type=checkbox][name=gm-group-check]", ->
box = $(this)
box.attr 'disabled', 'disabled'
groupName = box[0].dataset.group
count = Number $("span##{groupName}-members").html()
boxChecked = box[0].checked
count += if boxChecked then 1 else -1
$.ajax
url: '/ajax/group_membership'
type: 'POST'
data:
user: box[0].dataset.user
group: groupName
add: boxChecked
success: (data, status, jqxhr) ->
if data.success
box[0].checked = if data.checked? then data.checked else !boxChecked
$("span##{groupName}-members").html(count)
showNotification data.message, data.success
error: (jqxhr, status, error) ->
box[0].checked = false
console.log jqxhr, status, error
showNotification "An error occurred, a developer should check the console for details", false
complete: (jqxhr, status) ->
box.removeAttr "disabled"

View File

@ -0,0 +1,43 @@
class Ajax::GroupController < ApplicationController
def membership
@status = :err
@success = false
unless user_signed_in?
@status = :noauth
@message = "requires authentication"
return
end
params.require :user
params.require :group
params.require :add
add = params[:add] == 'true'
group = nil
begin
group = current_user.groups.find_by_name(params[:group])
rescue ActiveRecord::RecordNotFound
@status = :notfound
@message = "Group not found."
return
end
target_user = User.find_by_screen_name(params[:user])
if add
group.add_member target_user if group.members.find_by_user_id(target_user.id).nil?
@checked = true
@message = "Successfully added user to group."
else
group.remove_member target_user unless group.members.find_by_user_id(target_user.id).nil?
@checked = false
@message = "Successfully removed user from group."
end
@status = :okay
@success = true
end
end

View File

@ -6,7 +6,7 @@ class Group < ActiveRecord::Base
before_validation do
self.display_name.strip!
self.name = self.display_name.downcase.sub(/\s+/, '-')
self.name = self.display_name.parameterize
self.name = '-followers-' if self.name == 'followers'
end

View File

@ -0,0 +1,2 @@
json.partial! 'ajax/shared/status'
json.checked @checked

View File

@ -22,15 +22,14 @@
%li.list-group-item
.media
.pull-left.j2-table
%input.input--center{type: :checkbox, name: 'gm-group-check', data: { group_name: group.name }, checked: @user.member_of?(group)}
%input.input--center{type: :checkbox, name: 'gm-group-check', data: { group: group.name, user: @user.screen_name }, checked: @user.member_of?(group), autocomplete: 'off'}
.media-body
.list-group-item-heading= group.display_name
.list-group-item-text.text-muted.j2-up
%span{id: "#{group.name}-members"} x
%span{id: "#{group.name}-members"}= group.members.count
members
.tab-pane{role:"tabpanel", id: "create"}
.modal-body
Nothing here yet :(
.modal-footer
%button.btn.btn-default{"data-dismiss" => "modal", :type => "button"} Cancel
%button.btn.btn-primary{name: 'gm-save', :type => "button"} Save
%button.btn.btn-primary{name: 'gm-save', type: :button, data: { dismiss: :modal }} Done

View File

@ -75,6 +75,7 @@ Rails.application.routes.draw do
match '/create_comment', to: 'comment#create', via: :post, as: :create_comment
match '/destroy_comment', to: 'comment#destroy', via: :post, as: :destroy_comment
match '/report', to: 'report#create', via: :post, as: :report
match '/group_membership', to: 'group#membership', via: :post, as: :group_membership
end
match '/public', to: 'public#index', via: :get, as: :public_timeline