Skip to content

Commit

Permalink
Add rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTeacher committed Nov 17, 2023
1 parent 990ac19 commit 7689448
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ on:
- main

jobs:
rspec:
test:
runs-on: ubuntu-latest

services:
Expand Down Expand Up @@ -49,6 +49,9 @@ jobs:
bundle exec rails db:create
bundle exec rails db:migrate
- name: Run Rubocop
run: rubocop

- name: Run RSpec
env:
RAILS_ENV: test
Expand Down
27 changes: 27 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require: rubocop-rails

AllCops:
Exclude:
# App files
- "app/channels/application_cable/**/*"
- "app/graphql/kiln_backend_schema.rb"
- "app/jobs/application_job.rb"
- "app/mailers/application_mailer.rb"
- "app/models/application_record.rb"
- "bin/**/*"
- "config.ru"
- "config/**/*"
- "db/**/*"
- "Rakefile"
- "spec/rails_helper.rb"
- "spec/spec_helper.rb"
# Added files to review
- "app/controllers/graphql_controller.rb"
- "app/graphql/types/base_*"

Documentation:
Enabled: false

Metrics/BlockLength:
Exclude:
- "spec/**/*"
34 changes: 18 additions & 16 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
source "https://rubygems.org"
# frozen_string_literal: true

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "3.2.1"
ruby '3.2.1'

gem "bootsnap", require: false
gem "graphql"
gem "pg", "~> 1.1"
gem "puma", "~> 5.0"
gem "rack-cors"
gem "rails", "~> 7.0.8"
gem "rubocop-rails", require: false
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
gem 'bootsnap', require: false
gem 'graphql'
gem 'pg', '~> 1.1'
gem 'puma', '~> 5.0'
gem 'rack-cors'
gem 'rails', '~> 7.0.8'
gem 'rubocop-rails', require: false
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]

# Build JSON APIs with ease [https://github.com/rails/jbuilder]
# gem "jbuilder"
Expand All @@ -28,16 +30,16 @@ gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
# gem "image_processing", "~> 1.2"

group :development, :test do
gem "debug", platforms: %i[ mri mingw x64_mingw ]
gem "factory_bot_rails"
gem "faker"
gem "rspec-rails", "~> 6.0.0"
gem 'debug', platforms: %i[mri mingw x64_mingw]
gem 'factory_bot_rails'
gem 'faker'
gem 'rspec-rails', '~> 6.0.0'
end

group :development do
gem "graphiql-rails"
gem 'graphiql-rails'
end

group :test do
gem "shoulda-matchers", "~> 5.0"
gem 'shoulda-matchers', '~> 5.0'
end
2 changes: 2 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# frozen_string_literal: true

class ApplicationController < ActionController::API
end
5 changes: 3 additions & 2 deletions app/controllers/graphql_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ def execute
# Query context goes here, for example:
# current_user: current_user,
}
result = KilnBackendSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
result = KilnBackendSchema.execute(query, variables:, context:, operation_name:)
render json: result
rescue StandardError => e
raise e unless Rails.env.development?

handle_error_in_development(e)
end

Expand Down Expand Up @@ -47,6 +48,6 @@ def handle_error_in_development(e)
logger.error e.message
logger.error e.backtrace.join("\n")

render json: { errors: [{ message: e.message, backtrace: e.backtrace }], data: {} }, status: 500
render json: { errors: [{ message: e.message, backtrace: e.backtrace }], data: {} }, status: :internal_server_error
end
end
2 changes: 1 addition & 1 deletion app/graphql/types/member_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Types
class MemberType < Types::BaseObject
description "A Kiln Collective member"
description 'A Kiln Collective member'

field :id, ID, null: false
field :first_name, String
Expand Down
12 changes: 6 additions & 6 deletions app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

module Types
class MutationType < Types::BaseObject
field :create_member, MemberType, null: true, description: "Create a new member" do
field :create_member, MemberType, null: true, description: 'Create a new member' do
argument :first_name, String, required: true
argument :last_name, String, required: true
argument :title, String, required: true
end
def create_member(first_name:, last_name:, title:)
Member.create(
first_name: first_name,
last_name: last_name,
title: title
first_name:,
last_name:,
title:
)
end

field :delete_member, Boolean, null: false, description: "Delete a member permanently" do
field :delete_member, Boolean, null: false, description: 'Delete a member permanently' do
argument :id, ID, required: true
end
def delete_member(id:)
member = Member.find_by(id: id)
member = Member.find_by(id:)
return false unless member

member.destroy
Expand Down
19 changes: 10 additions & 9 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

module Types
class QueryType < Types::BaseObject
field :node, Types::NodeType, null: true, description: "Fetches an object given its ID." do
argument :id, ID, required: true, description: "ID of the object."
field :node, Types::NodeType, null: true, description: 'Fetches an object given its ID.' do
argument :id, ID, required: true, description: 'ID of the object.'
end

def node(id:)
context.schema.object_from_id(id, context)
end

field :nodes, [Types::NodeType, null: true], null: true, description: "Fetches a list of objects given a list of IDs." do
argument :ids, [ID], required: true, description: "IDs of the objects."
field :nodes, [Types::NodeType, { null: true }], null: true,
description: 'Fetches a list of objects given a list of IDs.' do
argument :ids, [ID], required: true, description: 'IDs of the objects.'
end

