Skip to content

Commit c4d6a08

Browse files
committed
fix crud column() to accept array
1 parent 0e8cda5 commit c4d6a08

File tree

5 files changed

+46
-16
lines changed

5 files changed

+46
-16
lines changed

src/app/Library/CrudPanel/CrudColumn.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ class CrudColumn
3838

3939
protected $attributes;
4040

41-
public function upload($upload = true)
41+
public function __construct($nameOrDefinitionArray)
4242
{
43-
$this->attributes['upload'] = $upload;
44-
45-
return $this->save();
46-
}
43+
if (is_array($nameOrDefinitionArray)) {
44+
$column = $this->crud()->addAndReturnColumn($nameOrDefinitionArray);
45+
$name = $column->getAttributes()['name'];
46+
} else {
47+
$name = $nameOrDefinitionArray;
48+
}
4749

48-
public function __construct($name)
49-
{
5050
$column = $this->crud()->firstColumnWhere('name', $name);
5151

5252
// if column exists
@@ -159,6 +159,13 @@ public function before($destinationColumn)
159159
return $this;
160160
}
161161

162+
public function upload($upload = true)
163+
{
164+
$this->attributes['upload'] = $upload;
165+
166+
return $this->save();
167+
}
168+
162169
/**
163170
* Make the current column the first one in the columns list.
164171
*

src/app/Library/CrudPanel/Traits/Columns.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ public function addColumn($column)
7474
return $this;
7575
}
7676

77+
/**
78+
* Add a column at the end of to the CRUD object's "columns" array and return it
79+
*
80+
* @param array|string $column
81+
* @return self
82+
*/
83+
public function addAndReturnColumn($column)
84+
{
85+
$column = $this->makeSureColumnHasNeededAttributes($column);
86+
$this->addColumnToOperationSettings($column);
87+
88+
$column = (new CrudColumn($column['name']))->callRegisteredAttributeMacros();
89+
90+
return $column;
91+
}
92+
7793
/**
7894
* Add multiple columns at the end of the CRUD object's "columns" array.
7995
*
@@ -405,15 +421,16 @@ public function countColumnsWithoutActions()
405421
* in addition to the existing options:
406422
* - CRUD::addColumn(['name' => 'price', 'type' => 'number']);
407423
* - CRUD::column('price')->type('number');
424+
* - CRUD::column(['name' => 'price', 'type' => 'number']);
408425
*
409426
* And if the developer uses the CrudColumn object as Column in their CrudController:
410427
* - Column::name('price')->type('number');
411428
*
412-
* @param string $name The name of the column in the db, or model attribute.
429+
* @param string|array $name The name of the column in the db, or model attribute.
413430
* @return CrudColumn
414431
*/
415-
public function column($name)
432+
public function column($nameOrDefinition)
416433
{
417-
return new CrudColumn($name);
434+
return new CrudColumn($nameOrDefinition);
418435
}
419436
}

src/app/Library/CrudPanel/Traits/Fields.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,15 +562,16 @@ public function holdsMultipleInputs(string $fieldName): bool
562562
* in addition to the existing options:
563563
* - CRUD::addField(['name' => 'price', 'type' => 'number']);
564564
* - CRUD::field('price')->type('number');
565+
* - CRUD::field(['name' => 'price', 'type' => 'number']);
565566
*
566567
* And if the developer uses the CrudField object as Field in their CrudController:
567568
* - Field::name('price')->type('number');
568569
*
569-
* @param string $name The name of the column in the db, or model attribute.
570+
* @param string|array $nameOrDefinition The name of the column in the db, or model attribute.
570571
* @return CrudField
571572
*/
572-
public function field($name)
573+
public function field($nameOrDefinition)
573574
{
574-
return new CrudField($name);
575+
return new CrudField($nameOrDefinition);
575576
}
576577
}

src/app/Library/CrudPanel/Traits/Support/MacroableWithAttributes.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,8 @@ public function getMacros()
2929

3030
/**
3131
* Call the macros registered for the given macroable attributes.
32-
*
33-
* @return void
3432
*/
35-
public function callRegisteredAttributeMacros()
33+
public function callRegisteredAttributeMacros(): self
3634
{
3735
$macros = $this->getMacros();
3836
$attributes = $this->getAttributes();
@@ -58,5 +56,6 @@ function ($item) use ($subfieldsWithMacros, $macro) {
5856
);
5957
}
6058
}
59+
return $this;
6160
}
6261
}

tests/Unit/CrudPanel/CrudPanelColumnsTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,4 +621,10 @@ public function testItCanAddAColumnToCrudFromClass()
621621
CrudColumn::name('test');
622622
$this->assertCount(1, $this->crudPanel->columns());
623623
}
624+
625+
public function testItCanAddAFluentColumnUsingArray()
626+
{
627+
$this->crudPanel->column($this->oneColumnArray);
628+
$this->assertCount(1, $this->crudPanel->columns());
629+
}
624630
}

0 commit comments

Comments
 (0)