Module: CanvasSync::MiscHelper

Defined in:
lib/canvas_sync/misc_helper.rb

Constant Summary collapse

MigrationClass =
Rails.version < '5.0' ? ActiveRecord::Migration : ActiveRecord::Migration[4.2]

Class Method Summary collapse

Class Method Details

.invoke_task(job) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/canvas_sync/misc_helper.rb', line 15

def self.invoke_task(job)
  job = job.symbolize_keys

  job_args = job[:args] || job[:parameters] || []
  job_kwargs = job[:kwargs] || {}

  if (mthd = job[:method]) && !(job[:class] || job[:instance_of] || job[:model])
    if mthd.include?('#')
      clazz, method = clazz.split("#")
      job[:instance_of] = clazz
      job[:method] = method
    elsif mthd.include?('.')
      clazz, method = mthd.split(".")
      job[:class] = clazz
      job[:method] = method
    end
  end

  if job[:model]
    # TODO Support globalid
    model_class = load_constant(job[:model])
    find_by = job[:find_by]
    target = find_by.is_a?(Hash) ? model_class.find_by(find_by) : model_class.find_by(id: find_by)
    target.send(job[:method], *job_args, **job_kwargs)
  elsif job[:class]
    target = load_constant(job[:class])
    target.send(job[:method], *job_args, **job_kwargs)
  elsif job[:instance_of]
    target = load_constant(job[:instance_of]).new
    target.send(job[:method], *job_args, **job_kwargs)
  elsif job[:job]
    job_class = load_constant(job[:job])
    job_class = job_class.set(job[:options]) if job[:options].present?
    if job_class < ActiveJob::Base
      job_class.perform_later(*job_args, **job_kwargs)
    else
      job_args << job_kwargs.symbolize_keys if job_kwargs
      # job_args[-1] = job_args[-1].symbolize_keys if job_args[-1].is_a?(Hash)
      job_class.perform_async(*job_args)
    end
  end
end

.load_constant(const) ⇒ Object



58
59
60
61
# File 'lib/canvas_sync/misc_helper.rb', line 58

def self.load_constant(const)
  const = const.constantize if const.is_a?(String)
  const
end

.to_boolean(v) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/canvas_sync/misc_helper.rb', line 7

def self.to_boolean(v)
  if Rails.version < '5.0'
    ActiveRecord::Type::Boolean.new.type_cast_from_user(v)
  else
    ActiveRecord::Type::Boolean.new.deserialize(v)
  end
end