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.json
|
||||
def index
|
||||
results = email_repository.search({
|
||||
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
|
||||
@emails = Email.all
|
||||
end
|
||||
|
||||
# GET /emails/1
|
||||
|
@ -69,7 +54,7 @@ class EmailsController < ApplicationController
|
|||
# DELETE /emails/1
|
||||
# DELETE /emails/1.json
|
||||
def destroy
|
||||
email_repository.delete(params[:id])
|
||||
@email.destroy
|
||||
respond_to do |format|
|
||||
format.html { redirect_to emails_url, notice: 'Email was successfully destroyed.' }
|
||||
format.json { head :no_content }
|
||||
|
@ -77,12 +62,9 @@ class EmailsController < ApplicationController
|
|||
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])
|
||||
@email = Email.find(params[:id])
|
||||
end
|
||||
|
||||
# Only allow a list of trusted parameters through.
|
||||
|
|
|
@ -1,13 +1,14 @@
|
|||
class InMailbox < ApplicationMailbox
|
||||
def process
|
||||
email_importer = EmailImporter.new
|
||||
repository = EmailRepository.new
|
||||
|
||||
email = email_importer.import(mail)
|
||||
|
||||
processor = RuleSetProcessor.new
|
||||
email = processor.process_all(RuleSet.enabled, email)
|
||||
|
||||
repository.save(email)
|
||||
# repository = EmailRepository.new
|
||||
# repository.save(email)
|
||||
email.save!
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,30 +1,15 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Email
|
||||
include ActiveModel::Model
|
||||
include ActiveModel::Attributes
|
||||
include ActiveModel::Conversion
|
||||
include ActiveModel::Dirty
|
||||
include GlobalID::Identification
|
||||
class Email < ApplicationRecord
|
||||
include Elasticsearch::Model
|
||||
|
||||
attribute :id
|
||||
attribute :message_id
|
||||
attribute :subject
|
||||
attribute :date, :datetime
|
||||
attribute :to
|
||||
attribute :delivered_to
|
||||
attribute :from
|
||||
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
|
||||
serialize :from, Array
|
||||
serialize :to, Array
|
||||
serialize :delivered_to, Array
|
||||
serialize :headers, Array
|
||||
serialize :organisations, Array
|
||||
serialize :servers, Array
|
||||
serialize :issues, Array
|
||||
|
||||
validates :message_id, presence: true
|
||||
validates :subject, presence: true
|
||||
|
@ -33,25 +18,6 @@ class Email
|
|||
validates :from, 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?
|
||||
postponed_until.present? && postponed_until > DateTime.now
|
||||
|
@ -64,8 +30,4 @@ class Email
|
|||
header["value"]
|
||||
}
|
||||
end
|
||||
|
||||
def self.find(id)
|
||||
EmailRepository.new.find(id)
|
||||
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.
|
||||
|
||||
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|
|
||||
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
|
||||
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|
|
||||
t.string "input"
|
||||
t.string "server"
|
||||
|
|
|
@ -1,7 +1,20 @@
|
|||
require "test_helper"
|
||||
|
||||
class EmailsControllerTest < ActionDispatch::IntegrationTest
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
include Devise::Test::IntegrationHelpers
|
||||
|
||||
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
|
||||
|
|
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
|
||||
# test "the truth" do
|
||||
|
|
Loading…
Reference in a new issue