From b1088b75b3aff9bc031ddd1337384246685137fe Mon Sep 17 00:00:00 2001 From: Bohung Date: Tue, 18 May 2021 09:45:52 +0800 Subject: [PATCH] Add album_widget method. --- app/controllers/galleries_controller.rb | 56 +++++++++++++++++++++++++ lib/gallery/engine.rb | 2 +- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/app/controllers/galleries_controller.rb b/app/controllers/galleries_controller.rb index 5050455..682511f 100644 --- a/app/controllers/galleries_controller.rb +++ b/app/controllers/galleries_controller.rb @@ -118,6 +118,62 @@ class GalleriesController < ApplicationController end tmp end + def album_widget + @album_setting = AlbumSetting.first + params = OrbitHelper.params + tags = OrbitHelper.widget_tags.empty? ? ["all"] : OrbitHelper.widget_tags + album_tp = Album.filter_by_widget_categories(OrbitHelper.widget_categories,false).filter_by_tags(tags) + all_count = album_tp.count + page_data_count = OrbitHelper.widget_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 + start_index = (page_data_count - (with_order_count % page_data_count) + page_data_count*(page_no - 1 - with_order_total_pages)) + 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 + galleries = albums.collect.with_index do |a,i| + doc = Nokogiri::HTML(a.description.to_s) + alt_text = doc.text.empty? ? 'gallery image' : doc.text + colors = album_color_map[a.id] + thumb_src = a.cover_path || "/assets/gallery/default.jpg" + cover_image = AlbumImage.find(a.cover) rescue a.album_images.first + image_description = a.description + image_short_description = a.name + { + "album-name" => a.name, + "album-description" => a.description, + "alt_title" => alt_text, + "link_to_show" => OrbitHelper.url_to_show(a.to_param), + "src" => thumb_src.gsub("thumb_",""), + "thumb-src" => thumb_src, + "thumb-large-src" => thumb_src.gsub("thumb_","thumb_large_"), + "mobile-src" => thumb_src.gsub("thumb_","mobile_"), + "theater-src" => thumb_src.gsub("thumb_","theater_"), + "image_description" => image_description, + "image_short_description" => image_short_description, + "album_color" => iterate_data(colors[1],colors[0],@album_setting.album_card_background_color,'transparent'), + "album_card_text_color" => iterate_data(colors[2],@album_setting.album_card_text_color), + "i" => i + } + end + { + "images" => galleries, + "extras" => {"widget-title"=>"Gallery","more_url" => OrbitHelper.widget_more_url} + } + end def widget @album_setting = AlbumSetting.first tags = OrbitHelper.widget_tags.empty? ? ["all"] : OrbitHelper.widget_tags diff --git a/lib/gallery/engine.rb b/lib/gallery/engine.rb index 318a515..d784c06 100644 --- a/lib/gallery/engine.rb +++ b/lib/gallery/engine.rb @@ -35,7 +35,7 @@ module Gallery OrbitApp.registration "Gallery", :type => "ModuleApp" do module_label "gallery.gallery" base_url File.expand_path File.dirname(__FILE__) - widget_methods ["widget"] + widget_methods ["widget","album_widget"] # widget_settings [] widget_settings [{"data_count"=>10}] models_to_cache [:album,:album_image]