Class: Organization

Inherits:
ApplicationRecord show all
Includes:
Mumuki::Domain::Helpers::Organization, Mumuki::Domain::Syncable, Mumukit::Login::OrganizationHelpers
Defined in:
app/models/organization.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mumuki::Domain::Helpers::Organization

#base?, #central?, #domain, #platform_class_name, #slug, #switch!, #test?, #to_param, #to_s, #url, #url_for

Methods included from Mumuki::Domain::Syncable

#platform_class_name, #sync_key

Methods inherited from ApplicationRecord

aggregate_of, all_except, defaults, #delete, #destroy!, numbered, organic_on, resource_fields, #save, #save_and_notify!, #save_and_notify_changes!, serialize_symbolized_hash_array, #update_and_notify!, update_or_create!, whitelist_attributes

Class Method Details

.accessible_as(user, role) ⇒ Object



90
91
92
# File 'app/models/organization.rb', line 90

def self.accessible_as(user, role)
  all.select { |it| it.public? || user.has_permission?(role, it.slug) }
end

.baseObject



144
145
146
# File 'app/models/organization.rb', line 144

def base
  find_by name: 'base'
end

.centralObject



140
141
142
# File 'app/models/organization.rb', line 140

def central
  find_by name: 'central'
end

.in_path(content) ⇒ Object

Answers organizations that have the given item in their paths.

Warning: unlike ‘in_path?`, this method does only work with content - child - items instead of both kind of items - content and content containers.

See ‘Organization#in_path?`



163
164
165
# File 'app/models/organization.rb', line 163

def in_path(content)
  joins(:usages).where('usages.item': content).distinct
end

.silenced?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'app/models/organization.rb', line 148

def silenced?
  !Mumukit::Platform::Organization.current? || current.silent?
end

.sync_key_id_fieldObject



152
153
154
# File 'app/models/organization.rb', line 152

def sync_key_id_field
  :name
end

Instance Method Details

#accessible_exams_for(user) ⇒ Object



78
79
80
# File 'app/models/organization.rb', line 78

def accessible_exams_for(user)
  exams.select { |exam| exam.accessible_for?(user) }
end

#ask_for_help_enabled?(user = nil) ⇒ Boolean

Tells if the given user can ask for help in this organization

Warning: this method does not strictly check user’s permission

Returns:

  • (Boolean)


106
107
108
# File 'app/models/organization.rb', line 106

def ask_for_help_enabled?(user = nil)
  report_issue_enabled? || community_link.present? || can_create_discussions?(user)
end

#can_create_discussions?(user = nil) ⇒ Boolean

Tells if the given user can create discussion in this organization

This is true only when this organization has a forum and the user has the discusser pseudo-permission

Returns:

  • (Boolean)


115
116
117
# File 'app/models/organization.rb', line 115

def can_create_discussions?(user = nil)
  forum_enabled? && (!user || user.discusser_of?(self))
end

#drop_usage_indices!Object



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

def drop_usage_indices!
  usages.destroy_all
end

#explain_error(code, advice) ⇒ Object



86
87
88
# File 'app/models/organization.rb', line 86

def explain_error(code, advice)
  errors_explanations.try { |it| it[code.to_s] } || I18n.t(advice)
end

#has_login_method?(login_method) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'app/models/organization.rb', line 82

def ()
  self..include? .to_s
end

#import_from_resource_h!(resource_h) ⇒ Object



119
120
121
122
123
# File 'app/models/organization.rb', line 119

def import_from_resource_h!(resource_h)
  attrs = self.class.slice_resource_h resource_h
  attrs[:book] = Book.locate! attrs[:book]
  update! attrs
end

#in_path?(item) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/models/organization.rb', line 45

def in_path?(item)
  usages.exists?(item: item) || usages.exists?(parent_item: item)
end

#index_usage_of!(item, parent) ⇒ Object



74
75
76
# File 'app/models/organization.rb', line 74

def index_usage_of!(item, parent)
  Usage.create! organization: self, item: item, parent_item: parent
end

#notify_assignments_by!(submitter) ⇒ Object



53
54
55
# File 'app/models/organization.rb', line 53

def notify_assignments_by!(submitter)
  notify_assignments! assignments.where(submitter_id: submitter.id)
end

#notify_recent_assignments!(date) ⇒ Object



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

def notify_recent_assignments!(date)
  notify_assignments! assignments.where('assignments.updated_at > ?', date)
end

#reindex_usages!Object



61
62
63
64
65
66
67
68
# File 'app/models/organization.rb', line 61

def reindex_usages!
  transaction do
    drop_usage_indices!
    book.index_usage! self
    exams.each { |exam| exam.index_usage! self }
  end
  reload
end

#silent?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'app/models/organization.rb', line 57

def silent?
  test?
end

#site_nameObject



98
99
100
# File 'app/models/organization.rb', line 98

def site_name
  central? ? 'mumuki' : name
end

#title_suffixObject



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

def title_suffix
  central? ? '' : " - #{book.name}"
end

#to_resource_hObject



125
126
127
# File 'app/models/organization.rb', line 125

def to_resource_h
  super.merge(book: book.slug)
end