• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

luttje / filament-user-attributes / 7254443569

18 Dec 2023 10:14PM UTC coverage: 73.72% (-0.2%) from 73.873%
7254443569

push

github

web-flow
Feature/custom config field support (#6)

* refactor so configs related to user attributes can be fetched + better livewire support

* let devs add custom config fields

* remove changelog workflow

43 of 67 new or added lines in 8 files covered. (64.18%)

1 existing line in 1 file now uncovered.

1296 of 1758 relevant lines covered (73.72%)

30.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

36.17
/src/Filament/Factories/BaseComponentFactory.php
1
<?php
2

3
namespace Luttje\FilamentUserAttributes\Filament\Factories;
4

5
use Closure;
6
use Filament\Forms\Components\Component;
7
use Filament\Forms\Components\Field;
8
use Filament\Forms\Get;
9
use Filament\Tables\Columns\Column;
10
use Illuminate\Database\Eloquent\Model;
11
use Luttje\FilamentUserAttributes\Contracts\HasUserAttributesContract;
12
use Luttje\FilamentUserAttributes\EloquentHelper;
13
use Luttje\FilamentUserAttributes\Filament\UserAttributeComponentFactoryInterface;
14

15
abstract class BaseComponentFactory implements UserAttributeComponentFactoryInterface
16
{
17
    public function __construct(
52✔
18
        protected string $modelType
19
    ) {
20
        //
21
    }
52✔
22

23
    private function makeInheritedDefault(array $userAttribute, mixed $default = null): Closure
×
24
    {
25
        return function (?Model $record, Get $get) use ($userAttribute, $default) {
×
26
            if ($userAttribute['inherit_relation'] === '__self') {
×
27
                $relatedField = $get($userAttribute['inherit_attribute']);
×
28

29
                if ($relatedField !== null) {
×
30
                    return $relatedField;
×
31
                }
32

33
                return data_get($record ?? [], $userAttribute['inherit_attribute']);
×
34
            }
35

36
            if ($record !== null) {
×
37
                $default = data_get($record, $userAttribute['inherit_relation'] . '.' . $userAttribute['inherit_attribute']);
×
38
            } else {
39
                // TODO: Should we support situations where a relation exists, but no form field is shown for it.
40
                // TODO: We currently only support relations that have a form field e.g: relation customer needs customer(_id) field
41
                $relatedField = $get($userAttribute['inherit_relation']);
×
42

43
                if (!$relatedField) {
×
44
                    $relatedField = $get($userAttribute['inherit_relation'] . '_id');
×
45
                }
46

47
                if ($relatedField !== null) {
×
NEW
48
                    $record = $this->modelType;
×
49
                    $inheritRelationInfo = EloquentHelper::getRelationInfo($record, $userAttribute['inherit_relation']);
×
50
                    $related = ($inheritRelationInfo->relatedType)::find($relatedField);
×
51
                    $default = data_get($related, $userAttribute['inherit_attribute']);
×
52
                }
53
            }
54

55
            return $default;
×
56
        };
×
57
    }
58

59
    protected function setUpColumn(Column $column, array $userAttribute): Column
12✔
60
    {
61
        $customizations = $userAttribute['customizations'] ?? [];
12✔
62
        $default = $customizations['default'] ?? false;
12✔
63

64
        if (isset($userAttribute['inherit']) && $userAttribute['inherit'] === true) {
12✔
65
            $default = $this->makeInheritedDefault($userAttribute, $default);
×
66
        }
67

68
        return $column
12✔
69
            ->label($userAttribute['label'])
12✔
70
            ->default($default);
12✔
71
    }
72

73
    public function setUpField(Field $field, array $userAttribute): Field
8✔
74
    {
75
        $default = $this->makeDefaultValue($userAttribute) ?? null;
8✔
76

77
        if (isset($userAttribute['inherit']) && $userAttribute['inherit'] === true) {
8✔
78
            $default = $this->makeInheritedDefault($userAttribute, $default);
×
79

80
            // Default won't work when user_attributes does not yet contain the value, therefor we also set it here
81
            $field->formatStateUsing(function (Component $component, ?Model $record, $state) use ($userAttribute, $default) {
×
82
                if ($record === null) {
×
83
                    return $state;
×
84
                }
85

86
                if (!in_array(HasUserAttributesContract::class, class_implements($record))) {
×
87
                    throw new \Exception('Record must implement HasUserAttributesContract');
×
88
                }
89

90
                /** @var HasUserAttributesContract $record */
91
                if (!$record->hasUserAttribute($userAttribute['name'])) {
×
92
                    // Force the default value
93
                    return $component->evaluate($default);
×
94
                }
95

96
                return $state;
×
97
            });
×
98
        }
99

100
        $field->required($userAttribute['required'] ?? false)
8✔
101
            ->statePath('user_attributes.' . $userAttribute['name'])
8✔
102
            ->label($userAttribute['label'])
8✔
103
            ->default($default);
8✔
104

105
        return $field;
8✔
106
    }
107
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc