Skip to content

Commit 81518f0

Browse files
authored
Merge pull request #6 from OnrampLab/5-fix-get-custom-fields-query-bug
5 fix get custom fields query bug
2 parents fb50229 + b916671 commit 81518f0

File tree

4 files changed

+22
-56
lines changed

4 files changed

+22
-56
lines changed

README.md

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,61 +24,24 @@ Custom fields can be utilized to extend a model and offer a more flexible approa
2424

2525
## Installation
2626

27-
```bash
27+
Install the package via composer
28+
29+
```
2830
composer require onramplab/laravel-custom-fields
2931
```
32+
Publish migration files and run command to build tables needed in the package
3033

31-
This will create a basic project structure for you:
32-
33-
* **/build** is used to store code coverage output by default;
34-
* **/src** is where your codes will live in, each class will need to reside in its own file inside this folder;
35-
* **/tests** each class that you write in src folder needs to be tested before it was even "included" into somewhere else. So basically we have tests classes there to test other classes;
36-
* **.gitignore** there are certain files that we don't want to publish in Git, so we just add them to this fle for them to "get ignored by git";
37-
* **CHANGELOG.md** to keep track of package updates;
38-
* **CONTRIBUTION.md** Contributor Covenant Code of Conduct;
39-
* **LICENSE** terms of how much freedom other programmers is allowed to use this library;
40-
* **README.md** it is a mini documentation of the library, this is usually the "home page" of your repo if you published it on GitHub and Packagist;
41-
* **composer.json** is where the information about your library is stored, like package name, author and dependencies;
42-
* **phpunit.xml** It is a configuration file of PHPUnit, so that tests classes will be able to test the classes you've written;
43-
* **.travis.yml** basic configuration for Travis CI with configured test coverage reporting for code climate.
34+
```
35+
php artisan vendor:publish --tag="custom-fields-migrations"
36+
```
4437

45-
Please refer to original [article](http://www.darwinbiler.com/creating-composer-package-library/) for more information.
38+
## Usage
4639

47-
## Useful Tools
4840

4941
## Running Tests:
5042

51-
php vendor/bin/phpunit
52-
53-
or
54-
5543
composer test
5644

57-
## Code Sniffer Tool:
58-
59-
php vendor/bin/phpcs --standard=PSR2 src/
60-
61-
or
62-
63-
composer psr2check
64-
65-
## Code Auto-fixer:
66-
67-
composer psr2autofix
68-
composer insights:fix
69-
rector:fix
70-
71-
## Building Docs:
72-
73-
php vendor/bin/phpdoc -d "src" -t "docs"
74-
75-
or
76-
77-
composer docs
78-
79-
## Changelog
80-
81-
To keep track, please refer to [CHANGELOG.md](https://github.com/Onramplab/laravel-custom-fields/blob/master/CHANGELOG.md).
8245

8346
## Contributing
8447

src/Concerns/Customizable.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
trait Customizable
1818
{
19-
protected ?array $validatedCustomFieldValues = [];
19+
protected array $validatedCustomFieldValues = [];
2020

2121
public static function bootCustomizable(): void
2222
{
@@ -37,21 +37,20 @@ public function customFieldValues(): MorphMany
3737
public function validateCustomFields(): void
3838
{
3939
$customFields = $this->getCustomFields();
40+
$tableColumns = $this->getTableColumns();
41+
$modelAttributes = Collection::make($this->getAttributes());
42+
$this->setRawAttributes($modelAttributes->only($tableColumns)->toArray());
4043
if ($customFields->isEmpty()) {
4144
return;
4245
}
43-
$tableColumns = $this->getTableColumns();
44-
$modelAttributes = Collection::make($this->getAttributes());
4546
$modelAttributeKeys = $modelAttributes->keys();
4647
$customFieldColumns = $modelAttributeKeys->diff($tableColumns);
4748
$customFieldsRules = $customFields->flatMap(function (CustomField $field) {
4849
return $field->getValidationRule();
4950
})->all();
50-
5151
$customFieldValues = $modelAttributes->only($customFieldColumns)->toArray();
5252
$validator = Validator::make($customFieldValues, $customFieldsRules);
5353
$this->validatedCustomFieldValues = $validator->validate();
54-
$this->setRawAttributes($modelAttributes->only($tableColumns)->toArray());
5554
}
5655

5756
protected function getTableColumns(): Collection
@@ -61,21 +60,23 @@ protected function getTableColumns(): Collection
6160

6261
public function getCustomFields(): Collection
6362
{
64-
$context = $this->getContext();
63+
$context = $this->getCustomFieldContext();
6564
$query = CustomField::query();
65+
$query->where('model_class', get_class($this));
66+
6667
if (is_null($context)) {
6768
$customFields = $query->get();
6869
} else {
6970
$customFields = $query
70-
->where('contextable_type', get_class($context))
71+
->where('contextable_type', $context->getMorphClass())
7172
->where('contextable_id', $context->id)
7273
->get();
7374
}
7475

7576
return $customFields;
7677
}
7778

78-
public function getContext(): ?Model
79+
public function getCustomFieldContext(): ?Model
7980
{
8081
return null;
8182
}
@@ -92,7 +93,7 @@ public function saveCustomFieldValues(): void
9293
$value = $validatedCustomFieldValues[$customField->key];
9394
$constraints = [
9495
'custom_field_id' => $customField->id,
95-
'customizable_type' => get_class($this),
96+
'customizable_type' => $this->getMorphClass(),
9697
'customizable_id' => $this->id
9798
];
9899
$values = ['value' => $value];

src/Models/CustomField.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class CustomField extends Model
2020
public const TYPE_FLOAT = 'float';
2121
public const TYPE_DATETIME = 'datetime';
2222
public const TYPE_SELECT = 'select';
23+
public const TYPE_BOOLEAN = 'boolean';
2324

2425
/**
2526
* The attributes that are mass assignable.
@@ -49,7 +50,8 @@ class CustomField extends Model
4950
CustomField::TYPE_INTEGER => IntegerCustomField::class,
5051
CustomField::TYPE_FLOAT => FloatCustomField::class,
5152
CustomField::TYPE_DATETIME => DateTimeCustomField::class,
52-
CustomField::TYPE_SELECT => SelectCustomField::class
53+
CustomField::TYPE_SELECT => SelectCustomField::class,
54+
CustomField::TYPE_BOOLEAN => BooleanCustomField::class
5355
];
5456

5557
protected static function newFactory(): Factory

tests/Classes/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function account(): BelongsTo
2525
return $this->belongsTo(Account::class);
2626
}
2727

28-
public function getContext(): Model
28+
public function getCustomFieldContext(): Model
2929
{
3030
return $this->account;
3131
}

0 commit comments

Comments
 (0)