Class: Organization

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

Class Method Summary collapse

Instance Method Summary collapse

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, #validate_active!

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, teaser_on, #update_and_notify!, update_or_create!, whitelist_attributes

Class Method Details

.accessible_as(user, role) ⇒ Object



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

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

.baseObject



205
206
207
# File 'app/models/organization.rb', line 205

def base
  find_by name: 'base'
end

.centralObject



201
202
203
# File 'app/models/organization.rb', line 201

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



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

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

.silenced?Boolean

Returns:

  • (Boolean)


209
210
211
# File 'app/models/organization.rb', line 209

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

.sync_key_id_fieldObject



213
214
215
# File 'app/models/organization.rb', line 213

def sync_key_id_field
  :name
end

Instance Method Details

#accessible_exams_for(user) ⇒ Object



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

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

#all_contentsObject



170
171
172
173
174
# File 'app/models/organization.rb', line 170

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)


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

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

#awardable_contentsObject



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

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

#display_descriptionObject



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

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

#display_nameObject

Display fields



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

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

#drop_usage_indices!Object



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

def drop_usage_indices!
  usages.destroy_all
end

#enable_progressive_display!(lookahead: 1) ⇒ Object



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

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

#explain_error(code, advice) ⇒ Object



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

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)


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

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)


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

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

#index_usage_of!(item, parent) ⇒ Object



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

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

#notify_assignments_by!(submitter) ⇒ Object



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

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

#notify_recent_assignments!(date) ⇒ Object



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

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

#page_descriptionObject



166
167
168
# File 'app/models/organization.rb', line 166

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



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

def page_name
  wins_page? ? display_name : book.name
end

#progressive_display_lookahead=(lookahead) ⇒ Object



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

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

#reindex_usages!Object



68
69
70
71
72
73
74
75
# File 'app/models/organization.rb', line 68

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)


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

def silent?
  test?
end

#site_nameObject



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

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

#title_suffixObject



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

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

#to_organizationObject



129
130
131
# File 'app/models/organization.rb', line 129

def to_organization
  self
end

#to_resource_hObject



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

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