From 3ee5ecd592ce30b1bc8ccaf8a8751d96a86f50ef Mon Sep 17 00:00:00 2001 From: Harry Bomrah Date: Tue, 10 May 2016 01:12:43 +0800 Subject: [PATCH] added order for faq --- app/assets/stylesheets/admin/faqs.css | 83 ++++++++++++++- app/controllers/admin/faqs_controller.rb | 25 +++++ app/controllers/faqs_controller.rb | 2 +- app/models/qa.rb | 3 +- app/views/admin/faqs/order.html.erb | 123 +++++++++++++++++++++++ config/locales/en.yml | 1 + config/locales/zh_tw.yml | 1 + config/routes.rb | 4 +- lib/faq/engine.rb | 8 +- 9 files changed, 242 insertions(+), 8 deletions(-) create mode 100644 app/views/admin/faqs/order.html.erb diff --git a/app/assets/stylesheets/admin/faqs.css b/app/assets/stylesheets/admin/faqs.css index afad32d..c2685a9 100644 --- a/app/assets/stylesheets/admin/faqs.css +++ b/app/assets/stylesheets/admin/faqs.css @@ -1,4 +1,79 @@ -/* - Place all the styles related to the matching controller here. - They will automatically be included in application.css. -*/ + /*reset*/ +h1, h2, h3, h4, h5, p{ + margin: 0; + padding: 0; +} +br{ + display: block; + margin: 0 0 5px; +} +ol, ul{ + margin: 0; + padding: 0; +} +li{ + list-style: none; +} +body > img { + display: none; +} + +img{ + border: none; + padding: 0; + margin: 0; +} + +a { + outline: none; + text-decoration: none !important; +} + +table, tr, td{ + padding: 0; + border-spacing: 0; +} + +/*reset end *//* CSS Document */ + +#web_resource_order_table{ + border-collapse: unset !important; + position: relative; + width: 593px; + margin: 40px auto; +} + +#web_resource_order_table th{ + background-color: #454545; + color: #fff; +} +.position-text{ + position: relative; + width: 85px; +} + +#web_resource_order_table .editable-input{ + position: relative; + width: 25px; + margin-bottom: 0; + padding: 0px 0px 0px 5px; +} + +.position-text-div{ + height: 22px; +} + +.order-edit-notification{ + background-color: #ffffd5; + z-index: 10; + display: none; + height: 25px; + left: 40%; + position: fixed; + text-align: center; + margin-top: 5px; + top: 85px; + width: 400px; + font-size: 13px; +} + diff --git a/app/controllers/admin/faqs_controller.rb b/app/controllers/admin/faqs_controller.rb index 1cf21a6..72733fa 100644 --- a/app/controllers/admin/faqs_controller.rb +++ b/app/controllers/admin/faqs_controller.rb @@ -60,6 +60,31 @@ class Admin::FaqsController < OrbitAdminController redirect_to admin_faqs_path end + def order + empty_position_qas = Qa.where(:order_position => nil) + if empty_position_qas.count > 0 + max_position = Qa.max(:order_position) + max_position = 0 if max_position.nil? + empty_position_qas.each_with_index do |epl,i| + epl.order_position = i + max_position + epl.save + end + end + @qas = Qa.all.asc(:order_position) + end + + def updateorder + ids_with_order = params[:order] + ids_with_order.each_with_index do |id,index| + qa = Qa.find(id) rescue nil + if !qa.nil? + qa.order_position = index + qa.save + end + end + render :json => {"success" => true}.to_json + end + private def setup_vars @module_app = ModuleApp.where(:key => "faq").first diff --git a/app/controllers/faqs_controller.rb b/app/controllers/faqs_controller.rb index 53797fe..8fe5c45 100644 --- a/app/controllers/faqs_controller.rb +++ b/app/controllers/faqs_controller.rb @@ -1,6 +1,6 @@ class FaqsController < ApplicationController def index - faqs = Qa.can_display.order_by(:created_at=>'desc').filter_by_categories + faqs = Qa.can_display.order_by(:order_position => "asc").filter_by_categories f = faqs.collect do |qa| statuses = qa.statuses_with_classname.collect do |status| { diff --git a/app/models/qa.rb b/app/models/qa.rb index 52a417c..2212203 100644 --- a/app/models/qa.rb +++ b/app/models/qa.rb @@ -16,7 +16,8 @@ class Qa field :create_user_id field :update_user_id field :uid, type: String - + field :order_position, type: Integer, default: -1 + has_many :qa_links, :autosave => true, :dependent => :destroy has_many :qa_files, :autosave => true, :dependent => :destroy diff --git a/app/views/admin/faqs/order.html.erb b/app/views/admin/faqs/order.html.erb new file mode 100644 index 0000000..3eda35a --- /dev/null +++ b/app/views/admin/faqs/order.html.erb @@ -0,0 +1,123 @@ +<% content_for :page_specific_css do %> + <%= stylesheet_link_tag "admin/faqs" %> +<% end %> +
Please click "Save Order" button when you are done.
+ + + + + + + + <% @qas.each_with_index do |qa,i| %> + + + + + <% end %> + +
PositionLink
+
<%= (i + 1).to_s %>
+
+ +
+
+ + +
+ \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index e9cdb00..afcb684 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3,6 +3,7 @@ en: faq: all: All new: New + order: Order default_widget: question: Question title: Question diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index aa932e8..fdd81c7 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -1,6 +1,7 @@ zh_tw: faq: + order: Order default_widget: question: 問題 title: 問題 diff --git a/config/routes.rb b/config/routes.rb index 5f859dc..51e4ec0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,9 @@ Rails.application.routes.draw do locales = Site.find_by(site_active: true).in_use_locales rescue I18n.available_locales scope "(:locale)", locale: Regexp.new(locales.join("|")) do namespace :admin do - resources :faqs + get "/faqs/order" => "faqs#order" + post "/faqs/updateorder" => "faqs#updateorder" + resources :faqs end end end diff --git a/lib/faq/engine.rb b/lib/faq/engine.rb index f95a429..4d1d3a0 100644 --- a/lib/faq/engine.rb +++ b/lib/faq/engine.rb @@ -38,10 +38,16 @@ module Faq :active_for_category => 'Faq', :available_for => 'managers' + context_link 'faq.order', + :link_path=>"admin_faqs_order_path" , + :priority=>4, + :active_for_action=>{'admin/faqs'=>'order'}, + :available_for => 'managers' + context_link 'tags', :link_path=>"admin_module_app_tags_path" , :link_arg=>"{:module_app_id=>ModuleApp.find_by(:key=>'faq').id}", - :priority=>4, + :priority=>5, :active_for_action=>{'admin/faqs'=>'tags'}, :active_for_tag => 'Faq', :available_for => 'managers'