Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client: Seems unnecessary and is getting in the way of things #1161

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/controllers/spaces_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def edit

def create
skip_policy_scope
authorize(Client)
authorize(Space)

space = Space::Factory.create(space_params)
Expand Down
17 changes: 6 additions & 11 deletions app/models/blueprint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
class Blueprint
attr_accessor :attributes

def initialize(attributes)
attr_writer :space

def initialize(attributes, space: nil)
@attributes = attributes
self.space = space
end

def find_or_create!
Expand Down Expand Up @@ -68,19 +71,11 @@ def set_members
end

def space
@space ||= attributes[:space] || client.spaces.find_or_create_by!(name: space_attributes[:name])
end

def client
@client ||= Client.find_or_create_by!(name: client_attributes[:name])
end

def client_attributes
attributes[:client]
@space ||= Space.find_or_create_by!(name: space_attributes[:name])
end

def space_attributes
client_attributes[:space]
attributes
end

# Normally, merge will clobber keys with values if there is a key with a nil
Expand Down
16 changes: 0 additions & 16 deletions app/models/client.rb

This file was deleted.

5 changes: 0 additions & 5 deletions app/models/space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ class Space < ApplicationRecord
include WithinLocation
THEME_OPTIONS = %w[purple_mountains desert_dunes].freeze

# Which client owns the space
belongs_to :client

accepts_nested_attributes_for :client

# The fully-qualified domain to enter the space.
# Spaces without a branded_domain are still accessible via their slug.
# The branded_domain must be unique to ensure we don't accidentally place
Expand Down
3 changes: 1 addition & 2 deletions app/models/space/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ def self.create(space_attributes)

if blueprint_name.present?
Blueprint.new(
client: {name: space.client.name,
space: Blueprint::BLUEPRINTS[blueprint_name]},
Blueprint::BLUEPRINTS[blueprint_name],
space: space
).find_or_create!
end
Expand Down
14 changes: 5 additions & 9 deletions app/models/system_test_space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@ def self.prepare

Space.find_by(name: "System Test")&.destroy

Blueprint.new(client: {name: "System Test",
space: DEFAULT_SPACE_CONFIG.merge(
name: "System Test Branded Domain",
branded_domain: "system-test.zinc.local"
)}).find_or_create!
Blueprint.new(DEFAULT_SPACE_CONFIG.merge(
name: "System Test Branded Domain",
branded_domain: "system-test.zinc.local"
)).find_or_create!

Blueprint.new(client: {name: "System Test",
space: DEFAULT_SPACE_CONFIG
.merge(name: "System Test")})
.find_or_create!
Blueprint.new(DEFAULT_SPACE_CONFIG.merge(name: "System Test")).find_or_create!
end

DEFAULT_SPACE_CONFIG = Blueprint::BLUEPRINTS[:system_test]
Expand Down
22 changes: 0 additions & 22 deletions app/policies/client_policy.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/policies/person_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def show?
alias_method :create?, :show?

def permitted_attributes(_params)
[:name, :slug, :theme, :blueprint, client_attributes: [:name]]
[:name, :slug, :theme, :blueprint]
end

class Scope < ApplicationScope
Expand Down
2 changes: 1 addition & 1 deletion app/policies/space_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def destroy?
alias_method :create?, :destroy?

def permitted_attributes(_params)
[:name, :slug, :theme, :entrance_id, :blueprint, client_attributes: [:name]]
[:name, :slug, :theme, :entrance_id, :blueprint]
end

class Scope < ApplicationScope
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20230302024315_drop_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class DropClient < ActiveRecord::Migration[7.0]
def change
drop_table :clients
end
end
9 changes: 1 addition & 8 deletions 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[7.0].define(version: 2023_02_27_232512) do
ActiveRecord::Schema[7.0].define(version: 2023_03_02_024315) do
# These are extensions that must be enabled in order to support this database
enable_extension "pgcrypto"
enable_extension "plpgsql"
Expand Down Expand Up @@ -72,13 +72,6 @@
t.index ["person_id"], name: "index_authentication_methods_on_person_id"
end

create_table "clients", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
t.string "name"
t.string "slug"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "friendly_id_slugs", force: :cascade do |t|
t.string "slug", null: false
t.integer "sluggable_id", null: false
Expand Down
Binary file modified docs/erd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions features/lib/Space.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class Space {
space: {
name: this.name,
slug: this.slug,
blueprint: "system_test",
clientAttributes: { name: this.name },
blueprint: "system_test"
},
};
}
Expand Down
5 changes: 0 additions & 5 deletions spec/factories/client.rb

