Class: PluginRoutes
- Inherits:
-
Object
- Object
- PluginRoutes
- Defined in:
- lib/plugin_routes.rb,
lib/generators/camaleon_cms/install_template/plugin_routes.rb
Constant Summary collapse
- @@_vars =
[]
- @@_after_reload =
[]
- @@anonymous_hooks =
{}
Class Method Summary collapse
-
.add_after_reload_routes(command) ⇒ Object
permit to add extra actions for reload routes.
-
.add_anonymous_hook(hook_key, callback, hook_id = '') ⇒ Object
add a new anonymous hook sample: PluginRoutes.add_anonymous_hook(‘before_admin’, lambda{|params| puts params }).
-
.all_apps ⇒ Object
return all apps loaded.
-
.all_enabled_apps ⇒ Object
return all enabled apps as []: system, themes, plugins.
-
.all_enabled_plugins ⇒ Object
return all enabled plugins (a theme is enabled if at least one site has installed).
-
.all_enabled_themes ⇒ Object
return all enabled themes (a theme is enabled if at least one site is assigned).
-
.all_helpers ⇒ Object
all helpers of enabled plugins.
-
.all_locales ⇒ Object
return all locales for all sites joined by |.
-
.all_locales_for_routes ⇒ Object
return all locales for translated routes.
-
.all_plugins ⇒ Object
return all plugins located in cms and in this project.
-
.all_themes ⇒ Object
return an array of all themes installed for all sites.
-
.all_translations(key, *args) ⇒ Object
return all translations for all languages, sample: [‘Sample’, ‘Ejemplo’, ‘.…’].
-
.apps_dir ⇒ Object
return apps directory path.
- .cache_variable(var_name, value = nil) ⇒ Object
-
.db_installed? ⇒ Boolean
check if db migrate already done.
-
.default_url_options ⇒ Object
return the default url options for Camaleon CMS.
-
.destroy_plugin(plugin_key) ⇒ Object
destroy plugin.
-
.draw_gems ⇒ Object
draw “all” gems registered for the plugins or themes and camaleon gems.
-
.enabled_apps(site, theme_slug = nil) ⇒ Object
return all enabled apps for site (themes + system + plugins) [] theme_slug: current theme slug.
-
.enabled_plugins(site) ⇒ Object
return all enabled plugins [].
-
.fixActionParameter(h) ⇒ Object
convert action parameter into hash.
-
.get_anonymous_hooks(hook_key) ⇒ Array
return all registered anonymous hooks for hook_key.
-
.get_gem(name) ⇒ Object
check if a gem is available or not Arguemnts: name: name of the gem return (Boolean) true/false.
-
.get_gem_plugins ⇒ Object
return all plugins registered as gems.
-
.get_gem_themes ⇒ Object
return all themes registered as gems.
-
.get_sites ⇒ Object
return all sites registered for Plugin routes.
-
.get_user_class_name ⇒ Object
return the class name for user model.
- .isRails4? ⇒ Boolean
- .isRails5? ⇒ Boolean
- .isRails6? ⇒ Boolean
-
.load(env = "admin") ⇒ Object
load plugin routes if it is enabled.
- .load_themes(env = "admin") ⇒ Object
- .migration_class ⇒ Object
-
.plugin_info(plugin_key) ⇒ Object
return plugin information.
-
.reload ⇒ Object
reload routes.
-
.remove_anonymous_hook(hook_key, hook_id) ⇒ Array
return all registered anonymous hooks for hook_key.
-
.site_plugin_helpers(site) ⇒ Object
all helpers of enabled plugins for site.
-
.static_system_info ⇒ Object
(also: system_info)
return system static settings (config.json values).
-
.theme_info(theme_name) ⇒ Object
return theme information if theme_name is nil, the use current site theme.
Class Method Details
.add_after_reload_routes(command) ⇒ Object
permit to add extra actions for reload routes
159 160 161 |
# File 'lib/plugin_routes.rb', line 159 def self.add_after_reload_routes(command) @@_after_reload << command end |
.add_anonymous_hook(hook_key, callback, hook_id = '') ⇒ Object
add a new anonymous hook sample: PluginRoutes.add_anonymous_hook(‘before_admin’, lambda{|params| puts params })
124 125 126 127 |
# File 'lib/plugin_routes.rb', line 124 def add_anonymous_hook(hook_key, callback, hook_id = '') @@anonymous_hooks[hook_key] ||= [] @@anonymous_hooks[hook_key] << {id: hook_id, callback: callback} end |
.all_apps ⇒ Object
return all apps loaded
347 348 349 |
# File 'lib/plugin_routes.rb', line 347 def self.all_apps all_plugins+all_themes end |
.all_enabled_apps ⇒ Object
return all enabled apps as []: system, themes, plugins
187 188 189 |
# File 'lib/plugin_routes.rb', line 187 def self.all_enabled_apps [system_info] + all_enabled_themes + all_enabled_plugins end |
.all_enabled_plugins ⇒ Object
return all enabled plugins (a theme is enabled if at least one site has installed)
203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/plugin_routes.rb', line 203 def self.all_enabled_plugins r = cache_variable("all_enabled_plugins") return r unless r.nil? res, enabled_ps = [], [] get_sites.each { |site| enabled_ps += site.plugins.active.pluck(:slug) } all_plugins.each do |plugin| if enabled_ps.include?(plugin["key"]) res << plugin end end cache_variable("all_enabled_plugins", res) end |
.all_enabled_themes ⇒ Object
return all enabled themes (a theme is enabled if at least one site is assigned)
192 193 194 195 196 197 198 199 200 |
# File 'lib/plugin_routes.rb', line 192 def self.all_enabled_themes r = cache_variable("all_enabled_themes"); return r unless r.nil? res = [] get_sites.each do |site| i = theme_info(site.get_theme_slug) res << i if i.present? end cache_variable("all_enabled_themes", res) end |
.all_helpers ⇒ Object
all helpers of enabled plugins
227 228 229 230 231 232 233 234 235 |
# File 'lib/plugin_routes.rb', line 227 def self.all_helpers r = cache_variable("plugins_helper") return r unless r.nil? res = [] all_apps.each do |settings| res += settings["helpers"] if settings["helpers"].present? end cache_variable("plugins_helper", res.uniq) end |
.all_locales ⇒ Object
return all locales for all sites joined by |
268 269 270 271 272 273 274 275 |
# File 'lib/plugin_routes.rb', line 268 def self.all_locales r = cache_variable("site_all_locales"); return r unless r.nil? res = [] get_sites.each do |s| res += s.get_languages end cache_variable("site_all_locales", res.uniq.join("|")) end |
.all_locales_for_routes ⇒ Object
return all locales for translated routes
284 285 286 287 288 289 290 291 292 |
# File 'lib/plugin_routes.rb', line 284 def self.all_locales_for_routes r = cache_variable("all_locales_for_routes"); return r unless r.nil? res = {} all_locales.split("|").each do |l| res[l] = "_#{l}" end res[false] = '' cache_variable("all_locales_for_routes", res) end |
.all_plugins ⇒ Object
return all plugins located in cms and in this project
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'lib/plugin_routes.rb', line 300 def self.all_plugins camaleon_gem = get_gem('camaleon_cms') return [] if !camaleon_gem r = cache_variable("all_plugins"); return r unless (r.nil? || r == []) res = get_gem_plugins entries = [".", ".."] res.each{|plugin| entries << plugin["key"] } (Dir["#{apps_dir}/plugins/*"] + Dir["#{camaleon_gem.gem_dir}/app/apps/plugins/*"]).each do |path| entry = path.split("/").last config = File.join(path, "config", "config.json") next if entries.include?(entry) || !File.directory?(path) || !File.exist?(config) p = JSON.parse(File.read(config)) p = p.with_indifferent_access rescue p p["key"] = entry p["path"] = path p["kind"] = "plugin" res << p entries << entry end cache_variable("all_plugins", res) end |
.all_themes ⇒ Object
return an array of all themes installed for all sites
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/plugin_routes.rb', line 323 def self.all_themes camaleon_gem = get_gem('camaleon_cms') return [] if !camaleon_gem r = cache_variable("all_themes"); return r unless (r.nil? || r == []) res = get_gem_themes entries = [".", ".."] res.each{|theme| entries << theme["key"] } (Dir["#{apps_dir}/themes/*"] + Dir["#{camaleon_gem.gem_dir}/app/apps/themes/*"]).each do |path| entry = path.split("/").last config = File.join(path, "config", "config.json") next if entries.include?(entry) || !File.directory?(path) || !File.exist?(config) p = JSON.parse(File.read(config)) p = p.with_indifferent_access rescue p p["key"] = entry p["path"] = path p["kind"] = "theme" p["title"] = p["name"] res << p entries << entry end cache_variable("all_themes", res) end |
.all_translations(key, *args) ⇒ Object
return all translations for all languages, sample: [‘Sample’, ‘Ejemplo’, ‘.…’]
278 279 280 281 |
# File 'lib/plugin_routes.rb', line 278 def self.all_translations(key, *args) args = args. all_locales.split('|').map{|_l| I18n.t(key, args.merge({locale: _l})) }.uniq end |
.apps_dir ⇒ Object
return apps directory path
295 296 297 |
# File 'lib/plugin_routes.rb', line 295 def self.apps_dir Rails.root.join("app", "apps").to_s end |
.cache_variable(var_name, value = nil) ⇒ Object
243 244 245 246 247 248 249 250 251 |
# File 'lib/plugin_routes.rb', line 243 def self.cache_variable(var_name, value=nil) @@_vars.push(var_name).uniq! #if Rails.env != "development" # disable cache plugin routes for develoment mode cache = class_variable_get("@@cache_#{var_name}") rescue nil return cache if value.nil? #end class_variable_set("@@cache_#{var_name}", value) value end |
.db_installed? ⇒ Boolean
check if db migrate already done
263 264 265 |
# File 'lib/plugin_routes.rb', line 263 def self.db_installed? @@is_db_installed ||= ActiveRecord::Base.connection.table_exists?(CamaleonCms::Site.table_name) end |
.default_url_options ⇒ Object
return the default url options for Camaleon CMS
404 405 406 407 408 |
# File 'lib/plugin_routes.rb', line 404 def self. = { host: (CamaleonCms::Site.main_site.slug rescue "") } .merge!({ protocol: 'https' }) if Rails.application.config.force_ssl end |
.destroy_plugin(plugin_key) ⇒ Object
destroy plugin
238 239 240 241 |
# File 'lib/plugin_routes.rb', line 238 def self.destroy_plugin(plugin_key) FileUtils.rm_r(Rails.root.join("app", "apps", "plugins", plugin_key)) rescue "" PluginRoutes.reload end |
.draw_gems ⇒ Object
draw “all” gems registered for the plugins or themes and camaleon gems
4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/generators/camaleon_cms/install_template/plugin_routes.rb', line 4 def self.draw_gems res = [] dirs = [] + Dir["#{apps_dir}/plugins/*"] + Dir["#{apps_dir}/themes/*"] dirs.each do |path| next if [".", ".."].include?(path) g = File.join(path, "config", "Gemfile") res << File.read(g) if File.exist?(g) end res.join("\n") end |
.enabled_apps(site, theme_slug = nil) ⇒ Object
return all enabled apps for site (themes + system + plugins) [] theme_slug: current theme slug
178 179 180 181 182 183 184 |
# File 'lib/plugin_routes.rb', line 178 def self.enabled_apps(site, theme_slug = nil) theme_slug = theme_slug || site.get_theme_slug r = cache_variable("enabled_apps_#{site.id}_#{theme_slug}") return r unless r.nil? res = [system_info] + enabled_plugins(site) + [theme_info(theme_slug)] cache_variable("enabled_apps_#{site.id}_#{theme_slug}", res) end |
.enabled_plugins(site) ⇒ Object
return all enabled plugins []
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/plugin_routes.rb', line 164 def self.enabled_plugins(site) r = cache_variable("enable_plugins_site_#{site.id}") return r unless r.nil? res = [] enabled_ps = site.plugins.active.pluck(:slug) all_plugins.each do |plugin| res << plugin if enabled_ps.include?(plugin["key"]) end res = res.sort_by{|e| e["position"] || 10 } cache_variable("enable_plugins_site_#{site.id}", res) end |
.fixActionParameter(h) ⇒ Object
convert action parameter into hash
114 115 116 |
# File 'lib/plugin_routes.rb', line 114 def fixActionParameter(h) (h.is_a?(ActionController::Parameters) ? (h.permit!.to_h rescue h.to_hash) : h) end |
.get_anonymous_hooks(hook_key) ⇒ Array
return all registered anonymous hooks for hook_key
132 133 134 |
# File 'lib/plugin_routes.rb', line 132 def get_anonymous_hooks(hook_key) (@@anonymous_hooks[hook_key.to_s] || []).map{|item| item[:callback] } end |
.get_gem(name) ⇒ Object
check if a gem is available or not Arguemnts: name: name of the gem return (Boolean) true/false
395 396 397 398 399 400 401 |
# File 'lib/plugin_routes.rb', line 395 def self.get_gem(name) Gem::Specification.find_by_name(name) rescue Gem::LoadError false rescue Gem.available?(name) end |
.get_gem_plugins ⇒ Object
return all plugins registered as gems
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/plugin_routes.rb', line 352 def self.get_gem_plugins entries = [] Gem::Specification.each do |gem| path = gem.gem_dir config = File.join(path, "config", "camaleon_plugin.json") if File.exist?(config) p = JSON.parse(File.read(config)) p = p.with_indifferent_access rescue p p["key"] = gem.name if p["key"].nil? # TODO REVIEW ERROR FOR conflict plugin keys p["version"] = gem.version.to_s p["path"] = path p["kind"] = "plugin" p["descr"] = gem.description unless p["descr"].present? p["gem_mode"] = true entries << p end end entries end |
.get_gem_themes ⇒ Object
return all themes registered as gems
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/plugin_routes.rb', line 373 def self.get_gem_themes entries = [] Gem::Specification.each do |gem| path = gem.gem_dir config = File.join(path, "config", "camaleon_theme.json") if File.exist?(config) p = JSON.parse(File.read(config)) p = p.with_indifferent_access rescue p p["key"] = gem.name if p["key"].nil? # TODO REVIEW ERROR FOR conflict plugin keys p["path"] = path p["kind"] = "theme" p["gem_mode"] = true entries << p end end entries end |
.get_sites ⇒ Object
return all sites registered for Plugin routes
254 255 256 257 258 259 260 |
# File 'lib/plugin_routes.rb', line 254 def self.get_sites begin @@all_sites ||= CamaleonCms::Site.order(id: :asc).all.to_a rescue [] end end |
.get_user_class_name ⇒ Object
return the class name for user model
145 146 147 |
# File 'lib/plugin_routes.rb', line 145 def get_user_class_name static_system_info['user_model'].presence || 'CamaleonCms::User' end |
.isRails4? ⇒ Boolean
101 102 103 |
# File 'lib/plugin_routes.rb', line 101 def isRails4? Rails.version.to_s[0].to_i == 4 end |
.isRails5? ⇒ Boolean
105 106 107 |
# File 'lib/plugin_routes.rb', line 105 def isRails5? Rails.version.to_s[0].to_i == 5 end |
.isRails6? ⇒ Boolean
109 110 111 |
# File 'lib/plugin_routes.rb', line 109 def isRails6? Rails.version.to_s[0].to_i == 6 end |
.load(env = "admin") ⇒ Object
load plugin routes if it is enabled
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/plugin_routes.rb', line 7 def self.load(env = "admin") plugins = all_enabled_plugins res = "" if env == "front" res << "namespace :plugins do \n" plugins.each do |plugin| res << "namespace '#{plugin["key"]}' do \n" res << "#{File.open(File.join(plugin["path"], "config", "routes_#{env}.txt")).read}\n" rescue "" res << "end\n" end res << "end\n" elsif env == "admin" # admin res << "scope 'admin', as: 'admin' do \n" res << "namespace :plugins do \n" plugins.each do |plugin| res << "namespace '#{plugin["key"]}' do \n" res << "#{File.open(File.join(plugin["path"], "config", "routes_#{env}.txt")).read}\n" rescue "" res << "end\n" end res << "end\n" res << "end\n" else # main plugins.each do |plugin| res << "#{File.open(File.join(plugin["path"], "config", "routes_#{env}.txt")).read}\n" rescue "" end end res + load_themes(env) end |
.load_themes(env = "admin") ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/plugin_routes.rb', line 37 def self.load_themes(env = "admin") plugins = all_enabled_themes res = "" if env == "front" res << "namespace :themes do \n" plugins.each do |plugin| res << "namespace '#{plugin["key"]}' do \n" res << "#{File.open(File.join(plugin["path"], "config", "routes_#{env}.txt")).read}\n" rescue "" res << "end\n" end res << "end\n" elsif env == "admin" # admin res << "scope 'admin', as: 'admin' do \n" res << "namespace :themes do \n" plugins.each do |plugin| res << "namespace '#{plugin["key"]}' do \n" res << "#{File.open(File.join(plugin["path"], "config", "routes_#{env}.txt")).read}\n" rescue "" res << "end\n" end res << "end\n" res << "end\n" else # main plugins.each do |plugin| res << "#{File.open(File.join(plugin["path"], "config", "routes_#{env}.txt")).read}\n" rescue "" end end res end |
.migration_class ⇒ Object
410 411 412 |
# File 'lib/plugin_routes.rb', line 410 def self.migration_class isRails4? ? ActiveRecord::Migration : ActiveRecord::Migration[4.2] end |
.plugin_info(plugin_key) ⇒ Object
return plugin information
69 70 71 72 |
# File 'lib/plugin_routes.rb', line 69 def plugin_info(plugin_key) self.all_plugins.each{|p| return p if p["key"] == plugin_key || p['path'].split('/').last == plugin_key } nil end |
.reload ⇒ Object
reload routes
151 152 153 154 155 156 |
# File 'lib/plugin_routes.rb', line 151 def self.reload @@all_sites = nil @@_vars.each { |v| class_variable_set("@@cache_#{v}", nil) } Rails.application.reload_routes! @@_after_reload.uniq.each{|r| eval(r) } end |
.remove_anonymous_hook(hook_key, hook_id) ⇒ Array
return all registered anonymous hooks for hook_key
140 141 142 |
# File 'lib/plugin_routes.rb', line 140 def remove_anonymous_hook(hook_key, hook_id) (@@anonymous_hooks[hook_key.to_s] || []).delete_if{|item| item[:id] == hook_id } end |
.site_plugin_helpers(site) ⇒ Object
all helpers of enabled plugins for site
217 218 219 220 221 222 223 224 |
# File 'lib/plugin_routes.rb', line 217 def self.site_plugin_helpers(site) r = cache_variable("site_plugin_helpers"); return r unless r.nil? res = [] enabled_apps(site).each do |settings| res += settings["helpers"] if settings["helpers"].present? end cache_variable("site_plugin_helpers", res) end |
.static_system_info ⇒ Object Also known as: system_info
return system static settings (config.json values)
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/plugin_routes.rb', line 82 def static_system_info r = cache_variable("statis_system_info"); return r unless r.nil? settings = {} gem_settings = File.join($camaleon_engine_dir, "config", "system.json") app_settings = Rails.root.join("config", "system.json") settings = settings.merge(JSON.parse(File.read(gem_settings))) if File.exist?(gem_settings) settings = settings.merge(JSON.parse(File.read(app_settings))) if File.exist?(app_settings) # custom settings settings["key"] = "system" settings["path"] = '' settings["kind"] = "system" settings["hooks"]["on_notification"] ||= [] cache_variable("statis_system_info", settings) end |
.theme_info(theme_name) ⇒ Object
return theme information if theme_name is nil, the use current site theme
76 77 78 79 |
# File 'lib/plugin_routes.rb', line 76 def theme_info(theme_name) self.all_themes.each{|p| return p if p["key"] == theme_name } nil end |