Skip to content

Commit

Permalink
* Rename account to company
Browse files Browse the repository at this point in the history
* add more db seeds
  • Loading branch information
simonfranzen committed Sep 18, 2020
1 parent 0923d9b commit 1cf8414
Show file tree
Hide file tree
Showing 26 changed files with 127 additions and 97 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ group :development, :test do
gem 'dotenv-rails' # craate a .env file to set local environment variables
gem 'factory_bot_rails' # model mocks with factory bot
gem 'rspec-rails', '~> 3.8' # used testframework
gem 'faker', '~> 1.8'
end

group :test do
gem 'database_cleaner', '~> 1.6'
gem 'faker', '~> 1.8'
gem 'rails-controller-testing'
gem 'shoulda-matchers', '4.0.0.rc1'
gem 'simplecov', require: false
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Download a GraphQL client like [GraphiQL](https://github.com/graphql/graphiql) o
## 🎁 What's included?

### 1. Database
The app uses a PostgreSQL database. It implements the connector with the gem `pg`. The app already includes a `User` and a `Account` model with basic setup. We see an `Account` as a company with it's users. We did **not** add multi-tenancy to this app. If you want to do it by yourself check out the [apartment](https://github.com/influitive/apartment) gem.
The app uses a PostgreSQL database. It implements the connector with the gem `pg`. The app already includes a `User` and a `Company` model with basic setup. We see an `Company` as a company with it's users. We did **not** add multi-tenancy to this app. If you want to do it by yourself check out the [apartment](https://github.com/influitive/apartment) gem.


### 2. Authentication
Expand Down Expand Up @@ -162,7 +162,7 @@ We enable HTTP auth currently for all controllers. The `ApplicationController` c


### 13. Auto generated slugs
To provider more user friendly urls for your frontend we are using [friendly_id](https://github.com/norman/friendly_id) to auto generate slugs for models. We have already implemented it for the `Account` model. For more configuration see `config/initializers/friendly_id.rb`.
To provider more user friendly urls for your frontend we are using [friendly_id](https://github.com/norman/friendly_id) to auto generate slugs for models. We have already implemented it for the `Company` model. For more configuration see `config/initializers/friendly_id.rb`.

To create a new slug field for a model add a field `slug`:

Expand All @@ -174,15 +174,15 @@ $ bundle exec rake db:migrate
Edit your model file as the following:

```ruby
class Account < ApplicationRecord
class Company < ApplicationRecord
extend FriendlyId
friendly_id :name, use: :slugged
end
```

Replace traditional `Account.find(params[:id])` with `Account.friendly.find(params[:id])`
Replace traditional `Company.find(params[:id])` with `Company.friendly.find(params[:id])`
```ruby
account = Account.friendly.find(params[:id])
company = Company.friendly.find(params[:id])
```


Expand Down
13 changes: 0 additions & 13 deletions app/graphql/resolvers/account.rb

This file was deleted.

13 changes: 13 additions & 0 deletions app/graphql/resolvers/company.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

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

def resolve
context[:current_user] ? context[:current_user].company : nil
end
end
end
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# frozen_string_literal: true

module Types
# GraphQL type for an account
class AccountType < BaseModel
# GraphQL type for a company
class CompanyType < BaseModel
field :name, String, null: false
field :slug, String, null: true
# field :users, [::Types::UserType], null: true
Expand Down
4 changes: 2 additions & 2 deletions app/graphql/types/query_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ class QueryType < BaseObject

field :users, Types::UserType.connection_type, null: false
def users(**_args)
::User.accessible_by(current_ability).includes(:account)
::User.accessible_by(current_ability).includes(:company)
end
field :user, resolver: Resolvers::User

field :account, resolver: Resolvers::Account
field :company, resolver: Resolvers::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 :account, Types::AccountType, null: false
field :company, Types::CompanyType, null: false
end
end
4 changes: 2 additions & 2 deletions app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def initialize(user)
can :access, :rails_admin # grant access to rails_admin
can :manage, :all # admins can manage all objects
elsif user.admin?
can :crud, User, account_id: user.account_id
can :crud, User, company_id: user.company_id
else
can :read, User, account_id: user.account_id
can :read, User, company_id: user.company_id
end

# See the wiki for details:
Expand Down
6 changes: 3 additions & 3 deletions app/models/account.rb → app/models/company.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# == Schema Information
#
# Table name: accounts
# Table name: companies
#
# id :uuid not null, primary key
# name :string
Expand All @@ -13,9 +13,9 @@
#
# Indexes
#
# index_accounts_on_slug (slug) UNIQUE
# index_companies_on_slug (slug) UNIQUE
#
class Account < ApplicationRecord
class Company < ApplicationRecord
extend FriendlyId

# - EXTENSIONS
Expand Down
16 changes: 8 additions & 8 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# unlock_token :string
# created_at :datetime not null
# updated_at :datetime not null
# account_id :uuid
# company_id :uuid
#
# Indexes
#
Expand Down Expand Up @@ -60,11 +60,11 @@ class User < ApplicationRecord
validates :last_name, length: { maximum: 255 }

# - RELATIONS
belongs_to :account, counter_cache: true
belongs_to :company, counter_cache: true

# - CALLBACKS
after_initialize :setup_new_user, if: :new_record?
before_validation :setup_account
before_validation :setup_company

# return first and lastname
def name
Expand All @@ -80,8 +80,8 @@ def status_color

private

def setup_account
self.account = Account.create!(name: 'My company') if account.nil?
def setup_company
self.company = Company.create!(name: 'My company') if company.nil?
end

def setup_new_user
Expand Down Expand Up @@ -121,7 +121,7 @@ def setup_new_user
field :email
field :role
field :last_sign_in_at
field :account
field :company
end

edit do
Expand All @@ -131,7 +131,7 @@ def setup_new_user
field :password
field :password_confirmation
field :role
field :account
field :company
end

show do
Expand All @@ -141,7 +141,7 @@ def setup_new_user
field :email
field :role
field :last_sign_in_at
field :account
field :company
end
end
# rubocop:enable Metrics/BlockLength
Expand Down
8 changes: 4 additions & 4 deletions config/locales/activerecord/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ de:
slug: Pfad
activerecord:
attributes:
account:
company:
name: Name des Unternehmens
users_count: Anzahl Mitarbeiter
user:
Expand Down Expand Up @@ -37,6 +37,6 @@ de:
user:
one: Benutzer
other: Benutzer
account:
one: Konto
other: Konten
company:
one: Unternehmen
other: Unternehmen
8 changes: 4 additions & 4 deletions config/locales/activerecord/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ en:
slug: Slug
activerecord:
attributes:
account:
company:
name: Name of company
users_count: Number of employees
user:
Expand Down Expand Up @@ -37,6 +37,6 @@ en:
user:
one: User
other: Users
account:
one: Account
other: Accounts
company:
one: Company
other: Companies
2 changes: 1 addition & 1 deletion db/migrate/20190209163712_devise_create_users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def change

t.timestamps null: false

t.uuid :account_id
t.uuid :company_id
end

add_index :users, :email, unique: true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class CreateAccounts < ActiveRecord::Migration[6.0]
class CreateCompanies < ActiveRecord::Migration[6.0]
def change
create_table :accounts, id: :uuid do |t|
create_table :companies, id: :uuid do |t|
t.string :name
t.integer :users_count
t.timestamps
Expand Down
6 changes: 0 additions & 6 deletions db/migrate/20200912153138_add_slug_to_accounts.rb

This file was deleted.

6 changes: 6 additions & 0 deletions db/migrate/20200912153138_add_slug_to_companies.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddSlugToCompanies < ActiveRecord::Migration[6.0]
def change
add_column :companies, :slug, :string
add_index :companies, :slug, unique: true
end
end
2 changes: 1 addition & 1 deletion db/migrate/20200912153858_create_friendly_id_slugs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ def change
add_index :friendly_id_slugs, [:slug, :sluggable_type, :scope], :unique => true
add_index :friendly_id_slugs, :sluggable_type

Account.all.each(&:save)
Company.all.each(&:save)
end
end
20 changes: 10 additions & 10 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@
enable_extension "pgcrypto"
enable_extension "plpgsql"

create_table "accounts", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "name"
t.integer "users_count"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "slug"
t.index ["slug"], name: "index_accounts_on_slug", unique: true
end

create_table "active_storage_attachments", force: :cascade do |t|
t.string "name", null: false
t.string "record_type", null: false
Expand All @@ -46,6 +37,15 @@
t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
end

create_table "companies", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "name"
t.integer "users_count"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "slug"
t.index ["slug"], name: "index_companies_on_slug", unique: true
end

create_table "friendly_id_slugs", force: :cascade do |t|
t.string "slug", null: false
t.integer "sluggable_id", null: false
Expand Down Expand Up @@ -82,7 +82,7 @@
t.integer "role", default: 0, null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.uuid "account_id"
t.uuid "company_id"
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["refresh_token"], name: "index_users_on_refresh_token", unique: true
Expand Down
32 changes: 31 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,34 @@

if user
Rails.logger.info "Login with #{ENV['ADMIN_EMAIL']} and #{ENV['ADMIN_PASSWORD']}"
end
end

# Create a admin user
admin = User.create(
email: Faker::Internet.email,
password: ENV['ADMIN_PASSWORD'],
password_confirmation: ENV['ADMIN_PASSWORD'],
first_name: Faker::Name.first_name,
last_name: Faker::Name.last_name,
role: 'admin'
)

User.create(
email: Faker::Internet.email,
password: ENV['ADMIN_PASSWORD'],
password_confirmation: ENV['ADMIN_PASSWORD'],
first_name: Faker::Name.first_name,
last_name: Faker::Name.last_name,
role: 'user',
company_id: admin.company_id
)

User.create(
email: Faker::Internet.email,
password: ENV['ADMIN_PASSWORD'],
password_confirmation: ENV['ADMIN_PASSWORD'],
first_name: Faker::Name.first_name,
last_name: Faker::Name.last_name,
role: 'user',
company_id: admin.company_id
)
6 changes: 3 additions & 3 deletions spec/factories/accounts.rb → spec/factories/companies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# == Schema Information
#
# Table name: accounts
# Table name: companies
#
# id :uuid not null, primary key
# name :string
Expand All @@ -13,10 +13,10 @@
#
# Indexes
#
# index_accounts_on_slug (slug) UNIQUE
# index_companies_on_slug (slug) UNIQUE
#
FactoryBot.define do
factory :account do
factory :company do
name { 'My Company' }
end
end
Loading

0 comments on commit 1cf8414

Please sign in to comment.