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

Attach query behavior in NgRestModel::find #530

Merged
merged 39 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
2054625
Block Preview styling
boehsermoe Nov 9, 2018
fcd0e42
Tooltip preview fix #246
boehsermoe Nov 13, 2018
715cab9
Merge branch 'master' of https://github.com/luyadev/luya-module-admin
boehsermoe Nov 22, 2018
572faf5
Merge branch 'master' of https://github.com/luyadev/luya-module-admin
boehsermoe Nov 26, 2018
082c9f6
Merge branch 'master' of github.com:boehsermoe/luya-module-admin
boehsermoe Jan 28, 2019
f775595
Merge branch 'master' of github.com:boehsermoe/luya-module-admin
boehsermoe Mar 4, 2019
d66099e
Date plugin with custom format
boehsermoe Mar 4, 2019
5aeeea8
Changelog for #270
boehsermoe Mar 5, 2019
13c52e2
Refactored namespace using
boehsermoe Mar 5, 2019
5d45200
Merge branch 'master' of https://github.com/luyadev/luya-module-admin
boehsermoe Mar 5, 2019
d1f211e
Merge branch 'master' of https://github.com/luyadev/luya-module-admin
boehsermoe Apr 10, 2019
e358e9a
Merge branch 'master' of https://github.com/luyadev/luya-module-admin
boehsermoe Apr 12, 2019
f1b737a
Connection lost handling
boehsermoe Apr 12, 2019
e1b77b0
Unit test fixed close #229
boehsermoe Apr 12, 2019
2899e60
CONNECTION_ID only available in mysql
boehsermoe May 11, 2019
029057e
Merge branch 'master' of https://github.com/luyadev/luya-module-admin
boehsermoe May 15, 2019
730eb32
Merge branch 'master' of github.com:boehsermoe/luya-module-admin
boehsermoe May 15, 2019
93fae42
Merge branch 'master' of https://github.com/luyadev/luya-module-admin
boehsermoe May 30, 2019
d93438d
Block Preview styling
boehsermoe Nov 9, 2018
2cddd5e
Tooltip preview fix #246
boehsermoe Nov 13, 2018
24536df
Merge branch 'master' of github.com:boehsermoe/luya-module-admin
boehsermoe Jul 14, 2019
1a79184
Merge branch 'master' of https://github.com/luyadev/luya-module-admin
boehsermoe Jul 27, 2019
c58df1e
Merge branch 'master' of https://github.com/luyadev/luya-module-admin
boehsermoe Aug 14, 2019
d70439b
Merge branch 'master' of https://github.com/luyadev/luya-module-admin
boehsermoe Aug 22, 2019
cbcfe66
Merge branch 'master' of https://github.com/luyadev/luya-module-admin
boehsermoe Dec 4, 2019
3b1c0a4
travis tests with php 7.4
boehsermoe Dec 4, 2019
7ed3a6e
clean up dist
boehsermoe Dec 4, 2019
af350e9
Merge branch 'master' of https://github.com/luyadev/luya-module-admin
boehsermoe Dec 4, 2019
76543c7
Merge branch 'master' of https://github.com/luyadev/luya-module-admin
boehsermoe Jan 26, 2020
cc5d2d0
Merge branch 'master' of https://github.com/luyadev/luya-module-admin
boehsermoe Apr 3, 2020
acb9e16
Merge branch 'master' of https://github.com/luyadev/luya-module-admin
boehsermoe Jul 17, 2020
09d01b9
attache find behaviours
boehsermoe Jul 17, 2020
eba5277
Changelogs
boehsermoe Jul 17, 2020
fa35fdd
Unit tests
boehsermoe Jul 17, 2020
1561d15
phpdocs & changelog
boehsermoe Jul 18, 2020
a5efffb
Update NgRestModel.php
nadar Jul 19, 2020
3f99a4d
Update NgRestModel.php
nadar Jul 19, 2020
9e5f342
Update NgRestModel.php
boehsermoe Jul 19, 2020
0e07e2f
Update TestNgRestModel.php
boehsermoe Jul 19, 2020
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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).
In order to read more about upgrading and BC breaks have a look at the [UPGRADE Document](UPGRADE.md).

