Skip to content

Commit

Permalink
Votes model + Users can vote on Links
Browse files Browse the repository at this point in the history
  • Loading branch information
ztratify committed Dec 1, 2020
1 parent 8a5afd0 commit 6d5d778
Show file tree
Hide file tree
Showing 13 changed files with 87 additions and 2 deletions.
14 changes: 14 additions & 0 deletions app/graphql/mutations/create_vote.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Mutations
class CreateVote < BaseMutation
argument :link_id, ID, required: false

type Types::VoteType

def resolve(link_id: nil)
Vote.create!(
link: Link.find(link_id),
user: context[:current_user]
)
end
end
end
13 changes: 13 additions & 0 deletions app/graphql/types/date_time_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Add DateTimeType for Link.created_at property
# https://github.com/rmosolgo/graphiql-rails/issues/29
module Types
class DateTimeType < BaseScalar
def self.coerce_input(value, _context)
Time.zone.parse(value)
end

def self.coerce_result(value, _context)
value.utc.iso8601
end
end
end
1 change: 1 addition & 0 deletions app/graphql/types/link_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ class LinkType < Types::BaseObject
# field can be nil, because we added users relationship later
# "method" option remaps field to an attribute of Link model
field :posted_by, UserType, null: true, method: :user
field :votes, [Types::VoteType], null: false
end
end
1 change: 1 addition & 0 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ class MutationType < BaseObject
field :create_link, mutation: Mutations::CreateLink
field :create_user, mutation: Mutations::CreateUser
field :login_user, mutation: Mutations::LoginUser
field :create_vote, mutation: Mutations::CreateVote
end
end
3 changes: 3 additions & 0 deletions app/graphql/types/user_type.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
module Types
class UserType < Types::BaseObject
field :id, ID, null: false
field :created_at, DateTimeType, null: false
field :name, String, null: false
field :email, String, null: false
field :votes, [VoteType], null: false
field :links, [LinkType], null: false
end
end
7 changes: 7 additions & 0 deletions app/graphql/types/vote_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Types
class VoteType < Types::BaseObject
field :id, ID, null: false
field :user, Types::UserType, null: false
field :link, Types::LinkType, null: false
end
end
4 changes: 3 additions & 1 deletion app/models/link.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
class Link < ApplicationRecord
belongs_to :user
belongs_to :user, optional: true # Prevent ActiveRecord::RecordInvalid

has_many :votes
end
3 changes: 3 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ class User < ApplicationRecord

validates :name, presence: true
validates :email, presence: true, uniqueness: true

has_many :votes
has_many :links
end
4 changes: 4 additions & 0 deletions app/models/vote.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Vote < ApplicationRecord
belongs_to :link
belongs_to :user
end
10 changes: 10 additions & 0 deletions db/migrate/20201201042112_create_votes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateVotes < ActiveRecord::Migration[6.0]
def change
create_table :votes do |t|
t.references :link, null: false, foreign_key: true
t.references :user, null: false, foreign_key: true

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

ActiveRecord::Schema.define(version: 2020_12_01_040432) do
ActiveRecord::Schema.define(version: 2020_12_01_042112) do

create_table "links", force: :cascade do |t|
t.string "url"
Expand All @@ -29,5 +29,16 @@
t.datetime "updated_at", precision: 6, null: false
end

create_table "votes", force: :cascade do |t|
t.integer "link_id", null: false
t.integer "user_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["link_id"], name: "index_votes_on_link_id"
t.index ["user_id"], name: "index_votes_on_user_id"
end

add_foreign_key "links", "users"
add_foreign_key "votes", "links"
add_foreign_key "votes", "users"
end
9 changes: 9 additions & 0 deletions test/fixtures/votes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
link: one
user: one

two:
link: two
user: two
7 changes: 7 additions & 0 deletions test/models/vote_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class VoteTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit 6d5d778

Please sign in to comment.