Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Email model changeable from config #36

Merged
merged 1 commit into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/filament-email.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

// config for RickDBCN/FilamentEmail
use RickDBCN\FilamentEmail\Filament\Resources\EmailResource;
use RickDBCN\FilamentEmail\Models\Email;

return [

'resource' => [
'class' => EmailResource::class,
'model' => Email::class,
'group' => null,
'sort' => null,
'default_sort_column' => 'created_at',
Expand Down
8 changes: 6 additions & 2 deletions database/factories/EmailFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@
namespace RickDBCN\FilamentEmail\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Facades\Config;
use RickDBCN\FilamentEmail\Models\Email;

class EmailFactory extends Factory
{
protected $model = Email::class;
public function modelName(): string
{
return Config::get('filament-email.resource.model') ?? Email::class;
}

public function definition()
public function definition(): array
{
return [
'from' => $this->faker->email(),
Expand Down
11 changes: 7 additions & 4 deletions src/Filament/Resources/EmailResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

class EmailResource extends Resource
{
protected static ?string $model = Email::class;

protected static ?string $navigationIcon = 'heroicon-o-envelope';

protected static ?string $slug = 'emails';
Expand All @@ -44,6 +42,11 @@ public static function getNavigationSort(): ?int
return Config::get('filament-email.resource.sort') ?? parent::getNavigationSort();
}

public static function getModel(): string
{
return Config::get('filament-email.resource.model') ?? Email::class;
}

public static function form(Form $form): Form
{
return $form
Expand Down Expand Up @@ -99,7 +102,7 @@ public static function table(Table $table): Table
fn ($action): array => [
$action->getModalCancelAction(),
])
->fillForm(function (Email $record) {
->fillForm(function ($record) {
$body = $record->html_body;

return [
Expand All @@ -113,7 +116,7 @@ public static function table(Table $table): Table
Action::make('resend')
->label(__('Send again'))
->icon('heroicon-o-envelope')
->action(function (Email $record) {
->action(function ($record) {
try {
Mail::to($record->to)
->cc($record->cc)
Expand Down
5 changes: 4 additions & 1 deletion src/Listeners/FilamentEmailLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace RickDBCN\FilamentEmail\Listeners;

use Illuminate\Support\Facades\Config;
use RickDBCN\FilamentEmail\Models\Email;

class FilamentEmailLogger
Expand All @@ -24,7 +25,9 @@ public function handle(object $event): void
$rawMessage = $event->sent->getSymfonySentMessage();
$email = $event->message;

Email::create([
$model = Config::get('filament-email.resource.model') ?? Email::class;

$model::create([
'from' => $this->recipientsToString($email->getFrom()),
'to' => $this->recipientsToString($email->getTo()),
'cc' => $this->recipientsToString($email->getCc()),
Expand Down
5 changes: 2 additions & 3 deletions src/Mail/ResendMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;
use RickDBCN\FilamentEmail\Models\Email;

class ResendMail extends Mailable
{
use Queueable, SerializesModels;

public Email $email;
public $email;

public function __construct(Email $email)
public function __construct($email)
{
$this->email = $email;
}
Expand Down
13 changes: 9 additions & 4 deletions tests/EmailModelTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Faker\Factory;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Mail;
use RickDBCN\FilamentEmail\Filament\Resources\EmailResource\Pages\ListEmails;
use RickDBCN\FilamentEmail\Models\Email;
Expand All @@ -10,6 +11,10 @@
use function Pest\Livewire\livewire;
use function PHPUnit\Framework\assertEquals;

beforeEach(function () {
$this->model = Config::get('filament-email.resource.model') ?? Email::class;
});

it('can create an Email model', function () {
$emailModel = Email::factory()->create();
assertModelExists($emailModel);
Expand All @@ -26,17 +31,17 @@

assertDatabaseCount('filament_email_log', 1);

assertEquals(Email::first()->to, $recipient);
assertEquals($this->model::first()->to, $recipient);
});

it('can render table page', function () {
Email::factory()->create();
$this->model::factory()->create();
livewire(ListEmails::class)->assertSuccessful();
});

it('can resend email', function () {
$email = Email::factory()->create();
$email = $this->model::factory()->create();
livewire(ListEmails::class)
->callTableAction('resend', $email);
assertDatabaseCount((new Email)->getTable(), 2);
assertDatabaseCount((new $this->model)->getTable(), 2);
});
Loading