Class: PluginRoutes

Inherits:
Object
  • Object
show all
Defined in:
lib/plugin_routes.rb,
lib/generators/camaleon_cms/install_template/plugin_routes.rb

Constant Summary collapse

@@_vars =
[]

Class Method Summary collapse

Class Method Details

.all_appsObject

return all apps loaded


311
312
313
# File 'lib/plugin_routes.rb', line 311

def self.all_apps
  all_plugins+all_themes
end

.all_enabled_appsObject

return all enabled apps as []: system, themes, plugins


167
168
169
# File 'lib/plugin_routes.rb', line 167

def self.all_enabled_apps
  [system_info] + all_enabled_themes + all_enabled_plugins
end

.all_enabled_pluginsObject

return all enabled plugins (a theme is enabled if at least one site has installed)


183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/plugin_routes.rb', line 183

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_themesObject

return all enabled themes (a theme is enabled if at least one site is assigned)


172
173
174
175
176
177
178
179
180
# File 'lib/plugin_routes.rb', line 172

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_localesObject

return all locales for all sites joined by |


249
250
251
252
253
254
255
256
# File 'lib/plugin_routes.rb', line 249

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_pluginsObject

return all plugins located in cms and in this project


264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/plugin_routes.rb', line 264

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_themesObject

return an array of all themes installed for all sites


287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/plugin_routes.rb', line 287

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

.apps_dirObject

return apps directory path


259
260
261
# File 'lib/plugin_routes.rb', line 259

def self.apps_dir
  Rails.root.join("app", "apps").to_s
end

.cache_variable(var_name, value = nil) ⇒ Object


229
230
231
232
233
234
235
236
237
# File 'lib/plugin_routes.rb', line 229

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

.destroy_plugin(plugin_key) ⇒ Object

destroy plugin


218
219
220
221
# File 'lib/plugin_routes.rb', line 218

def self.destroy_plugin(plugin_key)
  FileUtils.rm_r(Rails.root.join("app", "apps", "plugins", plugin_key)) rescue ""
  PluginRoutes.reload
end

.destroy_theme(theme_key) ⇒ Object

destroy theme


224
225
226
227
# File 'lib/plugin_routes.rb', line 224

def self.destroy_theme(theme_key)
  FileUtils.rm_r(Rails.root.join("app", "apps", "themes", theme_key)) rescue ""
  PluginRoutes.reload
end

.draw_gemsObject

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


158
159
160
161
162
163
164
# File 'lib/plugin_routes.rb', line 158

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 []


144
145
146
147
148
149
150
151
152
153
154
# File 'lib/plugin_routes.rb', line 144

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

.get_gem(name) ⇒ Object

check if a gem is available or not Arguemnts: name: name of the gem return (Boolean) true/false


358
359
360
361
362
363
364
# File 'lib/plugin_routes.rb', line 358

def self.get_gem(name)
  Gem::Specification.find_by_name(name)
rescue Gem::LoadError
  false
rescue
  Gem.available?(name)
end

.get_gem_pluginsObject

return all plugins registered as gems


316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/plugin_routes.rb', line 316

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["key"] = File.basename(path)
      p["path"] = path
      p["kind"] = "plugin"
      p["gem_mode"] = true
      entries << p
    end
  end
  entries
end

.get_gem_themesObject

return all themes registered as gems


336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/plugin_routes.rb', line 336

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_sitesObject

return all sites registered for Plugin routes


240
241
242
243
244
245
246
# File 'lib/plugin_routes.rb', line 240

def self.get_sites
  begin
    @@all_sites ||= CamaleonCms::Site.order(id: :asc).all
  rescue
    []
  end
end

.load(env = "admin") ⇒ Object

load plugin routes if it is enabled


5
6
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
# File 'lib/plugin_routes.rb', line 5

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


35
36
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
# File 'lib/plugin_routes.rb', line 35

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

.main_siteObject

return the main site


119
120
121
# File 'lib/plugin_routes.rb', line 119

def self.main_site
  @@main_site ||= get_sites.first rescue nil
end

.plugin_helpersObject

all helpers of enabled plugins


207
208
209
210
211
212
213
214
215
# File 'lib/plugin_routes.rb', line 207

def self.plugin_helpers
  r = cache_variable("plugins_helper")
  return r unless r.nil?
  res = []
  all_enabled_apps.each do |settings|
    res += settings["helpers"] if settings["helpers"].present?
  end
  cache_variable("plugins_helper", res)
end

.plugin_info(plugin_key) ⇒ Object

return plugin information


66
67
68
69
# File 'lib/plugin_routes.rb', line 66

def self.plugin_info(plugin_key)
  self.all_plugins.each{|p| return p if p["key"] == plugin_key }
  nil
end

.reloadObject

reload routes


136
137
138
139
140
141
# File 'lib/plugin_routes.rb', line 136

def self.reload
  @@all_sites = nil
  @@main_site = nil
  @@_vars.each { |v| class_variable_set("@@cache_#{v}", nil) }
  Rails.application.reload_routes!
end

.site_plugin_helpers(site) ⇒ Object

all helpers of enabled plugins for site


197
198
199
200
201
202
203
204
# File 'lib/plugin_routes.rb', line 197

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_infoObject

return system static settings (config.json values)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/plugin_routes.rb', line 79

def self.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"] = (settings["hooks"]["on_notification"] || []) + ["admin_system_notifications"]
  cache_variable("statis_system_info", settings)
end

.system_info(skip_site_settings_db = false) ⇒ Object

return system settings defined in:

+ gem/config/system.json
+ app/config/system.json
+ main_site.get_meta("main_settings")

skip_site_settings_db: is a fix to avoid infite cicle


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/plugin_routes.rb', line 102

def self.system_info(skip_site_settings_db = false)
  r = cache_variable("system_info");  return r unless r.nil?
  settings = static_system_info
  unless skip_site_settings_db
    main_site = self.main_site
    begin
      settings = settings.merge(main_site.get_meta("main_settings", {}))
      settings["base_domain"] = main_site.slug if main_site.slug.present?
      settings = settings.stringify_keys
      cache_variable("system_info", settings)
    rescue
    end
  end
  settings
end

.system_info_set(key, val) ⇒ Object

update a system value key: attribute name value: new value for attribute


126
127
128
129
130
131
132
133
# File 'lib/plugin_routes.rb', line 126

def self.system_info_set(key, val)
  s = main_site
  settings = s.get_meta("main_settings", {}).stringify_keys
  settings[key] = val
  s.set_meta("main_settings", settings)
  class_variable_set("@@cache_system_info", nil)
  val
end

.theme_info(theme_name) ⇒ Object

return theme information if theme_name is nil, the use current site theme


73
74
75
76
# File 'lib/plugin_routes.rb', line 73

def self.theme_info(theme_name)
  self.all_themes.each{|p| return p if p["key"] == theme_name }
  nil
end