additionals/lib/additionals.rb

108 lines
3.6 KiB
Ruby
Raw Normal View History

module Additionals
MAX_CUSTOM_MENU_ITEMS = 5
2017-11-17 07:10:50 +01:00
SELECT2_INIT_ENTRIES = 20
2018-08-23 14:56:32 +02:00
LIST_SEPARATOR = ' » '.html_safe # rubocop:disable Rails/OutputSafety
2017-10-03 14:09:32 +02:00
class << self
2018-02-12 14:53:05 +01:00
def setup
incompatible_plugins(%w[redmine_tweaks
redmine_issue_control_panel
2018-02-12 14:53:05 +01:00
redmine_editauthor
redmine_changeauthor
redmine_auto_watch])
2018-02-12 14:53:05 +01:00
patch(%w[AccountController
Issue
2018-07-17 23:04:49 +02:00
IssuePriority
2018-02-12 14:53:05 +01:00
TimeEntry
Wiki
WikiController
UserPreference
ApplicationController])
patch(%w[QueryFilter]) if Redmine::VERSION.to_s >= '3.4'
Rails.configuration.assets.paths << Emoji.images_path
# Send Emoji Patches to all wiki formatters available to be able to switch formatter without app restart
Redmine::WikiFormatting.format_names.each do |format|
case format
when 'markdown'
2018-07-30 11:14:07 +02:00
Redmine::WikiFormatting::Markdown::HTML.send(:include, Additionals::Patches::FormatterMarkdownPatch)
2018-02-12 14:53:05 +01:00
when 'textile'
2018-07-30 11:14:07 +02:00
Redmine::WikiFormatting::Textile::Formatter.send(:include, Additionals::Patches::FormatterTextilePatch)
2018-02-12 14:53:05 +01:00
end
end
# Static class patches
2018-07-31 16:09:40 +02:00
IssuesController.send(:helper, AdditionalsIssuesHelper)
WikiController.send(:helper, AdditionalsWikiPdfHelper)
2018-07-30 11:14:07 +02:00
Redmine::AccessControl.send(:include, Additionals::Patches::AccessControlPatch)
2018-02-12 14:53:05 +01:00
# Global helpers
2018-07-30 11:14:07 +02:00
ActionView::Base.send :include, Additionals::Helpers
2018-02-12 14:53:05 +01:00
# Hooks
require_dependency 'additionals/hooks'
# Macros
2018-08-30 17:27:34 +02:00
load_macros(%w[calendar cryptocompare date fa gist gmap group_users iframe issue
2018-06-30 13:17:28 +02:00
last_updated_at last_updated_by meteoblue member new_issue project
recently_updated reddit slideshare tradingview twitter user vimeo youtube])
2018-02-12 14:53:05 +01:00
end
2017-10-03 14:09:32 +02:00
def settings
2018-07-23 17:49:35 +02:00
ActionController::Parameters.new(Setting[:plugin_additionals])
2017-10-03 14:09:32 +02:00
end
2017-10-03 14:09:32 +02:00
def setting?(value)
2017-10-28 17:31:15 +02:00
true?(settings[value])
end
def true?(value)
return true if value.to_i == 1 || value.to_s.casecmp('true').zero?
false
2017-10-03 14:09:32 +02:00
end
2018-03-10 14:07:48 +01:00
def now_with_user_time_zone(user = User.current)
if user.time_zone.nil?
Time.zone.now
else
user.time_zone.now
end
end
2017-10-03 14:09:32 +02:00
def incompatible_plugins(plugins = [], title = 'additionals')
plugins.each do |plugin|
raise "\n\033[31m#{title} plugin cannot be used with #{plugin} plugin'.\033[0m" if Redmine::Plugin.installed?(plugin)
2017-07-13 12:05:17 +02:00
end
end
2017-10-03 14:09:32 +02:00
def patch(patches = [], plugin_id = 'additionals')
patches.each do |name|
patch_dir = Rails.root.join('plugins', plugin_id, 'lib', plugin_id, 'patches')
require "#{patch_dir}/#{name.underscore}_patch"
2017-10-03 14:09:32 +02:00
target = name.constantize
patch = "#{plugin_id.camelize}::Patches::#{name}Patch".constantize
2017-07-13 12:05:17 +02:00
2017-12-12 22:10:10 +01:00
target.send(:include, patch) unless target.included_modules.include?(patch)
end
end
2017-10-03 14:09:32 +02:00
def load_macros(macros = [], plugin_id = 'additionals')
macro_dir = Rails.root.join('plugins', plugin_id, 'lib', plugin_id, 'wiki_macros')
macros.each do |macro|
require_dependency "#{macro_dir}/#{macro.underscore}_macro"
end
end
2017-10-03 14:09:32 +02:00
def load_settings(plugin_id = 'additionals')
data = YAML.safe_load(ERB.new(IO.read(Rails.root.join('plugins',
plugin_id,
'config',
'settings.yml'))).result) || {}
data.symbolize_keys
end
end
end