From 1eb8c2753f1d931daf0397d977cb0e685b6cb603 Mon Sep 17 00:00:00 2001 From: Jez Caudle Date: Wed, 21 Jun 2023 09:31:20 +0100 Subject: [PATCH] Slowly getting the nested controllers working More tests required --- app/models/credential.rb | 1 + app/models/domain.rb | 2 ++ app/models/virtual.rb | 1 + app/views/domains/index.html.erb | 7 +++---- app/views/domains/show.html.erb | 19 +++++++++++++++++-- test/fixtures/virtuals.yml | 2 ++ 6 files changed, 26 insertions(+), 6 deletions(-) diff --git a/app/models/credential.rb b/app/models/credential.rb index cb9acd8..1c914ea 100644 --- a/app/models/credential.rb +++ b/app/models/credential.rb @@ -1,4 +1,5 @@ class Credential < ApplicationRecord + belongs_to :domain validates :email, presence: true validates :email, uniqueness: true validates :email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i , message: "email must be valid"} diff --git a/app/models/domain.rb b/app/models/domain.rb index a2e3053..2bff786 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -1,4 +1,6 @@ class Domain < ApplicationRecord + has_many :credentials, dependent: :destroy + has_many :virtuals, dependent: :destroy validates :domain, presence: true validates :domain, uniqueness: true end diff --git a/app/models/virtual.rb b/app/models/virtual.rb index 581dbbf..f72b887 100644 --- a/app/models/virtual.rb +++ b/app/models/virtual.rb @@ -1,4 +1,5 @@ class Virtual < ApplicationRecord + belongs_to :domain validate :domain_name_exists validates :email, presence: true validates :destination, presence: true diff --git a/app/views/domains/index.html.erb b/app/views/domains/index.html.erb index 703f7f2..5d5e685 100644 --- a/app/views/domains/index.html.erb +++ b/app/views/domains/index.html.erb @@ -3,12 +3,11 @@

Domains

+
<%= link_to "New domain", new_domain_path %> diff --git a/app/views/domains/show.html.erb b/app/views/domains/show.html.erb index d248bf8..ab491de 100644 --- a/app/views/domains/show.html.erb +++ b/app/views/domains/show.html.erb @@ -1,6 +1,5 @@

<%= notice %>

- -<%= render @domain %> +

<%= @domain.domain %>

<%= link_to "Edit this domain", edit_domain_path(@domain) %> | @@ -8,3 +7,19 @@ <%= button_to "Destroy this domain", @domain, method: :delete %>
+ +

Credentials (<%= @domain.credentials.count %>)

+ + +

Virtuals (<%= @domain.virtuals.count %>)

+ diff --git a/test/fixtures/virtuals.yml b/test/fixtures/virtuals.yml index 3509592..431682f 100644 --- a/test/fixtures/virtuals.yml +++ b/test/fixtures/virtuals.yml @@ -1,9 +1,11 @@ # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html one: + domain: one email: abuse@example.net destination: bob@example.net two: + domain: one email: postmaster@example.net destination: bob@example.net