Module: CamaleonCms::PluginsHelper

Includes:
SiteHelper
Included in:
CamaleonController, HooksHelper, HtmlMailer
Defined in:
app/helpers/camaleon_cms/plugins_helper.rb

Instance Method Summary collapse

Methods included from SiteHelper

#cama_current_site_host_port, #cama_get_list_layouts_files, #cama_get_list_template_files, #cama_is_test_request?, #current_locale, #current_site, #current_theme, #site_after_install, #site_install_theme, #site_uninstall_theme

Instance Method Details

#current_plugin(plugin_key = nil) ⇒ Object

method called only from files within plugins directory return the plugin model for current site calculated according to the file caller location



176
177
178
179
180
181
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 176

def current_plugin(plugin_key = nil)
  _key = plugin_key || self_plugin_key(1)
  cama_cache_fetch("current_plugin_#{_key}") do
    current_site.get_plugin(_key)
  end
end

#plugin_asset_path(asset, plugin_key = nil) ⇒ Object Also known as: plugin_asset, plugin_gem_asset

return plugin full asset path plugin_key: plugin name asset: (String) asset name sample: <script src=“<%= plugin_asset_path(”admin.js“) %>”></script> => /assets/plugins/my_plugin/admin-54505620f.js sample: <script src=“<%= plugin_asset_path(”admin.js“, ‘my_plugin’) %>”></script> => /assets/plugins/my_plugin/admin-54505620f.js



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 110

def plugin_asset_path(asset, plugin_key = nil)
  if plugin_key.present? && plugin_key.include?('/')
    return plugin_asset_url(plugin_key,
                            asset || self_plugin_key(1))
  end

  key = plugin_key || self_plugin_key(1)
  if PluginRoutes.plugin_info(key)['gem_mode']
    "plugins/#{key}/#{asset}"
  else
    "plugins/#{key}/assets/#{asset}"
  end
end

#plugin_asset_url(asset, plugin_key = nil) ⇒ Object

return the full url for asset of current plugin: asset: (String) asset name plugin_key: (optional) plugin name, default (current plugin caller to this function) sample:

plugin_asset_url("css/main.css") => return: http://myhost.com/assets/plugins/my_plugin/assets/css/main-54505620f.css


131
132
133
134
135
136
137
138
139
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 131

def plugin_asset_url(asset, plugin_key = nil)
  key = plugin_key || self_plugin_key(1)
  p = PluginRoutes.plugin_info(key)['gem_mode'] ? "plugins/#{key}/#{asset}" : "plugins/#{key}/assets/#{asset}"
  begin
    ActionController::Base.helpers.asset_url(p)
  rescue NoMethodError
    p
  end
end

#plugin_destroy(plugin_key) ⇒ Object

remove a plugin from current site plugin_key: key of the plugin return model of the plugin removed DEPRECATED: PLUGINS AND THEMES CANNOT BE DESTROYED



73
74
75
76
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 73

def plugin_destroy(plugin_key)
  hooks_run('plugin_after_destroy', { plugin: plugin_key })
  hooks_run("plugin_#{plugin_key}_after_destroy", { plugin: plugin_key })
end

#plugin_install(plugin_key) ⇒ Object

install a plugin for current site plugin_key: key of the plugin return model of the plugin



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 35

def plugin_install(plugin_key)
  if PluginRoutes.plugin_info(plugin_key).nil?
    Rails.logger.debug "Camaleon CMS - Plugin not found: #{plugin_key}"
  else
    plugin_model = current_site.plugins.where(slug: plugin_key).first_or_create!
    plugin_model.installed_version = plugin_model.settings['version']
    return plugin_model if plugin_model.active?

    plugin_model.active
    PluginRoutes.reload
    # plugins_initialize(self)
    hook_run(plugin_model.settings, 'on_active', plugin_model)
    hooks_run('plugin_after_install', { plugin: plugin_model })
    hooks_run("plugin_#{plugin_key}_after_install", { plugin: plugin_model })
    plugin_model
  end
end

#plugin_layout(layout_name, plugin_key = nil) ⇒ Object

