From 5c7dfb0d53b5fe0f4a1f555142bc709a581e9f89 Mon Sep 17 00:00:00 2001 From: chiu Date: Tue, 21 Jan 2020 22:31:59 +0800 Subject: [PATCH] add option for user can change image version at subpart --- app/controllers/announcements_controller.rb | 28 ++++++++++++++++++++- config/locales/en.yml | 4 +++ config/locales/zh_tw.yml | 4 +++ lib/announcement/engine.rb | 21 ++++++++++++++-- 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb index a3f9d5e..03661d3 100644 --- a/app/controllers/announcements_controller.rb +++ b/app/controllers/announcements_controller.rb @@ -223,6 +223,25 @@ class AnnouncementsController < ApplicationController def pack_data(announcements, top_anns = []) page = Page.where(:module => "announcement").first rescue nil + subpart = OrbitHelper.get_current_widget + @image_version = 'thumb' + if subpart.methods.include? 'select_options'.to_sym + ModuleApp.all.select{|tmp| tmp.key.to_s=='announcement'}.each do |modile_app| + @show_options = modile_app.show_options rescue nil + end + subpart.select_options.each do |select_option| + if !(@show_options.nil?) && select_option.field_name == @show_options.keys.first.to_s + value = YAML.load(select_option.value) + if value[I18n.locale] == t('announcement.small_size') + @image_version = 'thumb' + elsif value[I18n.locale] == t('announcement.medium_size') + @image_version = 'mobile' + elsif value[I18n.locale] == t('announcement.orignal_size') + @image_version = 'orignal' + end + end + end + end feeds_anns = get_feed_announcements("widget") announcements = announcements + feeds_anns if !feeds_anns.blank? @@ -250,6 +269,13 @@ class AnnouncementsController < ApplicationController desc = (desc.nil? || desc == "" ? "announcement image" : desc) link_to_show = a.is_external_link ? a.external_link : OrbitHelper.widget_item_url(a.to_param) target = a.is_external_link ? "_blank" : "_self" + if @image_version == 'thumb' + image_url = a.image.thumb.url + elsif @image_version == 'mobile' + image_url = a.image.mobile.url + else + image_url = a.image.url + end anns << { "bulletin_links" => links, "bulletin_files" => files, @@ -264,7 +290,7 @@ class AnnouncementsController < ApplicationController "author" => author, "link_to_show" => link_to_show, "target" => target, - "img_src" => a.image.thumb.url || "/assets/announcement-default.jpg", + "img_src" => image_url || "/assets/announcement-default.jpg", "img_description" => desc } else diff --git a/config/locales/en.yml b/config/locales/en.yml index 550dd01..96ee16e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3,6 +3,10 @@ en: feed: Feed import: Import announcement: + picture_showing_size: Picture Showing Size + orignal_size: Original Size + small_size: Small Size + medium_size: Medium Size table: title : Title date : Date diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 5980d73..6a8cbd9 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -3,6 +3,10 @@ zh_tw: feed: 供給 import: 匯入 announcement: + picture_showing_size: 圖片顯示大小 + orignal_size: 原圖大小 + small_size: 小張縮圖 + medium_size: 中等縮圖 table: title : 標題 date : 張貼日期 diff --git a/lib/announcement/engine.rb b/lib/announcement/engine.rb index cf06edf..1fe0061 100644 --- a/lib/announcement/engine.rb +++ b/lib/announcement/engine.rb @@ -1,7 +1,20 @@ require "yaml" module Announcement class Engine < ::Rails::Engine - initializer "announcement" do + initializer "announcement" do + translate_data = Dir["#{Announcement::Engine.root}/config/locales/*.yml"] .map{|yaml_file| YAML.load(File.read(yaml_file))} + data = {} + key1 = {} + value1 = {} + value2 = {} + value3 = {} + translate_data.each do |t_data| + key1[t_data.keys[0]] = t_data.values[0]['announcement']['picture_showing_size'] + value1[t_data.keys[0]] = t_data.values[0]['announcement']['small_size'] + value2[t_data.keys[0]] = t_data.values[0]['announcement']['medium_size'] + value3[t_data.keys[0]] = t_data.values[0]['announcement']['orignal_size'] + end + data[key1] = [value1,value2,value3] OrbitApp.registration "Announcement", :type => "ModuleApp" do module_label "announcement.announcement" base_url File.expand_path File.dirname(__FILE__) @@ -13,7 +26,11 @@ module Announcement frontend_enabled feeds_url "/xhr/announcements/feeds" data_count 1..30 - + begin + show_options data + rescue => e + puts ['there_was_no_show_option_method',e] + end side_bar do head_label_i18n 'announcement.announcement', icon_class: "icons-megaphone" available_for "users"