From 9af882826b3d5aabcffba805b312b8f386379d35 Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Sat, 30 Jan 2021 18:22:00 +0100 Subject: [PATCH] Transformation de Email en objet ActiveRecord --- app/controllers/emails_controller.rb | 24 ++-------- app/mailboxes/in_mailbox.rb | 5 +- app/models/email.rb | 56 ++++------------------ db/migrate/20210129124143_create_emails.rb | 22 +++++++++ db/schema.rb | 22 ++++++++- test/controllers/emails_controller_test.rb | 19 ++++++-- test/fixtures/emails.yml | 13 +++++ test/models/email_test.rb | 2 +- 8 files changed, 88 insertions(+), 75 deletions(-) create mode 100644 db/migrate/20210129124143_create_emails.rb create mode 100644 test/fixtures/emails.yml diff --git a/app/controllers/emails_controller.rb b/app/controllers/emails_controller.rb index 060f905..dbe97e1 100644 --- a/app/controllers/emails_controller.rb +++ b/app/controllers/emails_controller.rb @@ -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. diff --git a/app/mailboxes/in_mailbox.rb b/app/mailboxes/in_mailbox.rb index 2afac15..48026c5 100644 --- a/app/mailboxes/in_mailbox.rb +++ b/app/mailboxes/in_mailbox.rb @@ -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 diff --git a/app/models/email.rb b/app/models/email.rb index 6e5bb56..7cf1aa1 100644 --- a/app/models/email.rb +++ b/app/models/email.rb @@ -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 diff --git a/db/migrate/20210129124143_create_emails.rb b/db/migrate/20210129124143_create_emails.rb new file mode 100644 index 0000000..f4bc8e1 --- /dev/null +++ b/db/migrate/20210129124143_create_emails.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 1735e16..4ce2f11 100644 --- a/db/schema.rb +++ b/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" diff --git a/test/controllers/emails_controller_test.rb b/test/controllers/emails_controller_test.rb index eee26da..f1f3604 100644 --- a/test/controllers/emails_controller_test.rb +++ b/test/controllers/emails_controller_test.rb @@ -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 diff --git a/test/fixtures/emails.yml b/test/fixtures/emails.yml new file mode 100644 index 0000000..e2c28ba --- /dev/null +++ b/test/fixtures/emails.yml @@ -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) %> diff --git a/test/models/email_test.rb b/test/models/email_test.rb index dc054e5..a07b515 100644 --- a/test/models/email_test.rb +++ b/test/models/email_test.rb @@ -1,4 +1,4 @@ -require 'test_helper' +require "test_helper" class EmailTest < ActiveSupport::TestCase # test "the truth" do