return plugin full layout path plugin_key: plugin name



80
81
82
83
84
85
86
87
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 80

def plugin_layout(layout_name, plugin_key = nil)
  key = plugin_key || self_plugin_key(1)
  if PluginRoutes.plugin_info(key)['gem_mode']
    "plugins/#{key}/layouts/#{layout_name}"
  else
    "plugins/#{key}/views/layouts/#{layout_name}"
  end
end

#plugin_load_helpers(plugin) ⇒ Object

auto load all helpers of this plugin



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 142

def plugin_load_helpers(plugin)
  return if !plugin.present? || !plugin['helpers'].present?

  plugin['helpers'].each do |h|
    next if self.class.include?(h.constantize)

    class_eval do
      include h.constantize
    end
  rescue StandardError => e
    Rails.logger.debug "Camaleon CMS - App loading error for #{h}: #{e.message}. Please check the plugins and themes presence"
  end
end

#plugin_uninstall(plugin_key) ⇒ Object

uninstall a plugin from current site plugin_key: key of the plugin return model of the plugin



56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 56

def plugin_uninstall(plugin_key)
  plugin_model = current_site.plugins.where(slug: plugin_key).first_or_create!
  return plugin_model unless plugin_model.active?

  plugin_model.inactive
  PluginRoutes.reload
  # plugins_initialize(self)
  hook_run(plugin_model.settings, 'on_inactive', plugin_model)
  hooks_run('plugin_after_uninstall', { plugin: plugin_model })
  hooks_run("plugin_#{plugin_key}_after_uninstall", { plugin: plugin_model })
  plugin_model
end

#plugin_upgrade(plugin_key) ⇒ Object

upgrade installed plugin in current site for a new version plugin_key: key of the plugin trigger hook “on_upgrade” return model of the plugin



23
24
25
26
27
28
29
30
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 23

def plugin_upgrade(plugin_key)
  plugin_model = current_site.plugins.where(slug: plugin_key).first!
  hook_run(plugin_model.settings, 'on_upgrade', plugin_model)
  plugin_model.installed_version = plugin_model.settings['version']
  hooks_run('plugin_after_upgrade', { plugin: plugin_model })
  hooks_run("plugin_#{plugin_key}_after_upgrade", { plugin: plugin_model })
  plugin_model
end

#plugin_view(view_name, plugin_key = nil) ⇒ Object

return plugin full view path plugin_key: plugin name



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 91

def plugin_view(view_name, plugin_key = nil)
  if plugin_key.present? && plugin_key.include?('/') # fix for 1.x
    k = view_name
    view_name = plugin_key
    plugin_key = k
  end
  key = plugin_key || self_plugin_key(1)
  if PluginRoutes.plugin_info(key)['gem_mode']
    "plugins/#{key}/#{view_name}"
  else
    "plugins/#{key}/views/#{view_name}"
  end
end

#plugins_initialize(klass = nil) ⇒ Object

load all plugins + theme installed for current site METHOD IGNORED (is a partial solution to avoid load helpers and cache it for all sites) this method try to load helpers for each request without caching



7
8
9
10
11
12
13
14
15
16
17
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 7

def plugins_initialize(klass = nil)
  mod = Module.new
  PluginRoutes.enabled_apps(current_site).each do |plugin|
    next if !plugin.present? || !plugin['helpers'].present?

    plugin['helpers'].each do |h|
      mod.send :include, h.constantize
    end
  end
  (klass || self).send :extend, mod
end

#self_plugin_key(index = 0) ⇒ Object

return plugin key for current plugin file (helper|controller|view) index: internal control (ignored)



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 158

def self_plugin_key(index = 0)
  f = caller[index]
  key = if f.include?('/apps/plugins/')
          f.split('/apps/plugins/').last.split('/').first
        elsif f.include?('/plugins/')
          f.split('/plugins/').last.split('/').first
        else
          f.split('/gems/').last.split('/').first
        end
  begin
    PluginRoutes.plugin_info(key)['key']
  rescue StandardError
    raise("Not found plugin with key: #{key} or dirname: #{key}")
  end
end