From dd6af3f651b8468780e9dadb9d8e20560851f137 Mon Sep 17 00:00:00 2001 From: nilsding Date: Sat, 17 Jan 2015 17:50:25 +0100 Subject: [PATCH] adding and removing users to/from groups works now --- app/assets/javascripts/groups.coffee | 29 +++++++++++++ app/controllers/ajax/group_controller.rb | 43 +++++++++++++++++++ app/models/group.rb | 2 +- app/views/ajax/group/membership.json.jbuilder | 2 + .../user/_modal_group_memberships.html.haml | 7 ++- config/routes.rb | 1 + 6 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 app/assets/javascripts/groups.coffee create mode 100644 app/controllers/ajax/group_controller.rb create mode 100644 app/views/ajax/group/membership.json.jbuilder diff --git a/app/assets/javascripts/groups.coffee b/app/assets/javascripts/groups.coffee new file mode 100644 index 00000000..e82954da --- /dev/null +++ b/app/assets/javascripts/groups.coffee @@ -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" \ No newline at end of file diff --git a/app/controllers/ajax/group_controller.rb b/app/controllers/ajax/group_controller.rb new file mode 100644 index 00000000..d17cd402 --- /dev/null +++ b/app/controllers/ajax/group_controller.rb @@ -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 diff --git a/app/models/group.rb b/app/models/group.rb index dcd85d04..68ad2e29 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -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 diff --git a/app/views/ajax/group/membership.json.jbuilder b/app/views/ajax/group/membership.json.jbuilder new file mode 100644 index 00000000..707cf362 --- /dev/null +++ b/app/views/ajax/group/membership.json.jbuilder @@ -0,0 +1,2 @@ +json.partial! 'ajax/shared/status' +json.checked @checked \ No newline at end of file diff --git a/app/views/user/_modal_group_memberships.html.haml b/app/views/user/_modal_group_memberships.html.haml index 1c21e9e4..b3cfddc3 100644 --- a/app/views/user/_modal_group_memberships.html.haml +++ b/app/views/user/_modal_group_memberships.html.haml @@ -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 \ No newline at end of file + %button.btn.btn-primary{name: 'gm-save', type: :button, data: { dismiss: :modal }} Done \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 8920a3af..b71d13d6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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