Module: DrgCms
- Defined in:
- lib/drg_cms.rb,
lib/drg_cms/engine.rb,
lib/drg_cms/version.rb
Overview
drg_cms gem version
Defined Under Namespace
Classes: Engine
Constant Summary collapse
- VERSION =
:nodoc:
'0.6.1.9'.freeze
- @@paths =
{}
Class Method Summary collapse
-
.add_forms_path(path) ⇒ Object
When new plugin with its own DRG forms is added to application, path to forms directory must be send to DrgCms module.
-
.add_patches_path(path) ⇒ Object
Patching is one of the rubies best strenghts and also its curse.
-
.add_path(type, path) ⇒ Object
General add path method.
-
.cache_clear(key = nil) ⇒ Object
Clear cache.
-
.cache_read(keys) ⇒ Object
Read from cache.
-
.cache_write(keys, data) ⇒ Object
Write data to cache.
-
.from_root(file = nil) ⇒ Object
Will return name of file relative to drg_cms gem root.
- .model(model_name) ⇒ Object
-
.model_check(document, crash = false) ⇒ String
Checks if any errors exist on document and writes error log.
-
.paths(key) ⇒ Object
Will return value saved to internal @@paths hash.
-
.redis_cache_store? ⇒ Boolean
Determines if redis cache store is active.
-
.routes ⇒ Object
All Routes required by DrgCms.
Class Method Details
.add_forms_path(path) ⇒ Object
When new plugin with its own DRG forms is added to application, path to forms directory must be send to DrgCms module. Paths are saved into @@paths hash variable.
Adding path is best done in plugin mudule initialization code.
Parameters:
- path
-
String. Path to forms directory
Example:
# As used in MyPlugin plugin.
require "my_plugin/engine"
module MyPlugin
end
DrgCms.add_forms_path File.("../../app/forms", __FILE__)
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/drg_cms.rb', line 45 def self.add_forms_path(path) if @@paths[:forms].nil? @@paths[:forms] = [] # default application forms path # @@paths[:forms] << Rails.root.join('app/forms') # DrgCms forms path @@paths[:forms] << File.("../../app/forms", __FILE__) end @@paths[:forms] << path end |
.add_patches_path(path) ⇒ Object
Patching is one of the rubies best strenghts and also its curse. Loading patches in development has become real problem for developers. This is my way of patch loading.
Preferred location for patch files is lib/patches. But can be located anywhere in Rails application path. Add DrgCms.add_forms_path to initialization part and pass directory name where patching files are located as parameter.
Method will also load patch file so loading in config/initializers is not required.
Parameters:
- path
-
String. Path to patches directory
Example:
# As used in MyPlugin plugin.
require "my_plugin/engine"
module MyPlugin
end
DrgCms.add_patches_path File.dirname(__FILE__) + '/patches'
78 79 80 81 82 |
# File 'lib/drg_cms.rb', line 78 def self.add_patches_path(path) self.add_path(:patches, path) # Dir["#{path}/**/*.rb"].each { |path| p path; require_dependency path } # Dir["#{path}/**/*.rb"].each { |file| p file; require file } end |
.add_path(type, path) ⇒ Object
General add path method. Paths are saved into @@paths hash variable. Paths can then be reused in different parts of application.
Adding paths is best done in plugin mudule initialization code.
Parameters:
- type
-
Symbol. Defines type of data. Current used values are :forms, :patches
- path
-
String. Path or string which will be added to @@paths hash.
Example:
DrgCms.add_path(File.('patches', __FILE__), :patches)
97 98 99 100 |
# File 'lib/drg_cms.rb', line 97 def self.add_path(type, path) @@paths[type] ||= [] @@paths[type] << path end |
.cache_clear(key = nil) ⇒ Object
Clear cache. If key is specified only this key will be deleted.
167 168 169 170 171 172 173 174 175 |
# File 'lib/drg_cms.rb', line 167 def self.cache_clear(key = nil) return Rails.cache.clear if key.nil? if redis_cache_store? Rails.cache.redis.del(key) else Rails.cache.delete_matched("#{key}*") end end |
.cache_read(keys) ⇒ Object
Read from cache
184 185 186 187 188 189 190 191 192 193 |
# File 'lib/drg_cms.rb', line 184 def self.cache_read(keys) if redis_cache_store? keys = keys.dup first = keys.shift data = Rails.cache.redis.hget(first, keys.join('')) data ? Marshal.load(data) : nil else Rails.cache.read(keys.join('')) end end |
.cache_write(keys, data) ⇒ Object
Write data to cache
203 204 205 206 207 208 209 210 211 212 |
# File 'lib/drg_cms.rb', line 203 def self.cache_write(keys, data) if redis_cache_store? keys = keys.dup first = keys.shift Rails.cache.redis.hset(first, keys.join(''), Marshal.dump(data)) else Rails.cache.write(keys.join(''), data) end data end |
.from_root(file = nil) ⇒ Object
Will return name of file relative to drg_cms gem root
117 118 119 |
# File 'lib/drg_cms.rb', line 117 def self.from_root(file=nil) File.("../../#{file}", __FILE__).to_s end |
.model(model_name) ⇒ Object
24 25 26 |
# File 'lib/drg_cms.rb', line 24 def self.model(model_name) File.("../../app/models/#{model_name}.rb", __FILE__) end |
.model_check(document, crash = false) ⇒ String
Checks if any errors exist on document and writes error log. It can also crash if requested. This is mostly usefull in development for debuging model errors or when updating multiple collections and each save must be checked if succesfull.
140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/drg_cms.rb', line 140 def self.model_check(document, crash = false) return nil unless document.errors.any? msg = document.errors.inject('') { |r, error| r << "#{error.attribute}: #{error.}\n" } if crash && msg.size > 0 msg = "Validation errors in #{document.class}:\n" + msg pp msg Rails.logger.error(msg) raise "Validation error. See log for more information." end msg end |
.paths(key) ⇒ Object
Will return value saved to internal @@paths hash.
Parameters:
- key
-
String. Key
forms_paths = DrgCms.paths(:forms) patches_paths = DrgCms.paths(:patches)
110 111 112 |
# File 'lib/drg_cms.rb', line 110 def self.paths(key) @@paths[key] end |
.redis_cache_store? ⇒ Boolean
Determines if redis cache store is active
158 159 160 |
# File 'lib/drg_cms.rb', line 158 def self.redis_cache_store? (Rails.application.config.cache_store.first == :redis_cache_store) rescue false end |
.routes ⇒ Object
All Routes required by DrgCms.
Usage: put DrgCms.routes line anywhere into your application routes file
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/drg_cms.rb', line 220 def self.routes Rails.application.routes.draw do controller :dc_common do post 'dc_common/autocomplete' => :autocomplete post 'dc_common/ad_click' => :ad_click get 'dc_common/toggle_edit_mode' => :toggle_edit_mode match 'dc_common/process_login' => :process_login, via: [:put, :post] get 'dc_common/login' => :login get 'dc_common/logout' => :logout get 'dc_common/copy_clipboard' => :copy_clipboard match 'dc_common/paste_clipboard' => :paste_clipboard, via: [:get, :post] put 'dc_common/restore_from_journal' => :restore_from_journal get 'dc_common/add_json_ld_schema' => :add_json_ld_schema get 'dc_common/help' => :help end match 'elfinder' => 'dc_elfinder#connector', via: [:get, :post] get 'cms/login' => 'cmsedit#login' get 'cms/logout' => 'cmsedit#logout' match 'cmsedit/run' => 'cmsedit#run', via: [:get, :put, :post] resources :cmsedit end end |