diff --git a/app/assets/stylesheets/emails.scss b/app/assets/stylesheets/emails.scss new file mode 100644 index 0000000..98f8a47 --- /dev/null +++ b/app/assets/stylesheets/emails.scss @@ -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; +} diff --git a/app/assets/stylesheets/scaffolds.scss b/app/assets/stylesheets/scaffolds.scss new file mode 100644 index 0000000..bb2597f --- /dev/null +++ b/app/assets/stylesheets/scaffolds.scss @@ -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; } diff --git a/app/controllers/emails_controller.rb b/app/controllers/emails_controller.rb new file mode 100644 index 0000000..8e3ce4c --- /dev/null +++ b/app/controllers/emails_controller.rb @@ -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 diff --git a/app/helpers/emails_helper.rb b/app/helpers/emails_helper.rb new file mode 100644 index 0000000..b4dc6ec --- /dev/null +++ b/app/helpers/emails_helper.rb @@ -0,0 +1,2 @@ +module EmailsHelper +end diff --git a/app/models/email.rb b/app/models/email.rb index 611a3ea..f2f3c5c 100644 --- a/app/models/email.rb +++ b/app/models/email.rb @@ -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 diff --git a/app/repositories/email_repository.rb b/app/repositories/email_repository.rb index 7ae438f..06e2f98 100644 --- a/app/repositories/email_repository.rb +++ b/app/repositories/email_repository.rb @@ -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 diff --git a/app/views/emails/_email.json.jbuilder b/app/views/emails/_email.json.jbuilder new file mode 100644 index 0000000..a8cc2ec --- /dev/null +++ b/app/views/emails/_email.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! email, :id, :created_at, :updated_at +json.url email_url(email, format: :json) diff --git a/app/views/emails/_form.html.erb b/app/views/emails/_form.html.erb new file mode 100644 index 0000000..0cb69d2 --- /dev/null +++ b/app/views/emails/_form.html.erb @@ -0,0 +1,17 @@ +<%= form_with(model: email) do |form| %> + <% if email.errors.any? %> +
+

<%= pluralize(email.errors.count, "error") %> prohibited this email from being saved:

+ + +
+ <% end %> + +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/emails/edit.html.erb b/app/views/emails/edit.html.erb new file mode 100644 index 0000000..1431b41 --- /dev/null +++ b/app/views/emails/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Email

+ +<%= render 'form', email: @email %> + +<%= link_to 'Show', @email %> | +<%= link_to 'Back', emails_path %> diff --git a/app/views/emails/index.html.erb b/app/views/emails/index.html.erb new file mode 100644 index 0000000..ecbc1fb --- /dev/null +++ b/app/views/emails/index.html.erb @@ -0,0 +1,27 @@ +

<%= notice %>

+ +

Emails

+ + + + + + + + + + <% @emails.each do |email| %> + + + + + + + + <% end %> + +
<%= email.date %><%= email.subject %><%= link_to 'Show', email %><%= link_to 'Edit', edit_email_path(email) %><%= link_to 'Destroy', email, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Email', new_email_path %> diff --git a/app/views/emails/index.json.jbuilder b/app/views/emails/index.json.jbuilder new file mode 100644 index 0000000..70f812c --- /dev/null +++ b/app/views/emails/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @emails, partial: "emails/email", as: :email diff --git a/app/views/emails/new.html.erb b/app/views/emails/new.html.erb new file mode 100644 index 0000000..61c2c95 --- /dev/null +++ b/app/views/emails/new.html.erb @@ -0,0 +1,5 @@ +

New Email

+ +<%= render 'form', email: @email %> + +<%= link_to 'Back', emails_path %> diff --git a/app/views/emails/show.html.erb b/app/views/emails/show.html.erb new file mode 100644 index 0000000..e2aa5fd --- /dev/null +++ b/app/views/emails/show.html.erb @@ -0,0 +1,76 @@ +

<%= notice %>

+ +
+ + + + + + + + + + + + + + + + + + + <% if @email.clients.present? %> + + + + + <% end %> + <% if @email.servers.present? %> + + + + + <% end %> + <% if @email.tickets.present? %> + + + + + <% end %> + + + <% @email.headers.each do |header| %> + + + + <% end %> + + + + + + +
+ <% if @email.plain_body.present? %> +
<%= @email.plain_body %>
+ <% else %> + Empty body + <% end %> +
+
+ +<%= link_to 'Edit', edit_email_path(@email) %> | +<%= link_to 'Back', emails_path %> diff --git a/app/views/emails/show.json.jbuilder b/app/views/emails/show.json.jbuilder new file mode 100644 index 0000000..ff416bd --- /dev/null +++ b/app/views/emails/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "emails/email", email: @email diff --git a/config/routes.rb b/config/routes.rb index c06383a..0f13cee 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/test/controllers/emails_controller_test.rb b/test/controllers/emails_controller_test.rb new file mode 100644 index 0000000..eee26da --- /dev/null +++ b/test/controllers/emails_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class EmailsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end diff --git a/test/system/emails_test.rb b/test/system/emails_test.rb new file mode 100644 index 0000000..86ab52c --- /dev/null +++ b/test/system/emails_test.rb @@ -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