Skip to content
This repository has been archived by the owner on Jan 23, 2021. It is now read-only.

Commit

Permalink
First commit to prepare for Rails5, Redmine4.
Browse files Browse the repository at this point in the history
  • Loading branch information
akiko-pusu committed Feb 20, 2018
1 parent e597c6b commit 7495836
Show file tree
Hide file tree
Showing 18 changed files with 310 additions and 272 deletions.
5 changes: 2 additions & 3 deletions app/controllers/banner_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
class BannerController < ApplicationController
unloadable
#
# NOTE: Authorized user can turn off banner while their in session. (Changed from version 0.0.9)
# If Administrator hope to disable site wide banner, please go to settings page and uncheck
# eabned checkbox.
before_filter :require_login, only: [:off]
before_filter :find_user, :find_project, :authorize, except: [:preview, :off]
before_action :require_login, only: [:off]
before_action :find_user, :find_project, :authorize, except: [:preview, :off]

def preview
@text = params[:settings][:banner_description]
Expand Down
1 change: 0 additions & 1 deletion app/models/banner.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class Banner < ActiveRecord::Base
include Redmine::SafeAttributes
unloadable
belongs_to :project

validates_uniqueness_of :project_id
Expand Down
2 changes: 1 addition & 1 deletion app/views/banner/_project_body_bottom.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%
return unless BannerHelper.enabled?(@project)
return unless Banner::BannerHelper.enabled?(@project)
banner = Banner.where(:project_id => @project.id).first
return '' unless banner && banner.enable_banner?

Expand Down
2 changes: 1 addition & 1 deletion db/migrate/001_create_banners.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CreateBanners < ActiveRecord::Migration
class CreateBanners < ActiveRecord::Migration[4.2]
def self.up
create_table :banners do |t|
t.column :enabled, :boolean
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/002_rename_column_type.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class RenameColumnType < ActiveRecord::Migration
class RenameColumnType < ActiveRecord::Migration[4.2]
def self.up
# Not to use "type". It causes problem, "Can't mass-assign these protected attributes: type"
rename_column :banners, :type, :style
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/003_add_display_part_to_banners.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class AddDisplayPartToBanners < ActiveRecord::Migration
class AddDisplayPartToBanners < ActiveRecord::Migration[4.2]
def self.up
add_column :banners, :display_part, :string, :default => "all", :null => false
end
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/004_change_column_style.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# To change this template, choose Tools | Templates
# and open the template in the editor.

class ChangeColumnStyle < ActiveRecord::Migration
class ChangeColumnStyle < ActiveRecord::Migration[4.2]
def self.up
change_column :banners, :style, :string, :default => "info", :null => false
end
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/005_change_column_banner_description.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# #44: Changed banner description to text for advanced integration of images, etc
class ChangeColumnBannerDescription < ActiveRecord::Migration
class ChangeColumnBannerDescription < ActiveRecord::Migration[4.2]
def self.up
change_column :banners, :banner_description, :text
end
Expand Down
28 changes: 14 additions & 14 deletions init.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
require 'redmine'
require 'banner_application_hooks'
require 'banner_settings_controller_patch'
require 'banner_projects_helper_patch'
require 'banner/application_hooks'
require 'banner/settings_controller_patch'
require 'banner/projects_helper_patch'

Redmine::Plugin.register :redmine_banner do
name 'Redmine Banner plugin'
author 'Akiko Takano'
author_url 'http://twitter.com/akiko_pusu'
description 'Plugin to show site-wide message, such as maintenacne informations or notifications.'
version '0.1.2'
requires_redmine version_or_higher: '2.5.0'
version '0.1.2-dev'
requires_redmine version_or_higher: '3.4'
url 'https://github.com/akiko-pusu/redmine_banner'

settings partial: 'settings/redmine_banner',
Expand All @@ -35,13 +35,13 @@
{ banner: [:show, :edit, :project_banner_off] }, require: :member
end

