Adding Students

Students will be added as a separate class by uploading a datafile. Each student can be assigned to multiple Deliveries and thus be issued several Certificates.

A user story:

As a course administrator
In order to be able to issue course certificates
I want to be able to import a datafile with student information

We start by creating a new feature file in the features folder. Call it student_data_import.feature.

We will be doing this in small increments so we will not define all steps at once but rather work our way through the scenario in parts, doing new steps as we move along. Let's start with these initial steps:

# features/student_data_import.feature

Scenario: Data file upload
  Given the delivery for the course "Basic" is set to "2015-12-01"
  And I am on the Course index page
  And I click on "2015-12-01" for the "Basic programming" Course
  Then I should be on 2015-12-01 show page

Add the following step to your application_steps.rb

# features/step_definitions/application_steps.rb

Given(/^the delivery for the course "([^"]*)" is set to "([^"]*)"$/) do |name, date|
  steps %(
    Given the course "#{name}" is created
    And I am on the Course index page
    And I click on "Add Delivery date" for the "#{name}" Course
    And I fill in "Start" with "#{date}"
    And I click "Submit" link
  )
end

And in the support/paths.rb we have to configure the path for Cucumber to know where to go.

And lastly update your course/index.erb file as follows:

In our controller, we need to define that route as well:

In that route we are pointing to an erb template that is still missing (you'll see that when you run cucumber, right?)

We need to create a new folder and a new erb file. Can you figure out how?

If you run cucumber at this point, all the steps should be green.

Moving on. Let's add some more steps to our scenario.

We need to add two new step definitions:

To save some time, use the following two commands to create a fixture folder (where we will store some dummy data) and an empty students.csv:

Now run your tests again and you'll see that you have moved forward a bit but also that you can not move any further without defining a new Student class. Time for some RSpec and unit testing.

We start by creating a student_spec.rb file in the spec folder and add a few expectations:

And we need to, of course, create a Student class:

Don't forget to include the Student class in your controller:

Run the student_spec.rb:

The specs should pass.

Time to add a module to parse the uploaded file. In your lib folder, create a file named csv_parse.rb. There we will place some logic on how to parse a data file and create Student objects.

Now, let's go back to our controller.

You have to require and include the module (look closely at the code below) and update your post request route with this code:

Add some content to your fixture file, the students.csv:

Now, if you run the scenario, all the steps should pass.

Add another few steps to your scenario:

We need to add an relationship between Delivery and Student for the Delivery to know what Students it is associated with. It needs to do that so we can display that information on the page after we've uploaded and parsed the data file.

We start by adding a spec for that relation:

And we add that relation to our Delivery class:

Finally, add the following code to the show.erb template:

Run all your specs and features. You should see a lot of green on your screen. That is always a good sign. ;-)

Next, we will be creating the actual certificates. Exciting?

Last updated