Skip to content
Merged
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
5 changes: 5 additions & 0 deletions src/Abstracts/FloatingFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ abstract class FloatingFilter extends Visualizable
{
protected FloatingFilterType|string $floatingFilterType;

public function getFieldPrefix(): string
{
return 'floating_filter_';
}

/**
* @return array{field: string, header: string, type: string, meta: array<string, mixed>}
*/
Expand Down
6 changes: 4 additions & 2 deletions src/Abstracts/Visualizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ public function getFilterWithBindings(): array
return $this->filterWithBindings?->getBindings() ?? $this->selectWithBindings->getBindings();
}

abstract public function getFieldPrefix(): string;

public function getField(): string
{
return $this->field;
return $this->getFieldPrefix().$this->field;
}

protected function selectWith(string $selectWith): static
Expand Down Expand Up @@ -113,7 +115,7 @@ public function header(string $header): static

public function getHeader(): string
{
return $this->header ?? $this->getField();
return $this->header ?? $this->field;
}

/** @return array<string, mixed> */
Expand Down
5 changes: 5 additions & 0 deletions src/Charts/Abstracts/Dataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ abstract class Dataset extends Visualizable
{
use Macroable;

public function getFieldPrefix(): string
{
return 'dataset_';
}

/**
* The type of dataset, which tells the front-end how to render this series.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Charts/Labels/Label.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class Label extends Visualizable
{
use Macroable;

public function getFieldPrefix(): string
{
return 'label_';
}

/**
* Transform the label to a standardized specification for the front-end schema.
*
Expand Down
5 changes: 5 additions & 0 deletions src/DataGrids/Abstracts/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ abstract class Column extends Visualizable
{
use Macroable;

public function getFieldPrefix(): string
{
return 'column_';
}

/**
* Whether we want to hide the column in the grid. This is useful for columns that you may want exposed to the
* user, but not visible by default.
Expand Down
5 changes: 5 additions & 0 deletions src/Metrics/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class Value extends Visualizable
{
use Macroable;

public function getFieldPrefix(): string
{
return 'value_';
}

/** @return array<string, mixed> */
public function toArray(): array
{
Expand Down
6 changes: 3 additions & 3 deletions tests/Charts/Abstracts/ChartTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@
expect($schema)->toHaveKeys(['visualization_key', 'label', 'datasets', 'floating_filters']);

expect($schema['label'])->toBe([
'field' => 'date',
'field' => 'label_date',
'header' => 'date',
'meta' => [],
]);

expect($schema['datasets'])->toHaveCount(2);

expect($schema['datasets']->first())->toBe([
'field' => 'revenue',
'field' => 'dataset_revenue',
'header' => 'Revenue',
'type' => 'bar',
'meta' => [],
Expand All @@ -79,7 +79,7 @@
expect($schema['floating_filters'])->toHaveCount(1);

expect($schema['floating_filters']->first())->toBe([
'field' => 'date_range',
'field' => 'floating_filter_date_range',
'header' => 'Date Range',
'type' => 'date_range',
'meta' => [],
Expand Down
2 changes: 1 addition & 1 deletion tests/Charts/Datasets/AreaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
$dataset = Area::make('SUM(total)', 'revenue')->header('Revenue');

expect($dataset->toArray())->toBe([
'field' => 'revenue',
'field' => 'dataset_revenue',
'header' => 'Revenue',
'type' => 'area',
'meta' => [],
Expand Down
2 changes: 1 addition & 1 deletion tests/Charts/Datasets/BarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
$dataset = Bar::make('SUM(total)', 'revenue')->header('Revenue');

expect($dataset->toArray())->toBe([
'field' => 'revenue',
'field' => 'dataset_revenue',
'header' => 'Revenue',
'type' => 'bar',
'meta' => [],
Expand Down
2 changes: 1 addition & 1 deletion tests/Charts/Datasets/DoughnutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
$dataset = Doughnut::make('SUM(total)', 'revenue')->header('Revenue');

expect($dataset->toArray())->toBe([
'field' => 'revenue',
'field' => 'dataset_revenue',
'header' => 'Revenue',
'type' => 'doughnut',
'meta' => [],
Expand Down
2 changes: 1 addition & 1 deletion tests/Charts/Datasets/LineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
$dataset = Line::make('SUM(total)', 'revenue')->header('Revenue');

expect($dataset->toArray())->toBe([
'field' => 'revenue',
'field' => 'dataset_revenue',
'header' => 'Revenue',
'type' => 'line',
'meta' => [],
Expand Down
2 changes: 1 addition & 1 deletion tests/Charts/Datasets/PieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
$dataset = Pie::make('SUM(total)', 'revenue')->header('Revenue');

expect($dataset->toArray())->toBe([
'field' => 'revenue',
'field' => 'dataset_revenue',
'header' => 'Revenue',
'type' => 'pie',
'meta' => [],
Expand Down
2 changes: 1 addition & 1 deletion tests/Charts/Datasets/ScatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
$dataset = Scatter::make('SUM(total)', 'revenue')->header('Revenue');

expect($dataset->toArray())->toBe([
'field' => 'revenue',
'field' => 'dataset_revenue',
'header' => 'Revenue',
'type' => 'scatter',
'meta' => [],
Expand Down
2 changes: 1 addition & 1 deletion tests/Charts/Labels/LabelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
$label = Label::make('order_date', 'date')->header('Order Date');

expect($label->toArray())->toBe([
'field' => 'date',
'field' => 'label_date',
'header' => 'Order Date',
'meta' => [],
]);
Expand Down
2 changes: 1 addition & 1 deletion tests/DataGrids/Columns/BooleanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$array = $column->toArray();

expect($array)->toHaveKeys(['field', 'type', 'is_sortable', 'is_filterable', 'is_hidden', 'meta']);
expect($array['field'])->toBe('field');
expect($array['field'])->toBe('column_field');
expect($array['type'])->toBe(ColumnType::Boolean->value);
expect($array['is_sortable'])->toBeTrue();
expect($array['is_filterable'])->toBeTrue();
Expand Down
2 changes: 1 addition & 1 deletion tests/DataGrids/Columns/ChipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
$array = $column->toArray();

expect($array)->toHaveKeys(['field', 'type', 'is_sortable', 'is_filterable', 'is_hidden', 'meta']);
expect($array['field'])->toBe('Status');
expect($array['field'])->toBe('column_Status');
expect($array['type'])->toBe(ColumnType::Chip->value);
expect($array['is_sortable'])->toBeTrue();
expect($array['is_filterable'])->toBeTrue();
Expand Down
2 changes: 1 addition & 1 deletion tests/DataGrids/Columns/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$array = $column->toArray();

expect($array)->toHaveKeys(['field', 'type', 'is_sortable', 'is_filterable', 'is_hidden', 'meta']);
expect($array['field'])->toBe('field');
expect($array['field'])->toBe('column_field');
expect($array['type'])->toBe(ColumnType::Date->value);
expect($array['is_sortable'])->toBeTrue();
expect($array['is_filterable'])->toBeTrue();
Expand Down
2 changes: 1 addition & 1 deletion tests/DataGrids/Columns/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$array = $column->toArray();

expect($array)->toHaveKeys(['field', 'type', 'is_sortable', 'is_filterable', 'is_hidden', 'meta']);
expect($array['field'])->toBe('field');
expect($array['field'])->toBe('column_field');
expect($array['type'])->toBe(ColumnType::DateTime->value);
expect($array['is_sortable'])->toBeTrue();
expect($array['is_filterable'])->toBeTrue();
Expand Down
2 changes: 1 addition & 1 deletion tests/DataGrids/Columns/NumberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$array = $column->toArray();

expect($array)->toHaveKeys(['field', 'type', 'is_sortable', 'is_filterable', 'is_hidden', 'meta']);
expect($array['field'])->toBe('field');
expect($array['field'])->toBe('column_field');
expect($array['type'])->toBe(ColumnType::Number->value);
expect($array['is_sortable'])->toBeTrue();
expect($array['is_filterable'])->toBeTrue();
Expand Down
2 changes: 1 addition & 1 deletion tests/DataGrids/Columns/TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$array = $column->toArray();

expect($array)->toHaveKeys(['field', 'type', 'is_sortable', 'is_filterable', 'is_hidden', 'meta']);
expect($array['field'])->toBe('field');
expect($array['field'])->toBe('column_field');
expect($array['type'])->toBe(ColumnType::Text->value);
expect($array['is_sortable'])->toBeTrue();
expect($array['is_filterable'])->toBeTrue();
Expand Down
2 changes: 1 addition & 1 deletion tests/DataGrids/Columns/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
$array = $column->toArray();

expect($array)->toHaveKeys(['field', 'type', 'is_sortable', 'is_filterable', 'is_hidden', 'meta']);
expect($array['field'])->toBe('field');
expect($array['field'])->toBe('column_field');
expect($array['type'])->toBe(ColumnType::Time->value);
expect($array['is_sortable'])->toBeTrue();
expect($array['is_filterable'])->toBeTrue();
Expand Down
32 changes: 16 additions & 16 deletions tests/DataGrids/Grids/UserDataGridTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function validateDataGridSchema(array $schema): void

$columns = [
[
'field' => 'ID',
'field' => 'column_ID',
'header' => 'ID',
'type' => ColumnType::Number->value,
'pin' => 'none',
Expand All @@ -32,7 +32,7 @@ function validateDataGridSchema(array $schema): void
'meta' => [],
],
[
'field' => 'Name',
'field' => 'column_Name',
'header' => 'Name',
'type' => ColumnType::Text->value,
'pin' => 'none',
Expand All @@ -43,7 +43,7 @@ function validateDataGridSchema(array $schema): void
'meta' => [],
],
[
'field' => 'Email',
'field' => 'column_Email',
'header' => 'Email',
'type' => ColumnType::Text->value,
'pin' => 'none',
Expand Down Expand Up @@ -74,7 +74,7 @@ function validateDataGridSchema(array $schema): void
expect($schema['inline_actions'])->toContain($action);
}

$floatingFilters = [['field' => 'Joined On', 'header' => 'Joined On', 'type' => 'date_range', 'meta' => []]];
$floatingFilters = [['field' => 'floating_filter_Joined On', 'header' => 'Joined On', 'type' => 'date_range', 'meta' => []]];
foreach ($floatingFilters as $floatingFilter) {
expect($schema['floating_filters'])->toContain($floatingFilter);
}
Expand Down Expand Up @@ -104,12 +104,12 @@ function validateDataGridSchema(array $schema): void
$rows = $data['data'];

expect($rows)->toHaveCount(2);
expect($rows[0]['ID'])->toBe(1);
expect($rows[0]['Name'])->toBe('John Doe');
expect($rows[0]['Email'])->toBe('john@example.com');
expect($rows[1]['ID'])->toBe(2);
expect($rows[1]['Name'])->toBe('Jane Doe');
expect($rows[1]['Email'])->toBe('jane@example.com');
expect($rows[0]['column_ID'])->toBe(1);
expect($rows[0]['column_Name'])->toBe('John Doe');
expect($rows[0]['column_Email'])->toBe('john@example.com');
expect($rows[1]['column_ID'])->toBe(2);
expect($rows[1]['column_Name'])->toBe('Jane Doe');
expect($rows[1]['column_Email'])->toBe('jane@example.com');
expect($data['first'])->toBe(0);
expect($data['last'])->toBe(100);
});
Expand Down Expand Up @@ -137,12 +137,12 @@ function validateDataGridSchema(array $schema): void

expect($data['total'])->toBe(2);
expect($data['data'])->toHaveCount(2);
expect($data['data'][0]['ID'])->toBe(1);
expect($data['data'][0]['Name'])->toBe('John Doe');
expect($data['data'][0]['Email'])->toBe('john@example.com');
expect($data['data'][1]['ID'])->toBe(2);
expect($data['data'][1]['Name'])->toBe('Jane Doe');
expect($data['data'][1]['Email'])->toBe('jane@example.com');
expect($data['data'][0]['column_ID'])->toBe(1);
expect($data['data'][0]['column_Name'])->toBe('John Doe');
expect($data['data'][0]['column_Email'])->toBe('john@example.com');
expect($data['data'][1]['column_ID'])->toBe(2);
expect($data['data'][1]['column_Name'])->toBe('Jane Doe');
expect($data['data'][1]['column_Email'])->toBe('jane@example.com');
expect($data['per_page'])->toBe(250);
});

Expand Down
8 changes: 4 additions & 4 deletions tests/Metrics/Abstracts/MetricTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
expect($schema)->toHaveKeys(['visualization_key', 'value', 'floating_filters']);
expect($schema['visualization_key'])->toBe('metrics.revenues');
expect($schema['value'])->toBe([
'field' => 'revenue',
'field' => 'value_revenue',
'header' => 'Total Revenue',
'meta' => [],
]);
Expand All @@ -61,7 +61,7 @@

expect($schema['floating_filters'])->toHaveCount(1);
expect($schema['floating_filters']->first())->toBe([
'field' => 'date_range',
'field' => 'floating_filter_date_range',
'header' => 'Date Range',
'type' => 'date_range',
'meta' => [],
Expand All @@ -86,7 +86,7 @@

expect($data)->toHaveKeys(['visualization_key', 'value', 'floating_filters']);
expect($data['visualization_key'])->toBe('metrics.revenues');
expect($data['value']['field'])->toBe('revenue');
expect($data['value']['field'])->toBe('value_revenue');
expect($data['value']['header'])->toBe('Total Revenue');
});

Expand Down Expand Up @@ -149,7 +149,7 @@
[
'filter_set_operator' => 'and',
'filters' => [
['field' => 'total', 'value' => 100, 'filter_operator' => 'gte'],
['field' => 'floating_filter_total', 'value' => 100, 'filter_operator' => 'gte'],
],
],
],
Expand Down
4 changes: 2 additions & 2 deletions tests/Metrics/ValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
it('makes a Value with an expression and field', function () {
$value = Value::make('sum(orders.total)', 'revenue');

expect($value->getField())->toBe('revenue');
expect($value->getField())->toBe('value_revenue');
expect($value->getSelectWith())->toBe('sum(orders.total)');
});

Expand All @@ -27,7 +27,7 @@
->header('Total Revenue');

expect($value->toArray())->toBe([
'field' => 'revenue',
'field' => 'value_revenue',
'header' => 'Total Revenue',
'meta' => [],
]);
Expand Down
9 changes: 7 additions & 2 deletions tests/Traits/HandlesQueryExpressionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

class MockVisualizable extends Visualizable
{
public function getFieldPrefix(): string
{
return 'mock_';
}

public function toArray(): array
{
return [];
Expand All @@ -18,7 +23,7 @@ public function toArray(): array

test('can get field', function () {
$mock = MockVisualizable::make('test_column', 'test_field');
expect($mock->getField())->toBe('test_field');
expect($mock->getField())->toBe('mock_test_field');
});

test('can check if having is required', function () {
Expand All @@ -33,6 +38,6 @@ public function toArray(): array
$instance = MockVisualizable::make('test_column', 'test_field', ['test_binding']);

expect($instance->getSelectWith())->toBe('test_column');
expect($instance->getField())->toBe('test_field');
expect($instance->getField())->toBe('mock_test_field');
expect($instance->getSelectWithBindings())->toContain('test_binding');
});