├── .gitignore ├── .scrutinizer.yml ├── .styleci.yml ├── .travis.yml ├── CHANGELOG.md ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── dist ├── css │ └── card.css └── js │ └── card.js ├── mix-manifest.json ├── package.json ├── phpunit.xml.dist ├── resources ├── js │ ├── card.js │ └── components │ │ ├── Base │ │ ├── PartitionMetric.vue │ │ ├── TrendMetric.vue │ │ └── ValueMetric.vue │ │ ├── FilterModal.vue │ │ ├── FilterablePartitionMetric.vue │ │ ├── FilterableTrendMetric.vue │ │ ├── FilterableValueMetric.vue │ │ └── modal.js └── sass │ └── card.scss ├── src ├── CardServiceProvider.php ├── Filterable.php ├── FilterablePartition.php ├── FilterableTrend.php └── FilterableValue.php ├── tests ├── FilterableTest.php └── TestCase.php ├── webpack.mix.js └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /vendor 3 | /node_modules 4 | package-lock.json 5 | composer.phar 6 | composer.lock 7 | phpunit.xml 8 | .phpunit.result.cache 9 | .DS_Store 10 | Thumbs.db 11 | -------------------------------------------------------------------------------- /.scrutinizer.yml: -------------------------------------------------------------------------------- 1 | filter: 2 | excluded_paths: [tests/*] 3 | 4 | checks: 5 | php: 6 | remove_extra_empty_lines: true 7 | remove_php_closing_tag: true 8 | remove_trailing_whitespace: true 9 | fix_use_statements: 10 | remove_unused: true 11 | preserve_multiple: false 12 | preserve_blanklines: true 13 | order_alphabetically: true 14 | fix_php_opening_tag: true 15 | fix_linefeed: true 16 | fix_line_ending: true 17 | fix_identation_4spaces: true 18 | fix_doc_comments: true 19 | 20 | -------------------------------------------------------------------------------- /.styleci.yml: -------------------------------------------------------------------------------- 1 | preset: laravel 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - 7.1 4 | - 7.2 5 | env: 6 | matrix: 7 | - COMPOSER_FLAGS="--prefer-lowest" 8 | - COMPOSER_FLAGS="" 9 | global: 10 | - secure: V6rUCouaD13VDQCkcCjsVrDsxMzww0jjBzKhCeXQT6W2Hx57Q4ahz4c0MYvr8R5GBCkg3ccpDoJFdlv6NpVxDcW4LrPUO5BTNI/R0NTiz3RfW1Oz8l0JYRz772So9Nv6592TImd8maJg/Bp+KisP+f5Z3vNPvo4o3ZUKFYZtbIVTu7BdUrR5jGJFbQpMmC5rNiDSwZV0hts9k+bqLi+YZo/EGhk5NmYHeWy9IncMuzlouMN7lJhA+iydI571FwBhRihF3JUo4ra9SNNwinTOhCfaAMJxk2z8sc9087a1MZBGK6ZobBi5WPjAp10rZGbq3QgzfHZqQrjoUdGvZZsOBZTz4CDAuhkCwpHqz31+vWrwm06J6AQbZqpxKGhQ+fNbYfNrl8GGSj472vQmzGt44ZOIxeo/fe2IzW0b9DVtTPBML9Yc6cFauVzZ/vF6CxCq6XthhtcjKhVBaKNq9u6KYl/sER+Xj1c9SIbxMLhpJqpYm3F5ivBvUdF8/3iZ0tlsxfgdYKn6V0qK0EqKuLYznY7J62Tx6a76uSiEaVN0fs4I1F+QY6N+qCn2NwRIx8Nn7uNfUZeSZQiDKGxWkLfQOYX1AfrkHlMFhA/0FLUIi4n9OwKPE+oBipirWd/MNrCntuqQOH0yskhQc3UxgOhdumTr/M2seNskY83hCkUaqic= 11 | - secure: hUClvnkXL2licWMEud00jBgg7NbtrODwzAttrvQ2/n4VBwghyCnfKkwcauQ+oXFLWjLt1vbmiLxnEz6RoILVgkxoaGltm/eQhQRoNXJn04ATomwa2xwcCBmmeYgt7pDPLfrNn501d9rHrCMZNGSmJOBxbceTg97MMKKa2Qvw9qSpir6n7spJuMSj7eVjqbSEdAHPvRADE+btKDM8KVFP7rLIogSZXYlWM8aCMJB4Yo90A631183fo0IJ50yglGA/krfZl0lvia1DWzncKG74vNg9XPWn0EA5F/hvu05v5d5ZG39FxyV4HAWqef+PxYQgHnqOyI1yPlC4xxasvDm9Er7Ojmor0bnvYANLpp+0MjaEk1rWBlqU3/YJVyzvE4sDCXYtod4OAI6Q2BBaA7SMQwQrHjk+1gQPfIcRA87Vd2hKYNwdBq5Vg1Py60G+VVgK8ck2FG5ZkkdMa7aYRctfb92pUmVObxwcL8KdhFe/M1/PhLwTmWm+9MEJMkMmCJkPGCxUCUGtqaK097h5BXdNDl3bGVEDYmjhH5BEqHwEzu3IOHMxsLn3u29IVodEJijldORG7hIFxBoB3tlOkhaYxO6b7rFGfCy0hVk5yurN8fC6/lzYUEc3nDjK5gg6XIGVLaVbw6+VYb5Yjw2kvBSD5FIFx3Kw0/eCRbIAmfZn9IU= 12 | install: 13 | - echo "{\"http-basic\":{\"nova.laravel.com\":{\"username\":\"${NOVA_USERNAME}\",\"password\":\"${NOVA_PASSWORD}\"}}}" > auth.json 14 | before_script: 15 | - travis_retry composer self-update 16 | - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source 17 | script: 18 | - vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover 19 | after_script: 20 | - php vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover 21 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `nova-filterable-cards` will be documented in this file 4 | 5 | ## 1.0.0 - 201X-XX-XX 6 | 7 | - initial release 8 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Beyond Code GmbH 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Nova Filterable Metric Cards 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/beyondcode/nova-filterable-cards.svg?style=flat-square)](https://packagist.org/packages/beyondcode/nova-filterable-cards) 4 | [![Total Downloads](https://img.shields.io/packagist/dt/beyondcode/nova-filterable-cards.svg?style=flat-square)](https://packagist.org/packages/beyondcode/nova-filterable-cards) 5 | [![Build Status](https://travis-ci.org/beyondcode/nova-filterable-cards.svg?branch=master)](https://travis-ci.org/beyondcode/nova-filterable-cards) 6 | 7 | Add custom filters to your Laravel Nova metrics. 8 | 9 | ![screenshot](https://beyondco.de/github/nova-filterable-cards/screenshot.png) 10 | ![screenshot](https://beyondco.de/github/nova-filterable-cards/screenshot1.png) 11 | 12 | ## Installation 13 | 14 | You can install the package in to a Laravel app that uses [Nova](https://nova.laravel.com) via composer: 15 | 16 | ```bash 17 | composer require beyondcode/nova-filterable-cards 18 | ``` 19 | 20 | ## Usage 21 | 22 | To add the filter ability to your Laravel Nova metric cards, you need to add one of the `Filterable` traits to your metrics. 23 | 24 | Depending on your metric type, these are the available traits: 25 | 26 | - `FilterableValue` 27 | - `FilterableTrend` 28 | - `FilterablePartition` 29 | 30 | For example, within your custom Nova value metric card: 31 | ```php 32 | // in your Nova value metric card class: 33 | import Beyondcode\FilterableCard\FilterableValue; 34 | 35 | use FilterableValue; 36 | 37 | ``` 38 | 39 | ## Defining Filters 40 | 41 | The available filters for your cards can be defined, by adding a new property called `$filters` to your metrics. 42 | This must be an array and contains the names of the available filters, as well as the properties for this specific filter. 43 | 44 | Example: 45 | 46 | ```php 47 | // in your filterable Nova metric card class: 48 | 49 | protected $filters = [ 50 | 'Firstname' => [ 51 | 'type' => 'text' 52 | ], 53 | 'Status' => [ 54 | 'type' => 'select', 55 | 'options' => [ 56 | 'all' => 'All', 57 | 'active' => 'Active', 58 | 'inactive' => 'Inactive' 59 | ], 60 | ] 61 | ]; 62 | ``` 63 | 64 | ### Defining Filters Using Define Methods 65 | 66 | Sometimes you might want to set the available filter options by using a database call, or load them from the configuration. 67 | To enable this, you can also define the filter options using a method with the following naming convention: `defineStudlyCaseFilterName`. 68 | 69 | So for example, if you want to add and define a custom filter called `User Status`, you can do it like this: 70 | 71 | ```php 72 | // in your filterable Nova metric card class: 73 | 74 | protected $filters = [ 75 | 'Firstname' => [ 76 | 'type' => 'text' 77 | ], 78 | 'User Status' 79 | ]; 80 | 81 | public function defineUserStatus() 82 | { 83 | return [ 84 | 'type' => 'select', 85 | 'options' => [ 86 | 'all' => 'All', 87 | 'active' => 'Active', 88 | 'inactive' => 'Inactive' 89 | ], 90 | ]; 91 | } 92 | 93 | ``` 94 | 95 | ### Available Filter Types 96 | 97 | The available filter types are: 98 | 99 | - `select` 100 | - `checkbox` 101 | - `text` 102 | - `email` 103 | - `url` 104 | - `number` 105 | 106 | And all other types that can be applied to HTML `` tags. 107 | 108 | ### Apply The Filter Logic 109 | 110 | To define in which way you want to filter your custom metric query, once a user filters it using the modal, you need to define custom filter methods. The naming convention is similar to defining custom filter options. It's `filterStudlyCaseFilterName`. 111 | 112 | This method receives a query builder object and the value of the filter input. 113 | You can add your own queries to the builder class and modify as you need. Just make sure that you return the query object. 114 | 115 | Example: 116 | 117 | ```php 118 | // in your filterable Nova metric card class: 119 | 120 | public function filterUserStatus($query, $status) 121 | { 122 | return $query->where('status', $status); 123 | } 124 | ``` 125 | 126 | ### Testing 127 | 128 | ``` bash 129 | composer test 130 | ``` 131 | 132 | ### Changelog 133 | 134 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 135 | 136 | ## Contributing 137 | 138 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 139 | 140 | ### Security 141 | 142 | If you discover any security related issues, please email marcel@beyondco.de instead of using the issue tracker. 143 | 144 | ## Credits 145 | 146 | - [Marcel Pociot](https://github.com/mpociot) 147 | - [All Contributors](../../contributors) 148 | 149 | ## License 150 | 151 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 152 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "beyondcode/nova-filterable-cards", 3 | "description": "Filterable metric cards for Laravel Nova.", 4 | "keywords": [ 5 | "laravel", 6 | "nova" 7 | ], 8 | "license": "MIT", 9 | "require": { 10 | "php": ">=7.1.0", 11 | "laravel/nova": "~1.0", 12 | "orchestra/testbench": "^3.7" 13 | }, 14 | "autoload": { 15 | "psr-4": { 16 | "Beyondcode\\FilterableCard\\": "src/" 17 | } 18 | }, 19 | "autoload-dev": { 20 | "psr-4": { 21 | "Beyondcode\\FilterableCard\\Tests\\": "tests/" 22 | } 23 | }, 24 | "extra": { 25 | "laravel": { 26 | "providers": [ 27 | "Beyondcode\\FilterableCard\\CardServiceProvider" 28 | ] 29 | } 30 | }, 31 | "repositories": [ 32 | { 33 | "type": "composer", 34 | "url": "https://nova.laravel.com" 35 | } 36 | ], 37 | "config": { 38 | "sort-packages": true 39 | }, 40 | "minimum-stability": "dev", 41 | "prefer-stable": true, 42 | "require-dev": { 43 | "mockery/mockery": "^1.1" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /dist/css/card.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/beyondcode/nova-filterable-cards/5d91877898147dc3224f1c1abaece63da6dddf50/dist/css/card.css -------------------------------------------------------------------------------- /mix-manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "/dist/js/card.js": "/dist/js/card.js", 3 | "/dist/css/card.css": "/dist/css/card.css" 4 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "scripts": { 4 | "dev": "npm run development", 5 | "development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js", 6 | "watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js", 7 | "watch-poll": "npm run watch -- --watch-poll", 8 | "hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js", 9 | "prod": "npm run production", 10 | "production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js" 11 | }, 12 | "devDependencies": { 13 | "cross-env": "^5.0.0", 14 | "laravel-mix": "^1.0" 15 | }, 16 | "dependencies": { 17 | "laravel-nova": "^1.0.2", 18 | "vue": "^2.5.0" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 14 | tests 15 | 16 | 17 | 18 | 19 | src/ 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /resources/js/card.js: -------------------------------------------------------------------------------- 1 | Nova.booting((Vue, router) => { 2 | Vue.component('filterable-modal', require('./components/FilterModal')); 3 | Vue.component('filterable-value-metric', require('./components/FilterableValueMetric')); 4 | Vue.component('filterable-trend-metric', require('./components/FilterableTrendMetric')); 5 | Vue.component('filterable-partition-metric', require('./components/FilterablePartitionMetric')); 6 | }) 7 | -------------------------------------------------------------------------------- /resources/js/components/Base/PartitionMetric.vue: -------------------------------------------------------------------------------- 1 | 45 | 46 | 59 | -------------------------------------------------------------------------------- /resources/js/components/Base/TrendMetric.vue: -------------------------------------------------------------------------------- 1 | 41 | 42 | 52 | -------------------------------------------------------------------------------- /resources/js/components/Base/ValueMetric.vue: -------------------------------------------------------------------------------- 1 | 66 | 67 | 77 | -------------------------------------------------------------------------------- /resources/js/components/FilterModal.vue: -------------------------------------------------------------------------------- 1 | 72 | -------------------------------------------------------------------------------- /resources/js/components/FilterablePartitionMetric.vue: -------------------------------------------------------------------------------- 1 | 10 | 11 | 68 | -------------------------------------------------------------------------------- /resources/js/components/FilterableTrendMetric.vue: -------------------------------------------------------------------------------- 1 | 16 | 17 | 97 | -------------------------------------------------------------------------------- /resources/js/components/FilterableValueMetric.vue: -------------------------------------------------------------------------------- 1 | 16 | 17 | 81 | -------------------------------------------------------------------------------- /resources/js/components/modal.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | props: [ 3 | 'filters' 4 | ], 5 | 6 | data() { 7 | return { 8 | modalOpen: false, 9 | }; 10 | }, 11 | 12 | methods: { 13 | openModal() { 14 | this.modalOpen = true; 15 | }, 16 | 17 | closeModal() { 18 | this.modalOpen = false; 19 | }, 20 | 21 | filtered(payload) { 22 | this.$emit('filtered', payload); 23 | }, 24 | 25 | selected(payload) { 26 | this.$emit('selected', payload) 27 | }, 28 | } 29 | } -------------------------------------------------------------------------------- /resources/sass/card.scss: -------------------------------------------------------------------------------- 1 | // Nova Card CSS 2 | -------------------------------------------------------------------------------- /src/CardServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->booted(function () { 20 | $this->routes(); 21 | }); 22 | 23 | Nova::serving(function (ServingNova $event) { 24 | Nova::script('filterable-card', __DIR__.'/../dist/js/card.js'); 25 | Nova::style('filterable-card', __DIR__.'/../dist/css/card.css'); 26 | }); 27 | } 28 | 29 | /** 30 | * Register the card's routes. 31 | * 32 | * @return void 33 | */ 34 | protected function routes() 35 | { 36 | 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/Filterable.php: -------------------------------------------------------------------------------- 1 | component; 12 | } 13 | 14 | /** 15 | * @param \Illuminate\Http\Request $request 16 | * @param \Illuminate\Database\Eloquent\Builder|string $model 17 | * @return Builder 18 | */ 19 | public function applyFiltersToModel($request, $model) 20 | { 21 | $model = $model instanceof Builder ? $model : (new $model)->newQuery(); 22 | 23 | $filters = collect($request->all())->filter(function($value, $key) { 24 | return starts_with($key, 'filter') && method_exists(self::class, $key); 25 | }); 26 | foreach ($filters as $method => $value) { 27 | $model = $this->$method($model, $value); 28 | } 29 | 30 | return $model; 31 | } 32 | 33 | public function filters(array $filters) 34 | { 35 | $this->filters = array_merge($this->filters, $filters); 36 | 37 | $this->withMeta([ 38 | 'filters' => array_keys($filters) 39 | ]); 40 | } 41 | 42 | /** 43 | * Prepare the metric for JSON serialization. 44 | * 45 | * @return array 46 | */ 47 | public function jsonSerialize() 48 | { 49 | return array_merge(parent::jsonSerialize(), [ 50 | 'filters' => collect($this->filters ?? [])->map(function ($definition, $filter) { 51 | if (is_string($filter)) { 52 | $label = $filter; 53 | $studly = studly_case($filter); 54 | } else { 55 | $label = $definition; 56 | $studly = studly_case($definition); 57 | } 58 | 59 | if (method_exists($this, 'define'.$studly)) { 60 | $definition = $this->{'define'.$studly}(); 61 | } 62 | 63 | return [ 64 | 'label' => $label, 65 | 'value' => 'filter'.$studly, 66 | 'definition' => $definition, 67 | ]; 68 | })->values()->all(), 69 | ]); 70 | } 71 | 72 | } -------------------------------------------------------------------------------- /src/FilterablePartition.php: -------------------------------------------------------------------------------- 1 | applyFiltersToModel($request, $model); 22 | 23 | return parent::aggregate($request, $model, $function, $column, $groupBy); 24 | } 25 | } -------------------------------------------------------------------------------- /src/FilterableTrend.php: -------------------------------------------------------------------------------- 1 | applyFiltersToModel($request, $model); 23 | 24 | return parent::aggregate($request, $model, $unit, $function, $column, $dateColumn); 25 | } 26 | } -------------------------------------------------------------------------------- /src/FilterableValue.php: -------------------------------------------------------------------------------- 1 | applyFiltersToModel($request, $model); 21 | 22 | return parent::aggregate($request, $model, $function, $column); 23 | } 24 | } -------------------------------------------------------------------------------- /tests/FilterableTest.php: -------------------------------------------------------------------------------- 1 | calculate($request); 26 | 27 | $this->assertSame(2.0, $count->value); 28 | 29 | $request = Request::create('', 'GET', [ 30 | 'filterName' => 'Marcel', 31 | ]); 32 | 33 | $count = $filter->calculate($request); 34 | 35 | $this->assertSame(1.0, $count->value); 36 | } 37 | 38 | /** @test */ 39 | public function can_filter_trend_metrics() 40 | { 41 | $filter = new FilterTrendMetric(); 42 | 43 | $request = Request::create('', 'GET', ['range' => '30']); 44 | 45 | $count = $filter->calculate($request); 46 | 47 | $this->assertSame(2.0, collect($count->trend)->sum()); 48 | 49 | $request = Request::create('', 'GET', [ 50 | 'range' => '30', 51 | 'filterName' => 'Marcel', 52 | ]); 53 | 54 | $count = $filter->calculate($request); 55 | 56 | $this->assertSame(1.0, collect($count->trend)->sum()); 57 | } 58 | 59 | /** @test */ 60 | public function can_filter_partition_metrics() 61 | { 62 | $filter = new FilterPartitionMetric(); 63 | 64 | $request = Request::create('', 'GET', ['range' => '30']); 65 | 66 | $count = $filter->calculate($request); 67 | 68 | $this->assertSame(2, count($count->value)); 69 | 70 | $request = Request::create('', 'GET', [ 71 | 'range' => '30', 72 | 'filterName' => 'Marcel', 73 | ]); 74 | 75 | $count = $filter->calculate($request); 76 | 77 | $this->assertSame(1, count($count->value)); 78 | } 79 | 80 | } 81 | 82 | 83 | class FilterValueMetric extends Value 84 | { 85 | use FilterableValue; 86 | 87 | protected $filters = [ 88 | 'Name' => [ 89 | 'type' => 'text' 90 | ] 91 | ]; 92 | 93 | public function calculate(Request $request) 94 | { 95 | return $this->count($request, User::class); 96 | } 97 | 98 | public function filterName($query, $value) 99 | { 100 | return $query->where('name', $value); 101 | } 102 | } 103 | 104 | class FilterTrendMetric extends Trend 105 | { 106 | use FilterableTrend; 107 | 108 | protected $filters = [ 109 | 'Name' => [ 110 | 'type' => 'text' 111 | ] 112 | ]; 113 | 114 | public function calculate(Request $request) 115 | { 116 | return $this->countByDays($request, User::class); 117 | } 118 | 119 | public function filterName($query, $value) 120 | { 121 | return $query->where('name', $value); 122 | } 123 | } 124 | 125 | class FilterPartitionMetric extends Partition 126 | { 127 | use FilterablePartition; 128 | 129 | protected $filters = [ 130 | 'Name' => [ 131 | 'type' => 'text' 132 | ] 133 | ]; 134 | 135 | public function calculate(Request $request) 136 | { 137 | return $this->count($request, User::class, 'email'); 138 | } 139 | 140 | public function filterName($query, $value) 141 | { 142 | return $query->where('name', $value); 143 | } 144 | } -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 | loadLaravelMigrations(['--database' => 'testbench']); 19 | 20 | $this->createUsers(); 21 | } 22 | 23 | protected function getPackageProviders($app) 24 | { 25 | return [ 26 | CardServiceProvider::class, 27 | ]; 28 | } 29 | 30 | /** 31 | * Define environment setup. 32 | * 33 | * @param \Illuminate\Foundation\Application $app 34 | * @return void 35 | */ 36 | protected function getEnvironmentSetUp($app) 37 | { 38 | // Setup default database to use sqlite :memory: 39 | $app['config']->set('database.default', 'testbench'); 40 | $app['config']->set('database.connections.testbench', [ 41 | 'driver' => 'sqlite', 42 | 'database' => ':memory:', 43 | 'prefix' => '', 44 | ]); 45 | } 46 | 47 | protected function createUsers() 48 | { 49 | User::forceCreate([ 50 | 'name' => 'Marcel', 51 | 'email' => 'marcel@beyondco.de', 52 | 'password' => 'test' 53 | ]); 54 | User::forceCreate([ 55 | 'name' => 'Sebastian', 56 | 'email' => 'sebastian@beyondco.de', 57 | 'password' => 'test' 58 | ]); 59 | } 60 | } -------------------------------------------------------------------------------- /webpack.mix.js: -------------------------------------------------------------------------------- 1 | let mix = require('laravel-mix') 2 | 3 | mix.js('resources/js/card.js', 'dist/js') 4 | .sass('resources/sass/card.scss', 'dist/css') 5 | .webpackConfig({ 6 | resolve: { 7 | symlinks: false, 8 | alias: { 9 | '@': path.resolve(__dirname, 'vendor/laravel/nova/resources/js/'), 10 | }, 11 | }, 12 | }) 13 | --------------------------------------------------------------------------------