Skip to content
This repository was archived by the owner on Nov 2, 2018. It is now read-only.
This repository was archived by the owner on Nov 2, 2018. It is now read-only.

Create Server-side Modelling for Events and Resources #46

@ErisMik

Description

@ErisMik

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 -> 0
  • Topic.immigration -> 1
  • Topic.legal -> 2
  • Topic.women_rights -> 3
  • Topic.personal_development -> 4
  • Topic.professional_development -> 5
  • Topic.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

  • title should not be blank (length == 0 or all whitespace)
  • title should have max length 140 chars
  • user_id should be an integer and match an existing id in Users table
  • description should not be blank
  • description should have max length 1000 chars
  • visibility should be an integer and exist as an enumerable in Visibility model
  • tier should be an integer and exist as an enumerable in Tier model
  • topic should be an integer and exist as an enumerable in Topic model

Additionally for Event:

  • begins_at should not be blank and should be a valid formatted datetime
  • ends_at should 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 like begins_at = nil or begins_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:

No Documentation

Requirements:

List Special Instructions Needed When Pulling/Deploying

Metadata

Metadata

Labels

TodoA feature/assignment that needs to be done

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions