Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 3 commits
  • 7 files changed
  • 0 comments
  • 1 contributor
32 app/controllers/registrations_controller.rb
@@ -41,36 +41,8 @@ def edit
41 41 # POST /registrations
42 42 # POST /registrations.json
43 43 def create
44   - @registration = Registration.new(params[:registration])
45   - @registration.section_id = params[:section_id]
46   -
47   - unless current_user.admin?
48   - @registration.registration_fee = @registration.section.registration_fee
49   - end
50   -
51   - if params[:registration][:study_group_id].to_s.length == 0
52   - study_group = StudyGroup.create!(params[:study_group])
53   - @registration.study_group = study_group
54   - end
55   -
56   - if params[:scholarship_code].to_s.length > 0
57   - code = ScholarshipCode.find_by_department_id_and_code(@registration.section.course.department_id, params[:scholarship_code])
58   -
59   - if code
60   - if code.discount_type == ScholarshipCode::DiscountTypes[:PERCENT][:id]
61   - if code.discount_amount > 1
62   - discount = @registration.registration_fee.to_f * code.discount_amount.to_f / 100.0
63   - else
64   - discount = @registration.registration_fee.to_f * code.discount_amount.to_f
65   - end
66   - else
67   - discount = code.discount_amount
68   - end
69   -
70   - @registration.registration_fee -= discount
71   - @registration.scholarship_code = code
72   - end
73   - end
  44 + params[:registration][:section_id] = params[:section_id]
  45 + @registration = Registrar.new.register(current_user, params[:registration], params[:study_group], params[:scholarship_code])
74 46
75 47 respond_to do |format|
76 48 if @registration.save
31 app/models/registrar.rb
... ... @@ -0,0 +1,31 @@
  1 +class Registrar
  2 + def register(requester, registration_info, group_info, scholarship)
  3 + registration = Registration.new(registration_info)
  4 + set_user_and_registration_fee(registration, requester)
  5 + set_study_group(registration, group_info)
  6 + account_for_scholarship(registration, scholarship)
  7 + registration.save
  8 + return registration
  9 + end
  10 +
  11 + private
  12 + def set_user_and_registration_fee(reg, user)
  13 + unless user && user.admin?
  14 + reg.registration_fee = reg.section.registration_fee
  15 + reg.user = user
  16 + end
  17 + end
  18 +
  19 + def set_study_group(reg, attrs)
  20 + if attrs && !reg.study_group_id
  21 + new_group = StudyGroup.create(attrs)
  22 + reg.study_group = new_group if new_group.valid?
  23 + end
  24 + end
  25 +
  26 + def account_for_scholarship(reg, scholarship)
  27 + code = ScholarshipCode.for_department_with_code(reg.section.course.department, scholarship)
  28 + reg.scholarship_code = code
  29 + reg.registration_fee -= code.value(reg.registration_fee) if code
  30 + end
  31 +end
2  app/models/registration.rb
@@ -6,7 +6,7 @@ class Registration < ActiveRecord::Base
6 6 belongs_to :scholarship_code
7 7
8 8 validates :user, :presence=>true
9   - validates_presence_of :signup_reason
  9 + validates_presence_of :signup_reason, :study_group
10 10
11 11 class PaymentStatuses < Enumeration
12 12 self.add_item(:UNPAID, {:id => 0, :name => 'Unpaid'})
16 app/models/scholarship_code.rb
@@ -6,4 +6,20 @@ class DiscountTypes < Enumeration
6 6 self.add_item(:PERCENT, {:id => 0, :name => 'Percent'})
7 7 self.add_item(:FLATAMOUNT, {:id => 1, :name => 'Flat amount'})
8 8 end
  9 +
  10 + def self.for_department_with_code(department, code)
  11 + self.find_by_department_id_and_code(department.id, code)
  12 + end
  13 +
  14 + def value(original_price)
  15 + result = self.discount_amount.to_f
  16 + if self.discount_type == ScholarshipCode::DiscountTypes[:PERCENT][:id]
  17 + if self.discount_amount > 1
  18 + result = original_price * self.discount_amount / 100
  19 + else
  20 + result = original_price * self.discount_amount
  21 + end
  22 + end
  23 + return result
  24 + end
9 25 end
19 test/unit/registrar_test.rb
... ... @@ -0,0 +1,19 @@
  1 +require 'test_helper'
  2 +
  3 +class RegistrarTest < ActiveSupport::TestCase
  4 + def setup
  5 + @registrar = Registrar.new
  6 + end
  7 +
  8 + def test_registers_student_for_section
  9 + student = User.new
  10 + section = Section.new
  11 + registration_info = {:section_id=>sections(:MWF).id}
  12 + group_info = {}
  13 + scholarship = ""
  14 + registration = @registrar.register(student, registration_info, group_info, scholarship)
  15 + assert_equal registration.user, student
  16 + end
  17 +
  18 +
  19 +end
4 test/unit/registration_test.rb
@@ -2,12 +2,12 @@
2 2
3 3 class RegistrationTest < ActiveSupport::TestCase
4 4 setup do
5   - @reg = Registration.new
  5 + @reg = Registration.new({:section_id=>sections(:MWF).id})
6 6 end
7 7
8 8 test "paid? is correct" do
9 9 @reg.payment_status = Registration::PaymentStatuses[:UNPAID][:id]
10   - assert_false @reg.paid?
  10 + assert !@reg.paid?
11 11 @reg.payment_status = Registration::PaymentStatuses[:PAID][:id]
12 12 assert @reg.paid?
13 13 end
24 test/unit/scholarship_code_test.rb
... ... @@ -1,7 +1,25 @@
1 1 require 'test_helper'
2 2
3 3 class ScholarshipCodeTest < ActiveSupport::TestCase
4   - # test "the truth" do
5   - # assert true
6   - # end
  4 + setup do
  5 + @code = ScholarshipCode.new
  6 + end
  7 +
  8 + test "value calculates flat amount correctly" do
  9 + @code.discount_type = ScholarshipCode::DiscountTypes[:FLATAMOUNT][:id]
  10 + @code.discount_amount = 15
  11 + assert_equal 15, @code.value(100)
  12 + end
  13 +
  14 + test "value calculates percentage greater than 1 discount amount correctly" do
  15 + @code.discount_type = ScholarshipCode::DiscountTypes[:PERCENT][:id]
  16 + @code.discount_amount = 20
  17 + assert_equal 40, @code.value(200)
  18 + end
  19 +
  20 + test "value calculates percentage less than 1 discount amount correctly" do
  21 + @code.discount_type = ScholarshipCode::DiscountTypes[:PERCENT][:id]
  22 + @code.discount_amount = 0.20
  23 + assert_equal 40, @code.value(200)
  24 + end
7 25 end

No commit comments for this range

Something went wrong with that request. Please try again.