parliament-calendar.herokuapp.com

Calculator controller to build the form and run the calculations.

Individual calculations for different flavours of instrument are packaged into separate files. This code requires those files to be loaded.

 4 require 'calculations/bicameral_both_houses_sitting'
 5 require 'calculations/bicameral_si_either_house_sitting'
 6 require 'calculations/commons_only_si'
 7 require 'calculations/pnsi'
 8 require 'calculations/treaty'

The controller itself.

 11 class CalculatorController < ApplicationController

Include code from each of the modules for the different styles of calculation.

 14   include CALCULATION_BICAMERAL_BOTH_HOUSES_SITTING
 15   include CALCULATION_BICAMERAL_SI_EITHER_HOUSE_SITTING
 16   include CALCULATION_COMMONS_ONLY_SI
 17   include CALCULATION_PNSI
 18   include CALCULATION_TREATY

This is the code to provide information for the form that users can fill in.

 21   def index

Set a title for the page.

 24     @title = "Calculate scrutiny periods"

Find all the active procedures in display order - to populate the procedure radio buttons on the form.

 27     @procedures = Procedure.all.where( 'active is true' ).order( 'display_order asc')
 28   end

This code runs the calculation.

 31   def calculate

In order to calculate the scrutiny period, we need:

 36     procedure = params['procedure'].to_i if params['procedure']
 39     start_date = params['start-date']
 42     day_count = params['day-count']

If the procedure and the start date have not been provided by the initial form ...

 45     if start_date.blank? or procedure.nil?

... we set an error message ...

 48 	    @title = "Sorry, there was not enough information provided."

... and display the error.

 51       render :template => 'calculator/not_enough_information'

If the procedure and the start date have been provided by the initial form, we ...

 54     else

* find the procedure

 57       @procedure = Procedure.find( procedure )
 60       @start_date = Date.parse( start_date )

If the day count has not been provided by the day count form or the day count is 0 ...

 63       if day_count.blank? or day_count.to_i == 0

... we set a title for the page.

 66     	  @title = "Calculate scrutiny period"

We render the day count form.

 69         render :template => 'calculator/day_count_form'

If the day count has been provided by the day count form ...

 72       else

... we set a title for the page.

 75     	  @title = "Calculated scrutiny period"

We get the day count as an integer.

 78         @day_count = day_count.to_i

To calculate the anticipated end date, we select the calculation based on the type of procedure:

 81         case @procedure.id
 84         when 1, 2, 4
 86           @start_date_type = "laying date"
 87           @scrutiny_end_date = bicameral_calculation_both_houses_sitting( @start_date, @day_count )
 90         when 3
 92           @start_date_type = "laying date"
 93           @scrutiny_end_date = pnsi_calculation( @start_date, @day_count )
 96         when 5
 98           @start_date_type = "laying date"
 99           @scrutiny_end_date = commons_only_si_calculation( @start_date, @day_count )
 102         when 6, 13, 14
 104           @start_date_type = "laying date"
 105           @scrutiny_end_date = bicameral_si_either_house_sitting_calculation( @start_date, @day_count )
 108         when 7
 110           @start_date_type = "making date"
 111           @scrutiny_end_date = commons_only_si_calculation( @start_date, @day_count )
 114         when 8
 116           @start_date_type = "making date"
 117           @scrutiny_end_date = bicameral_calculation_both_houses_sitting( @start_date, @day_count )
 120         when 9, 15, 16
 122           @start_date_type = "making date"
 123           @scrutiny_end_date = bicameral_si_either_house_sitting_calculation( @start_date, @day_count )
 126         when 10
 128           @start_date_type = "laying date"
 129           @scrutiny_end_date = treaty_calculation( @start_date, @day_count )
 132         when 11
 134           @start_date_type = "date of Ministerial statement"
 135           @scrutiny_end_date = treaty_calculation( @start_date, @day_count )
 138         when 12
 140           @start_date_type = "date of publication"
 141           @scrutiny_end_date = bicameral_calculation_both_houses_sitting( @start_date, @day_count )
 142         end
 143       end
 144     end
 145   end
 146 end