diff --git a/app/controllers/admin/news_controller.rb b/app/controllers/admin/news_controller.rb index d214b0d..32e8d35 100644 --- a/app/controllers/admin/news_controller.rb +++ b/app/controllers/admin/news_controller.rb @@ -112,6 +112,12 @@ class Admin::NewsController < OrbitAdminController def update uid = params[:id].split('-').last news_bulletin = NewsBulletin.where(:uid=>uid).first rescue nil + NewsBulletin.where(:copy_id=>news_bulletin.id.to_s).destroy + news_bulletin.is_edit = true + news_bulletin.save + set_approved = news_bulletin.is_preview + news_bulletin = news_bulletin.get_org_model + news_bulletin.approved = true if set_approved news_bulletin_params[:tags] = news_bulletin_params[:tags].blank? ? [] : news_bulletin_params[:tags] news_bulletin_params[:email_member_ids] = news_bulletin_params[:email_member_ids].blank? ? [] : news_bulletin_params[:email_member_ids] @@ -207,6 +213,7 @@ class Admin::NewsController < OrbitAdminController news_bulletin_data.delete('news_bulletin_files_attributes') news_bulletin_data.delete('news_bulletin_links_attributes') news_bulletin.update_attributes(news_bulletin_data) + news_bulletin.copy_id = params['news_bulletin_id'] else news_bulletin = NewsBulletin.new(news_bulletin_params) end @@ -215,7 +222,18 @@ class Admin::NewsController < OrbitAdminController news_bulletin.save render :text=>page_for_news_bulletin(news_bulletin) end - + def get_preview_action + news_bulletin = NewsBulletin.find_by(:uid=>params['uid']) + is_not_edit = (!news_bulletin.is_edit) + org_bulletin = news_bulletin.get_org_model + NewsBulletin.where(:copy_id=>org_bulletin.id.to_s).destroy + if is_not_edit + news_bulletin.destroy + render :json=> {:success=>true,:action=>"close"} + else + render :json=> {:success=>true,:action=>"redirect",:path=>edit_admin_news_path(:id=>org_bulletin.id)} + end + end def destroy_preview news_bulletin = NewsBulletin.find_by(:uid=>params['uid']) if news_bulletin.is_preview diff --git a/app/models/news_bulletin.rb b/app/models/news_bulletin.rb index f98f27d..7d1e9d7 100644 --- a/app/models/news_bulletin.rb +++ b/app/models/news_bulletin.rb @@ -9,6 +9,9 @@ class NewsBulletin include OrbitCategory::Categorizable include Slug + field :is_edit, type: Boolean, default: false #use to check whether the preview record changed + field :copy_id + field :title, as: :slug_title, type: String, localize: true field :subtitle, localize: true field :text, localize: true @@ -57,6 +60,18 @@ class NewsBulletin scope :can_display_postdate, ->{where(:is_hidden=>false,:postdate.lt=>Time.now).order_by([:is_top, :desc])} scope :is_approved, ->{where(:approved => true)} + def get_org_model + if self.is_preview + org_model = nil + if self.copy_id + org_model = self.class.find(self.copy_id) rescue nil + end + org_model.nil? ? self : org_model + else + self + end + end + def update_user User.find(update_user_id) rescue nil end diff --git a/app/views/admin/news/_form.html.erb b/app/views/admin/news/_form.html.erb index 976cf05..8f78747 100644 --- a/app/views/admin/news/_form.html.erb +++ b/app/views/admin/news/_form.html.erb @@ -400,11 +400,26 @@ processData: false, contentType: false }).done(function(data){ + if(window.location.protocol === "https:"){ + data = data.replace("http:","https:"); + } + window.preview_news_uid = data.split("-").slice(-1)[0].split("?")[0]; $('.modal-body iframe').attr('src',data); $('#show_preview .modal').modal(); $('#show_preview .modal').height(function() { return $(window).height() * 0.7; }); + $('#show_preview .modal').on('hidden.bs.modal', function () { + $.ajax({ + type: "post", + url: '<%= admin_news_get_preview_action_path %>', + data: {uid: window.preview_news_uid} + }).done(function(data){ + if(data["action"] == "redirect"){ + window.location.href = data["path"]; + } + }) + }); var slug = data.split('/')[(data.split('/').length-1)]; // $('#preview-iframe').on('load', function(){ diff --git a/config/routes.rb b/config/routes.rb index a573825..bc3da5f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,6 +4,7 @@ Rails.application.routes.draw do scope "(:locale)", locale: Regexp.new(locales.join("|")) do namespace :admin do + post 'news/get_preview_action', to: 'news#get_preview_action' post 'news/preview', to: 'news#preview' get 'news/destroy_preview/:slug_title-:uid', to: 'news#destroy_preview' get 'newsbulletins.json', to: 'newsbulletins#get_bulletins'