Class: PandaPal::Organization

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
PandaPal::OrganizationConcerns::SettingsValidation, PandaPal::OrganizationConcerns::TaskScheduling
Defined in:
app/models/panda_pal/organization.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PandaPal::OrganizationConcerns::TaskScheduling

build_settings_entry, #generate_schedule, #sync_schedule

Methods included from PandaPal::OrganizationConcerns::SettingsValidation

#settings_structure, #validate_settings

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



99
100
101
102
103
104
105
106
# File 'app/models/panda_pal/organization.rb', line 99

def method_missing(method, *args, &block)
  ext = platform_org_extension_module
  if (ext.instance_method(method) rescue nil)
    ext.instance_method(method).bind_call(self, *args, &block)
  else
    super
  end
end

Class Method Details

.currentObject



49
50
51
# File 'app/models/panda_pal/organization.rb', line 49

def self.current
  find_by_name(Apartment::Tenant.current)
end

Instance Method Details

#create_api(logic, expiration: nil, uses: nil, host: nil) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/panda_pal/organization.rb', line 57

def create_api(logic, expiration: nil, uses: nil, host: nil)
  switch_tenant do
    logic = "current_organization.#{logic}" if logic.is_a?(Symbol)
    ac = ApiCall.create!(
      logic: logic,
      expiration: expiration,
      uses_remaining: uses,
    )
    ac.call_url(host: host)
  end
end

#encryption_keyObject



31
32
33
34
35
36
37
38
39
# File 'app/models/panda_pal/organization.rb', line 31

def encryption_key
  # production environment might not have loaded secret_key_base yet.
  # In that case, just read it from env.
  if (Rails.application.secrets.secret_key_base)
    Rails.application.secrets.secret_key_base[0,32]
  else
    ENV["SECRET_KEY_BASE"][0,32]
  end
end

#lti_platformObject



69
70
71
# File 'app/models/panda_pal/organization.rb', line 69

def lti_platform
  lti_platform_type.new(self)
end

#lti_platform_typeObject



73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/panda_pal/organization.rb', line 73

def lti_platform_type
  platform = PandaPal.lti_options[:platform] || 'canvas.instructure.com'
  case platform
  when 'canvas.instructure.com'
    PandaPal::Platform::Canvas
  # when 'bridgeapp.com'
    # TODO Add support for Bridge?
  else
    raise "Unknown platform '#{platform}'"
  end
end

#rename!(new_name) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'app/models/panda_pal/organization.rb', line 85

def rename!(new_name)
  do_switch = Apartment::Tenant.current == name
  ActiveRecord::Base.connection.execute(
    "ALTER SCHEMA \"#{name}\" RENAME TO \"#{new_name}\";"
  )
  self.class.where(id: id).update_all(name: new_name)
  reload
  switch_tenant if do_switch
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'app/models/panda_pal/organization.rb', line 95

def respond_to_missing?(name, include_private = false)
  (platform_org_extension_module&.instance_method(name) rescue nil) || super
end

#switch_tenant(&block) ⇒ Object



41
42
43
44
45
46
47
# File 'app/models/panda_pal/organization.rb', line 41

def switch_tenant(&block)
  if block_given?
    Apartment::Tenant.switch(name, &block)
  else
    Apartment::Tenant.switch!(name)
  end
end