From c028b62f054ee02fbf231933dc0d08bd7e05c992 Mon Sep 17 00:00:00 2001 From: chiu Date: Sat, 25 Apr 2020 11:29:52 +0800 Subject: [PATCH] enable category sort feature --- app/controllers/e_papers_controller.rb | 120 ++++++++++++++++--------- 1 file changed, 77 insertions(+), 43 deletions(-) diff --git a/app/controllers/e_papers_controller.rb b/app/controllers/e_papers_controller.rb index 24f1ab7..5fa3d62 100644 --- a/app/controllers/e_papers_controller.rb +++ b/app/controllers/e_papers_controller.rb @@ -72,27 +72,36 @@ class EPapersController < ApplicationController params = OrbitHelper.params criteria = PaperCriteria.where(:uid => params[:uid]).first if params["category"].present? - papers = criteria.epaper_topics.where(:category_id => params["category"]).group_by(&:category) + papers = criteria.epaper_topics.where(:category_id => params["category"]).group_by(&:category) else - papers = criteria.epaper_topics.group_by(&:category) + papers = criteria.epaper_topics.group_by(&:category) end data = [] - papers.each do |category, topics| - topics_data = Array(topics).compact.collect do |topic| - { - "title" => topic.title, - "link_to_show" => OrbitHelper.url_to_show(topic.to_param), - "description" => topic.description, - "img_url" => topic.image.url, - "img_url_thumb" => topic.image.thumb.url, - "category" => category.title + papers_sorted = get_all_categories.map do |v| + tmp = papers.select{|cat,topics| (cat.id==v.id rescue false)} + if tmp.count==0 + tmp = nil + end + tmp + end.compact + papers_sorted.each do |paper| + paper.each do |category, topics| + topics_data = Array(topics).compact.collect do |topic| + { + "title" => topic.title, + "link_to_show" => OrbitHelper.url_to_show(topic.to_param), + "description" => topic.description, + "img_url" => topic.image.url, + "img_url_thumb" => topic.image.thumb.url, + "category" => category.title + } + end + data << { + "category_title" => category.title, + "category_link" => params["url"] + "/" + params["page"] + "?method=topics&category=#{category.id.to_s}", + "topics" => topics_data } end - data << { - "category_title" => category.title, - "category_link" => params["url"] + "/" + params["page"] + "?method=topics&category=#{category.id.to_s}", - "topics" => topics_data - } end { "categories" => data, @@ -159,21 +168,30 @@ class EPapersController < ApplicationController criteria = PaperCriteria.last papers = criteria.epaper_topics.group_by(&:category) data = [] - papers.each do |category, topics| - topics_data = Array(topics).compact.collect do |topic| - { - "title" => topic.title, - "link_to_show" => OrbitHelper.widget_item_url(topic.to_param), - "description" => topic.description, - "img_url" => topic.image.url, - "img_url_thumb" => topic.image.thumb.url, - "category" => (category.title rescue nil) + papers_sorted = get_all_categories.map do |v| + tmp = papers.select{|cat,topics| (cat.id==v.id rescue false)} + if tmp.count==0 + tmp = nil + end + tmp + end.compact + papers_sorted.each do |paper| + paper.each do |category, topics| + topics_data = Array(topics).compact.collect do |topic| + { + "title" => topic.title, + "link_to_show" => OrbitHelper.widget_item_url(topic.to_param), + "description" => topic.description, + "img_url" => topic.image.url, + "img_url_thumb" => topic.image.thumb.url, + "category" => (category.title rescue nil) + } + end + data << { + "category_title" => (category.title rescue nil), + "topics" => topics_data } end - data << { - "category_title" => (category.title rescue nil), - "topics" => topics_data - } end { "categories" => data, @@ -187,21 +205,30 @@ class EPapersController < ApplicationController criteria = PaperCriteria.last papers = criteria.epaper_topics.group_by(&:category) data = [] - papers.each do |category, topics| - topics_data = Array(topics).compact.collect do |topic| - { - "title" => topic.title, - "link_to_show" => OrbitHelper.widget_item_url(topic.to_param), - "description" => topic.description, - "img_url" => topic.image.url, - "img_url_thumb" => topic.image.thumb.url, - "category" => (category.title rescue nil) + papers_sorted = get_all_categories.map do |v| + tmp = papers.select{|cat,topics| (cat.id==v.id rescue false)} + if tmp.count==0 + tmp = nil + end + tmp + end.compact + papers_sorted.each do |paper| + paper.each do |category, topics| + topics_data = Array(topics).compact.collect do |topic| + { + "title" => topic.title, + "link_to_show" => OrbitHelper.widget_item_url(topic.to_param), + "description" => topic.description, + "img_url" => topic.image.url, + "img_url_thumb" => topic.image.thumb.url, + "category" => (category.title rescue nil) + } + end + data << { + "category_title" => (category.title rescue nil), + "topics" => topics_data } end - data << { - "category_title" => (category.title rescue nil), - "topics" => topics_data - } end { "categories" => data, @@ -244,5 +271,12 @@ class EPapersController < ApplicationController end render :json => data.to_json end - + def get_all_categories + app = ModuleApp.where(key: 'e_paper').first + asc_flag = app.asc rescue true + app.categories.enabled.sort_by do |category| + tmp = category.sort_number.to_i rescue 0 + asc_flag ? tmp : -tmp + end + end end \ No newline at end of file