Class: RedminePluginKit::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/redmine_plugin_kit/loader.rb

Defined Under Namespace

Classes: ExistingControllerPatchForHelper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(plugin_id:, debug: false) ⇒ Loader

Returns a new instance of Loader.



46
47
48
49
50
51
# File 'lib/redmine_plugin_kit/loader.rb', line 46

def initialize(plugin_id:, debug: false)
  self.plugin_id = plugin_id
  self.debug = debug

  apply_reset
end

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



7
8
9
# File 'lib/redmine_plugin_kit/loader.rb', line 7

def debug
  @debug
end

#plugin_idObject

Returns the value of attribute plugin_id.



7
8
9
# File 'lib/redmine_plugin_kit/loader.rb', line 7

def plugin_id
  @plugin_id
end

Class Method Details

.after_initialize(&block) ⇒ Object



24
25
26
# File 'lib/redmine_plugin_kit/loader.rb', line 24

def after_initialize(&block)
  Rails.application.config.after_initialize(&block)
end

.persistingObject

same as in init.rb, but in future with more functionality - or for debugging



20
21
22
# File 'lib/redmine_plugin_kit/loader.rb', line 20

def persisting
  yield
end

.plugin_dir(plugin_id:) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/redmine_plugin_kit/loader.rb', line 37

def plugin_dir(plugin_id:)
  if Gem.loaded_specs[plugin_id].nil?
    File.join Redmine::Plugin.directory, plugin_id
  else
    Gem.loaded_specs[plugin_id].full_gem_path
  end
end

.redmine_database_ready?(with_table = nil) ⇒ Boolean

required multiple times because of this bug: www.redmine.org/issues/33290

Returns:

  • (Boolean)


29
30
31
32
33
34
35
# File 'lib/redmine_plugin_kit/loader.rb', line 29

def redmine_database_ready?(with_table = nil)
  ActiveRecord::Base.connection
rescue ActiveRecord::NoDatabaseError
  false
else
  with_table.nil? || ActiveRecord::Base.connection.table_exists?(with_table)
end

.to_prepareObject



10
11
12
13
14
15
16
17
# File 'lib/redmine_plugin_kit/loader.rb', line 10

def to_prepare(...)
  if Rails.version > '6.0'
    # INFO: https://www.redmine.org/issues/36245
    Rails.logger.info 'after_plugins_loaded hook should be used instead'
  else
    Rails.configuration.to_prepare(...)
  end
end

Instance Method Details

#add_global_helper(helper) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/redmine_plugin_kit/loader.rb', line 140

def add_global_helper(helper)
  if helper.is_a? Array
    @global_helpers += helper
  else
    @global_helpers << helper
  end
end

#add_helper(helper) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/redmine_plugin_kit/loader.rb', line 132

def add_helper(helper)
  if helper.is_a? Array
    @helpers += helper
  else
    @helpers << helper
  end
end

#add_patch(patch) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/redmine_plugin_kit/loader.rb', line 124

def add_patch(patch)
  if patch.is_a? Array
    @patches += patch
  else
    @patches << patch
  end
end

#apply!Object



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/redmine_plugin_kit/loader.rb', line 148

def apply!
  validate_apply

  apply_patches!
  apply_helpers!
  apply_global_helpers!

  # reset patches and helpers
  apply_reset

  true
end

#apply_resetObject



71
72
73
74
75
# File 'lib/redmine_plugin_kit/loader.rb', line 71

def apply_reset
  @patches = []
  @helpers = []
  @global_helpers = []
end

#default_settingsObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/redmine_plugin_kit/loader.rb', line 53

def default_settings
  cached_settings_name = "@default_settings_#{plugin_id}"
  cached_settings = instance_variable_get cached_settings_name
  if cached_settings.nil?
    data = yaml_config_load 'settings.yml', with_erb: true
    instance_variable_set cached_settings_name, data.symbolize_keys
  else
    cached_settings
  end
end

#incompatible?(plugins = []) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
# File 'lib/redmine_plugin_kit/loader.rb', line 98

def incompatible?(plugins = [])
  plugins.each do |plugin|
    raise "\n\033[31m#{plugin_id} plugin cannot be used with #{plugin} plugin.\033[0m" if Redmine::Plugin.installed? plugin
  end
end

#load_custom_field_format!(reverse: false) ⇒ Object



118
119
120
121
122
# File 'lib/redmine_plugin_kit/loader.rb', line 118

def load_custom_field_format!(reverse: false)
  require_files File.join('custom_field_formats', '**/*_format.rb'),
                reverse: reverse,
                ignore_autoload: true
end

#load_macros!Object



114
115
116
# File 'lib/redmine_plugin_kit/loader.rb', line 114

def load_macros!
  require_files File.join('wiki_macros', '**/*_macro.rb')
end

#load_model_hooks!Object



104
105
106
107
# File 'lib/redmine_plugin_kit/loader.rb', line 104

def load_model_hooks!
  target = plugin_id.camelize.constantize
  target::Hooks::ModelHook
end

#load_view_hooks!Object



109
110
111
112
# File 'lib/redmine_plugin_kit/loader.rb', line 109

def load_view_hooks!
  target = plugin_id.camelize.constantize
  target::Hooks::ViewHook
end

#plugin_dirObject



77
78
79
# File 'lib/redmine_plugin_kit/loader.rb', line 77

def plugin_dir
  @plugin_dir ||= self.class.plugin_dir plugin_id: plugin_id
end

#require_files(spec, use_app: false, reverse: false, ignore_autoload: false) ⇒ Object

use_app: false => :plugin_dir/lib/:plugin_id directory



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/redmine_plugin_kit/loader.rb', line 82

def require_files(spec, use_app: false, reverse: false, ignore_autoload: false)
  dir = if use_app
          File.join plugin_dir, 'app', spec
        else
          File.join plugin_dir, 'lib', plugin_id, spec
        end

  files = Dir[dir].sort

  files.reverse! if reverse
  files.each do |f|
    Rails.autoloaders.main.ignore << f if Rails.version > '6.0' && ignore_autoload
    require f
  end
end

#yaml_config_load(yaml_file, with_erb: false) ⇒ Object



64
65
66
67
68
69
# File 'lib/redmine_plugin_kit/loader.rb', line 64

def yaml_config_load(yaml_file, with_erb: false)
  file_to_load = File.read File.join(plugin_dir, 'config', yaml_file)
  file_to_load = ERB.new(file_to_load).result if with_erb

  YAML.safe_load(file_to_load) || {}
end