def nodes(ids:)
Expand All @@ -21,16 +22,16 @@ def nodes(ids:)
# Add root-level fields here.
# They will be entry points for queries on your schema.

field :members, [MemberType], null: false, description: "Fetches all members"
field :members, [MemberType], null: false, description: 'Fetches all members'
def members
Member.all.order("first_name ASC")
Member.all.order('first_name ASC')
end

field :member, MemberType, null: true, description: "Fetches a member by first_name" do
argument :first_name, String, required: true, description: "First name of the member"
field :member, MemberType, null: true, description: 'Fetches a member by first_name' do
argument :first_name, String, required: true, description: 'First name of the member'
end
def member(first_name:)
Member.find_by(first_name: first_name)
Member.find_by(first_name:)
end
end
end
6 changes: 4 additions & 2 deletions app/models/member.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# frozen_string_literal: true

class Member < ApplicationRecord
validates_presence_of :first_name
validates_presence_of :last_name
validates :first_name, presence: true
validates :last_name, presence: true
end
2 changes: 2 additions & 0 deletions spec/factories/member.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

FactoryBot.define do
factory :member do
first_name { Faker::Name.first_name }
Expand Down
40 changes: 21 additions & 19 deletions spec/graphql/mutations/members/create_member_spec.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require "rails_helper"
# frozen_string_literal: true

require 'rails_helper'

module Mutations
RSpec.describe Member, type: :request do
describe "Member create mutation" do
let(:mutation) {
describe 'Member create mutation' do
let(:mutation) do
<<~GQL
mutation {
createMember(
Expand All @@ -16,54 +18,54 @@ module Mutations
}
}
GQL
}
end

let(:result) { KilnBackendSchema.execute(mutation).as_json }

it "returns the created Member" do
data = result["data"]["createMember"]
it 'returns the created Member' do
data = result['data']['createMember']

expect(data).to include(
{
"id" => be_present,
"firstName" => "Test"
'id' => be_present,
'firstName' => 'Test'
}
)
expect(Member.count).to be 1
end
end

describe "Member delete mutation" do
describe 'Member delete mutation' do
let!(:result) { KilnBackendSchema.execute(mutation).as_json }

context "when member exists" do
context 'when member exists' do
let(:member) { create(:member) }
let(:mutation) {
let(:mutation) do
<<~GQL
mutation {
deleteMember(id: "#{member.id}")
}
GQL
}
end

it "returns true" do
data = result["data"]["deleteMember"]
it 'returns true' do
data = result['data']['deleteMember']
expect(data).to be true
expect(Member.count).to be 0
end
end

context "when member does not exist" do
let(:mutation) {
context 'when member does not exist' do
let(:mutation) do
<<~GQL
mutation {
deleteMember(id: "999")
}
GQL
}
end

it "returns false" do
expect(result["data"]["deleteMember"]).to be false
it 'returns false' do
expect(result['data']['deleteMember']).to be false
end
end
end
Expand Down
48 changes: 25 additions & 23 deletions spec/graphql/queries/member_spec.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require "rails_helper"
# frozen_string_literal: true

require 'rails_helper'

module Queries
RSpec.describe Member, type: :request do
describe "Member query" do
describe 'Member query' do
let!(:member) { create(:member) }
let(:query) {
let(:query) do
<<~GQL
query {
member(firstName: "#{member.first_name}") {
Expand All @@ -15,26 +17,26 @@ module Queries
}
}
GQL
}
end

let(:result) { KilnBackendSchema.execute(query).as_json }

it "returns the expected Member" do
data = result["data"]["member"]
it 'returns the expected Member' do
data = result['data']['member']

expect(data).to include(
"id" => member.id.to_s,
"firstName" => member.first_name,
"lastName" => member.last_name,
"title" => member.title
'id' => member.id.to_s,
'firstName' => member.first_name,
'lastName' => member.last_name,
'title' => member.title
)
end
end
end

describe "Members query" do
describe 'Members query' do
let!(:members) { create_list(:member, 2) }
let(:query) {
let(:query) do
<<~GQL
query {
members {
Expand All @@ -45,25 +47,25 @@ module Queries
}
}
GQL
}
end

let(:result) { KilnBackendSchema.execute(query).as_json }

it "returns all Members" do
data = result["data"]["members"]
it 'returns all Members' do
data = result['data']['members']

expect(data).to include(
{
"id" => members[0].id.to_s,
"firstName" => members[0].first_name,
"lastName" => members[0].last_name,
"title" => members[0].title
'id' => members[0].id.to_s,
'firstName' => members[0].first_name,
'lastName' => members[0].last_name,
'title' => members[0].title
},
{
"id" => members[1].id.to_s,
"firstName" => members[1].first_name,
"lastName" => members[1].last_name,
"title" => members[1].title
'id' => members[1].id.to_s,
'firstName' => members[1].first_name,
'lastName' => members[1].last_name,
'title' => members[1].title
}
)
end
Expand Down
Loading

0 comments on commit 7689448

Please sign in to comment.