From f81894af90048a0178dc71387d4a54beb9afc48d Mon Sep 17 00:00:00 2001 From: chris Date: Thu, 26 Sep 2013 18:59:23 +0800 Subject: [PATCH] User ordering Run the task new_ui:save_users --- .../admin/users_new_interface_controller.rb | 26 +++++++++ app/models/user/user.rb | 2 + .../_index_paginator.html.erb | 25 +++++---- .../_user_edit_order.html.erb | 15 +++++ .../users_new_interface/edit_order.html.erb | 56 +++++++++++++++++++ .../users_new_interface/update_order.js.erb | 1 + config/locales/new_ui.en.yml | 2 + config/locales/new_ui.zh_tw.yml | 2 + config/routes.rb | 4 ++ lib/tasks/new_ui.rake | 4 ++ 10 files changed, 125 insertions(+), 12 deletions(-) create mode 100644 app/views/admin/users_new_interface/_user_edit_order.html.erb create mode 100644 app/views/admin/users_new_interface/edit_order.html.erb create mode 100644 app/views/admin/users_new_interface/update_order.js.erb diff --git a/app/controllers/admin/users_new_interface_controller.rb b/app/controllers/admin/users_new_interface_controller.rb index f7a1bdf4..d484dae5 100644 --- a/app/controllers/admin/users_new_interface_controller.rb +++ b/app/controllers/admin/users_new_interface_controller.rb @@ -240,6 +240,32 @@ class Admin::UsersNewInterfaceController < OrbitMemberController end end + + def edit_order + @users = User.all + end + + def update_order + if params[:users].present? + params[:users].values.sort.each do |pair| + to_go = pair[0].to_i + if to_go > 0 + user_at_position = User.where(position: to_go - 1).first + user = User.find(pair[1]) + if user_at_position && !user_at_position == user + if user.position > user_at_position.position + user.move_above(user_at_position) + else + user.move_below(user_at_position) + end + elsif to_go > User.count + user.move_to_bottom + end + end + end + end + @users = User.all + end protected def get_tags diff --git a/app/models/user/user.rb b/app/models/user/user.rb index b726c0f3..90b61f6d 100644 --- a/app/models/user/user.rb +++ b/app/models/user/user.rb @@ -1,6 +1,8 @@ class User include Mongoid::Document + include Mongoid::Tree + include Mongoid::Tree::Ordering # use mongoid-tree because mongoid-ordering needs mongoid(~>3.0) include Mongoid::Timestamps devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable, :validatable #, :timeoutable diff --git a/app/views/admin/users_new_interface/_index_paginator.html.erb b/app/views/admin/users_new_interface/_index_paginator.html.erb index ef222841..058d8f27 100644 --- a/app/views/admin/users_new_interface/_index_paginator.html.erb +++ b/app/views/admin/users_new_interface/_index_paginator.html.erb @@ -1,12 +1,13 @@ -
-
- <% if is_admin? %> - <%= link_to(new_admin_users_new_interface_path,:class=> "btn btn-primary pull-right") do%> - <%= t(:add)%> - <% end -%> - <%end -%> -
- -
+
+
+ <% if is_admin? %> + <%= link_to t(:edit_order), edit_order_admin_users_new_interface_index_path, :class => "btn btn-primary" %> + <%= link_to(new_admin_users_new_interface_path,:class=> "btn btn-primary") do %> + <%= t(:add) %> + <% end -%> + <% end -%> +
+ +
diff --git a/app/views/admin/users_new_interface/_user_edit_order.html.erb b/app/views/admin/users_new_interface/_user_edit_order.html.erb new file mode 100644 index 00000000..35150b5a --- /dev/null +++ b/app/views/admin/users_new_interface/_user_edit_order.html.erb @@ -0,0 +1,15 @@ +<% + if user_edit_order.sex == 'male' + @user_sex = 'gender-man' + elsif user_edit_order.sex == 'female' + @user_sex = 'gender-woman' + else + @user_sex = 'gender-none' + end +%> + + + <%= link_to user_edit_order.position + 1, '#', class: 'edit_position', 'data-user-id' => user_edit_order.id %> + <%= link_to user_edit_order.name, admin_users_new_interface_path(user_edit_order) %> + <%= user_edit_order.email %> + \ No newline at end of file diff --git a/app/views/admin/users_new_interface/edit_order.html.erb b/app/views/admin/users_new_interface/edit_order.html.erb new file mode 100644 index 00000000..3ea93f07 --- /dev/null +++ b/app/views/admin/users_new_interface/edit_order.html.erb @@ -0,0 +1,56 @@ +<% content_for :side_bar do %> + <%= render :partial => 'admin/users_new_interface/side_bar' %> +<% end %> + +
+ + + + + + + + + + + <%= render partial: "user_edit_order", collection: @users %> + +
<%= t(:position) %><%= t(:name) %><%= t(:email) %>
+
+ +
+
+ <%= link_to t(:update_), '#', class: "btn btn-primary pull-right" %> +
+
+ +<% content_for :page_specific_javascript do %> + +<% end %> \ No newline at end of file diff --git a/app/views/admin/users_new_interface/update_order.js.erb b/app/views/admin/users_new_interface/update_order.js.erb new file mode 100644 index 00000000..e9de9566 --- /dev/null +++ b/app/views/admin/users_new_interface/update_order.js.erb @@ -0,0 +1 @@ +$('#list-view table tbody').html("<%= j render partial: 'user_edit_order', collection: @users %>") \ No newline at end of file diff --git a/config/locales/new_ui.en.yml b/config/locales/new_ui.en.yml index b6f072b3..c064a883 100644 --- a/config/locales/new_ui.en.yml +++ b/config/locales/new_ui.en.yml @@ -16,6 +16,7 @@ en: deselect_all: Deselect all detail: Detail edit_category: Edit Categorie + edit_order: Edit order editing: tag: Editing tag file: @@ -37,6 +38,7 @@ en: no_date: No date no_file: No file path: Path + position: Position previous: Previous remove: Remove remove_default: Remove default diff --git a/config/locales/new_ui.zh_tw.yml b/config/locales/new_ui.zh_tw.yml index bb4ee69b..b6bdfd4e 100644 --- a/config/locales/new_ui.zh_tw.yml +++ b/config/locales/new_ui.zh_tw.yml @@ -16,6 +16,7 @@ zh_tw: deselect_all: 取消全選 detail: 細節 edit_category: 編輯類別 + edit_order: 編輯排序 editing: tag: 編輯標籤 file: @@ -37,6 +38,7 @@ zh_tw: no_date: 無日期 no_file: 無檔案 path: 路徑 + position: 排序 previous: 上一頁 remove: 移除 remove_default: 移除預設 diff --git a/config/routes.rb b/config/routes.rb index 2263b3c7..843cff3b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -214,6 +214,10 @@ Orbit::Application.routes.draw do member do get 'temp_edit' end + collection do + get 'edit_order' + post 'update_order' + end match "edit_passwd" => "users_new_interface#edit_passwd" ,:as => :edit_passwd end diff --git a/lib/tasks/new_ui.rake b/lib/tasks/new_ui.rake index 70584a8d..84ed715c 100644 --- a/lib/tasks/new_ui.rake +++ b/lib/tasks/new_ui.rake @@ -91,4 +91,8 @@ namespace :new_ui do end end + task :save_users => :environment do + User.all.each(&:save) + end + end