added virtuals

This commit is contained in:
Jez Caudle 2023-05-10 15:14:06 +01:00
parent 793e944b5f
commit 8f1fe8c7c2
27 changed files with 297 additions and 15 deletions

View File

@ -0,0 +1,70 @@
class VirtualsController < ApplicationController
before_action :set_virtual, only: %i[ show edit update destroy ]
# GET /virtuals or /virtuals.json
def index
@virtuals = Virtual.all
end
# GET /virtuals/1 or /virtuals/1.json
def show
end
# GET /virtuals/new
def new
@virtual = Virtual.new
end
# GET /virtuals/1/edit
def edit
end
# POST /virtuals or /virtuals.json
def create
@virtual = Virtual.new(virtual_params)
respond_to do |format|
if @virtual.save
format.html { redirect_to virtual_url(@virtual), notice: "Virtual was successfully created." }
format.json { render :show, status: :created, location: @virtual }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @virtual.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /virtuals/1 or /virtuals/1.json
def update
respond_to do |format|
if @virtual.update(virtual_params)
format.html { redirect_to virtual_url(@virtual), notice: "Virtual was successfully updated." }
format.json { render :show, status: :ok, location: @virtual }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @virtual.errors, status: :unprocessable_entity }
end
end
end
# DELETE /virtuals/1 or /virtuals/1.json
def destroy
@virtual.destroy
respond_to do |format|
format.html { redirect_to virtuals_url, notice: "Virtual was successfully destroyed." }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_virtual
@virtual = Virtual.find(params[:id])
end
# Only allow a list of trusted parameters through.
def virtual_params
params.require(:virtual).permit(:email, :destination)
end
end

View File

@ -0,0 +1,2 @@
module VirtualsHelper
end

View File

@ -1,2 +1,4 @@
class Domain < ApplicationRecord class Domain < ApplicationRecord
validates :domain, presence: true
validates :domain, uniqueness: true
end end

2
app/models/virtual.rb Normal file
View File

@ -0,0 +1,2 @@
class Virtual < ApplicationRecord
end

View File

@ -0,0 +1,27 @@
<%= form_with(model: virtual) do |form| %>
<% if virtual.errors.any? %>
<div style="color: red">
<h2><%= pluralize(virtual.errors.count, "error") %> prohibited this virtual from being saved:</h2>
<ul>
<% virtual.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div>
<%= form.label :email, style: "display: block" %>
<%= form.text_field :email %>
</div>
<div>
<%= form.label :destination, style: "display: block" %>
<%= form.text_field :destination %>
</div>
<div>
<%= form.submit %>
</div>
<% end %>

View File

@ -0,0 +1,12 @@
<div id="<%= dom_id virtual %>">
<p>
<strong>Email:</strong>
<%= virtual.email %>
</p>
<p>
<strong>Destination:</strong>
<%= virtual.destination %>
</p>
</div>

View File

@ -0,0 +1,2 @@
json.extract! virtual, :id, :email, :destination, :created_at, :updated_at
json.url virtual_url(virtual, format: :json)

View File

@ -0,0 +1,10 @@
<h1>Editing virtual</h1>
<%= render "form", virtual: @virtual %>
<br>
<div>
<%= link_to "Show this virtual", @virtual %> |
<%= link_to "Back to virtuals", virtuals_path %>
</div>

View File

@ -0,0 +1,14 @@
<p style="color: green"><%= notice %></p>
<h1>Virtuals</h1>
<div id="virtuals">
<% @virtuals.each do |virtual| %>
<%= render virtual %>
<p>
<%= link_to "Show this virtual", virtual %>
</p>
<% end %>
</div>
<%= link_to "New virtual", new_virtual_path %>

View File

@ -0,0 +1 @@
json.array! @virtuals, partial: "virtuals/virtual", as: :virtual

View File

@ -0,0 +1,9 @@
<h1>New virtual</h1>
<%= render "form", virtual: @virtual %>
<br>
<div>
<%= link_to "Back to virtuals", virtuals_path %>
</div>

View File

@ -0,0 +1,10 @@
<p style="color: green"><%= notice %></p>
<%= render @virtual %>
<div>
<%= link_to "Edit this virtual", edit_virtual_path(@virtual) %> |
<%= link_to "Back to virtuals", virtuals_path %>
<%= button_to "Destroy this virtual", @virtual, method: :delete %>
</div>

View File

@ -0,0 +1 @@
json.partial! "virtuals/virtual", virtual: @virtual

View File

