additionals/lib/additionals.rb

164 lines
5.1 KiB
Ruby
Raw Normal View History

module Additionals
MAX_CUSTOM_MENU_ITEMS = 5
2017-11-17 09:10:50 +03:00
SELECT2_INIT_ENTRIES = 20
2019-01-27 11:40:17 +03:00
GOTO_LIST = " \xc2\xbb".freeze
2020-08-06 09:18:30 +03:00
LIST_SEPARATOR = "#{GOTO_LIST} ".freeze
2018-08-23 15:56:32 +03:00
2020-07-27 11:49:23 +03:00
RenderAsync.configuration.jquery = true
2017-10-03 15:09:32 +03:00
class << self
2018-02-12 16:53:05 +03:00
def setup
incompatible_plugins(%w[redmine_issue_control_panel
2018-02-12 16:53:05 +03:00
redmine_editauthor
redmine_changeauthor
redmine_auto_watch])
2020-03-27 17:08:37 +03:00
2020-04-12 10:13:59 +03:00
patch(%w[AccountController
ApplicationController
AutoCompletesController
Issue
2018-07-18 00:04:49 +03:00
IssuePriority
2018-02-12 16:53:05 +03:00
TimeEntry
Project
2018-02-12 16:53:05 +03:00
Wiki
WikiController
2020-07-27 11:49:23 +03:00
ProjectsController
WelcomeController
2019-10-01 18:19:31 +03:00
ReportsController
2018-12-03 12:15:50 +03:00
Principal
QueryFilter
Role
User
2018-09-12 21:42:36 +03:00
UserPreference])
2018-02-12 16:53:05 +03:00
Redmine::WikiFormatting.format_names.each do |format|
case format
when 'markdown'
2019-10-01 12:21:49 +03:00
Redmine::WikiFormatting::Markdown::HTML.include Patches::FormatterMarkdownPatch
Redmine::WikiFormatting::Markdown::Helper.include Patches::FormattingHelperPatch
2018-02-12 16:53:05 +03:00
when 'textile'
2019-10-01 12:21:49 +03:00
Redmine::WikiFormatting::Textile::Formatter.include Patches::FormatterTextilePatch
Redmine::WikiFormatting::Textile::Helper.include Patches::FormattingHelperPatch
2018-02-12 16:53:05 +03:00
end
end
2018-07-31 17:09:40 +03:00
IssuesController.send(:helper, AdditionalsIssuesHelper)
2019-04-24 13:12:34 +03:00
SettingsController.send(:helper, AdditionalsSettingsHelper)
2018-07-31 17:09:40 +03:00
WikiController.send(:helper, AdditionalsWikiPdfHelper)
CustomFieldsController.send(:helper, AdditionalsCustomFieldsHelper)
2019-04-24 13:12:34 +03:00
# Static class patches
2019-10-01 12:21:49 +03:00
Redmine::AccessControl.include Additionals::Patches::AccessControlPatch
2018-02-12 16:53:05 +03:00
# Global helpers
2019-10-01 12:21:49 +03:00
ActionView::Base.include Additionals::Helpers
ActionView::Base.include AdditionalsFontawesomeHelper
ActionView::Base.include AdditionalsMenuHelper
ActionView::Base.include Additionals::AdditionalsSelect2Helper
2018-02-12 16:53:05 +03:00
# Hooks
require_dependency 'additionals/hooks'
# Macros
load_macros(%w[cryptocompare date fa gist gmap google_docs group_users iframe
2018-08-31 18:02:03 +03:00
issue redmine_issue redmine_wiki
2018-06-30 14:17:28 +03:00
last_updated_at last_updated_by meteoblue member new_issue project
2020-01-22 13:00:54 +03:00
recently_updated reddit slideshare tradingview twitter user vimeo youtube asciinema])
2018-02-12 16:53:05 +03:00
end
2018-12-29 23:33:57 +03:00
def settings_compatible(plugin_name)
if Setting[plugin_name].class == Hash
# convert Rails 4 data (this runs only once)
new_settings = ActiveSupport::HashWithIndifferentAccess.new(Setting[plugin_name])
Setting.send("#{plugin_name}=", new_settings)
new_settings
2018-09-26 13:52:48 +03:00
else
# Rails 5 uses ActiveSupport::HashWithIndifferentAccess
2018-12-29 23:33:57 +03:00
Setting[plugin_name]
2018-09-26 13:52:48 +03:00
end
2017-10-03 15:09:32 +03:00
end
# support with default setting as fall back
def setting(value)
if settings.key? value
settings[value]
else
load_settings[value]
end
end
2017-10-03 15:09:32 +03:00
def setting?(value)
true?(setting(value))
2017-10-28 18:31:15 +03:00
end
def true?(value)
return false if value.is_a? FalseClass
return true if value.is_a?(TrueClass) || value.to_i == 1 || value.to_s.casecmp('true').zero?
2018-09-10 18:24:07 +03:00
2017-10-28 18:31:15 +03:00
false
2017-10-03 15:09:32 +03:00
end
2018-03-10 16:07:48 +03: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 15:09:32 +03: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 13:05:17 +03:00
end
end
2017-10-03 15:09:32 +03:00
def patch(patches = [], plugin_id = 'additionals')
patches.each do |name|
patch_dir = Rails.root.join("plugins/#{plugin_id}/lib/#{plugin_id}/patches")
2017-10-03 15:09:32 +03:00
require "#{patch_dir}/#{name.underscore}_patch"
2017-10-03 15:09:32 +03:00
target = name.constantize
patch = "#{plugin_id.camelize}::Patches::#{name}Patch".constantize
2017-07-13 13:05:17 +03:00
2020-04-12 10:13:59 +03:00
target.include(patch) unless target.included_modules.include?(patch)
end
end
2017-10-03 15:09:32 +03:00
def load_macros(macros = [], plugin_id = 'additionals')
macro_dir = Rails.root.join("plugins/#{plugin_id}/lib/#{plugin_id}/wiki_macros")
2017-10-03 15:09:32 +03:00
macros.each do |macro|
require_dependency "#{macro_dir}/#{macro.underscore}_macro"
end
end
2017-10-03 15:09:32 +03:00
def load_settings(plugin_id = 'additionals')
2020-08-06 09:18:30 +03:00
cached_settings_name = "@load_settings_#{plugin_id}"
cached_settings = instance_variable_get(cached_settings_name)
if cached_settings.nil?
data = YAML.safe_load(ERB.new(IO.read(Rails.root.join("plugins/#{plugin_id}/config/settings.yml"))).result) || {}
instance_variable_set(cached_settings_name, data.symbolize_keys)
else
cached_settings
end
end
2019-12-30 18:09:04 +03:00
def hash_remove_with_default(field, options, default = nil)
value = nil
if options.key? field
value = options[field]
options.delete(field)
elsif !default.nil?
value = default
end
[value, options]
end
private
def settings
settings_compatible(:plugin_additionals)
2017-10-03 15:09:32 +03:00
end
end
end