Module: EffectiveResources

Includes:
EffectiveGem
Defined in:
lib/effective_resources.rb,
lib/effective_resources/engine.rb,
lib/effective_resources/version.rb,
lib/generators/effective_resources/install_generator.rb

Defined Under Namespace

Modules: Generators Classes: Engine

Constant Summary collapse

VERSION =
'1.9.4'.freeze

Class Method Summary collapse

Class Method Details

.advance_date(date, business_days: 1, holidays: [:ca, :observed]) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/effective_resources.rb', line 67

def self.advance_date(date, business_days: 1, holidays: [:ca, :observed])
  raise('business_days must be an integer <= 365') unless business_days.kind_of?(Integer) && business_days <= 365

  business_days.times do
    loop do
      date = date + 1.day
      break if business_day?(date, holidays: holidays)
    end
  end

  date
end

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



26
27
28
# File 'lib/effective_resources.rb', line 26

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)


13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/effective_resources.rb', line 13

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)

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

.best(name) ⇒ Object

This looks up the best class give the name If the Tenant is present, use those classes first.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/effective_resources.rb', line 38

def self.best(name)
  klass = if defined?(Tenant)
    ('::' + Tenant.module_name + '::' + name).safe_constantize ||
    ('::' + Tenant.module_name + '::Effective::' + name).safe_constantize
  end

  klass ||= begin
    ('::' + name).safe_constantize ||
    ('::Effective::' + name).safe_constantize
  end

  raise("unable to find best #{name}") if klass.blank?

  klass
end

.business_day?(date, holidays: [:ca, :observed]) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
# File 'lib/effective_resources.rb', line 80

def self.business_day?(date, holidays: [:ca, :observed])
  require 'holidays' unless defined?(Holidays)
  date.wday != 0 && date.wday != 6 && Holidays.on(date, *holidays).blank?
end

.clone_blob(blob, options = {}) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/effective_resources.rb', line 86

def self.clone_blob(blob, options = {})
  raise('expected an ActiveStorage::Blob') unless blob.kind_of?(ActiveStorage::Blob)

  atts = {
    filename: blob.filename,
    byte_size: blob.byte_size,
    checksum: blob.checksum,
    content_type: blob.content_type,
    metadata: blob.,
  }.merge(options)

  service = blob.service
  duplicate = ActiveStorage::Blob.create_before_direct_upload!(**atts)

  case service.class.name
  when 'ActiveStorage::Service::S3Service'
    bucket = service.bucket
    object = bucket.object(blob.key)
    object.copy_to(bucket.object(duplicate.key))
  when 'ActiveStorage::Service::DiskService'
    path = service.path_for(blob.key)
    duplicate_path = service.path_for(duplicate.key)
    FileUtils.mkdir_p(File.dirname(duplicate_path))
    FileUtils.ln(path, duplicate_path) if File.exists?(path)
  else
    raise "unknown storage service #{service.class.name}"
  end

  duplicate
end

.config_keysObject



7
8
9
# File 'lib/effective_resources.rb', line 7

def self.config_keys
  [:authorization_method, :default_submits]
end

.default_submitsObject



30
31
32
# File 'lib/effective_resources.rb', line 30

def self.default_submits
  (['Save', 'Continue', 'Add New'] & Array(config.default_submits)).inject({}) { |h, v| h[v] = true; h }
end

.deliver_methodObject



62
63
64
65
# File 'lib/effective_resources.rb', line 62

def self.deliver_method
  config = Rails.application.config
  (config.respond_to?(:active_job) && config.active_job.queue_adapter) ? :deliver_later : :deliver_now
end

.truthy?(value) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/effective_resources.rb', line 54

def self.truthy?(value)
  if defined?(::ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES)  # Rails <5
    ::ActiveRecord::ConnectionAdapters::Column::TRUE_VALUES.include?(value)
  else
    ::ActiveRecord::Type::Boolean.new.cast(value)
  end
end