Skip to content

Commit

Permalink
Make package compatible with nova-attach-many package
Browse files Browse the repository at this point in the history
  • Loading branch information
milewski committed Nov 16, 2019
1 parent 7494441 commit 260a6e6
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

25 changes: 24 additions & 1 deletion resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
this.setBagValue(attribute, component[watchableAttribute])
component.$once('hook:beforeDestroy', () => this.deleteBagAttribute(attribute))
component.$watch(watchableAttribute, value => this.setBagValue(attribute, value))
component.$watch(watchableAttribute, value => this.setBagValue(attribute, this.parseComponentValue(component, value)))
}
Expand Down Expand Up @@ -232,6 +232,12 @@
}
if (!isNaN(conditionValue)) {
conditionValue = parseInt(conditionValue)
}
if (['true', 'false'].includes(conditionValue)) {
conditionValue = conditionValue === 'true'
Expand Down Expand Up @@ -276,6 +282,23 @@
},
parseComponentValue(component, value) {
switch (component.field.component) {
/**
* For some unknown reason this component stringify the value
* https://github.com/dillingham/nova-attach-many/blob/2d461048d3e85de54795f6c03ae0bdad3356df6b/resources/js/components/FormField.vue#L210
*/
case 'nova-attach-many':
return JSON.parse(value)
}
return value
},
findWatchableComponentAttribute(component) {
switch (component.field.component) {
Expand Down
84 changes: 81 additions & 3 deletions src/ConditionalContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace DigitalCreative\ConditionalContainer;

use App\Nova\Resource;
use Laravel\Nova\Resource;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -111,15 +111,31 @@ public function resolve($resource, $attribute = null)
public function fill(NovaRequest $request, $model)
{

$callbacks = [];

/**
* @var Field $field
*/
foreach ($this->fields as $field) {

$field->fill($request, $model);
$callbacks[] = $field->fill($request, $model);

}

return function () use ($callbacks) {

foreach ($callbacks as $callback) {

if (is_callable($callback)) {

call_user_func($callback);

}

}

};

}

public function useAndOperator(): self
Expand Down Expand Up @@ -154,6 +170,28 @@ private function executeCondition($attributeValue, string $operator, $conditionV

}

/**
* This is due that sometimes this function is call with data that comes
* directly from the request, therefore there may be occasion that the "model" casts
* may not be applied, if it doesnt cause any unforeseen issue it`s safe to keep this in here
*/
if (is_string($attributeValue)) {

$attributeValue = json_decode($attributeValue, true) ?? $attributeValue;

}

/**
* If $attributeValue is an array and $operator contains/includes
* assumes user is trying to match id of a belongsToMany relationship
*/
if (is_array($attributeValue) && in_array($operator, [ 'contains', 'includes' ])) {

$attributeValue = collect($attributeValue);
$conditionValue = (int) $conditionValue;

}

if (in_array($conditionValue, [ 'true', 'false' ])) {

$conditionValue = $conditionValue === 'true';
Expand Down Expand Up @@ -184,7 +222,18 @@ private function executeCondition($attributeValue, string $operator, $conditionV
return $conditionValue ? !!$attributeValue : !$attributeValue;
case 'includes':
case 'contains':

/**
* On the javascript side it uses ('' || []).includes() which works with array and string
*/
if ($attributeValue instanceof Collection) {

return $attributeValue->contains($conditionValue);

}

return Str::contains($attributeValue, $conditionValue);

case 'starts with':
case 'startsWith':
return Str::startsWith($attributeValue, $conditionValue);
Expand Down Expand Up @@ -282,12 +331,41 @@ public function resolveDependencyFieldUsingRequest($resource, NovaRequest $reque
public function resolveDependencyFieldUsingResource($resource): array
{

$matched = $this->runConditions(collect($resource->toArray()));
$matched = $this->runConditions(
$this->flattenRelationships($resource)
);

return $matched ? $this->fields->toArray() : [];

}

/**
* @param Model|Resource $resource
* @return Collection
*/
private function flattenRelationships($resource): Collection
{

$data = collect($resource->toArray());

foreach ($resource->getRelations() as $relationName => $relation) {

if ($relation instanceof Collection) {

$data->put($relationName, $relation->map->getKey());

} else if ($relation instanceof Model) {

$data->put($relationName, $relation->getKey());

}

}

return $data;

}

public function jsonSerialize()
{
return array_merge([
Expand Down

0 comments on commit 260a6e6

Please sign in to comment.