Module: EffectiveDatatables

Defined in:
lib/effective_datatables.rb,
lib/effective_datatables/engine.rb,
lib/effective_datatables/version.rb,
lib/generators/effective_datatables/install_generator.rb

Defined Under Namespace

Modules: Generators Classes: Engine

Constant Summary collapse

AVAILABLE_LOCALES =
%w(en es nl)
VERSION =
'4.27.0'.freeze

Class Method Summary collapse

Class Method Details

.authorize!(controller, action, resource) ⇒ Object

Raises:

  • (Effective::AccessDenied)


48
49
50
# File 'lib/effective_datatables.rb', line 48

def self.authorize!(controller, action, resource)
  raise Effective::AccessDenied.new('Access Denied', action, resource) unless authorized?(controller, action, resource)
end

.authorized?(controller, action, resource) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/effective_datatables.rb', line 35

def self.authorized?(controller, action, resource)
  @_exceptions ||= [Effective::AccessDenied, (CanCan::AccessDenied if defined?(CanCan)), (Pundit::NotAuthorizedError if defined?(Pundit))].compact

  return !!authorization_method unless authorization_method.respond_to?(:call)
  controller = controller.controller if controller.respond_to?(:controller) # Do the right thing with a view

  begin
    !!(controller || self).instance_exec((controller || self), action, resource, &authorization_method)
  rescue *@_exceptions
    false
  end
end

.decrypt(payload) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/effective_datatables.rb', line 77

def self.decrypt(payload)
  return unless payload.present?
  return payload if payload.kind_of?(Hash)

  attributes = message_encrypter.decrypt_and_verify(payload)

  raise 'invalid decoded inline payload' unless attributes.kind_of?(Hash)

  attributes.symbolize_keys
end

.encrypt(attributes) ⇒ Object



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

def self.encrypt(attributes)
  payload = message_encrypter.encrypt_and_sign(attributes)
end

.find(id, attributes = nil) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/effective_datatables.rb', line 52

def self.find(id, attributes = nil)
  id = id.to_s.gsub(/-\d+\z/, '').gsub('-', '/')
  klass = (id.classify.safe_constantize || id.classify.pluralize.safe_constantize)
  attributes = decrypt(attributes) || {}

  klass.try(:new, **attributes) || raise('unable to find datatable')
end

.language(locale) ⇒ Object

Locale is coming from view. I think it can be dynamic. We currently support: en, es, nl



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

def self.language(locale)
  @_languages ||= {}

  locale = :en unless AVAILABLE_LOCALES.include?(locale.to_s)

  @_languages[locale] ||= begin
    path = Gem::Specification.find_by_name('effective_datatables').gem_dir + "/app/assets/javascripts/dataTables/locales/#{locale}.lang"
    JSON.parse(File.read(path)).to_json
  end
end

.message_encrypterObject



88
89
90
91
92
93
94
95
# File 'lib/effective_datatables.rb', line 88

def self.message_encrypter
  key = (Rails.application.secret_key_base.presence rescue nil)
  key ||= (Rails.application.try(:credentials).try(:secret_key_base).presence rescue nil)
  key ||= (Rails.application.try(:secrets).try(:secret_key_base).presence rescue nil)
  key ||= 10.times.map { "".hash.to_s }.join

  ActiveSupport::MessageEncryptor.new(key.to_s.first(32))
end

.setup {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



31
32
33
# File 'lib/effective_datatables.rb', line 31

def self.setup
  yield self
end