Skip to content
This repository has been archived by the owner on Feb 21, 2021. It is now read-only.

Commit

Permalink
adding lists
Browse files Browse the repository at this point in the history
  • Loading branch information
mstubna committed Feb 15, 2015
1 parent 1416f30 commit 6b15a65
Show file tree
Hide file tree
Showing 11 changed files with 121 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/assets/javascripts/lists.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/lists.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Lists controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
2 changes: 2 additions & 0 deletions app/controllers/lists_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class ListsController < ApplicationController
end
8 changes: 8 additions & 0 deletions app/models/list.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class List < ActiveRecord::Base
belongs_to :user

validates(
:user,
presence: true
)
end
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@ class User < ActiveRecord::Base
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

has_many(
:lists,
dependent: :destroy
)
end
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Rails.application.routes.draw do
devise_for :users
resources :users, only: [:show]
resources :users, only: [:show] do
resources :lists
end

root 'application#index'
end
12 changes: 12 additions & 0 deletions db/migrate/20150215203738_create_lists.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class CreateLists < ActiveRecord::Migration
def change
create_table :lists do |t|
t.string :title
t.text :body
t.references :user, index: true, null: false

t.timestamps
end
add_foreign_key :lists, :users, on_delete: :cascade
end
end
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,17 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150212183029) do
ActiveRecord::Schema.define(version: 20150215203738) do

create_table "lists", force: :cascade do |t|
t.string "title", limit: 255
t.text "body", limit: 65535
t.integer "user_id", limit: 4, null: false
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "lists", ["user_id"], name: "index_lists_on_user_id", using: :btree

create_table "users", force: :cascade do |t|
t.string "email", limit: 255, default: "", null: false
Expand All @@ -31,4 +41,5 @@
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

add_foreign_key "lists", "users", on_delete: :cascade
end
5 changes: 5 additions & 0 deletions spec/controllers/lists_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe UsersController, type: :controller do
it_behaves_like 'an Application controller'
end
59 changes: 59 additions & 0 deletions spec/models/list_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'rails_helper'

RSpec.describe List, type: :model do
it_behaves_like 'an ActiveRecord model'

describe 'schema' do
describe 'columns' do
describe 'title' do
example do
expect(subject).to have_db_column(:title)
.of_type(:string)
.with_options(
null: true,
limit: 255
)
end
end

describe 'body' do
example do
expect(subject).to have_db_column(:body)
.of_type(:text)
.with_options(
null: true,
limit: 65_535
)
end
end

describe 'user_id' do
example do
expect(subject).to have_db_column(:user_id)
.of_type(:integer)
.with_options(
null: false,
limit: 4
)
end
end
end

describe 'indices' do
describe 'user_id' do
example do
expect(subject).to have_db_index(:user_id)
end
end
end

describe 'associations' do
describe 'user' do
example do
expect(subject).to belong_to(:user)
.class_name('User')
end
end
end
end
end
9 changes: 9 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@
end
end
end

describe 'associations' do
describe 'lists' do
example do
expect(subject).to have_many(:lists)
.dependent(:destroy)
end
end
end
end

describe 'devise' do
Expand Down

0 comments on commit 6b15a65

Please sign in to comment.