Class: Course::ExperiencePoints::Disbursement

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, ActiveModel::Validations
Defined in:
app/models/course/experience_points/disbursement.rb

Direct Known Subclasses

ForumDisbursement

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#courseCourse

The course that this disbursement is for. This attribute is read during authorization.

Returns:


15
16
17
# File 'app/models/course/experience_points/disbursement.rb', line 15

def course
  @course
end

#group_idInteger|nil

ID of the group that this disbursement is for. nil is returned if no group is specified.

Returns:

  • (Integer|nil)

20
21
22
# File 'app/models/course/experience_points/disbursement.rb', line 20

def group_id
  @group_id
end

#reasonString

This reason for the disbursement. This will become the reason for each experience points record awarded.

Returns:

  • (String)

10
11
12
# File 'app/models/course/experience_points/disbursement.rb', line 10

def reason
  @reason
end

Instance Method Details

#experience_points_recordsArray<Course::ExperiencePointsRecords>

Returns experience points records for the disbursement. It creates empty records if no records are present.

Returns:

  • (Array<Course::ExperiencePointsRecords>)

    The points records for this disbursement.


28
29
30
31
32
# File 'app/models/course/experience_points/disbursement.rb', line 28

def experience_points_records
  @experience_points_records ||= filtered_students.order_alphabetically.map do |student|
    student.experience_points_records.build
  end
end

#experience_points_records_attributes=(attributes) ⇒ Hash

Processes the experience points records attributes hash, instantiating new experience points records for attributes hashes that represents a valid award.

Parameters:

  • attributes (Hash)

    Experience points records attributes hash

Returns:

  • (Hash)

    Experience points records attributes hash


39
40
41
42
43
44
45
46
# File 'app/models/course/experience_points/disbursement.rb', line 39

def experience_points_records_attributes=(attributes)
  valid_attributes = attributes.values.select(&method(:valid_points_record_attributes?))
  @experience_points_records = valid_attributes.map do |hash|
    hash[:reason] = reason
    Course::ExperiencePointsRecord.new(hash)
  end
  attributes
end

#groupCourse::Group|nil

Returns the group that this disbursement is for if a valid group is specified, otherwise return nil.

Returns:

  • (Course::Group|nil)

    The group that this disbursement is for


52
53
54
# File 'app/models/course/experience_points/disbursement.rb', line 52

def group
  @group ||= group_id && course.groups.find_by(id: group_id)
end

#saveBoolean

Saves the newly built experience points records.

Returns:

  • (Boolean)

    True if bulk saving was successful


59
60
61
62
63
# File 'app/models/course/experience_points/disbursement.rb', line 59

def save
  Course::ExperiencePointsRecord.transaction { @experience_points_records.map(&:save!).all? }
rescue ActiveRecord::RecordInvalid
  false
end