Class: Instance

Inherits:
ApplicationRecord show all
Includes:
CourseComponentsConcern
Defined in:
app/models/instance.rb

Defined Under Namespace

Modules: CourseComponentsConcern Classes: Announcement, Settings, UserRoleRequest

Constant Summary collapse

DEFAULT_INSTANCE_ID =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CourseComponentsConcern

#available_components, #disableable_components

Methods included from CourseComponentQueryConcern

#available_components, #disableable_components, #enabled_components, #enabled_components_keys=, #set_component_enabled_boolean, #set_component_enabled_boolean!, #undisableable_components, #user_enabled_components

Instance Attribute Details

#announcementsObject (readonly)

Note:

You are scoped by the current tenant, you might not see all.


57
# File 'app/models/instance.rb', line 57

has_many :announcements, class_name: Instance::Announcement.name, dependent: :destroy

#course_countObject (readonly)

The number of courses in the instance.


89
90
91
# File 'app/models/instance.rb', line 89

calculated :course_count, (lambda do
  Course.unscoped.where('courses.instance_id = instances.id').select("count('*')")
end)

#coursesObject (readonly)

Note:

You are scoped by the current tenant, you might not see all.


60
# File 'app/models/instance.rb', line 60

has_many :courses, dependent: :destroy

#instance_usersObject (readonly)

Note:

You are scoped by the current tenant, you might not see all.


46
# File 'app/models/instance.rb', line 46

has_many :instance_users, dependent: :destroy

#user_countObject (readonly)

The number of users in the instance.


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

calculated :user_count, (lambda do
  InstanceUser.unscoped.where('instance_users.instance_id = instances.id').select("count('*')")
end)

#usersObject (readonly)

Note:

You are scoped by the current tenant, you might not see all.


53
# File 'app/models/instance.rb', line 53

has_many :users, through: :instance_users

Class Method Details

.defaultInstance

Finds the default instance.

Returns:


13
14
15
16
17
# File 'app/models/instance.rb', line 13

def default
  @default ||= find_by(id: DEFAULT_INSTANCE_ID)
  raise 'Unknown instance. Did you run rake db:seed?' unless @default
  @default
end

.find_tenant_by_host(host) ⇒ Instance

Finds the given tenant by host.

Parameters:

  • host (String)

    The host to look up. This is case insensitive, however prefixes (such as www) are not handled automatically.

Returns:


24
25
26
# File 'app/models/instance.rb', line 24

def find_tenant_by_host(host)
  where.has { self.host.lower == host.downcase }.take
end

.find_tenant_by_host_or_default(host) ⇒ Instance

Finds the given tenant by host, falling back to the default is none is found.

Parameters:

  • host (String)

    The host to look up. This is case insensitive, however prefixes (such as www) are not handled automatically.

Returns:


33
34
35
36
37
38
39
# File 'app/models/instance.rb', line 33

def find_tenant_by_host_or_default(host)
  tenants = where.has do
    (self.host.lower == host.downcase) | (id == DEFAULT_INSTANCE_ID)
  end.to_a

  tenants.find { |tenant| !tenant.default? } || tenants.first
end

.order_by_id(direction = :asc) ⇒ Object

Orders the instances by ID.


64
# File 'app/models/instance.rb', line 64

scope :order_by_id, ->(direction = :asc) { order(id: direction) }

.use_relative_model_naming?Boolean

Returns:

  • (Boolean)

105
106
107
# File 'app/models/instance.rb', line 105

def self.use_relative_model_naming?
  true
end

Instance Method Details

#containing_userObject

Selects all the instance with user as one of its members Note: Must be used with ActsAsTenant#without_tenant block.


77
78
79
# File 'app/models/instance.rb', line 77

scope :containing_user, (lambda do |user|
  joins(:instance_users).where('instance_users.user_id = ?', user.id)
end)

#default?Boolean

Checks if the current instance is the default instance.

Returns:

  • (Boolean)

112
113
114
# File 'app/models/instance.rb', line 112

def default?
  id == DEFAULT_INSTANCE_ID
end

#hostObject

Replace the hostname of the default instance.


117
118
119
120
# File 'app/models/instance.rb', line 117

def host
  return Application.config.x.default_host if default?
  super
end