Rails.configuration.to_prepare do
require_dependency 'projects_helper'
unless SettingsController.included_modules.include?(BannerSettingsControllerPatch)
SettingsController.send(:include, BannerSettingsControllerPatch)
end
unless ProjectsHelper.included_modules.include? BannerProjectsHelperPatch
ProjectsHelper.send(:include, BannerProjectsHelperPatch)
end
end
# Rails.configuration.to_prepare do
# require_dependency 'projects_helper'
# unless SettingsController.included_modules.include?(BannerSettingsControllerPatch)
# SettingsController.send(:include, BannerSettingsControllerPatch)
# end
# unless ProjectsHelper.included_modules.include? BannerProjectsHelperPatch
# ProjectsHelper.send(:include, BannerProjectsHelperPatch)
# end
# end
end
104 changes: 104 additions & 0 deletions lib/banner/application_hooks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
module Banner
class BannerHeaderHooks < Redmine::Hook::ViewListener
include ApplicationHelper
include BannerHelper

def view_layouts_base_html_head(_context = {})
o = stylesheet_link_tag('banner', plugin: 'redmine_banner')
o << javascript_include_tag('banner', plugin: 'redmine_banner')
o
end
end

#
# for Project Banner
#
class ProjectBannerMessageHooks < Redmine::Hook::ViewListener
def view_layouts_base_content(context = {})
context[:controller].send(
:render_to_string,
partial: 'banner/project_body_bottom'
)
end
end

class BannerMessageHooks < Redmine::Hook::ViewListener
include BannerHelper

# Override for conditional render_on
# Ref. http://www.redmine.org/boards/3/topics/4316
#
def self.render_on(hook, options = {})
define_method hook do |context|
if !options.include?(:if) || evaluate_if_option(options[:if], context)
context[:controller].send(:render_to_string, { locals: context }.merge(options))
end
end
end

def pass_timer?(_context)
banner_setting = Setting.plugin_redmine_banner
return true unless banner_setting['use_timer'] == 'true'

now = Time.now
start_date = get_time(
banner_setting['start_ymd'],
banner_setting['start_hour'],
banner_setting['start_min']
)

end_date = get_time(
banner_setting['end_ymd'],
banner_setting['end_hour'],
banner_setting['end_min']
)
now.between?(start_date, end_date)
end

def should_display_header?(context)
# When Disabled, false.
return false if Setting.plugin_redmine_banner['display_part'] == 'footer'
pass_timer?(context)
end

def should_display_footer?(context)
# When Disabled, false.
return false if Setting.plugin_redmine_banner['display_part'] == 'header'
pass_timer?(context)
end

render_on :view_layouts_base_body_bottom, partial: 'banner/body_bottom',
if: :should_display_footer?

private

def evaluate_if_option(if_option, context)
return false unless should_display(context)
case if_option
when Symbol
send(if_option, context)
when Method, Proc
if_option.call(context)
end
end

def should_display(context)
banner_setting = Setting.plugin_redmine_banner
return false if ((context[:controller].class.name != 'AccountController') &&
(context[:controller].action_name != 'login')) &&
(banner_setting['display_only_login_page'] == 'true')

return false if !User.current.logged? && banner_setting['only_authenticated'] == 'true'
return false unless banner_setting['enable'] == 'true'
true
end
end

# TODO: view_layouts_base_after_top_menu is not supported Redmine itself.
# Now use javascript to insert after top-menu. (Submitted ticket: http://www.redmine.org/issues/9915)
class BannerTopMenuHooks < BannerMessageHooks
render_on :view_layouts_base_body_bottom, partial: 'banner/after_top_menu',
if: :should_display_header?
end
end

35 changes: 35 additions & 0 deletions lib/banner/banner_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Banner
module BannerHelper
def get_time(ymd, h, m)
d = Date.strptime(ymd, '%Y-%m-%d')
Time.mktime(d.year, d.month, d.day, h.to_i, m.to_i)
end

def enabled?(project)
return false if project.nil?
project.module_enabled? :banner
end

def action_to_display?(controller, display_part)
action_name = controller.action_name
controller_name = controller.controller_name
return true if display_part == 'all'

case display_part
when 'overview' then
return true if controller_name == 'projects' && action_name == 'show'
when 'overview_and_issues' then
if controller_name == 'issues' || (controller_name == 'projects' && action_name == 'show')
return true
end
when 'new_issue' then
return true if controller_name == 'issues' && action_name == 'new'
else
return false
end
end

