Skip to content

Commit

Permalink
* move company under company folder
Browse files Browse the repository at this point in the history
* add companyUpdate mutation + specs
  • Loading branch information
simonfranzen committed Sep 18, 2020
1 parent 1cf8414 commit 79c83cf
Show file tree
Hide file tree
Showing 21 changed files with 288 additions and 96 deletions.
10 changes: 10 additions & 0 deletions app/graphql/mutations/base_mutation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Mutations
# Base mutation class for app
class BaseMutation < GraphQL::Schema::Mutation
def current_ability
Ability.new(context[:current_user])
end
end
end
25 changes: 25 additions & 0 deletions app/graphql/mutations/companies/update_company.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

module Mutations
module Companies
# Updates an existing company.
class UpdateCompany < Mutations::BaseMutation
description 'Updates an existing company.'
argument :id, ID, required: true
argument :attributes, Types::Companies::CompanyInputType, required: true
payload_type Types::Companies::CompanyType

def resolve(id:, attributes:)
# find company
company = ::Companies::Company.accessible_by(current_ability).find_by(id: id)
if company.nil?
raise I18n.t('errors.messages.resource_not_found', resource: ::Companies::Company.model_name.human)
end

company.attributes = attributes.to_h
current_ability.authorize! :update, company
return company if company.save!
end
end
end
end
15 changes: 15 additions & 0 deletions app/graphql/resolvers/companies/company.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

module Resolvers
module Companies
# Resolver to return a user
class Company < ::Resolvers::BaseResolver
type Types::Companies::CompanyType, null: true
description 'Returns the company for the user.'

def resolve
context[:current_user] ? context[:current_user].company : nil
end
end
end
end
13 changes: 0 additions & 13 deletions app/graphql/resolvers/company.rb

This file was deleted.

11 changes: 11 additions & 0 deletions app/graphql/types/companies/company_input_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

module Types
module Companies
# Attributes to update a company.
class CompanyInputType < Types::BaseInputObject
description 'Attributes to update a company.'
argument :name, String, 'Name of company', required: true
end
end
end
12 changes: 12 additions & 0 deletions app/graphql/types/companies/company_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

module Types
module Companies
# GraphQL type for a company
class CompanyType < ::Types::BaseModel
field :name, String, null: false
field :slug, String, null: true
# field :users, [::Types::UserType], null: true
end
end
end
10 changes: 0 additions & 10 deletions app/graphql/types/company_type.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/graphql/types/mutation_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ class MutationType < Types::BaseObject
implements ::Types::GraphqlAuth

# Mutations
# field :login, mutation: Mutations::User::Login
field :update_company, mutation: Mutations::Companies::UpdateCompany
end
end
2 changes: 1 addition & 1 deletion app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def users(**_args)
end
field :user, resolver: Resolvers::User

field :company, resolver: Resolvers::Company
field :company, resolver: Resolvers::Companies::Company

def current_ability
Ability.new(context[:current_user])
Expand Down
2 changes: 1 addition & 1 deletion app/graphql/types/user_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ class UserType < BaseModel
field :first_name, String, null: false
field :last_name, String, null: false
field :email, String, null: true
field :company, Types::CompanyType, null: false
field :company, Types::Companies::CompanyType, null: false
end
end
5 changes: 3 additions & 2 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ def initialize(user)
can :manage, :all # admins can manage all objects
elsif user.admin?
can :crud, User, company_id: user.company_id
else
can :read, User, company_id: user.company_id
can :update, Companies::Company, id: user.company_id
end
can :read, Companies::Company, id: user.company_id
can :read, User, company_id: user.company_id

# See the wiki for details:
# https://github.com/CanCanCommunity/cancancan/wiki/Defining-Abilities
Expand Down
67 changes: 67 additions & 0 deletions app/models/companies/company.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# frozen_string_literal: true

module Companies
# == Schema Information
#
# Table name: companies
#
# id :uuid not null, primary key
# name :string
# slug :string
# users_count :integer
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_companies_on_slug (slug) UNIQUE
#
class Company < ApplicationRecord
extend FriendlyId

self.table_name = 'companies'

# - EXTENSIONS
friendly_id :name, use: :slugged

# - VALIDATIONS
validates :name, presence: true
validates :name, length: { maximum: 255 }
validates :slug, length: { maximum: 255 }

# - RELATIONS
has_many :users, dependent: :destroy

# override friendly id checker for categories
def should_generate_new_friendly_id?
(slug.nil? || slug.blank?) || (name_changed? && !slug_changed?)
end

# :nocov:
rails_admin do
weight 10
navigation_icon 'fa fa-building'

list do
field :id
field :name
field :slug
field :users_count
end

edit do
field :name
field :slug
end

show do
field :id
field :name
field :slug
field :users_count
end
end

# :nocov:
end
end
63 changes: 0 additions & 63 deletions app/models/company.rb

This file was deleted.

7 changes: 5 additions & 2 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class User < ApplicationRecord
validates :last_name, length: { maximum: 255 }

# - RELATIONS
belongs_to :company, counter_cache: true
belongs_to :company, counter_cache: true, class_name: 'Companies::Company'

# - CALLBACKS
after_initialize :setup_new_user, if: :new_record?
Expand All @@ -81,7 +81,10 @@ def status_color
private

def setup_company
self.company = Company.create!(name: 'My company') if company.nil?
return unless company.nil?

self.company = Companies::Company.create!(name: 'My company')
self.role = :admin # make this user the admin
end

def setup_new_user
Expand Down
1 change: 1 addition & 0 deletions config/locales/errors/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ de:
not_saved:
one: "%{resource} konnte aufgrund eines Fehlers nicht gespeichert werden:"
other: "%{count} Fehler verhinderten das Speichern von %{resource}:"
resource_not_found: "%{resource} nicht gefunden."
1 change: 1 addition & 0 deletions config/locales/errors/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ en:
not_saved:
one: "1 error prohibited this %{resource} from being saved:"
other: "%{count} errors prohibited this %{resource} from being saved:"
resource_not_found: "%{resource} not found."
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# index_companies_on_slug (slug) UNIQUE
#
FactoryBot.define do
factory :company do
factory :company, class: 'Companies::Company' do
name { 'My Company' }
end
end
Loading

0 comments on commit 79c83cf

Please sign in to comment.