@ -67,4 +67,6 @@ Rails.application.configure do
# Uncomment if you wish to allow Action Cable access from any origin. # Uncomment if you wish to allow Action Cable access from any origin.
# config.action_cable.disable_request_forgery_protection = true # config.action_cable.disable_request_forgery_protection = true
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
end end

View File

@ -1,4 +1,5 @@
Rails.application.routes.draw do Rails.application.routes.draw do
resources :virtuals
resources :domains resources :domains
devise_for :users devise_for :users
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

View File

@ -31,7 +31,7 @@ class DeviseCreateUsers < ActiveRecord::Migration[7.0]
t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
t.string :unlock_token # Only if unlock strategy is :email or :both t.string :unlock_token # Only if unlock strategy is :email or :both
t.datetime :locked_at t.datetime :locked_at
t.integer :role, default: 1000, null: false
t.timestamps null: false t.timestamps null: false
end end

View File

@ -0,0 +1,10 @@
class CreateVirtuals < ActiveRecord::Migration[7.0]
def change
create_table :virtuals do |t|
t.string :email
t.string :destination
t.timestamps
end
end
end

BIN
git.core Normal file

Binary file not shown.

View File

@ -0,0 +1,48 @@
require "test_helper"
class VirtualsControllerTest < ActionDispatch::IntegrationTest
setup do
@virtual = virtuals(:one)
end
test "should get index" do
get virtuals_url
assert_response :success
end
test "should get new" do
get new_virtual_url
assert_response :success
end
test "should create virtual" do
assert_difference("Virtual.count") do
post virtuals_url, params: { virtual: { destination: @virtual.destination, email: @virtual.email } }
end
assert_redirected_to virtual_url(Virtual.last)
end
test "should show virtual" do
get virtual_url(@virtual)
assert_response :success
end
test "should get edit" do
get edit_virtual_url(@virtual)
assert_response :success
end
test "should update virtual" do
patch virtual_url(@virtual), params: { virtual: { destination: @virtual.destination, email: @virtual.email } }
assert_redirected_to virtual_url(@virtual)
end
test "should destroy virtual" do
assert_difference("Virtual.count", -1) do
delete virtual_url(@virtual)
end
assert_redirected_to virtuals_url
end
end

View File

@ -1,7 +1,7 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one: one:
domain: MyString domain: example.net
two: two:
domain: MyString domain: example.org

View File

@ -1,11 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html # # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
# This model initially had no columns defined. If you add columns to the
# model remove the "{}" from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
# #
one: {} # # This model initially had no columns defined. If you add columns to the
# column: value # # model remove the "{}" from the fixture names and add the columns immediately
# # # below each fixture, per the syntax in the comments below
two: {} # #
# column: value # one: {}
# # column: value
# #
# two: {}
# # column: value

9
test/fixtures/virtuals.yml vendored Normal file
View File

@ -0,0 +1,9 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
one:
email: MyString
destination: MyString
two:
email: MyString
destination: MyString

View File

@ -8,11 +8,11 @@ class DomainTest < ActiveSupport::TestCase
test "domain can no tbe duplicated" do test "domain can no tbe duplicated" do
@d1 = Domain.new @d1 = Domain.new
@d1.name = "example.com" @d1.domain = "example.com"
assert @d1.save assert @d1.save
@d2 = Domain.new @d2 = Domain.new
@d2.name = "example.com" @d2.domain = "example.com"
assert !@d2.save assert !@d2.save
end end
end end

View File

@ -0,0 +1,7 @@
require "test_helper"
class VirtualTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View File

@ -0,0 +1,43 @@
require "application_system_test_case"
class VirtualsTest < ApplicationSystemTestCase
setup do
@virtual = virtuals(:one)
end
test "visiting the index" do
visit virtuals_url
assert_selector "h1", text: "Virtuals"
end
test "should create virtual" do
visit virtuals_url
click_on "New virtual"
fill_in "Destination", with: @virtual.destination
fill_in "Email", with: @virtual.email
click_on "Create Virtual"
assert_text "Virtual was successfully created"
click_on "Back"
end
test "should update Virtual" do
visit virtual_url(@virtual)
click_on "Edit this virtual", match: :first
fill_in "Destination", with: @virtual.destination
fill_in "Email", with: @virtual.email
click_on "Update Virtual"
assert_text "Virtual was successfully updated"
click_on "Back"
end
test "should destroy Virtual" do
visit virtual_url(@virtual)
click_on "Destroy this virtual", match: :first
assert_text "Virtual was successfully destroyed"
end
end