## 3.3.3
## 3.4.0

### Added

+ [#530](https://github.com/luyadev/luya-module-admin/pull/530) Attach query behaviors in `luya\admin\ngrest\base\NgRestModel::find`.

### Fixed

+ [#529](https://github.com/luyadev/luya-module-admin/pull/529) Fixed an issue with OpenApi path params.
+ [#527](https://github.com/luyadev/luya-module-admin/issues/527) Fixed a bug where deleted user emails where not validated when save or update an existing user account.
Expand Down
56 changes: 55 additions & 1 deletion src/ngrest/base/NgRestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,14 +109,68 @@ public function extraFields()
return array_merge(parent::extraFields(), $this->extractRootFields($extraFieldKeys));
}

/**
* Attach behaviors to the Active Query.
*
* Attach behaviours to every new {{\luya\admin\ngrest\base\NgRestActiveQuery}} on find() and ngRestFind().
* Returns a list of behaviors that the query component should behave as.
*
* As behavior methods can be access from the inner class, use full functions can be used inside the active query.
* It also enables the option to share standardized behaviors with functions (conditions), for example a soft delete condition.
*
* A behavior example
*
* ```php
* class MySuperBehavioir extends yii\base\Behavior
* {
* public function active($isActive = true)
* {
* return $this->andWhere(['is_active' => $isActive]);
* }
* }
* ```
*
* After attaching this behavior, it can be used like `MyModel::find()->active()->one()`.
*
* > Whenever possible, directly create a custom Active Query, as it provices full IDE support. The behavior
* > does not, the example above will even show an IDE error because the mmethod `andWhere()` does not exsist
* > in the yii\base\Behavior class.
*
* The return value of this method should be an array of behavior objects or configurations
* indexed by behavior names. A behavior configuration can be either a string specifying
* the behavior class or an array of the following structure:
*
* ```php
* 'behaviorName' => [
* 'class' => 'BehaviorClass',
* 'property1' => 'value1',
* 'property2' => 'value2',
* ]
* ```
* @see {{\yii\base\Component::behaviors}}
*
boehsermoe marked this conversation as resolved.
Show resolved Hide resolved
* @return array
* @since 3.4.0
*/
public static function findActiveQueryBehaviors()
{
return [];
}

/**
* {@inheritdoc}
*
* @return NgRestActiveQuery
*/
public static function find()
{
return new NgRestActiveQuery(get_called_class());
$config = [];

foreach (static::findActiveQueryBehaviors() as $name => $class) {
$config['as ' . $name] = $class;
}

return new NgRestActiveQuery(get_called_class(), $config);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/admin/ngrest/base/NgRestModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ public function testBehaviorIsAttached()
$this->assertArrayHasKey('NgRestEventBehavior', $behaviors);
$this->assertArrayHasKey('LogBehavior', $behaviors);
}

public function testQueryBehaviorsAreAttached()
{
$query = TestNgRestModel::find();
$behaviors = $query->behaviors;

$this->assertArrayHasKey('DummyBehavior', $behaviors);
}

public function testGenericSearchFields()
{
Expand Down
8 changes: 8 additions & 0 deletions tests/data/models/TestNgRestModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace admintests\data\models;

use admintests\data\stubs\StubBehavior;
use luya\admin\ngrest\base\NgRestModel;
use luya\admin\aws\TaggableActiveWindow;

Expand All @@ -22,6 +23,13 @@ public static function ngRestApiEndpoint()
return 'foo-bar';
}

public static function findActiveQueryBehaviors()
{
return [
'DummyBehavior' => StubBehavior::class
];
}

public function rules()
{
return [
Expand Down
10 changes: 10 additions & 0 deletions tests/data/stubs/StubBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace admintests\data\stubs;

use yii\base\Behavior;

class StubBehavior extends Behavior
{

}