module_function :enabled?
module_function :action_to_display?
end
end
19 changes: 19 additions & 0 deletions lib/banner/projects_helper_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'projects_helper'

module Banner
module ProjectsHelperPatch
extend ActiveSupport::Concern

def project_settings_tabs
tabs = super
action = { name: 'banner',
controller: 'banner',
action: :show,
partial: 'banner/show', label: :banner }
tabs << action if User.current.allowed_to?(action, @project)
tabs
end
end
end

ProjectsHelper.prepend Banner::ProjectsHelperPatch
96 changes: 96 additions & 0 deletions lib/banner/settings_controller_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Patch for banner plugin. This affects in "plugin" action of Redmine Settings
# controller.
# Now banner plugin does not have own model(table). So, datetime informations
# are stored as string and required datetime validation by controller.
#
# TODO Store banner settings to banner's own model (table).
#
module Banner
module SettingsControllerPatch
extend ActiveSupport::Concern
include BannerHelper
# included do
# alias_method_chain(:plugin, :banner_date_validation)
# end

#
# Before posting start / end date, do validation check.(In case setting "Use timer".)
#
def plugin
param_id = params[:id]
return super if param_id != 'redmine_banner'
plugin = Redmine::Plugin.find(param_id)
settings = Setting["plugin_#{plugin.id}"]

@banner_updated_on = nil
if Setting.find_by_name('plugin_redmine_banner').present?
@banner_updated_on = Setting.find_by_name('plugin_redmine_banner').updated_on.localtime
end

# date range check
current_time = Time.now
begin
# date range check
@start_datetime = generate_time(settings, 'start', current_time)
@end_datetime = generate_time(settings, 'end', current_time)
rescue => ex
# Ref. https://github.com/akiko-pusu/redmine_banner/issues/11
# Logging when Argument Error
if logger
logger.warn "Redmine Banner Warning: #{ex} / Invalid date setting / From #{settings['start_ymd']} to #{settings['end_ymd']}. Reset to current datetime. "
end
@start_datetime = current_time
@end_datetime = current_time
settings['use_timer'] = 'false'
end

if request.post?
param_settings = params[:settings]
return super if param_settings[:use_timer] != 'true'
begin
unless validate_date_range?(param_settings)
flash[:error] = l(:error_banner_date_range)
redirect_to action: 'plugin', id: plugin.id
return
end

rescue => ex
# Argument Error
# TODO: Exception will happen about 2038 problem. (Fixed on Ruby1.9)
s_string = "#{param_settings[:start_ymd]} #{param_settings[:start_hour]}:#{param_settings[:start_min]}"
e_string = "#{param_settings[:end_ymd]} #{param_settings[:end_hour]}:#{param_settings[:end_min]}"

flash[:error] = "#{l(:error_banner_date_range)} / #{ex}: From #{s_string} to #{e_string} "
redirect_to action: 'plugin', id: plugin.id
return
end
end

# Continue to do default action
super
end

private

def generate_time(settings, type, current_time)
return current_time if settings["#{type}_ymd"].blank?

# generate time
d = Date.strptime(settings["#{type}_ymd"], '%Y-%m-%d')
d_year = d.year.to_i
d_month = d.month.to_i
d_day = d.day.to_i
d_hour = settings["#{type}_hour"].blank? ? current_time.hour.to_i : settings["#{type}_hour"].to_i
d_min = settings["#{type}_min"].blank? ? current_time.min.to_i : settings["#{type}_min"].to_i
Time.mktime(d_year, d_month, d_day, d_hour, d_min)
end

def validate_date_range?(param_settings)
s_time = get_time(param_settings[:start_ymd], param_settings[:start_hour], param_settings[:start_min])
e_time = get_time(param_settings[:end_ymd], param_settings[:end_hour], param_settings[:end_min])
e_time > s_time
end
end
end

SettingsController.prepend Banner::SettingsControllerPatch
Loading

0 comments on commit 7495836

Please sign in to comment.