Skip to content
This repository was archived by the owner on Sep 24, 2019. It is now read-only.

Commit 0f640c1

Browse files
authored
Merge pull request #26 from GraphQLAcademy/ratings
Ratings model
2 parents e96e9dc + ed47408 commit 0f640c1

File tree

9 files changed

+61
-1
lines changed

9 files changed

+61
-1
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ gem 'graphiql-rails', '~> 1.4.1'
1717

1818
gem 'uglifier'
1919

20+
gem 'bcrypt'
21+
2022
group :development, :test do
2123
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
2224
gem 'byebug', platform: :mri

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ GEM
3939
minitest (~> 5.1)
4040
tzinfo (~> 1.1)
4141
arel (7.1.4)
42+
bcrypt (3.1.11)
4243
builder (3.2.3)
4344
byebug (9.0.6)
4445
concurrent-ruby (1.0.4)
@@ -138,6 +139,7 @@ PLATFORMS
138139
ruby
139140

140141
DEPENDENCIES
142+
bcrypt
141143
byebug
142144
graphiql-rails (~> 1.4.1)
143145
graphql (~> 1.4.3)

app/models/film.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ class Film < ApplicationRecord
88
join_table: 'films_people',
99
foreign_key: 'film_id',
1010
association_foreign_key: 'person_id'
11+
12+
has_many :ratings
13+
has_many :critics, through: :ratings, source: :user
1114
end

app/models/rating.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Rating < ApplicationRecord
2+
belongs_to :user
3+
belongs_to :film
4+
5+
validates_inclusion_of :number, in: 0..5
6+
end

app/models/user.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class User < ApplicationRecord
2+
has_secure_password
3+
has_many :ratings
4+
has_many :rated_films, through: :ratings, source: :film
5+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class CreateRatings < ActiveRecord::Migration[5.0]
2+
def change
3+
create_table :users do |t|
4+
t.string :name
5+
t.string :email
6+
t.string :password_digest
7+
t.timestamps
8+
end
9+
10+
create_table :ratings do |t|
11+
t.belongs_to :user, index: true
12+
t.belongs_to :film, index: true
13+
t.integer :rating
14+
t.timestamps
15+
end
16+
end
17+
end

db/schema.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 20170211182808) do
13+
ActiveRecord::Schema.define(version: 20170212195638) do
1414

1515
create_table "films", force: :cascade do |t|
1616
t.string "title"
@@ -103,6 +103,16 @@
103103
t.datetime "updated_at", null: false
104104
end
105105

106+
create_table "ratings", force: :cascade do |t|
107+
t.integer "user_id"
108+
t.integer "film_id"
109+
t.integer "rating"
110+
t.datetime "created_at", null: false
111+
t.datetime "updated_at", null: false
112+
t.index ["film_id"], name: "index_ratings_on_film_id"
113+
t.index ["user_id"], name: "index_ratings_on_user_id"
114+
end
115+
106116
create_table "species", force: :cascade do |t|
107117
t.string "name"
108118
t.string "classification"
@@ -137,6 +147,14 @@
137147
t.datetime "updated_at", null: false
138148
end
139149

150+
create_table "users", force: :cascade do |t|
151+
t.string "name"
152+
t.string "email"
153+
t.string "password_digest"
154+
t.datetime "created_at", null: false
155+
t.datetime "updated_at", null: false
156+
end
157+
140158
create_table "vehicles", force: :cascade do |t|
141159
t.string "name"
142160
t.string "model"

test/fixtures/ratings.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
good-rating:
2+
user: test-user
3+
film: a-new-hope
4+
rating: 5

test/fixtures/users.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
test-user:
2+
name: xuorig
3+
password_digest: <%= BCrypt::Password.create('averysecurepassword', cost: BCrypt::Engine.cost) %>

0 commit comments

Comments
 (0)