Module: Agile

Defined in:
lib/agile.rb,
lib/agile/engine.rb,
lib/agile/version.rb,
lib/agile/configuration.rb,
lib/generators/agile/new_form_generator.rb

Overview

– Copyright © 2024+ Damjan Rems

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++

Defined Under Namespace

Modules: Generators Classes: Configuration, Engine

Constant Summary collapse

VERSION =

:nodoc:

'0.0.0.2'.freeze
@@paths =
{}
@@configuration =
nil

Class Method Summary collapse

Class Method Details

.add_forms_path(path) ⇒ Object

When new plugin with its own AgileRails forms is added to application, path to forms directory must be sent to Agile module. Paths are saved into @@paths hash 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

Agile.add_forms_path File.expand_path("../../app/forms", __FILE__)


64
65
66
67
68
69
70
71
# File 'lib/agile.rb', line 64

def self.add_forms_path(path) 
  if @@paths[:forms].nil?
    @@paths[:forms] = []
    # Agile forms path
    @@paths[:forms] << File.expand_path('../../app/forms', __FILE__)
  end
  @@paths[:forms] << path  
end

.add_patches_path(path) ⇒ Object

Not used anymore!

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 Agile.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

Agile.add_patches_path File.dirname(__FILE__) + '/patches'


97
98
99
100
101
# File 'lib/agile.rb', line 97

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:

Agile.add_path(File.expand_path('patches', __FILE__), :patches)


116
117
118
119
# File 'lib/agile.rb', line 116

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



187
188
189
190
191
192
193
194
195
# File 'lib/agile.rb', line 187

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

Returns:

  • (Object)

    Data returned from cache



204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/agile.rb', line 204

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 unless block_given?

  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 cache_write can be used as last statement in method.



227
228
229
230
231
232
233
234
235
236
# File 'lib/agile.rb', line 227

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

.config(what, new_value = nil) ⇒ Object

If new_value is nil method will return current value of application configuration. Otherwise it will set new value to the configurations.

Parameters:

  • what (Object)

    : Config name

  • new_value (Object) (defaults to: nil)

    : new config value. Default = nil.



38
39
40
41
42
43
44
45
# File 'lib/agile.rb', line 38

def self.config(what, new_value = nil)
  @@configuration ||= Agile::Configuration.defaults
  if new_value
    @@configuration[what.to_sym] = new_value
  else
    @@configuration[what.to_sym]
  end
end

.dump_exception(exception) ⇒ Object

Will dump exception to rails log and console.

Parameters:

exception

Object: Exception caught



244
245
246
247
248
# File 'lib/agile.rb', line 244

def self.dump_exception(exception)
  msg = [exception.message, *exception.backtrace].join($/)
  pp msg
  Rails.logger.error msg
end

.from_root(file = nil) ⇒ Object

Will return name of file relative to agile_rails gem root



137
138
139
# File 'lib/agile.rb', line 137

def self.from_root(file = nil)
  File.expand_path("../../#{file}", __FILE__).to_s
end

.model(model_name) ⇒ Object

Returns source file name of required model.

Used wherever end user wants to extend basic Agile model. Model source definition file is tricky to require since it is included in Agile gem and thus a moving target. Whenever gem version changes file location changes with it. This way programmer doesn’t have to care about actual source file location.

Parameters:

model_name

String. Must be passed in lower case, just like the file name is.

Example:

require Agile.model 'ar_page'


27
28
29
# File 'lib/agile.rb', line 27

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 usefull in development for debuging model errors or when updating multiple collections and each save must be checked if succesfull.

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.



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

def self.model_check(document, crash = false)
  return 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 = Agile.paths(:forms) patches_paths = Agile.paths(:patches)



130
131
132
# File 'lib/agile.rb', line 130

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



178
179
180
# File 'lib/agile.rb', line 178

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

.routesObject

All Routes required by agile_rails.

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



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/agile.rb', line 256

def self.routes
  Rails.application.routes.draw do
    controller :agile_common do
      post 'agile/autocomplete'            => :autocomplete
      post 'agile_common/ad_click'         => :ad_click
      get 'agile/toggle_edit_mode'         => :toggle_edit_mode
      match 'agile_common/process_login'   => :process_login, via: [:put, :post]
      get 'agile_common/login'             => :login
      get 'agile_common/logout'            => :logout
      get 'agile_common/copy_clipboard'    => :copy_clipboard
      match 'agile_common/paste_clipboard' => :paste_clipboard, via: [:get, :post]
      put 'agile_common/restore_from_journal'  => :restore_from_journal
      get 'agile_common/add_json_ld_schema'    => :add_json_ld_schema
      get 'agile_common/help'                  => :help
      put 'agile_common/poll_submit'           => :poll_submit
    end

    match 'elfinder'     => 'agile_elfinder#connector', via: [:get, :post]
    get   'agile/login'  => 'agile#login'
    get   'agile/logout' => 'agile#logout'
    match 'agile/run'    => 'agile#run', via: [:get, :put, :post]

    resources :agile
  end
end