diff --git a/announcement.gemspec b/announcement.gemspec index 24c77d8..957d0c8 100644 --- a/announcement.gemspec +++ b/announcement.gemspec @@ -1,6 +1,6 @@ # encoding: UTF-8 $:.push File.expand_path("../lib", __FILE__) - +$:.push File.expand_path("../app/models", __FILE__) # Maintain your gem's version: require "announcement/version" require 'json' @@ -293,7 +293,10 @@ Gem::Specification.new do |s| s.summary = "Announcements for Orbit" s.description = "Announcements for Orbit" s.license = "MIT" - + s.metadata = { + "_require" => "announcement_setting", + "global_hash" => "{enable_manually_sort: (AnnouncementSetting.first.enable_manually_sort rescue false)}" + } s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"] s.test_files = Dir["test/**/*"] diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb index 7280693..4ab867b 100644 --- a/app/controllers/admin/announcements_controller.rb +++ b/app/controllers/admin/announcements_controller.rb @@ -13,6 +13,32 @@ class Admin::AnnouncementsController < OrbitAdminController super @app_title = "announcement" end + + def update_sort + ids = params[:ids] + ids.each_with_index do |id,i| + Bulletin.where(id: id).update(sort_number: i) + end + edit_sort + render 'update_sort',layout: false + + end + + def update_sort_setting + setting = @announcement_setting + setting.update_attributes(settings_params) + if defined?(OrbitHelper::SharedHash) + OrbitHelper::SharedHash['announcement'][:enable_manually_sort] = setting.enable_manually_sort + end + redirect_to edit_sort_admin_announcement_path + end + + def edit_sort + @setting = AnnouncementSetting.first + @announcements = Bulletin.where(is_top: true).order_by({is_top: -1,sort_number: 1,postdate: -1, _id: -1}) + @table_fields = ['announcement.table.sort_number','announcement.table.title','announcement.default_widget.postdate'] + end + def comment_hidden b = BulletinComment.find(params[:id]) rescue nil if !b.nil? diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index 8da40ad..f828850 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -1,5 +1,20 @@ class AnnouncementsController < ApplicationController include AnnouncementsHelper + def initialize + super + @app_title = 'announcement' + @manually_sort = manually_sort + Bulletin.instance_variable_set('@manually_sort',@manually_sort) + #self.request = OrbitHelper.request + end + + def manually_sort + if defined?(OrbitHelper::SharedHash) + OrbitHelper::SharedHash['announcement'][:enable_manually_sort] + else + AnnouncementSetting.first.enable_manually_sort rescue false + end + end def comment @bulletin = Bulletin.where(:uid=>params[:uid]).first comment_val = params['comment'] @@ -207,7 +222,11 @@ class AnnouncementsController < ApplicationController else cats = ["all"] + cats end - anns = anns.sort_by { |a| [ (a["is_top"] ? 1 : 0) , a["postdate"].blank? ? nil : a["postdate"].to_time] }.reverse + if @manually_sort + anns = anns.sort_by { |a| [ (a["is_top"] ? 1 : 0) , -a['sort_number'].to_i, a["postdate"].blank? ? nil : a["postdate"].to_time] }.reverse + else + anns = anns.sort_by { |a| [ (a["is_top"] ? 1 : 0) , a["postdate"].blank? ? nil : a["postdate"].to_time] }.reverse + end end cats = cats.uniq tags = tags.uniq @@ -559,7 +578,11 @@ class AnnouncementsController < ApplicationController if devide_flag feeds_anns = get_feed_announcements("widget",nil,cats,widget_data_count - top_anns.count) top_anns = top_anns + feeds_anns.select{|v| v['is_top'] == true} - top_anns = top_anns.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse + if @manually_sort + top_anns = top_anns.sort_by { |a| tmp=a["postdate"].blank?;[-a['sort_number'].to_i,tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse + else + top_anns = top_anns.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse + end rest_count = widget_data_count - top_anns.count if rest_count <= 0 anns = top_anns.take(widget_data_count) diff --git a/app/helpers/announcements_helper.rb b/app/helpers/announcements_helper.rb index 2f60a21..d5140da 100644 --- a/app/helpers/announcements_helper.rb +++ b/app/helpers/announcements_helper.rb @@ -48,6 +48,7 @@ module AnnouncementsHelper "is_top" => a.is_top, "bulletin_links" => links, "bulletin_files" => files, + "sort_number" => a.sort_number, "title" => a.title, "source-site" => "", "source-site-title" => "", @@ -194,6 +195,7 @@ module AnnouncementsHelper .can_display_and_sorted.is_approved .filter_by_categories(categories,false).filter_by_tags(tags).to_a end + print('announcements',announcements.collect(&:title)) if !(defined? SiteFeed).nil? if @type != "show_widget" feeds_anns = get_feed_announcements("index",nil,categories,page_number*page_data_count) @@ -214,7 +216,17 @@ module AnnouncementsHelper top_anns = announcements.select{|v| v.is_top} + feeds_anns.select{|v| v['is_top']} rest_all_anns = feeds_anns.select{|v| v['is_top'] != true} + announcements.select{|v| !v.is_top} rest_anns = rest_all_anns.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse - all_sorted = top_anns.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse + rest_anns + if @manually_sort + all_sorted = top_anns.sort_by do |a| + tmp = a["postdate"].blank? + [-a['sort_number'].to_i,tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] + end.reverse + rest_anns + else + all_sorted = top_anns.sort_by do |a| + tmp = a["postdate"].blank? + [tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] + end.reverse + rest_anns + end else all_sorted = feeds_anns.select{|v| v['is_top']}.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse + feeds_anns.select{|v| v['is_top'] != true}.sort_by { |a| tmp=a["postdate"].blank?;[tmp ? 0 : 1, tmp ? nil : a["postdate"].to_time] }.reverse end diff --git a/app/models/announcement_setting.rb b/app/models/announcement_setting.rb index f2d4bd3..0cceb59 100644 --- a/app/models/announcement_setting.rb +++ b/app/models/announcement_setting.rb @@ -10,9 +10,10 @@ class AnnouncementSetting field :email_to, type: Array, :default => ["admins","managers","approvers"] field :is_display_edit_only, type: Boolean, :default => false field :only_manager_can_edit_status, type: Boolean, :default => false - field :top_text , type: String , localize: true - field :hot_text , type: String , localize: true - field :hidden_text , type: String , localize: true + field :top_text , type: String , localize: true + field :hot_text , type: String , localize: true + field :hidden_text , type: String , localize: true + field :enable_manually_sort, type: Boolean, default: false has_many :anns_status_settings, :autosave => true, :dependent => :destroy accepts_nested_attributes_for :anns_status_settings, :allow_destroy => true def self.check_limit_for_user(user_id, b_id = nil) @@ -31,4 +32,12 @@ class AnnouncementSetting self.sub_annc_title_trans.blank? ? I18n.t("announcement.table.title") : self.sub_annc_title_trans end end + + before_save do + if self.enable_manually_sort_changed? && self.enable_manually_sort + AnnsCache.all.delete + Bulletin.index({approved: -1,is_hidden: 1,is_preview: 1, is_top: -1,sort_number: 1,postdate: -1,_id: -1,deadline: -1}, { unique: false, background: true }) + Bulletin.create_indexes + end + end end \ No newline at end of file diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index af4886c..eb3328f 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -100,7 +100,7 @@ class Bulletin scope :open_in_future, ->{where(:is_hidden.ne=>true,:is_preview.ne => true,:postdate.gt=>Time.now).order(postdate: :asc)} scope :can_display_and_sorted, ->{where(:approved => true,:is_hidden.ne=>true,:is_preview.ne => true).valid_time_range} - scope :valid_time_range, ->{any_of({:postdate.lte=>Time.now, :deadline.gte=>Time.now},{:postdate.lte=>Time.now, :deadline=>nil},{:postdate=>nil,:deadline.gte=>Time.now},{:postdate=>nil,:deadline=>nil}).order(is_top: :desc,postdate: :desc,id: :desc)} + scope :valid_time_range, ->{any_of({:postdate.lte=>Time.now, :deadline.gte=>Time.now},{:postdate.lte=>Time.now, :deadline=>nil},{:postdate=>nil,:deadline.gte=>Time.now},{:postdate=>nil,:deadline=>nil}).order((@manually_sort ? {is_top: :desc,sort_number: :asc,postdate: :desc,id: :desc} : {is_top: :desc,postdate: :desc,id: :desc}))} scope :is_approved, ->{where(:approved => true)} scope :is_approved_and_show, ->{where(:approved => true,:is_hidden.ne=>true,:is_preview.ne => true)} scope :filter_cats_and_tags, ->(cats,tags) {filter_by_widget_categories(cats,false).filter_by_tags(tags)} @@ -109,6 +109,7 @@ class Bulletin index({postdate: 1}, { unique: false, background: true }) index({is_top: -1,postdate: -1, _id: -1}, { unique: false, background: true }) index({approved: -1,is_hidden: 1,is_preview: 1, is_top: -1,postdate: -1,_id: -1,deadline: -1}, { unique: false, background: true }) + field :sort_number, type: Integer def get_org_model if self.is_preview org_model = nil diff --git a/app/views/admin/announcements/_edit_sort.html.erb b/app/views/admin/announcements/_edit_sort.html.erb new file mode 100644 index 0000000..2bc9766 --- /dev/null +++ b/app/views/admin/announcements/_edit_sort.html.erb @@ -0,0 +1,35 @@ +
<%= th_data.html_safe %> | + <% end %> +||
---|---|---|
+ <%= number_field_tag nil,annc.sort_number,class: 'sort_number',step: 1 %> + | ++ <%= annc.title %> + | ++ <%= format_value annc.postdate %> + | +