refactoring javascript and controller

- move to contacts.js
- use json
- move to aspects_controller
- add route
- rewrite test
- fix css in chrome
This commit is contained in:
Benjamin Neff 2015-04-26 02:25:49 +02:00
parent cee4f1c3cd
commit 3c09756417
10 changed files with 37 additions and 46 deletions

View file

@ -22,6 +22,8 @@ app.pages.Contacts = Backbone.View.extend({
this.aspectCreateView = new app.views.AspectCreate({ el: $("#newAspectContainer") });
this.aspectCreateView.render();
this.setupAspectSorting();
},
toggleChatPrivilege: function() {
@ -73,6 +75,18 @@ app.pages.Contacts = Backbone.View.extend({
searchContactList: function(e) {
this.stream.search($(e.target).val());
},
setupAspectSorting: function() {
$("#aspect_nav .nav").sortable({
items: "li.aspect[data-aspect-id]",
update: function() {
var data = JSON.stringify({ ordered_aspect_ids: $(this).sortable("toArray", { attribute: "data-aspect-id" }) });
$.ajax("/aspects/order", { type: "put", dataType: "text", contentType: "application/json", data: data });
},
revert: true,
helper: "clone"
});
}
});
// @license-end

View file

@ -1,17 +0,0 @@
/* Copyright (c) 2010-2011, Diaspora Inc. This file is
* licensed under the Affero General Public License version 3 or later. See
* the COPYRIGHT file.
*/
$(document).ready(function() {
$('#aspect_nav .nav').sortable({
items: "li.aspect[data-aspect-id]",
update: function(event, ui) {
var order = $(this).sortable("toArray", {attribute: "data-aspect-id"}),
obj = { 'aspect_order': order };
$.ajax('/user', { type: 'put', dataType: 'text', data: obj });
},
revert: true,
helper: 'clone'
});
});

View file

@ -43,7 +43,6 @@
//= require_tree ./widgets
//= require view
//= require aspects-dropdown
//= require aspects-sorting
//= require mentions
//= require bootstrap-tooltip
//= require bootstrap-popover

View file

@ -64,6 +64,7 @@
#aspect_nav {
li.aspect > a { padding-left: 30px; }
li.new_aspect > a { padding-left: 30px; }
li.aspect.ui-sortable-handle.ui-sortable-helper { background: #FFF; }
}
#no_contacts {

View file

@ -64,6 +64,13 @@ class AspectsController < ApplicationController
render :json => { :id => @aspect.id, :name => @aspect.name }
end
def update_order
params[:ordered_aspect_ids].each_with_index do |id, i|
current_user.aspects.find(id).update_attributes(order_id: i)
end
render nothing: true
end
def toggle_chat_privilege
@aspect = current_user.aspects.where(:id => params[:aspect_id]).first

View file

@ -20,9 +20,7 @@ class UsersController < ApplicationController
password_changed = false
@user = current_user
if aspect_order = params[:aspect_order]
@user.reorder_aspects(aspect_order)
elsif u = user_params
if u = user_params
# change email notifications
if u[:email_preferences]

View file

@ -486,14 +486,6 @@ class User < ActiveRecord::Base
end
end
def reorder_aspects(aspect_order)
i = 0
aspect_order.each do |id|
self.aspects.find(id).update_attributes({ :order_id => i })
i += 1
end
end
# Generate public/private keys for User and associated Person
def generate_keys
key_size = (Rails.env == 'test' ? 512 : 4096)

View file

@ -63,6 +63,7 @@ Diaspora::Application.routes.draw do
get "commented" => "streams#commented", :as => "commented_stream"
get "aspects" => "streams#aspects", :as => "aspects_stream"
put "aspects/order" => "aspects#update_order"
resources :aspects do
put :toggle_contact_visibility
put :toggle_chat_privilege

View file

@ -96,6 +96,19 @@ describe AspectsController, :type => :controller do
end
end
describe "update_order" do
it "updates the aspect order" do
@alices_aspect_1.update_attributes(order_id: 10)
@alices_aspect_2.update_attributes(order_id: 20)
ordered_aspect_ids = [@alices_aspect_2.id, @alices_aspect_1.id]
put(:update_order, ordered_aspect_ids: ordered_aspect_ids)
expect(Aspect.find(@alices_aspect_1.id).order_id).to eq(1)
expect(Aspect.find(@alices_aspect_2.id).order_id).to eq(0)
end
end
describe "#destroy" do
before do
@alices_aspect_1 = alice.aspects.create(name: "Contacts")

View file

@ -121,23 +121,6 @@ describe UsersController, :type => :controller do
expect(response.status).to eq(204)
end
describe 'aspect_order' do
it 'updates the aspect order' do
aspect_order = []
@user.aspects.each do |aspect|
aspect.update_attributes({ :order_id => nil })
aspect_order.push(aspect.id)
end
put(:update, :id => @user.id, :aspect_order => aspect_order)
@user.reload
@user.aspects.each do |aspect|
expect(aspect.order_id).not_to eq(nil)
end
end
end
describe 'password updates' do
let(:password_params) do
{:current_password => 'bluepin7',