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

Added opportunity to apply several scopes in relation form element #2688

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions public/css/orchid.css

Large diffs are not rendered by default.

14 changes: 9 additions & 5 deletions public/css/orchid.rtl.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/css/orchid.rtl.css.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions public/js/orchid.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions public/mix-manifest.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions resources/js/controllers/relation_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class extends ApplicationController {
const model = this.data.get('model');
const name = this.data.get('name');
const key = this.data.get('key');
const scope = this.data.get('scope');
const scopes = this.data.get('scopes');
const append = this.data.get('append');
const searchColumns = this.data.get('search-columns');
const chunk = this.data.get('chunk');
Expand All @@ -59,7 +59,7 @@ export default class extends ApplicationController {
model,
name,
key,
scope,
scopes: JSON.parse(scopes),
append,
searchColumns,
chunk,
Expand Down
2 changes: 1 addition & 1 deletion resources/views/fields/relation.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
data-relation-model="{{ $relationModel }}"
data-relation-name="{{ $relationName }}"
data-relation-key="{{ $relationKey }}"
data-relation-scope="{{ $relationScope }}"
data-relation-scopes="{{ json_encode($relationScopes) ?? '' }}"
data-relation-search-columns="{{ $relationSearchColumns }}"
data-relation-append="{{ $relationAppend }}"
data-relation-chunk="{{ $chunk }}"
Expand Down
26 changes: 17 additions & 9 deletions src/Platform/Http/Controllers/RelationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function view(RelationRequest $request)
'model' => $model,
'name' => $name,
'key' => $key,
'scope' => $scope,
'scopes' => $scopes,
'append' => $append,
'searchColumns' => $searchColumns,
] = collect($request->all())
Expand All @@ -32,10 +32,16 @@ public function view(RelationRequest $request)
return null;
}

if ($key === 'scope' || $key === 'searchColumns') {
if ($key === 'searchColumns') {
return Crypt::decrypt($item);
}

if ($key === 'scopes') {
return array_map(function ($scope) {
return Crypt::decrypt($scope);
}, $item);
}

return Crypt::decryptString($item);
});

Expand All @@ -44,7 +50,7 @@ public function view(RelationRequest $request)
$model = new $model;
$search = $request->get('search', '');

$items = $this->buildersItems($model, $name, $key, $search, $scope, $append, $searchColumns, (int) $request->get('chunk', 10));
$items = $this->buildersItems($model, $name, $key, $search, $scopes, $append, $searchColumns, (int) $request->get('chunk', 10));

return response()->json($items);
}
Expand All @@ -53,18 +59,20 @@ public function view(RelationRequest $request)
* @return mixed
*/
private function buildersItems(
Model $model,
Model $model,
string $name,
string $key,
string $search = null,
?array $scope = [],
?array $scopes = [],
string $append = null,
array $searchColumns = null,
?int $chunk = 10
array $searchColumns = null,
?int $chunk = 10
) {
if ($scope !== null) {
if ($scopes !== null) {
/** @var Collection|array $model */
$model = $model->{$scope['name']}(...$scope['parameters']);
foreach ($scopes as $scope) {
$model = $model->{$scope['name']}(...$scope['parameters']);
}
}

if (is_array($model)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Platform/Http/Requests/RelationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function rules()
'model' => 'string|required',
'key' => 'string|required',
'name' => 'string|required',
'scope' => 'string|nullable',
'scopes' => 'array|nullable',
'append' => 'nullable',
'searchColumns' => 'string|nullable',
];
Expand Down
12 changes: 12 additions & 0 deletions src/Screen/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,18 @@ public function set(string $key, $value = true): self
return $this;
}

/**
* @param mixed $value
*
* @return static
*/
public function add(string $key, $value = true): self
{
$this->attributes[$key][] = $value;

return $this;
}

/**
* @throws Throwable
*
Expand Down
15 changes: 9 additions & 6 deletions src/Screen/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Relation extends Field
protected $attributes = [
'class' => 'form-control',
'value' => [],
'relationScope' => null,
'relationScopes' => null,
'relationAppend' => null,
'relationSearchColumns' => null,
'chunk' => 10,
Expand All @@ -61,7 +61,7 @@ class Relation extends Field
'relationModel',
'relationName',
'relationKey',
'relationScope',
'relationScopes',
'relationAppend',
'relationSearchColumns',
];
Expand Down Expand Up @@ -139,7 +139,7 @@ public function fromClass(string $class, string $name, string $key = 'id'): self
return $this->set('value', $value);
}

$scope = $this->get('scope', 'handler');
$scopes = $this->get('scopes', 'handler');
$class = resolve($class);

if (! is_iterable($value)) {
Expand All @@ -150,7 +150,10 @@ public function fromClass(string $class, string $name, string $key = 'id'): self
$class->value = $value;
}

$class = $class->{$scope['name']}(...$scope['parameters']);
foreach ($scopes as $scope) {
$class = $class->{$scope['name']}(...$scope['parameters']);
}


$item = collect($class)
->whereIn($key, $value)
Expand Down Expand Up @@ -179,8 +182,8 @@ public function applyScope(string $scope, ...$parameters): self
'name' => lcfirst($scope),
'parameters' => $parameters,
];
$this->set('scope', $data);
$this->set('relationScope', Crypt::encrypt($data));
$this->add('scopes', $data);
$this->add('relationScopes', Crypt::encrypt($data));

return $this;
}
Expand Down
10 changes: 6 additions & 4 deletions tests/Feature/Platform/RelationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private function getScope(array $scope = null, string $append = null): TestRespo
];

if ($scope !== null) {
$params['scope'] = Crypt::encrypt($scope);
$params['scopes'] = [Crypt::encrypt($scope)];
}

if ($append !== null) {
Expand All @@ -115,7 +115,7 @@ public function testSearchColumns()
'name' => Crypt::encryptString('name'),
'key' => Crypt::encryptString('id'),
'searchColumns' => Crypt::encrypt(['email']),
'scope' => null,
'scopes' => null,
'append' => Crypt::encryptString('full'),
];

Expand All @@ -137,10 +137,12 @@ public function testSearchColumnsWithScopes()
'name' => Crypt::encryptString('email'),
'key' => Crypt::encryptString('id'),
'searchColumns' => Crypt::encrypt(['id']),
'scope' => Crypt::encrypt([
'scopes' => [
Crypt::encrypt([
'name' => 'asBuilder',
'parameters' => [],
]),
])
],
'append' => Crypt::encryptString('full'),
];

Expand Down
8 changes: 4 additions & 4 deletions tests/Unit/Screen/Fields/RelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,18 @@ public function testNotScope(): void

$view = self::renderField($select);

$this->assertStringContainsString('data-relation-scope=""', $view);
$this->assertStringContainsString('data-relation-scopes=""', $view);
}

public function testScopeWithAttributes(): void
public function testScopesWithAttributes(): void
{
$select = Relation::make('users')
->fromModel(EmptyUserModel::class, 'name')
->applyScope('exampleScope', 1, 'string', ['foo', 'bar']);

$view = self::renderField($select);

$crypt = Str::between($view, 'data-relation-scope="', '=="');
$crypt = Str::between($view, 'data-relation-scopes="', '=="');

$this->assertEquals([
'name' => lcfirst('exampleScope'),
Expand All @@ -156,7 +156,7 @@ public function testScopeWithNotAttributes(): void

$view = self::renderField($select);

$crypt = Str::between($view, 'data-relation-scope="', '=="');
$crypt = Str::between($view, 'data-relation-scopes="', '=="');

$this->assertEquals([
'name' => lcfirst('exampleScope'),
Expand Down