Module: CamaleonCms::PluginsHelper

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

Overview

Camaleon CMS is a content management system

Copyright (C) 2015 by Owen Peredo Diaz
Email: [email protected]
This program is free software: you can redistribute it and/or modify   it under the terms of the GNU Affero General Public License as  published by the Free Software Foundation, either version 3 of the  License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the  GNU Affero General Public License (GPLv3) for more details.

Instance Method Summary collapse

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


158
159
160
161
162
163
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 158

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


102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 102

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
  settings = current_plugin(plugin_key || self_plugin_key(1)).settings
  folder_name = settings["key"]
  if settings["gem_mode"]
    "plugins/#{folder_name}/#{asset}"
  else
    "plugins/#{folder_name}/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

122
123
124
125
126
127
128
129
130
131
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 122

def plugin_asset_url(asset, plugin_key = nil)
  settings = current_plugin(plugin_key || self_plugin_key(1)).settings
  folder_name = settings["key"]
  p = settings["gem_mode"] ? "plugins/#{folder_name}/#{asset}" : "plugins/#{folder_name}/assets/#{asset}"
  begin
    asset_url(p)
  rescue NoMethodError => e
    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


66
67
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 66

def plugin_destroy(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


38
39
40
41
42
43
44
45
46
47
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 38

def plugin_install(plugin_key)
  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)
  plugin_model
end

#plugin_layout(layout_name, plugin_key = nil) ⇒ Object

return plugin full layout path plugin_key: plugin name


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

def plugin_layout(layout_name, plugin_key = nil)
  plugin = current_plugin(plugin_key || self_plugin_key(1))
  if plugin.settings["gem_mode"]
    "plugins/#{plugin.slug}/layouts/#{layout_name}"
  else
    "plugins/#{plugin.slug}/views/layouts/#{layout_name}"
  end
end

#plugin_load_helpers(plugin) ⇒ Object

auto load all helpers of this plugin


134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 134

def plugin_load_helpers(plugin)
  return if !plugin.present? || !plugin["helpers"].present?
  plugin["helpers"].each do |h|
    begin
      next if self.class.include?(h.constantize)
      self.class_eval do
        include h.constantize
      end
    rescue => e
      Rails.logger.info "---------------------------app loading error for #{h}: #{e.message}. Please check the plugins and themes presence"
    end
  end
end

#plugin_uninstall(plugin_key) ⇒ Object

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


52
53
54
55
56
57
58
59
60
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 52

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)
  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


28
29
30
31
32
33
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 28

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"]
  plugin_model
end

#plugin_view(view_name, plugin_key = nil) ⇒ Object

return plugin full view path plugin_key: plugin name


82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 82

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

  plugin = current_plugin(plugin_key || self_plugin_key(1))
  if plugin.settings["gem_mode"]
    "plugins/#{plugin.slug}/#{view_name}"
  else
    "plugins/#{plugin.slug}/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


13
14
15
16
17
18
19
20
21
22
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 13

def plugins_initialize(klass = nil)
  mod = Module.new
  PluginRoutes.enabled_apps(current_site).each{|plugin|
    next if !plugin.present? || !plugin["helpers"].present?
    plugin["helpers"].each do |h|
      mod.send :include, h.constantize
    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)


150
151
152
153
154
# File 'app/helpers/camaleon_cms/plugins_helper.rb', line 150

def self_plugin_key(index = 0)
  k = "/plugins/"
  f = caller[index]
  f.split(k).last.split("/").first if f.include?(k)
end