The overall goal of this assignment is to assess your ability to implement basic CRUD functionality of Active Record. This includes:
- Creating Active Record Models using a rails-provided generator (
rails g modelorrails g scaffold) - Creating a DataBase (DB) schema
- Inserting rows in the DB
- Updating rows in the DB
- Querying the DB with exact matching
- Getting rows from the DB by Primary Key (PK)
- Deleting rows from the DB
- Retrieving paginated results from the DB using
limitandoffsetkeywords
This does NOT include:
- Seeding with seeds.rb
- Relationships
- Validations
- Advanced queries
These topics of Active Record are covered in Module 2.
The functional goal of this assignment is to implement CRUD behavior for four (4) Model classes
- User
- Profile
- TodoList
- TodoItem
-
Create several Active Record Model classes
- User
- Profile
- TodoList
- TodoItem
-
Create a DB schema for each Model class
-
Demonstrate CRUD access to information in the DB using Active Record Model methods.
-
Create a new Rails application called
todolists. -
Add the following specification to your Gemfile to enable rspec testing.
group :test do gem 'rspec-rails', '~> 3.0' end
-
Run the
bundlecommand to resolve new gems -
From the
todolistsapplication root directory, initialize the rspec tests usingrails generate rspec:installcommand.[todolists]$ rails generate rspec:install create .rspec create spec create spec/spec_helper.rb create spec/rails_helper.rb
Add the following line to
.rspecto add verbose output to test results.--format documentation
-
Download and extract the starter set of boostrap files.
|-- Gemfile |-- assignment | `-- assignment.rb `-- spec `-- assignment_spec.rb
-
overwrite your existing Gemfile with the Gemfile from the bootstrap fileset. They should be nearly identical, but this is done to make sure the gems and versions you use in your solution can be processed by the automated Grader when you submit. Any submission should be tested with this version of the file.
-
add the
assignment/assignment.rbfile provided with the boostrap fileset to a correspondingassignmentdirectory under your application root directory (e.g.,application-root-directory/assignment/assignment.rb). (Note: You will need to create theassignmentdirectory if you are copying the file.) Theassignment.rbfile contains a skeleton of methods for you to implement as part of your assignment. -
add the
spec/assignment_spec.rbfile provided with the bootstrap fileset to the correspondingspecdirectory that already exists under your application root directory (e.g.,application-root-directory/spec/assignment_spec.rb). Thisassignment_spec.rbfile contains tests that will help determine whether you have completed the assignment.
-
-
Run the rspec test(s) to receive feedback.
rspecmust be run from the root directory of your application. All tests will (obviously) fail until you complete the specified solution.$ rspec ... Finished in 0.02069 seconds (files took 1.63 seconds to load) 52 examples, 1 failure, 50 pendingTo focus test feedback on a specific step of the requirements, add "-e rq##" to the rspec command line to only evaluate that requirement. Pad all step numbers to two digits.
$ rspec -e rq01 Run options: include {:full_description=>/rq01/} Assignment rq01 Generate Rails application must have top level structure of a rails application Finished in 0.00465 seconds (files took 1.56 seconds to load) 1 example, 0 failures -
Implement your Model and assignment.rb solution and use the rspec tests to help verify your completed solution.
-
Submit your Rails app solution for grading.
-
Create a new Rails app called
todolists.$ rspec -e rq01
Note: Use the Gemfile provided in the boostrap files. Windows users may need to add this gem into the Gemfile:
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem gem 'tzinfo-data', platforms: [:mingw, :mswin]
Otherwise, do not change the Gemfile from what is provided or your submitted solution may not be able to be processed by the grader (i.e., do not add any additional gems or change gem versions).
-
Generate four (4) model classes and DB migrations having the following business-related fields using one of the
rails generatecommands.You can individually grade your results after each model class is created by migrating the database and running the rspec test listed after each classname. Example:
$ rake db:migrate $ rspec -e rq02.1
-
User (
rq02.1)- username - a string to hold account identity
- password_digest - a string to hold password information
-
Profile (
rq02.2)- gender - a string to hold the words "male" or "female"
- birth_year - a number to hold the year the individual was born
- first_name - a string with given name of user
- last_name - a string with family name of user
-
TodoList (
rq02.3)- list_name - a string name assigned to the list
- list_due_date - a date when TODO items in the list are to be complete. This is a date. We are not concerned with the time of day.
-
TodoItem (
rq02.4)- due_date - date when the specific task is to be complete
- title - a string with short name for specific task
- description - a text field with narrative text for specific task
- completed - a boolean value (default=false), indicating whether item is complete
Reminder: Ruby/Rails conventions are that class names are CamelCase and file and method names are snake_case.
Note: We will only be using the Model and DB migration classes for this assignment. It is assumed that each Model will also contain the
id,created_at, andupdated_atfields. All four (4) Model Classes/DB tables must be created, but the detailed tests will be focused on the User and TodoList only. You will use the remaining classes more in a follow-on assignment. -
-
Insert rows in DB
-
Implement the
create_usermethod withinassignment/assignment.rbto- accept a hash of user properties (
:usernameand:password_digest) as an input parameter. Note these are 100% same as model class. - use the User Model class to create a new user in the DB
- return an instance of the class with primary key (
id), and dates (created_atandupdated_at) assigned
$ rspec -e rq03.1 - accept a hash of user properties (
-
Implement the
create_todolistmethod withinassignment/assignment.rbto- accept a hash of todolist properties (
:nameand:due_date) as an input parameter. Note these are not 100% the same as Model class. - use the TodoList Model class to create a new user in the DB
- return an instance of the class with primary key (
id), and dates (created_atandupdated_at) assigned
$ rspec -e rq03.2 - accept a hash of todolist properties (
-
-
Retrieve paginated results from DB
-
Implement the
find_allusersmethod withinassignment/assignment.rbto- accept offset and limit input parameters
- use the User Model class to find all Users, ordered by
updated_atascending, with specified row offset and row limit - return a collection of User instances that represent the specified page
$ rspec -e rq04.1 -
Implement the
find_alllistsmethod withinassignment/assignment.rbto- accept offset and limit input parameters
- use the TodoList Model class to find all TodoLists, ordered by
list_due_datedescending, with specified row offset and row limit - return a collection of TodoList instances that represent the specified page
$ rspec -e rq04.2
-
-
Query DB with exact match
-
Implement the
find_user_bynamemethod withinassignment/assignment.rbto- accept a username input parameter
- use the User Model class to find all Users with the supplied username. Note that we have not yet constrained the username to be unique.
- return a collection of User instances that match the provided username
$ rspec -e rq05.1 -
Implement the
find_todolist_bynamemethod withinassignment/assignment.rbto- accept a name input parameter
- use the TodoList Model class to find all TodoLists with the supplied list_name. Note that list_name is not required to be unique.
- return a collection of TodoList instances that match the provided name
$ rspec -e rq05.2
-
-
Get rows from DB by PK
-
Implement the
get_user_byidmethod withinassignment/assignment.rbto- accept an id input parameter
- use the User Model class to get the User associated with the
idprimary key - return the User instance that matches the provided id
$ rspec -e rq06.1 -
Implement the
get_todolist_byidmethod withinassignment/assignment.rbto- accept an id input parameter
- use the TodoList Model class to get the TodoList associated with the
idprimary key - return the TodoList instance that matches the provided id
$ rspec -e rq06.2
-
-
Update rows in DB
-
Implement the
update_passwordmethod withinassignment/assignment.rbto- accept an id and password_digest input parameters
- use the User Model class to update the
password_digestfor the User associated with the id primary key - (no return is required)
$ rspec -e rq07.1 -
Implement the
update_listnamemethod withinassignment/assignment.rbto- accept an id and name input parameters
- use the TodoList Model class to update the
list_namefor the TodoList associated with id primary key - (no return is required)
$ rspec -e rq07.2
-
-
Delete rows from DB
-
Implement the
delete_usermethod withinassignment/assignment.rbto- accept an id input parameter
- use the User Model class to delete the User associated with the
idprimary key from the database - (no return is required)
$ rspec -e rq08.1 -
Implement the
delete_todolistmethod withinassignment/assignment.rbto- accept an id input parameter
- use the TodoList Model class to delete the TodoList associated with the
idprimary key. - (no return is required)
$ rspec -e rq08.2
-
Some unit tests have been provided in the bootstrap files and provide examples of tests the grader will be evaluating for when you submit your solution. They must be run from the project root directory.
$ rspec
...
Finished in 3.39 seconds (files took 1.47 seconds to load)
52 examples, 0 failuresYou can run as many specific tests you wish be adding -e rq## -e rq##
$ rspec -e rq01 -e rq02Submit an .zip archive (other archive forms not currently supported) with your solution root directory as the top-level (e.g., your Gemfile and sibling files must be in the root of the archive and not in a sub-folder. The grader will replace the spec files with fresh copies and will perform a test with different query terms.
|-- app
| |-- assets
| |-- controllers
| |-- helpers
| |-- mailers
| |-- models
| `-- views
|-- assignment
| `-- assignment.rb
|-- bin
|-- config
|-- config.ru
|-- db
|-- Gemfile
|-- Gemfile.lock
|-- lib
|-- log
|-- public
|-- Rakefile
|-- README.rdoc
|-- test
`-- vendor