added virtuals
This commit is contained in:
parent
793e944b5f
commit
8f1fe8c7c2
70
app/controllers/virtuals_controller.rb
Normal file
70
app/controllers/virtuals_controller.rb
Normal 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
|
||||
2
app/helpers/virtuals_helper.rb
Normal file
2
app/helpers/virtuals_helper.rb
Normal file
@ -0,0 +1,2 @@
|
||||
module VirtualsHelper
|
||||
end
|
||||
@ -1,2 +1,4 @@
|
||||
class Domain < ApplicationRecord
|
||||
validates :domain, presence: true
|
||||
validates :domain, uniqueness: true
|
||||
end
|
||||
|
||||
2
app/models/virtual.rb
Normal file
2
app/models/virtual.rb
Normal file
@ -0,0 +1,2 @@
|
||||
class Virtual < ApplicationRecord
|
||||
end
|
||||
27
app/views/virtuals/_form.html.erb
Normal file
27
app/views/virtuals/_form.html.erb
Normal 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 %>
|
||||
12
app/views/virtuals/_virtual.html.erb
Normal file
12
app/views/virtuals/_virtual.html.erb
Normal 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>
|
||||
2
app/views/virtuals/_virtual.json.jbuilder
Normal file
2
app/views/virtuals/_virtual.json.jbuilder
Normal file
@ -0,0 +1,2 @@
|
||||
json.extract! virtual, :id, :email, :destination, :created_at, :updated_at
|
||||
json.url virtual_url(virtual, format: :json)
|
||||
10
app/views/virtuals/edit.html.erb
Normal file
10
app/views/virtuals/edit.html.erb
Normal 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>
|
||||
14
app/views/virtuals/index.html.erb
Normal file
14
app/views/virtuals/index.html.erb
Normal 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 %>
|
||||
1
app/views/virtuals/index.json.jbuilder
Normal file
1
app/views/virtuals/index.json.jbuilder
Normal file
@ -0,0 +1 @@
|
||||
json.array! @virtuals, partial: "virtuals/virtual", as: :virtual
|
||||
9
app/views/virtuals/new.html.erb
Normal file
9
app/views/virtuals/new.html.erb
Normal file
@ -0,0 +1,9 @@
|
||||
<h1>New virtual</h1>
|
||||
|
||||
<%= render "form", virtual: @virtual %>
|
||||
|
||||
<br>
|
||||
|
||||
<div>
|
||||
<%= link_to "Back to virtuals", virtuals_path %>
|
||||
</div>
|
||||
10
app/views/virtuals/show.html.erb
Normal file
10
app/views/virtuals/show.html.erb
Normal 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>
|
||||
1
app/views/virtuals/show.json.jbuilder
Normal file
1
app/views/virtuals/show.json.jbuilder
Normal file
@ -0,0 +1 @@
|
||||
json.partial! "virtuals/virtual", virtual: @virtual
|
||||
Binary file not shown.
@ -67,4 +67,6 @@ Rails.application.configure do
|
||||
|
||||
# Uncomment if you wish to allow Action Cable access from any origin.
|
||||
# config.action_cable.disable_request_forgery_protection = true
|
||||
|
||||
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
|
||||
end
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
Rails.application.routes.draw do
|
||||
resources :virtuals
|
||||
resources :domains
|
||||
devise_for :users
|
||||
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
|
||||
|
||||
Binary file not shown.
@ -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.string :unlock_token # Only if unlock strategy is :email or :both
|
||||
t.datetime :locked_at
|
||||
|
||||
t.integer :role, default: 1000, null: false
|
||||
|
||||
t.timestamps null: false
|
||||
end
|
||||
|
||||
10
db/migrate/20230510141218_create_virtuals.rb
Normal file
10
db/migrate/20230510141218_create_virtuals.rb
Normal 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
|
||||
48
test/controllers/virtuals_controller_test.rb
Normal file
48
test/controllers/virtuals_controller_test.rb
Normal 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
|
||||
4
test/fixtures/domains.yml
vendored
4
test/fixtures/domains.yml
vendored
@ -1,7 +1,7 @@
|
||||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
|
||||
one:
|
||||
domain: MyString
|
||||
domain: example.net
|
||||
|
||||
two:
|
||||
domain: MyString
|
||||
domain: example.org
|
||||
|
||||
20
test/fixtures/users.yml
vendored
20
test/fixtures/users.yml
vendored
@ -1,11 +1,11 @@
|
||||
# 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
|
||||
# # Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
|
||||
#
|
||||
one: {}
|
||||
# column: value
|
||||
#
|
||||
two: {}
|
||||
# column: value
|
||||
# # 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: {}
|
||||
# # column: value
|
||||
# #
|
||||
# two: {}
|
||||
# # column: value
|
||||
|
||||
9
test/fixtures/virtuals.yml
vendored
Normal file
9
test/fixtures/virtuals.yml
vendored
Normal 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
|
||||
@ -8,11 +8,11 @@ class DomainTest < ActiveSupport::TestCase
|
||||
|
||||
test "domain can no tbe duplicated" do
|
||||
@d1 = Domain.new
|
||||
@d1.name = "example.com"
|
||||
@d1.domain = "example.com"
|
||||
assert @d1.save
|
||||
|
||||
@d2 = Domain.new
|
||||
@d2.name = "example.com"
|
||||
@d2.domain = "example.com"
|
||||
assert !@d2.save
|
||||
end
|
||||
end
|
||||
|
||||
7
test/models/virtual_test.rb
Normal file
7
test/models/virtual_test.rb
Normal file
@ -0,0 +1,7 @@
|
||||
require "test_helper"
|
||||
|
||||
class VirtualTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
||||
43
test/system/virtuals_test.rb
Normal file
43
test/system/virtuals_test.rb
Normal 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
|
||||
Loading…
x
Reference in New Issue
Block a user