Skip to content

Commit 83d6aad

Browse files
authored
Merge pull request #11 from OnrampLab/10-custom-fields-conflict-with-standard-columns
Fix custom fields conflict with standard columns
2 parents 34bf42a + ea27ed2 commit 83d6aad

File tree

4 files changed

+25
-16
lines changed

4 files changed

+25
-16
lines changed

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,18 @@ class Lead extends Model
148148
After setting up the above configurations, you can proceed with the following steps:
149149

150150
1. Define custom fields using the `CustomField` model.
151-
2. Create leads as usual, but with additional custom fields as you defined in the first step.
151+
2. Create leads as you normally would, but include additional custom fields under the custom key, as defined in the previous step.
152+
```php
153+
[
154+
'phone' => '12345678',
155+
'custom' => [
156+
'zip' => '123'
157+
]
158+
]
159+
160+
```
152161
3. If the custom field value passes validation, it will be stored in the `CustomFieldValue` table.
153-
4. Retrieve custom field values just like any other attribute of the Lead model. For example, you can access a custom field value using `$lead->customField`.
162+
4. Retrieve custom field values just like any other attribute of the Lead model but with the prefix `custom`. For example, you can access a custom field value using `$lead->custom_zip`.
154163

155164
By following these steps, you can seamlessly work with custom fields for the Lead model and access their values as if they were regular attributes.
156165

src/Concerns/Customizable.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ public function validateCustomFields(): void
4646
if ($customFields->isEmpty()) {
4747
return;
4848
}
49-
$modelAttributeKeys = $modelAttributes->keys();
50-
$customFieldColumns = $modelAttributeKeys->diff($tableColumns);
49+
$unvalidatedCustomFields = $modelAttributes->get('custom');
50+
if (empty($unvalidatedCustomFields)) {
51+
return;
52+
}
5153
$customFieldsRules = $customFields->flatMap(function (CustomField $field) {
5254
return $field->getValidationRule();
5355
})->all();
54-
$customFieldValues = $modelAttributes->only($customFieldColumns)->toArray();
55-
$validator = Validator::make($customFieldValues, $customFieldsRules);
56+
$validator = Validator::make($unvalidatedCustomFields, $customFieldsRules);
5657
$this->validatedCustomFieldValues = $validator->validate();
5758
}
5859

@@ -115,12 +116,12 @@ public function saveCustomFieldValues(): void
115116
*/
116117
public function loadCustomFieldValues(): void
117118
{
118-
if (! $this->customFieldValues) {
119+
if ($this->customFieldValues->isEmpty()) {
119120
return;
120121
}
121122

122123
$this->customFieldValues->each(function (CustomFieldValue $customFieldValue) {
123-
$attribute = $customFieldValue->customField->key;
124+
$attribute = "custom_{$customFieldValue->customField->key}";
124125
$this->setAttribute($attribute, $customFieldValue->customField->parseValue($customFieldValue->value));
125126
});
126127
}

tests/Feature/Concerns/CustomizableTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected function setUp(): void
2323
'contextable_type' => get_class($this->account)
2424
];
2525
$this->customField = CustomField::factory()->create($attributes);
26-
$this->user = User::factory()->create(['account_id' => $this->account->id, 'zip_code' => '12345']);
26+
$this->user = User::factory()->create(['account_id' => $this->account->id, 'custom' => ['zip_code' => '12345']]);
2727
}
2828

2929
/**
@@ -43,7 +43,7 @@ public function create_model_should_save_custom_field_value(): void
4343
*/
4444
public function update_model_should_update_custom_field_values(): void
4545
{
46-
$attributes = ['zip_code' => '67890'];
46+
$attributes = ['custom' => ['zip_code' => '67890']];
4747
$this->user->fill($attributes);
4848
$this->user->save();
4949
$customFieldValue = $this->user->customFieldValues()->where('custom_field_id', $this->customField->id)->first();
@@ -61,6 +61,6 @@ public function create_user_with_invalid_custom_field_value_should_throw_excepti
6161
{
6262
$this->expectException(ValidationException::class);
6363
$this->expectExceptionMessage('The given data was invalid.');
64-
User::factory()->create(['account_id' => $this->account->id, 'zip_code' => 123]);
64+
User::factory()->create(['account_id' => $this->account->id, 'custom' => ['zip_code' => 123]]);
6565
}
6666
}

tests/Unit/Concerns/CustomizableTest.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ protected function setUp(): void
2323
'contextable_type' => $this->account->getMorphClass()
2424
];
2525
$this->customField = CustomField::factory()->create($attributes);
26-
$this->user = User::factory()->create(['account_id' => $this->account->id, 'zip_code' => '12345']);
26+
$this->user = User::factory()->create(['account_id' => $this->account->id, 'custom' => ['zip_code' => '12345']]);
2727
}
2828

2929
/**
@@ -43,8 +43,7 @@ public function custom_field_values_relationship_should_work(): void
4343
public function validate_custom_fields_should_work()
4444
{
4545
$this->expectException(ValidationException::class);
46-
$this->user->zip_code = 123;
47-
$this->user->validateCustomFields();
46+
$user = User::factory()->create(['account_id' => $this->account->id, 'custom' => ['zip_code' => 123]]);
4847
}
4948

5049
/**
@@ -73,9 +72,9 @@ public function custom_load_custom_field_values_should_work($type, $value, $expe
7372
];
7473
}
7574
$customField = CustomField::factory()->create($attributes);
76-
$user = User::factory()->create(['account_id' => $this->account->id, 'field' => $value]);
75+
$user = User::factory()->create(['account_id' => $this->account->id, 'custom' => ['field' => $value]]);
7776
$user->loadCustomFieldValues();
78-
$this->assertEquals($expected, $user->field);
77+
$this->assertEquals($expected, $user->custom_field);
7978
}
8079

8180
public function customFieldDataProvider(): array

0 commit comments

Comments
 (0)