Transformation de Email en objet ActiveRecord
This commit is contained in:
parent
a1c7ef14d0
commit
9af882826b
|
@ -4,22 +4,7 @@ class EmailsController < ApplicationController
|
||||||
# GET /emails
|
# GET /emails
|
||||||
# GET /emails.json
|
# GET /emails.json
|
||||||
def index
|
def index
|
||||||
results = email_repository.search({
|
@emails = Email.all
|
||||||
query: {
|
|
||||||
match_all: {}
|
|
||||||
},
|
|
||||||
sort: [{
|
|
||||||
date: { order: 'desc' }
|
|
||||||
}],
|
|
||||||
size: 100,
|
|
||||||
from: 0,
|
|
||||||
aggregations: {
|
|
||||||
organisations: { terms: { field: 'organisations.keyword' } },
|
|
||||||
servers: { terms: { field: 'servers.keyword' } },
|
|
||||||
issues: { terms: { field: 'issues.keyword' } }
|
|
||||||
},
|
|
||||||
})
|
|
||||||
@emails = results.results
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# GET /emails/1
|
# GET /emails/1
|
||||||
|
@ -69,7 +54,7 @@ class EmailsController < ApplicationController
|
||||||
# DELETE /emails/1
|
# DELETE /emails/1
|
||||||
# DELETE /emails/1.json
|
# DELETE /emails/1.json
|
||||||
def destroy
|
def destroy
|
||||||
email_repository.delete(params[:id])
|
@email.destroy
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html { redirect_to emails_url, notice: 'Email was successfully destroyed.' }
|
format.html { redirect_to emails_url, notice: 'Email was successfully destroyed.' }
|
||||||
format.json { head :no_content }
|
format.json { head :no_content }
|
||||||
|
@ -77,12 +62,9 @@ class EmailsController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def email_repository
|
|
||||||
@email_repository ||= EmailRepository.new
|
|
||||||
end
|
|
||||||
# Use callbacks to share common setup or constraints between actions.
|
# Use callbacks to share common setup or constraints between actions.
|
||||||
def set_email
|
def set_email
|
||||||
@email = email_repository.find(params[:id])
|
@email = Email.find(params[:id])
|
||||||
end
|
end
|
||||||
|
|
||||||
# Only allow a list of trusted parameters through.
|
# Only allow a list of trusted parameters through.
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
class InMailbox < ApplicationMailbox
|
class InMailbox < ApplicationMailbox
|
||||||
def process
|
def process
|
||||||
email_importer = EmailImporter.new
|
email_importer = EmailImporter.new
|
||||||
repository = EmailRepository.new
|
|
||||||
|
|
||||||
email = email_importer.import(mail)
|
email = email_importer.import(mail)
|
||||||
|
|
||||||
processor = RuleSetProcessor.new
|
processor = RuleSetProcessor.new
|
||||||
email = processor.process_all(RuleSet.enabled, email)
|
email = processor.process_all(RuleSet.enabled, email)
|
||||||
|
|
||||||
repository.save(email)
|
# repository = EmailRepository.new
|
||||||
|
# repository.save(email)
|
||||||
|
email.save!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,30 +1,15 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Email
|
class Email < ApplicationRecord
|
||||||
include ActiveModel::Model
|
include Elasticsearch::Model
|
||||||
include ActiveModel::Attributes
|
|
||||||
include ActiveModel::Conversion
|
|
||||||
include ActiveModel::Dirty
|
|
||||||
include GlobalID::Identification
|
|
||||||
|
|
||||||
attribute :id
|
serialize :from, Array
|
||||||
attribute :message_id
|
serialize :to, Array
|
||||||
attribute :subject
|
serialize :delivered_to, Array
|
||||||
attribute :date, :datetime
|
serialize :headers, Array
|
||||||
attribute :to
|
serialize :organisations, Array
|
||||||
attribute :delivered_to
|
serialize :servers, Array
|
||||||
attribute :from
|
serialize :issues, Array
|
||||||
attribute :headers, default: []
|
|
||||||
attribute :plain_body
|
|
||||||
attribute :cron, :boolean, default: false
|
|
||||||
attribute :mailing_list, :boolean, default: false
|
|
||||||
attribute :junk, :boolean, default: false
|
|
||||||
attribute :organisations, default: []
|
|
||||||
attribute :servers, default: []
|
|
||||||
attribute :issues, default: []
|
|
||||||
attribute :postponed_until, :datetime
|
|
||||||
attribute :created_at, :datetime, default: DateTime.now
|
|
||||||
attribute :updated_at, :datetime, default: DateTime.now
|
|
||||||
|
|
||||||
validates :message_id, presence: true
|
validates :message_id, presence: true
|
||||||
validates :subject, presence: true
|
validates :subject, presence: true
|
||||||
|
@ -33,25 +18,6 @@ class Email
|
||||||
validates :from, presence: true
|
validates :from, presence: true
|
||||||
validates :headers, presence: true
|
validates :headers, presence: true
|
||||||
|
|
||||||
def to_hash
|
|
||||||
attributes
|
|
||||||
end
|
|
||||||
|
|
||||||
def persisted?
|
|
||||||
id.present?
|
|
||||||
end
|
|
||||||
|
|
||||||
def cron?
|
|
||||||
cron
|
|
||||||
end
|
|
||||||
|
|
||||||
def mailing_list?
|
|
||||||
mailing_list
|
|
||||||
end
|
|
||||||
|
|
||||||
def junk?
|
|
||||||
junk
|
|
||||||
end
|
|
||||||
|
|
||||||
def postponed?
|
def postponed?
|
||||||
postponed_until.present? && postponed_until > DateTime.now
|
postponed_until.present? && postponed_until > DateTime.now
|
||||||
|
@ -64,8 +30,4 @@ class Email
|
||||||
header["value"]
|
header["value"]
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.find(id)
|
|
||||||
EmailRepository.new.find(id)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
22
db/migrate/20210129124143_create_emails.rb
Normal file
22
db/migrate/20210129124143_create_emails.rb
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
class CreateEmails < ActiveRecord::Migration[6.1]
|
||||||
|
def change
|
||||||
|
create_table :emails do |t|
|
||||||
|
t.string :message_id, null: false
|
||||||
|
t.string :subject, null: false
|
||||||
|
t.datetime :date, null: false
|
||||||
|
t.string :to, null: false
|
||||||
|
t.string :delivered_to, null: false
|
||||||
|
t.string :from, null: false
|
||||||
|
t.text :headers
|
||||||
|
t.text :plain_body
|
||||||
|
t.boolean :cron, default: false, null: false
|
||||||
|
t.boolean :mailing_list, default: false, null: false
|
||||||
|
t.boolean :junk, default: false, null: false
|
||||||
|
t.text :organisations
|
||||||
|
t.text :servers
|
||||||
|
t.text :issues
|
||||||
|
t.datetime :postponed_until
|
||||||
|
t.timestamps
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
22
db/schema.rb
22
db/schema.rb
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 2021_01_18_132809) do
|
ActiveRecord::Schema.define(version: 2021_01_29_124143) do
|
||||||
|
|
||||||
create_table "action_mailbox_inbound_emails", force: :cascade do |t|
|
create_table "action_mailbox_inbound_emails", force: :cascade do |t|
|
||||||
t.integer "status", default: 0, null: false
|
t.integer "status", default: 0, null: false
|
||||||
|
@ -60,6 +60,26 @@ ActiveRecord::Schema.define(version: 2021_01_18_132809) do
|
||||||
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
|
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
create_table "emails", force: :cascade do |t|
|
||||||
|
t.string "message_id", null: false
|
||||||
|
t.string "subject", null: false
|
||||||
|
t.datetime "date", null: false
|
||||||
|
t.string "to", null: false
|
||||||
|
t.string "delivered_to", null: false
|
||||||
|
t.string "from", null: false
|
||||||
|
t.text "headers"
|
||||||
|
t.text "plain_body"
|
||||||
|
t.boolean "cron", default: false, null: false
|
||||||
|
t.boolean "mailing_list", default: false, null: false
|
||||||
|
t.boolean "junk", default: false, null: false
|
||||||
|
t.text "organisations"
|
||||||
|
t.text "servers"
|
||||||
|
t.text "issues"
|
||||||
|
t.datetime "postponed_until"
|
||||||
|
t.datetime "created_at", precision: 6, null: false
|
||||||
|
t.datetime "updated_at", precision: 6, null: false
|
||||||
|
end
|
||||||
|
|
||||||
create_table "metadata_mappings", force: :cascade do |t|
|
create_table "metadata_mappings", force: :cascade do |t|
|
||||||
t.string "input"
|
t.string "input"
|
||||||
t.string "server"
|
t.string "server"
|
||||||
|
|
|
@ -1,7 +1,20 @@
|
||||||
require "test_helper"
|
require "test_helper"
|
||||||
|
|
||||||
class EmailsControllerTest < ActionDispatch::IntegrationTest
|
class EmailsControllerTest < ActionDispatch::IntegrationTest
|
||||||
# test "the truth" do
|
include Devise::Test::IntegrationHelpers
|
||||||
# assert true
|
|
||||||
# end
|
setup do
|
||||||
|
sign_in users(:alice)
|
||||||
|
@email = emails(:one)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should get index" do
|
||||||
|
get emails_url
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
|
|
||||||
|
test "should show email" do
|
||||||
|
get email_url(@email)
|
||||||
|
assert_response :success
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
13
test/fixtures/emails.yml
vendored
Normal file
13
test/fixtures/emails.yml
vendored
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
# 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:
|
||||||
|
message_id: "message-id"
|
||||||
|
subject: "Dummy subject"
|
||||||
|
date: <%= 2.days.ago %>
|
||||||
|
to: <%= %w(to@example.com) %>
|
||||||
|
delivered_to: <%= %w(delivered-to@example.com) %>
|
||||||
|
from: <%= %w(from@example.com) %>
|
|
@ -1,4 +1,4 @@
|
||||||
require 'test_helper'
|
require "test_helper"
|
||||||
|
|
||||||
class EmailTest < ActiveSupport::TestCase
|
class EmailTest < ActiveSupport::TestCase
|
||||||
# test "the truth" do
|
# test "the truth" do
|
||||||
|
|
Loading…
Reference in a new issue