additionals/lib/additionals.rb

186 lines
5.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2021-12-07 21:03:09 +03:00
require 'redmine_plugin_kit'
module Additionals
MAX_CUSTOM_MENU_ITEMS = 5
SELECT2_INIT_ENTRIES = 30
API_LIMIT = 100
DEFAULT_MODAL_WIDTH = '350px'
GOTO_LIST = " \xc2\xbb"
LIST_SEPARATOR = "#{GOTO_LIST} "
EMOJI_ASSERT_PATH = 'plugin_assets/additionals/images/emojis'
2018-08-23 15:56:32 +03:00
2021-12-07 21:03:09 +03:00
include RedminePluginKit::PluginBase
2020-12-11 15:35:37 +03:00
class << self
2022-01-04 20:41:34 +03:00
def full_url(path = nil)
"#{Setting.protocol}://#{Setting.host_name.chomp '/'}#{path}"
end
2020-10-13 20:41:52 +03:00
def class_prefix(klass)
klass_name = klass.is_a?(String) ? klass : klass.name
klass_name.underscore.tr '/', '_'
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
2021-08-25 18:55:16 +03:00
def time_zone_correct(time, user: User.current)
timezone = user.time_zone || Time.zone
timezone.utc_offset - Time.zone.local_to_utc(time).localtime.utc_offset
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]
2020-09-08 10:40:06 +03:00
options.delete field
2019-12-30 18:09:04 +03:00
elsif !default.nil?
value = default
end
[value, options]
end
def split_ids(phrase, limit: nil)
limit ||= Setting.per_page_options_array.first || 25
raw_ids = phrase.split(',').map(&:strip)
ids = []
raw_ids.each do |id|
if id.include? '-'
range = id.split('-').map(&:strip)
if range.size == 2
left_id = range.first.to_i
right_id = range.last.to_i
min = [left_id, right_id].min
max = [left_id, right_id].max
# if range to large, take lowest numbers + last possible number
ids << if max - min > limit
old_max = max
max = limit + min - 2
ids << (min..max).to_a
old_max
else
(min..max).to_a
end
end
else
ids << id.to_i
end
end
ids.flatten!
ids.uniq!
ids.take limit
end
2022-03-12 19:50:45 +03:00
def max_live_search_results
if setting(:max_live_search_results).present?
setting(:max_live_search_results).to_i
else
50
end
end
def debug(message = 'running', console: false)
if console
RedminePluginKit::Debug.msg message
else
RedminePluginKit::Debug.log message
end
end
private
def setup
RenderAsync.configuration.jquery = true
loader.incompatible? %w[redmine_editauthor
redmine_changeauthor]
loader.add_patch %w[ApplicationController
AutoCompletesController
Issue
IssuePriority
TimeEntry
2022-05-26 19:41:01 +03:00
Mailer
Project
ProjectQuery
Wiki
ProjectsController
WelcomeController
ReportsController
Principal
Query
QueryFilter
Role
User
UserPreference]
loader.add_helper %w[Issues
Settings
Wiki
CustomFields]
loader.add_global_helper [Additionals::Helpers,
AdditionalsFontawesomeHelper,
AdditionalsMenuHelper,
AdditionalsSelect2Helper]
Redmine::WikiFormatting.format_names.each do |format|
case format
when 'markdown'
loader.add_patch [{ target: Redmine::WikiFormatting::Markdown::HTML, patch: 'FormatterMarkdown' },
{ target: Redmine::WikiFormatting::Markdown::Helper, patch: 'FormattingHelper' }]
when 'common_mark'
2022-04-10 14:45:24 +03:00
loader.add_patch [{ target: Redmine::WikiFormatting::CommonMark::Formatter, patch: 'FormatterCommonMark' }]
loader.add_patch [{ target: Redmine::WikiFormatting::CommonMark::Helper, patch: 'FormattingHelper' }]
when 'textile'
loader.add_patch [{ target: Redmine::WikiFormatting::Textile::Formatter, patch: 'FormatterTextile' },
{ target: Redmine::WikiFormatting::Textile::Helper, patch: 'FormattingHelper' }]
end
end
2022-04-10 14:45:24 +03:00
# Clients
loader.require_files File.join('wiki_formatting', 'common_mark', '**/*_filter.rb')
# Apply patches and helper
loader.apply!
# Macros
loader.load_macros!
# Load view hooks
loader.load_view_hooks!
2017-10-03 15:09:32 +03:00
end
end
2020-11-29 15:17:02 +03:00
# Run the classic redmine plugin initializer after rails boot
class Plugin < ::Rails::Engine
2022-04-10 14:45:24 +03:00
require 'tanuki_emoji'
2020-11-29 15:17:02 +03:00
require 'render_async'
require 'rss'
require 'slim'
2020-11-29 15:17:02 +03:00
config.after_initialize do
# engine_name could be used (additionals_plugin), but can
# create some side effencts
plugin_id = 'additionals'
2022-04-10 14:45:24 +03:00
Additionals::Gemify.install_emoji_assets
2020-11-29 15:17:02 +03:00
# if plugin is already in plugins directory, use this and leave here
next if Redmine::Plugin.installed? plugin_id
# gem is used as redmine plugin
require File.expand_path '../init', __dir__
Additionals::Gemify.install_assets plugin_id
Additionals::Gemify.create_plugin_hint plugin_id
end
end
end