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.7.1.1'.freeze
@@paths =
{}

Class Method Summary collapse

Class Method Details

.add_forms_path(*paths) ⇒ 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 internal @@paths variable.

Adding path is best done in plugin module 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(Rails.root.join('app/forms'), Rails.root.join('app/reports/forms'))


45
46
47
48
49
50
51
# File 'lib/drg_cms.rb', line 45

def self.add_forms_path(*paths)
  if @@paths[:forms].nil?
    # DrgCms own forms path by default
    @@paths[:forms] = [File.expand_path("../../app/forms", __FILE__)]
  end
  @@paths[:forms] += paths
end

.add_patches_path(path) ⇒ Object

Patching is one of the rubies best strengths 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'


75
76
77
# File 'lib/drg_cms.rb', line 75

def self.add_patches_path(path)
  self.add_path(:patches, path)
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 module 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.expand_path('patches', __FILE__), :patches)


92
93
94
95
# File 'lib/drg_cms.rb', line 92

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.

Parameters:

  • Optional (String)

    key to be deleted



162
163
164
165
166
167
168
169
170
171
# File 'lib/drg_cms.rb', line 162

def self.cache_clear(key = nil)
  return unless Rails.cache
  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

Returns:

  • (Object)

    Data returned from cache



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/drg_cms.rb', line 180

def self.cache_read(keys)
  data = if redis_cache_store?
           tmp_keys  = keys.dup
           first_key = tmp_keys.shift
           cached    = Rails.cache.redis.hget(first_key, tmp_keys.join(''))
           cached ? Marshal.load(cached) : nil
         else
           Rails.cache.read(keys.join(''))
         end
  return data if data
  return nil unless block_given?

  self.cache_write(keys, yield)
end

.cache_write(keys, data) ⇒ Object

Write data to cache

Parameters:

  • keys (Array)

    : array of keys

  • data (Object)

    : Document object

Returns:

  • (Object)

    data so dc_cache_write can be used as last statement in method.



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?
    tmp_keys  = keys.dup
    first_key = tmp_keys.shift
    Rails.cache.redis.hset(first_key, tmp_keys.join(''), Marshal.dump(data))
  else
    Rails.cache.write(keys.join(''), data)
  end
  data
end

.cached(model, id) ⇒ Object

Reads data from mongoid cache. Mongoid has it’s own cache, which can be used to cache results on single request.

Parameters:

  • class: (model)

    Model name

  • String (id)

    || BSON::ObjectId : Id

Returns:

  • (Object)

    data document.



223
224
225
# File 'lib/drg_cms.rb', line 223

def self.cached(model, id)
  id ? Mongo::QueryCache.cache { model.find(id) } : nil
end

.from_root(file = nil) ⇒ Object

Will return name of file relative to drg_cms gem root



112
113
114
# File 'lib/drg_cms.rb', line 112

def self.from_root(file=nil)
  File.expand_path("../../#{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.expand_path("../../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 useful in development for debugging model errors or when updating multiple collections and each save must be checked if successful.

Parameters:

  • Document (Document)

    object which will be checked

  • If (Boolean)

    true method should end in runtime error. Default = false.

Returns:

  • (String)

    Error messages or empty string if everything is OK.



135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/drg_cms.rb', line 135

def self.model_check(document, crash = false)
  return nil unless document.errors.any?

  msg = document.errors.inject('') { |r, error| r << "#{error.attribute}: #{error.message}\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)



105
106
107
# File 'lib/drg_cms.rb', line 105

def self.paths(key)
  @@paths[key]
end

.redis_cache_store?Boolean

Determines if redis cache store is active

Returns:

  • (Boolean)

    : True if redis cache store is active



153
154
155
# File 'lib/drg_cms.rb', line 153

def self.redis_cache_store?
  (Rails.application.config.cache_store.first == :redis_cache_store) rescue false
end

.routesObject

All Routes required by DrgCms.

Usage: put DrgCms.routes line anywhere into your application routes file



233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/drg_cms.rb', line 233

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