From 82809c20cfa7716ccf92fb2ac84c58f8b6cb147e Mon Sep 17 00:00:00 2001 From: bohung Date: Sun, 4 Dec 2022 00:26:47 +0800 Subject: [PATCH] Fix order bug and add index. --- app/assets/stylesheets/gallery.css | 30 +++++++------ app/controllers/admin/galleries_controller.rb | 33 +++++++------- app/controllers/admin/images_controller.rb | 11 +++-- app/controllers/galleries_controller.rb | 40 ++++------------- app/models/album.rb | 25 +++++++---- app/models/album_image.rb | 43 +++++++++++++----- app/models/album_setting.rb | 1 + config/routes.rb | 44 +++++++++++++++++++ lib/gallery/engine.rb | 11 ----- 9 files changed, 142 insertions(+), 96 deletions(-) diff --git a/app/assets/stylesheets/gallery.css b/app/assets/stylesheets/gallery.css index bdda4c1..5da47c6 100644 --- a/app/assets/stylesheets/gallery.css +++ b/app/assets/stylesheets/gallery.css @@ -73,7 +73,7 @@ color: #F2F2F2; text-shadow: 0px -1px 0 rgba(0,0,0,0.4); letter-spacing: -0.5px; - font-size: 30px; + font-size: 2.5em; font-family: 'Playfair Display SC', sans-serif; line-height: 28px; z-index: 1; @@ -220,7 +220,6 @@ div.rgbody{ display: inline-block; float: left; color: #777; - font-size: 11px; line-height: 30px; cursor: pointer; width: 33%; @@ -382,7 +381,7 @@ div.rgbody{ top: 0; right: 0; margin: 0; - font-size: 23px; + font-size: 1.92em; opacity: 0; -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)"; filter: alpha(opacity=0); @@ -422,7 +421,7 @@ div.rgbody{ text-align: right; text-shadow: 0 1px 0 #ffffff; letter-spacing: -0.1em; - font-size: 12px; + font-size: 1em; font-family: 'Varela Round', sans-serif; line-height: 40px; } @@ -477,7 +476,7 @@ div.rgbody{ } #fileupload #file-list { position: relative; - z-index: 1; + z-index: 4; height: 209px; margin: 2px 0; } @@ -542,13 +541,16 @@ div.rgbody{ .order-edit-notification{ background-color: #ffffd5; z-index: 10; - display: none; - height: 25px; - margin-left: 40%; - position: fixed; - text-align: center; - margin-top: 5px; - top: 85px; - width: 250px; - font-size: 13px; + display: none; + height: 25px; + margin-left: 40%; + position: fixed; + text-align: center; + margin-top: 5px; + top: 85px; + width: 250px; + font-size: 1.08em; +} +.bottomnav{ + z-index: 4; } \ No newline at end of file diff --git a/app/controllers/admin/galleries_controller.rb b/app/controllers/admin/galleries_controller.rb index 678d122..2869877 100644 --- a/app/controllers/admin/galleries_controller.rb +++ b/app/controllers/admin/galleries_controller.rb @@ -275,11 +275,8 @@ class Admin::GalleriesController < OrbitAdminController categories = @module_app.categories.enabled @filter_fields = filter_fields(categories, @tags) @filter_fields.delete(:status) - albums1 = Album.where(:order.in => [nil,-1]).desc(:created_at).with_categories(filters("category")).with_tags(filters("tag")) - albums1 = search_data(albums1,[:name]) - albums2 = Album.all.where(:order.ne=>-1).and(:order.ne=>nil).asc(:order).with_categories(filters("category")).with_tags(filters("tag")) - albums2 = search_data(albums2,[:name]) - @fiter_albums = albums1.concat(albums2) + albums = Album.all.asc(:order).with_categories(filters("category")).with_tags(filters("tag")) + @fiter_albums = search_data(albums,[:name]) end def update_album_setting if params[:album_setting_limit] @@ -331,12 +328,10 @@ class Admin::GalleriesController < OrbitAdminController def show @album = Album.find(params[:id]) - @images = @album.album_images.where(:order.in => [nil,-1]).desc(:created_at) - images = @album.album_images.where(:order.gt => -1).asc(:order) - @images = @images.concat(images) + @images = @album.album_images.asc(:order) image_content = [] @images.each do |image| - image_content << {"id" => image.id.to_s,"title"=> image.title_translations, "description" => image.description_translations,"tags" => image.tags.collect{|t| t.id.to_s}} + image_content << {"id" => image.id.to_s,"title"=> image.title_translations, "description" => image.description_translations,"tags" => image.tag_ids.map(&:to_s)} end @tags = @module_app.tags @photos_data = {"galleries" => image_content}.to_json @@ -414,12 +409,10 @@ class Admin::GalleriesController < OrbitAdminController def get_photoData_json @album = Album.find(params[:id]) - @images = @album.album_images.where(:order => [nil,-1]).desc(:created_at) - images = @album.album_images.where(:order.gt => -1).asc(:order) - @images = @images.concat(images) + @images = @album.album_images.asc(:order) image_content = [] @images.each do |image| - image_content << {"id" => image.id.to_s,"title"=> image.title_translations, "description" => image.description_translations,"tags" => image.tags.collect{|t| t.id.to_s}} + image_content << {"id" => image.id.to_s,"title"=> image.title_translations, "description" => image.description_translations,"tags" => image.tag_ids.map(&:to_s)} end render :json=>{"galleries" => image_content}.to_json end @@ -580,10 +573,14 @@ class Admin::GalleriesController < OrbitAdminController def new_images @album = Album.find(params[:album_id]) if params[:last_image_id].present? - lastimage = AlbumImage.find(params[:last_image_id]) - @newimages = @album.album_images.where(:created_at.gt => lastimage.created_at,:order => -1).desc(:created_at) + lastimage = AlbumImage.find(params[:last_image_id]) rescue nil + if lastimage + @newimages = @album.album_images.where(:order.lt=>lastimage.order).desc(:created_at) + else + @newimages = @album.album_images.where(:id.gt=>params[:last_image_id]).desc(:created_at) + end else - @newimages = @album.album_images.where(:order => [nil,-1]).desc(:created_at) + @newimages = @album.album_images.desc(:created_at) end render :layout=>false end @@ -604,7 +601,7 @@ class Admin::GalleriesController < OrbitAdminController tagsToDestroy = [] tagsToAdd = [] new_tags = params[:album][:tags] || [] - old_tags = album.tags.collect{|t| t.id.to_s} + old_tags = album.tag_ids.map(&:to_s) old_tags.each do |tag| if !new_tags.include?(tag) tagsToDestroy << tag @@ -627,7 +624,7 @@ class Admin::GalleriesController < OrbitAdminController # tagsToAdd will contain all tag ids which ve to be added in tall album_images images = album.album_images images.each do |image| - image_tags = image.tags.collect{|t| t.id.to_s} + image_tags = image.tag_ids.map(&:to_s) tagsToAdd.each do |tag| image_tags << tag end diff --git a/app/controllers/admin/images_controller.rb b/app/controllers/admin/images_controller.rb index dc49e9c..d0a0455 100644 --- a/app/controllers/admin/images_controller.rb +++ b/app/controllers/admin/images_controller.rb @@ -24,23 +24,28 @@ class Admin::ImagesController < OrbitAdminController def changeorder type = params[:type] if type == "imgholder" + album = Album.find(params[:album_id]) rescue nil images = params[:imageids] - images.each_with_index do |image, i| + images.each_with_index.reverse_each do |image, i| img = AlbumImage.find(image) rescue nil if !img.nil? img.order = i img.save end end + if album + album.update(:min_order=>-1) + end elsif type == "orbit_gallery" albums = params[:imageids] - albums.each_with_index do |albumid, i| + albums.each_with_index.reverse_each do |albumid, i| album = Album.find(albumid) rescue nil if !album.nil? album.order = i album.save end end + AlbumSetting.update_all(:min_order=>-1) end render :json => {"success" => true}.to_json end @@ -49,12 +54,12 @@ class Admin::ImagesController < OrbitAdminController images = params['images'] images.each do |image| img = AlbumImage.find(image) + img.destroy begin FileUtils.rm_rf(File.dirname(img.file.path)) rescue => e puts ["can't delete",e] end - img.delete end if params['delete_cover'] == "true" album = Album.find(params['album_id']) diff --git a/app/controllers/galleries_controller.rb b/app/controllers/galleries_controller.rb index 5e78054..cb8efc6 100644 --- a/app/controllers/galleries_controller.rb +++ b/app/controllers/galleries_controller.rb @@ -37,33 +37,10 @@ class GalleriesController < ApplicationController def index @album_setting = AlbumSetting.first - params = OrbitHelper.params - album_tp = Album.filter_by_categories.filter_by_tags - all_count = album_tp.count - page_data_count = OrbitHelper.page_data_count - no_order_count = album_tp.where(:order.in=>[-1,nil]).count - with_order_count = all_count - no_order_count - with_order_total_pages = (with_order_count.to_f / page_data_count).ceil - albums_with_order = album_tp.asc(:order).where(:order.ne=>-1).and(:order.ne=>nil).to_a rescue [] - page_no = (params[:page_no] || 1).to_i - if page_no < with_order_total_pages - albums_no_order = [] - elsif page_no == with_order_total_pages - if albums_with_order.count == page_data_count - albums_no_order = [] - else - albums_no_order = album_tp.desc(:created_at).where(:order.in=>[-1,nil]).page(nil).per(all_count)[0...(page_data_count - albums_with_order.count)] - end - else - albums_with_order = [] - start_index = page_data_count*(page_no - 1 - with_order_total_pages) - if with_order_count != 0 - start_index += (page_data_count - (with_order_count % page_data_count)) - end - albums_no_order = album_tp.desc(:created_at).where(:order.in=>[-1,nil]).page(nil).per(all_count)[start_index...(start_index+page_data_count)].to_a - end - albums = albums_with_order.concat(albums_no_order) - album_color_map = AlbumColor.where(:album_id.in=> albums.map{|v| v.id}).pluck(:album_id,:color,:album_card_background_color,:album_card_text_color).map{|v| [v[0],v[1..-1]]}.to_h + page_no = OrbitHelper.page_number + data_count = OrbitHelper.page_data_count + albums = Album.filter_by_categories.filter_by_tags.asc(:order).page(page_no).per(data_count) + album_color_map = AlbumColor.where(:album_id.in=> albums.pluck(:id)).pluck(:album_id,:color,:album_card_background_color,:album_card_text_color).map{|v| [v[0],v[1..-1]]}.to_h galleries = albums.collect do |a| doc = Nokogiri::HTML(a.description.to_s) alt_text = doc.text.empty? ? 'gallery image' : doc.text @@ -86,7 +63,7 @@ class GalleriesController < ApplicationController { "albums" => galleries, "extras" => {"widget-title"=>"Gallery"}, - "total_pages" => album_tp.total_pages + "total_pages" => albums.total_pages } end def show @@ -132,8 +109,8 @@ class GalleriesController < ApplicationController @album_setting = AlbumSetting.first params = OrbitHelper.params tags = OrbitHelper.widget_tags.empty? ? ["all"] : OrbitHelper.widget_tags - albums = Album.filter_by_widget_categories(OrbitHelper.widget_categories,false).filter_by_tags(tags).to_a - album_color_map = AlbumColor.where(:album_id.in=> albums.map{|v| v.id}).pluck(:album_id,:color,:album_card_background_color,:album_card_text_color).map{|v| [v[0],v[1..-1]]}.to_h + albums = Album.filter_by_widget_categories(OrbitHelper.widget_categories,false).filter_by_tags(tags) + album_color_map = AlbumColor.where(:album_id.in=> albums.pluck(:id)).pluck(:album_id,:color,:album_card_background_color,:album_card_text_color).map{|v| [v[0],v[1..-1]]}.to_h galleries = albums.collect.with_index do |a,i| doc = Nokogiri::HTML(a.description.to_s) alt_text = doc.text.empty? ? 'gallery image' : doc.text.strip @@ -178,7 +155,7 @@ class GalleriesController < ApplicationController album_color_map = AlbumColor.where(:album_id.in=> album_ids).pluck(:album_id,:color,:album_card_background_color,:album_card_text_color).map{|v| [v[0],v[1..-1]]}.to_h params = OrbitHelper.params counts = OrbitHelper.widget_data_count - images = AlbumImage.where({album_id:{"$in"=>album_ids}}).desc(:id).limit(counts *5).sample(counts) + images = AlbumImage.where(:album_id.in=>album_ids).desc(:id).limit(counts *5).sample(counts) images = images.each_with_index.collect do |a,i| colors = album_color_map[a.album_id] || [] alt_text = (a.description.blank? ? "gallery image" : Nokogiri::HTML(a.description).text().strip) @@ -208,7 +185,6 @@ class GalleriesController < ApplicationController end def imgs(album_id) album = Album.find(album_id) - tag_names = Array.new images = album.album_images.asc(:order) output = Array.new images.each do |values| diff --git a/app/models/album.rb b/app/models/album.rb index 5c6408c..b0a435f 100644 --- a/app/models/album.rb +++ b/app/models/album.rb @@ -13,20 +13,29 @@ class Album field :uid, type: String field :rss2_id, type: String field :order, type: Integer, default: -1 + field :min_order, type: Integer, default: -1 field :resize_gravity # has_and_belongs_to_many :tags, :class_name => "GalleryTag" has_many :album_images, :autosave => true, :dependent => :destroy has_many :album_colors, :autosave => true, :dependent => :destroy accepts_nested_attributes_for :album_images, :allow_destroy => true accepts_nested_attributes_for :album_colors, :allow_destroy => true - before_save do |record| - if record.order.nil? || record.order == -1 - topest_order = Album.all.pluck(:order).sort{|a,b| a.to_i<=>b.to_i}.first - if topest_order.nil? || topest_order == 0 - record.order = -2 - else - record.order = topest_order - 1 - end + index({"order" => 1}, { unique: false, background: false }) + before_create do |record| + topest_order = AlbumSetting.pluck(:min_order).first.to_i + if topest_order.nil? || topest_order == 0 + tmp_order = -2 + else + tmp_order = topest_order - 1 + end + record.order = tmp_order + AlbumSetting.update_all(:min_order=>tmp_order) + true + end + after_destroy do + min_order = AlbumSetting.pluck(:min_order).first + if self.order == min_order + AlbumSetting.update_all(:min_order=>min_order + 1) end end def resize_gravity diff --git a/app/models/album_image.rb b/app/models/album_image.rb index 875b164..cca000a 100644 --- a/app/models/album_image.rb +++ b/app/models/album_image.rb @@ -3,14 +3,40 @@ class AlbumImage include Mongoid::Document include Mongoid::Timestamps include OrbitTag::Taggable - mount_uploader :file, GalleryUploader - field :title + mount_uploader :file, GalleryUploader + field :title field :title_translations, type: Hash, default: {} - field :description, type: String, localize: true, default: "" - field :rss2_id, type: String - field :order, type: Integer, default: -1 - - # has_and_belongs_to_many :tags, :class_name => "GalleryTag" + field :description, type: String, localize: true, default: "" + field :rss2_id, type: String + field :order, type: Integer, default: -1 + belongs_to :album + has_many :album_crops, :autosave => true, :dependent => :destroy + accepts_nested_attributes_for :album_crops, :allow_destroy => true + # has_and_belongs_to_many :tags, :class_name => "GalleryTag" + index({"order" => 1}, { unique: false, background: false }) + before_create do + tmp_album = self.album + min_order = nil + if tmp_album + min_order = tmp_album.min_order + min_order = -1 if min_order == 0 + end + if min_order.nil? + self.order = -Time.now.to_i + else + tmp_order = min_order - 1 + self.order = tmp_order + Album.where(:id=>tmp_album.id).update_all(:min_order=>tmp_order) #present call save callback twice due to autosave + end + true + end + after_destroy do + min_order = Album.where(:id=>self.album_id).pluck(:min_order).first + puts "min_order: #{min_order} #{self.order}" + if self.order == min_order + Album.where(:id=>self.album_id).update_all(:min_order=>min_order + 1) + end + end def title_translations tmp = super || {} if tmp == {} @@ -21,7 +47,4 @@ class AlbumImage def title self.title_translations[I18n.locale] end - belongs_to :album - has_many :album_crops, :autosave => true, :dependent => :destroy - accepts_nested_attributes_for :album_crops, :allow_destroy => true end \ No newline at end of file diff --git a/app/models/album_setting.rb b/app/models/album_setting.rb index a87e0f4..9d5c2a5 100644 --- a/app/models/album_setting.rb +++ b/app/models/album_setting.rb @@ -6,4 +6,5 @@ class AlbumSetting ResizeGravities = %w[Center NorthWest North NorthEast West East SouthWest South SouthEast] field :album_card_background_color, default: '' field :album_card_text_color, default: '#000000' + field :min_order, type: Integer, default: -1 end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index ebf3153..fcf9e33 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,48 @@ Rails.application.routes.draw do + if ENV['worker_num']=='0' && File.basename($0) != 'rake' && !Rails.const_defined?('Console') + Thread.new do + if AlbumSetting.count == 0 + album_setting = AlbumSetting.create + else + album_settings = AlbumSetting.all.to_a + album_setting = album_settings.first + if album_settings.count > 1 + album_settings[1..-1].each do |album_setting| + album_setting.destroy + end + end + end + s = Site.first + update_flag = s.respond_to?(:tmp_flags) + need_update = !update_flag || !(s.tmp_flags.include?('ga1')) + if need_update + albums1 = Album.where(:order.in => [nil,-1]).desc(:created_at) + albums2 = Album.all.where(:order.ne=>-1).and(:order.ne=>nil).asc(:order) + fiter_albums = albums1.concat(albums2) + fiter_albums.each_with_index do |album, i| + album.order = i + album.min_order = -1 + album.save + images1 = album.album_images.where(:order.in => [nil,-1]).desc(:created_at).to_a + images2 = album.album_images.where(:order.gt => -1).asc(:order).to_a + images = images1.concat(images2) + images.each_with_index do |image, j| + image.order = i + image.save + end + end + AlbumSetting.update_all(:min_order=>-1) + if update_flag + s = Site.first + s.tmp_flags << 'ga1' + s.save + end + puts "Gallery order fix!" + end + Album.create_indexes + AlbumImage.create_indexes + end + end locales = Site.first.in_use_locales rescue I18n.available_locales scope "(:locale)", locale: Regexp.new(locales.join("|")) do get "/xhr/galleries/theater/:id" => "galleries#theater" diff --git a/lib/gallery/engine.rb b/lib/gallery/engine.rb index 408fbc9..37a1217 100644 --- a/lib/gallery/engine.rb +++ b/lib/gallery/engine.rb @@ -20,17 +20,6 @@ module Gallery rescue => e puts ['error in gallery',e] end - - require File.expand_path('../../../app/models/album_setting', __FILE__) - if defined?(AlbumSetting) - if AlbumSetting.first.nil? - AlbumSetting.create() - elsif AlbumSetting.count > 1 - AlbumSetting.all.to_a[1..-1].each do |album_setting| - album_setting.destroy - end - end - end OrbitApp.registration "Gallery", :type => "ModuleApp" do module_label "gallery.gallery"