From 2624454d964a5ee910df9c8e196947ac726b60ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Giroux?= Date: Sun, 12 Feb 2017 15:04:19 -0500 Subject: [PATCH 1/6] Ratings model --- app/models/film.rb | 3 +++ app/models/rating.rb | 4 ++++ app/models/user.rb | 4 ++++ db/migrate/20170212195638_create_ratings.rb | 15 +++++++++++++++ db/schema.rb | 18 +++++++++++++++++- test/fixtures/ratings.yml | 4 ++++ test/fixtures/users.yml | 2 ++ 7 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 app/models/rating.rb create mode 100644 app/models/user.rb create mode 100644 db/migrate/20170212195638_create_ratings.rb create mode 100644 test/fixtures/ratings.yml create mode 100644 test/fixtures/users.yml diff --git a/app/models/film.rb b/app/models/film.rb index 60d69bf..deff528 100644 --- a/app/models/film.rb +++ b/app/models/film.rb @@ -8,4 +8,7 @@ class Film < ApplicationRecord join_table: 'films_people', foreign_key: 'film_id', association_foreign_key: 'person_id' + + has_many :ratings + has_many :users, through: :ratings end diff --git a/app/models/rating.rb b/app/models/rating.rb new file mode 100644 index 0000000..bf63d23 --- /dev/null +++ b/app/models/rating.rb @@ -0,0 +1,4 @@ +class Rating < ApplicationRecord + belongs_to :user + belongs_to :film +end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000..630a354 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,4 @@ +class User < ApplicationRecord + has_many :ratings + has_many :films, through: :ratings +end diff --git a/db/migrate/20170212195638_create_ratings.rb b/db/migrate/20170212195638_create_ratings.rb new file mode 100644 index 0000000..b8b0343 --- /dev/null +++ b/db/migrate/20170212195638_create_ratings.rb @@ -0,0 +1,15 @@ +class CreateRatings < ActiveRecord::Migration[5.0] + def change + create_table :users do |t| + t.string :name + t.timestamps + end + + create_table :ratings do |t| + t.belongs_to :user, index: true + t.belongs_to :film, index: true + t.integer :rating + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index adce7bd..aecb66f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170211182808) do +ActiveRecord::Schema.define(version: 20170212195638) do create_table "films", force: :cascade do |t| t.string "title" @@ -103,6 +103,16 @@ t.datetime "updated_at", null: false end + create_table "ratings", force: :cascade do |t| + t.integer "user_id" + t.integer "film_id" + t.integer "rating" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["film_id"], name: "index_ratings_on_film_id" + t.index ["user_id"], name: "index_ratings_on_user_id" + end + create_table "species", force: :cascade do |t| t.string "name" t.string "classification" @@ -137,6 +147,12 @@ t.datetime "updated_at", null: false end + create_table "users", force: :cascade do |t| + t.string "name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "vehicles", force: :cascade do |t| t.string "name" t.string "model" diff --git a/test/fixtures/ratings.yml b/test/fixtures/ratings.yml new file mode 100644 index 0000000..dfaf058 --- /dev/null +++ b/test/fixtures/ratings.yml @@ -0,0 +1,4 @@ +good-rating: + user: test-user + film: a-new-hope + rating: 5 diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000..f9f4211 --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,2 @@ +test-user: + name: xuorig From b1407d11fbceb2bbe0cb918379c3591810175508 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Giroux?= Date: Sun, 12 Feb 2017 15:07:37 -0500 Subject: [PATCH 2/6] add a validation --- app/models/rating.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/models/rating.rb b/app/models/rating.rb index bf63d23..4f63083 100644 --- a/app/models/rating.rb +++ b/app/models/rating.rb @@ -1,4 +1,6 @@ class Rating < ApplicationRecord belongs_to :user belongs_to :film + + validates_inclusion_of :number, :in => 0..5 end From dab0516f1294e5d0ded92b15f7c9b590c7323c06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Giroux?= Date: Sun, 12 Feb 2017 15:08:33 -0500 Subject: [PATCH 3/6] add email to user --- db/migrate/20170212195638_create_ratings.rb | 1 + db/schema.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/db/migrate/20170212195638_create_ratings.rb b/db/migrate/20170212195638_create_ratings.rb index b8b0343..6758d7d 100644 --- a/db/migrate/20170212195638_create_ratings.rb +++ b/db/migrate/20170212195638_create_ratings.rb @@ -2,6 +2,7 @@ class CreateRatings < ActiveRecord::Migration[5.0] def change create_table :users do |t| t.string :name + t.string :email t.timestamps end diff --git a/db/schema.rb b/db/schema.rb index aecb66f..29457cf 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -149,6 +149,7 @@ create_table "users", force: :cascade do |t| t.string "name" + t.string "email" t.datetime "created_at", null: false t.datetime "updated_at", null: false end From 00b15042e0fe77552aca7db880be13468874f211 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Giroux?= Date: Sun, 12 Feb 2017 15:16:17 -0500 Subject: [PATCH 4/6] Rename assocaitons --- app/models/film.rb | 2 +- app/models/rating.rb | 2 +- app/models/user.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/film.rb b/app/models/film.rb index deff528..6ecf05d 100644 --- a/app/models/film.rb +++ b/app/models/film.rb @@ -10,5 +10,5 @@ class Film < ApplicationRecord association_foreign_key: 'person_id' has_many :ratings - has_many :users, through: :ratings + has_many :critics, through: :ratings, source: :user end diff --git a/app/models/rating.rb b/app/models/rating.rb index 4f63083..c9ac3c4 100644 --- a/app/models/rating.rb +++ b/app/models/rating.rb @@ -2,5 +2,5 @@ class Rating < ApplicationRecord belongs_to :user belongs_to :film - validates_inclusion_of :number, :in => 0..5 + validates_inclusion_of :number, in: 0..5 end diff --git a/app/models/user.rb b/app/models/user.rb index 630a354..4edb759 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,4 @@ class User < ApplicationRecord has_many :ratings - has_many :films, through: :ratings + has_many :rated_films, through: :ratings, source: :film end From 1827161bc1c6bff31d8765030f3026ebbd3074c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Giroux?= Date: Sun, 12 Feb 2017 15:22:36 -0500 Subject: [PATCH 5/6] add password --- Gemfile | 2 ++ Gemfile.lock | 2 ++ app/models/user.rb | 1 + db/migrate/20170212195638_create_ratings.rb | 1 + db/schema.rb | 5 +++-- 5 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 897dd45..bb31e8a 100644 --- a/Gemfile +++ b/Gemfile @@ -18,6 +18,8 @@ gem 'graphiql-rails', '~> 1.4.1' gem 'uglifier' +gem 'bcrypt' + group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platform: :mri diff --git a/Gemfile.lock b/Gemfile.lock index be02e6d..c2add28 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -39,6 +39,7 @@ GEM minitest (~> 5.1) tzinfo (~> 1.1) arel (7.1.4) + bcrypt (3.1.11) builder (3.2.3) byebug (9.0.6) concurrent-ruby (1.0.4) @@ -134,6 +135,7 @@ PLATFORMS ruby DEPENDENCIES + bcrypt byebug graphiql-rails (~> 1.4.1) graphql (~> 1.4.3) diff --git a/app/models/user.rb b/app/models/user.rb index 4edb759..e16c703 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,5 @@ class User < ApplicationRecord + has_secure_password has_many :ratings has_many :rated_films, through: :ratings, source: :film end diff --git a/db/migrate/20170212195638_create_ratings.rb b/db/migrate/20170212195638_create_ratings.rb index 6758d7d..c97432d 100644 --- a/db/migrate/20170212195638_create_ratings.rb +++ b/db/migrate/20170212195638_create_ratings.rb @@ -3,6 +3,7 @@ def change create_table :users do |t| t.string :name t.string :email + t.string :password_digest t.timestamps end diff --git a/db/schema.rb b/db/schema.rb index 29457cf..6ea8d94 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -150,8 +150,9 @@ create_table "users", force: :cascade do |t| t.string "name" t.string "email" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false + t.string "password_digest" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end create_table "vehicles", force: :cascade do |t| From ed47408d4a48ebf3bf850e41d625765da5f88fff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Giroux?= Date: Sun, 12 Feb 2017 15:30:31 -0500 Subject: [PATCH 6/6] fixture --- test/fixtures/users.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index f9f4211..0d210b9 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -1,2 +1,3 @@ test-user: name: xuorig + password_digest: <%= BCrypt::Password.create('averysecurepassword', cost: BCrypt::Engine.cost) %>