This file was deleted.

5 changes: 0 additions & 5 deletions spec/factories/space.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@

FactoryBot.define do
factory :space do
client
sequence(:name) { |n| "#{Faker::Book.title} #{n}" }

trait :default do
slug { Neighborhood.config.default_space_slug }
end

trait :with_client_attributes do
client_attributes { attributes_for(:client) }
end

trait :with_members do
transient do
member_count { 4 }
Expand Down
27 changes: 11 additions & 16 deletions spec/models/blueprint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,17 @@

RSpec.describe Blueprint do
EXAMPLE_CONFIG = {
client: {
name: "Client A",
space: {
name: "Client A's Space",
members: [{email: "client-a@example.com"}],
rooms: [{
name: "Room A",
access_level: :unlocked,
publicity_level: :listed,
furniture_placements: {
markdown_text_block: {content: "Obi Swan Kenobi"}
}
}],
utility_hookups: []
name: "Client A's Space",
members: [{email: "client-a@example.com"}],
rooms: [{
name: "Room A",
access_level: :unlocked,
publicity_level: :listed,
furniture_placements: {
markdown_text_block: {content: "Obi Swan Kenobi"}
}
}
}],
utility_hookups: []
}.freeze
describe "#find_or_create" do
it "respects the Space's current settings" do
Expand All @@ -41,7 +36,7 @@
it "Updates a given space" do
space = create(:space)

Blueprint.new(EXAMPLE_CONFIG.merge(space: space)).find_or_create!
Blueprint.new(EXAMPLE_CONFIG, space: space).find_or_create!

expect(space.rooms.count).to be(1)
end
Expand Down
8 changes: 3 additions & 5 deletions spec/models/room_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

describe ".slug" do
it "creates unique slugs by space scope" do
client = Client.create(name: "test")
space_1 = client.spaces.create(name: "space1")
space_2 = client.spaces.create(name: "space2")
space_1 = Space.create(name: "space1")
space_2 = Space.create(name: "space2")
space_1_room = space_1.rooms.create(name: "room1", publicity_level: :listed)
space_2_room = space_2.rooms.create(name: "room1", publicity_level: :listed)

Expand All @@ -18,8 +17,7 @@

describe ".listed" do
it "does not include rooms whose publicity level is unlisted" do
client = Client.create(name: "test")
space = client.spaces.create(name: "space")
space = Space.create(name: "space")
listed_room = space.rooms.create(publicity_level: :listed, name: "Listed Room")
unlisted_room = space.rooms.create(publicity_level: :unlisted, name: "Unlisted Room")

Expand Down
10 changes: 5 additions & 5 deletions spec/models/space/factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
RSpec.describe Space::Factory do
describe ".create" do
it "creates a Space from the given attributes" do
attributes = attributes_for(:space, :with_client_attributes)
space = Space::Factory.create(attributes)
attributes = attributes_for(:space)
space = described_class.create(attributes)

expect(space).to be_persisted
expect(space.name).to eq(attributes[:name])
Expand All @@ -17,15 +17,15 @@
it "upserts if a Space has the provided name already" do
existing_space = create(:space)

attributes = attributes_for(:space, :with_client_attributes,
attributes = attributes_for(:space,
name: existing_space.name)
space = Space::Factory.create(attributes)
space = described_class.create(attributes)

expect(space).to eql(existing_space)
end

it "applies the blueprint if it's provided" do
space = Space::Factory.create(attributes_for(:space, :with_client_attributes,
space = described_class.create(attributes_for(:space,
blueprint: :system_test))

expect(space.rooms).to be_present
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/spaces_controller_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
let(:Authorization) { encode_authorization_token(api_key) }
let(:body) { {space: attributes} }
response "201", "space created" do
let(:attributes) { attributes_for(:space, :with_client_attributes) }
let(:attributes) { attributes_for(:space) }
run_test! do |_response|
space = Space.find_by(name: attributes[:name])
expect(space.rooms).to be_empty
Expand All @@ -55,7 +55,7 @@
end

context "with a blueprint" do
let(:attributes) { attributes_for(:space, :with_client_attributes, blueprint: :system_test) }
let(:attributes) { attributes_for(:space, blueprint: :system_test) }

response "201", "space created from the blueprint" do
run_test! do |_response|
Expand Down