scaffold pour les emails

This commit is contained in:
Jérémy Lecour 2020-12-28 13:02:56 +01:00
parent 3c8e56e230
commit 6fc74551e8
17 changed files with 363 additions and 0 deletions

View file

@ -0,0 +1,20 @@
// Place all the styles related to the emails controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
.email_attribute_server {
background-color: cyan;
padding: 1px 2px;
}
.email_attribute_client {
background-color: yellow;
padding: 1px 2px;
}
.email_attribute_ticket {
background-color: orange;
padding: 1px 2px;
}
.email_content th {
text-align: left;
}

View file

@ -0,0 +1,65 @@
body {
background-color: #fff;
color: #333;
margin: 33px; }
body, p, ol, ul, td {
font-family: verdana, arial, helvetica, sans-serif;
font-size: 13px;
line-height: 18px; }
pre {
background-color: #eee;
padding: 10px;
font-size: 11px; }
a {
color: #000; }
a:visited {
color: #666; }
a:hover {
color: #fff;
background-color: #000; }
th {
padding-bottom: 5px; }
td {
padding: 0 5px 7px; }
div.field,
div.actions {
margin-bottom: 10px; }
#notice {
color: green; }
.field_with_errors {
padding: 2px;
background-color: red;
display: table; }
#error_explanation {
width: 450px;
border: 2px solid red;
padding: 7px 7px 0;
margin-bottom: 20px;
background-color: #f0f0f0; }
#error_explanation h2 {
text-align: left;
font-weight: bold;
padding: 5px 5px 5px 15px;
font-size: 12px;
margin: -7px -7px 0;
background-color: #c00;
color: #fff; }
#error_explanation ul li {
font-size: 12px;
list-style: square; }
label {
display: block; }

View file

@ -0,0 +1,82 @@
class EmailsController < ApplicationController
before_action :set_email, only: [:show, :edit, :update, :destroy]
# GET /emails
# GET /emails.json
def index
results = email_repository.search({
query: {
match_all: {}
}
})
@emails = results.results
end
# GET /emails/1
# GET /emails/1.json
def show
end
# GET /emails/new
def new
@email = Email.new
end
# GET /emails/1/edit
def edit
end
# POST /emails
# POST /emails.json
def create
@email = Email.new(email_params)
respond_to do |format|
if @email.save
format.html { redirect_to @email, notice: 'Email was successfully created.' }
format.json { render :show, status: :created, location: @email }
else
format.html { render :new }
format.json { render json: @email.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /emails/1
# PATCH/PUT /emails/1.json
def update
respond_to do |format|
if @email.update(email_params)
format.html { redirect_to @email, notice: 'Email was successfully updated.' }
format.json { render :show, status: :ok, location: @email }
else
format.html { render :edit }
format.json { render json: @email.errors, status: :unprocessable_entity }
end
end
end
# DELETE /emails/1
# DELETE /emails/1.json
def destroy
email_repository.delete(params[:id])
respond_to do |format|
format.html { redirect_to emails_url, notice: 'Email was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def email_repository
@email_repository ||= EmailRepository.new
end
# Use callbacks to share common setup or constraints between actions.
def set_email
@email = email_repository.find(params[:id])
end
# Only allow a list of trusted parameters through.
def email_params
params.fetch(:email, {})
end
end

View file

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

View file

@ -1,7 +1,9 @@
class Email
include ActiveModel::Model
include ActiveModel::Attributes
include ActiveModel::Conversion
attribute :id
attribute :message_id
attribute :subject
attribute :date, :datetime
@ -28,4 +30,8 @@ class Email
def to_hash
attributes
end
def persisted?
id.present?
end
end

View file

@ -28,4 +28,8 @@ class EmailRepository
indexes :created_at, type: 'date'
indexes :updated_at, type: 'date'
end
def deserialize(document)
Email.new document['_source'].merge('id' => document['_id'])
end
end

View file

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

View file

@ -0,0 +1,17 @@
<%= form_with(model: email) do |form| %>
<% if email.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(email.errors.count, "error") %> prohibited this email from being saved:</h2>
<ul>
<% email.errors.each do |error| %>
<li><%= error.full_message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="actions">
<%= form.submit %>
</div>
<% end %>

View file

@ -0,0 +1,6 @@
<h1>Editing Email</h1>
<%= render 'form', email: @email %>
<%= link_to 'Show', @email %> |
<%= link_to 'Back', emails_path %>

View file

@ -0,0 +1,27 @@
<p id="notice"><%= notice %></p>
<h1>Emails</h1>
<table>
<thead>
<tr>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @emails.each do |email| %>
<tr>
<td><%= email.date %></td>
<td><%= email.subject %></td>
<td><%= link_to 'Show', email %></td>
<td><%= link_to 'Edit', edit_email_path(email) %></td>
<td><%= link_to 'Destroy', email, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Email', new_email_path %>

View file

@ -0,0 +1 @@
json.array! @emails, partial: "emails/email", as: :email

View file

@ -0,0 +1,5 @@
<h1>New Email</h1>
<%= render 'form', email: @email %>
<%= link_to 'Back', emails_path %>

View file

@ -0,0 +1,76 @@
<p id="notice"><%= notice %></p>
<div class="email_container">
<table class="email_content">
<thead class="email_main_headers">
<tr>
<th>Date</th>
<td><%= @email.date %></td>
</tr>
<tr>
<th>From</th>
<td><%= @email.from.join(', ') %></td>
</tr>
<tr>
<th>To</th>
<td><%= @email.to.join(', ') %></td>
</tr>
<tr>
<th>Subject</th>
<td><%= @email.subject %></td>
</tr>
<% if @email.clients.present? %>
<tr>
<th>Clients</th>
<td>
<% @email.clients.presence.each do |client| %>
<span class="email_attribute_client"><%= client %></span>
<% end %>
</td>
</tr>
<% end %>
<% if @email.servers.present? %>
<tr>
<th>Servers</th>
<td>
<% @email.servers.presence.each do |server| %>
<span class="email_attribute_server"><%= server %></span>
<% end %>
</td>
</tr>
<% end %>
<% if @email.tickets.present? %>
<tr>
<th>Tickets</th>
<td>
<% @email.tickets.presence.each do |ticket| %>
<span class="email_attribute_ticket"><%= ticket %></span>
<% end %>
</td>
</tr>
<% end %>
</thead>
<thead class="email_additional_headers">
<% @email.headers.each do |header| %>
<tr>
<th><%= header["name"] %></th>
<td><%= header["value"] %><td>
</tr>
<% end %>
</thead>
<tbody>
<tr>
<td colspan="2">
<% if @email.plain_body.present? %>
<pre><%= @email.plain_body %></pre>
<% else %>
<i>Empty body</i>
<% end %>
</td>
</tr>
</tbody>
</table>
</div>
<%= link_to 'Edit', edit_email_path(@email) %> |
<%= link_to 'Back', emails_path %>

View file

@ -0,0 +1 @@
json.partial! "emails/email", email: @email

View file

@ -1,3 +1,4 @@
Rails.application.routes.draw do
resources :emails
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
end

View file

@ -0,0 +1,7 @@
require "test_helper"
class EmailsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

View file

@ -0,0 +1,41 @@
require "application_system_test_case"
class EmailsTest < ApplicationSystemTestCase
setup do
@email = emails(:one)
end
test "visiting the index" do
visit emails_url
assert_selector "h1", text: "Emails"
end
test "creating a Email" do
visit emails_url
click_on "New Email"
click_on "Create Email"
assert_text "Email was successfully created"
click_on "Back"
end
test "updating a Email" do
visit emails_url
click_on "Edit", match: :first
click_on "Update Email"
assert_text "Email was successfully updated"
click_on "Back"
end
test "destroying a Email" do
visit emails_url
page.accept_confirm do
click_on "Destroy", match: :first
end
assert_text "Email was successfully destroyed"
end
end