Class: Organization

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WithOrganizationStatus

#status, #validate_active!, #validate_active_for!

Methods included from Mumuki::Domain::Area

#to_mumukit_grant, #to_mumukit_slug

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

#base?, #central?, #domain, #immersed_in?, #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

active_between, aggregate_of, all_except, defaults, #delete, #destroy!, enum_prefixed_translations_for, numbered, organic_on, resource_fields, #save, #save_and_notify!, #save_and_notify_changes!, serialize_symbolized_hash_array, teaser_on, #update_and_notify!, update_or_create!, whitelist_attributes, with_pg_retry, with_temporary_token

Methods included from WithPgLock

#with_pg_lock

Class Method Details

.accessible_as(user, role) ⇒ Object



100
101
102
# File 'app/models/organization.rb', line 100

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

.baseObject



220
221
222
# File 'app/models/organization.rb', line 220

def base
  find_by name: 'base'
end

.centralObject



216
217
218
# File 'app/models/organization.rb', line 216

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?`



239
240
241
# File 'app/models/organization.rb', line 239

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

.silenced?Boolean

Returns:

  • (Boolean)


224
225
226
# File 'app/models/organization.rb', line 224

def silenced?
  !current? || current.silent?
end

.sync_key_id_fieldObject



228
229
230
# File 'app/models/organization.rb', line 228

def sync_key_id_field
  :name
end

Instance Method Details

#accessible_exams_for(user) ⇒ Object



88
89
90
# File 'app/models/organization.rb', line 88

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

#activity_start_date(default_date) ⇒ Object



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

def activity_start_date(default_date)
  [default_date, in_preparation_until&.to_date].compact.max
end

#all_contentsObject



185
186
187
188
189
# File 'app/models/organization.rb', line 185

def all_contents
  Usage.where(organization: self)
       .group_by(&:item_type)
       .flat_map { |item_type, item| item_type.constantize.where(id: item.map(&:item_id)) }
end

#ask_for_help_enabled?(user) ⇒ 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)


122
123
124
# File 'app/models/organization.rb', line 122

def ask_for_help_enabled?(user)
  report_issue_enabled? || community_link.present? || user.can_discuss_in?(self)
end

#awardable_contentsObject



191
192
193
# File 'app/models/organization.rb', line 191

def awardable_contents
  gamification_enabled? ? all_contents.select(&:medal_id) : []
end

#discussionsObject



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

def discussions
  book.discussions_in_organization(self)
end

#display_descriptionObject



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

def display_description
  self[:display_description].presence || I18n.t('defaults.organization.display_description', name: name)
end

#display_nameObject

Display fields



160
161
162
# File 'app/models/organization.rb', line 160

def display_name
  self[:display_name].presence || name.try { |it| it.gsub(/\W/, ' ').titleize }
end

#drop_usage_indices!Object



80
81
82
# File 'app/models/organization.rb', line 80

def drop_usage_indices!
  usages.destroy_all
end

#enable_progressive_display!(lookahead: 1) ⇒ Object



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

def enable_progressive_display!(lookahead: 1)
  update! progressive_display_lookahead: lookahead
end

#explain_error(code, advice) ⇒ Object



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

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)


92
93
94
# File 'app/models/organization.rb', line 92

def ()
  self..include? .to_s
end

#import_from_resource_h!(resource_h) ⇒ Object



126
127
128
129
130
# File 'app/models/organization.rb', line 126

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)


55
56
57
# File 'app/models/organization.rb', line 55

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

#index_usage_of!(item, parent) ⇒ Object



84
85
86
# File 'app/models/organization.rb', line 84

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

#notify_assignments_by!(submitter) ⇒ Object



63
64
65
# File 'app/models/organization.rb', line 63

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

#notify_recent_assignments!(date) ⇒ Object



59
60
61
# File 'app/models/organization.rb', line 59

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

#ongoing_certificate_programs?Boolean

Returns:

  • (Boolean)


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

def ongoing_certificate_programs?
  certificate_programs.ongoing.exists?
end

#page_descriptionObject



181
182
183
# File 'app/models/organization.rb', line 181

def page_description
  wins_page? ? display_description : book.description
end

#page_nameObject

Since an organization has a single book, both concepts may be merged when describing a site. In such contexts, wins_page? control whether the book or the organization header fields are more important



177
178
179
# File 'app/models/organization.rb', line 177

def page_name
  wins_page? ? display_name : book.name
end

#progressive_display_lookahead=(lookahead) ⇒ Object



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

def progressive_display_lookahead=(lookahead)
  self[:progressive_display_lookahead] = lookahead.to_i.positive? ? lookahead : nil
end

#reindex_usages!Object



71
72
73
74
75
76
77
78
# File 'app/models/organization.rb', line 71

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)


67
68
69
# File 'app/models/organization.rb', line 67

def silent?
  test?
end

#site_nameObject



109
110
111
112
# File 'app/models/organization.rb', line 109

def site_name
  warn "Don't use site_name. Use display_name instead"
  name
end

#title_suffixObject



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

def title_suffix
  warn "Don't use title_suffix. Use page_name instead"
  " - #{page_name}"
end

#to_organizationObject



136
137
138
# File 'app/models/organization.rb', line 136

def to_organization
  self
end

#to_resource_hObject



132
133
134
# File 'app/models/organization.rb', line 132

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