-
Notifications
You must be signed in to change notification settings - Fork 0
Create Server-side Modelling for Events and Resources #46
Description
Issue Description
Users Affected:
All users
Functionality Requested:
Write database migrations to create an events table and resources table
Create a Topic model for enumerating resource topics
Create a Resource model with association configurations and validations
Create an Event model with association configurations and validations
These will together form the core model to support storing and querying events and resources
Database Migrations
To get you started, use this command to automatically generate a new database migration: rails generate migration <name_of_my_new_migration>
This will create the following file:
db/migrate/<really_long_timestamp>_<name_of_my_new_migration>.rb
Open this file and you'll see a short ruby script, then open up
db/migrate/20180216024515_create_users.rb
This is an old migration script used to create the first iteration of the users table.
Refer to this as a "template" when you're writing your new migration. I will describe the migrations needed below.
Resources Table
Create a migration that would result in the following table in schema.rb:
create_table "resources", force: :cascade do |t|
t.string "title", default: "Untitled", null: false
t.integer "user_id", null: false
t.integer "topic", default: 0, null: false
t.text "description", default: "No Description", null: false
t.integer "visibility", default: 0, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "tier", default: 0, null: false
t.index ["user_id"], name: "index_resources_on_user_id"
t.index ["visibility"], name: "index_resources_on_visibility"
t.index ["topic"], name: "index_resources_on_topic"
end
Do not directly modify schema.rb or any of the existing migrations in db/migrate/.
Instead write new ones that will be executed after them.
Events Table
Create a migration that would result in the following table in schema.rb:
create_table "events", force: :cascade do |t|
t.string "title", default: "Untitled", null: false
t.integer "user_id", null: false
t.integer "topic", default: 0, null: false
t.datetime "begins_at", null: false
t.datetime "ends_at", null: false
t.text "description", default: "No Description", null: false
t.integer "visibility", default: 0, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "tier", default: 0, null: false
t.index ["user_id"], name: "index_events_on_user_id"
t.index ["visibility"], name: "index_events_on_visibility"
t.index ["topic"], name: "index_events_on_topic"
t.index ["begins_at"], name: "index_events_on_begins_at"
t.index ["ends_at"], name: "index_events_on_ends_at"
end
Do not directly modify schema.rb or any of the existing migrations in db/migrate/.
Instead write new ones that will be executed after them.
Once you're done a migration, run rails db:migrate to try it out. Check schema.rb to make sure your migration made the right changes to this file. If you did it congratulations! If not, that's ok. Just undo the previous migration by running rails db:rollback
See http://guides.rubyonrails.org/active_record_migrations.html for help with writing and executing database migrations.
Creating an Enumerable class for Topic
Create a file app/model/topic.rb, copy the content from app/model/role.rb
Rename this class to Topic
Modify the existing enumerable mapping so that the class has the following methods/functions
Topic.general -> 0Topic.immigration -> 1Topic.legal -> 2Topic.women_rights -> 3Topic.personal_development -> 4Topic.professional_development -> 5Topic.networking -> 6
Model Associations and Validations
Associations
Add these configurations to get started with assoications on model/event.rb and model/resource.rb
You first need to generate the model by running the command rails generate model event
and the command rails generate model resource
has_many :attachments, as: :attachables, dependent: :destroy
belongs_to :user
alias_attribute :creator, :user
Validations
Validate on the following fields / attributes in app/models/event.rb and app/models/resource.rb
titleshould not be blank (length == 0 or all whitespace)titleshould have max length 140 charsuser_idshould be an integer and match an existingidin Users tabledescriptionshould not be blankdescriptionshould have max length 1000 charsvisibilityshould be an integer and exist as an enumerable in Visibility modeltiershould be an integer and exist as an enumerable in Tier modeltopicshould be an integer and exist as an enumerable in Topic model
Additionally for Event:
begins_atshould not be blank and should be a valid formatted datetimeends_atshould not be blank and should be a valid formatted datetime
Don't worry about formatting, this is done by the framework. Just make sure you can't do something silly likebegins_at = nilorbegins_at = -2
See http://guides.rubyonrails.org/active_record_validations.html for examples of configuring a model with validations.
Testing
For each part of a route or validation, please write a unit test under test/models or test/controllers
You can add "dummy" instances that can be loaded from the test database to help with your work. These are referred to as fixtures http://guides.rubyonrails.org/testing.html#maintaining-the-test-database-schema
Progress
Status: Assigned
Implementation
Commit Hash: Not Yet Complete
Author: @Jojo991 @jordankr
Reviewer: @Jimmy-Lin, @bchugg
Prior Behaviour:
Events and Resources don't exist but we have a very similar Courses model we can copy and extend to add these features to the App.
New/Modified Behaviour:
At a console level (rails console) we should be able to create, update, view, and delete events and resources and the database should give us errors if we violate the model or database validations.
Validation:
Test Files:
List Test Files Added/Modified
Manual Validation Procedure:
List Manual Checks Needed
Follow-Up:
Usage Documentation:
Requirements:
List Special Instructions Needed When Pulling/Deploying