Skip to content

Commit

Permalink
extend exportables: check module for exportable method as well
Browse files Browse the repository at this point in the history
  • Loading branch information
Endi authored and endihunter committed Mar 15, 2019
1 parent 9ff89c6 commit aedcf2c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
26 changes: 19 additions & 7 deletions src/Traits/ExportsCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@ trait ExportsCollection
* @param Builder $query
* @param $format
*
* @throws Exception
*
* @return mixed
* @throws Exception
*/
public function export(Builder $query, $format)
{
$method = 'to'.strtoupper($format);

if (!method_exists($this, $method)) {
throw new Exception(sprintf('Don\'t know how to export to %s format', $format));
if (method_exists($this, $method)) {
return call_user_func_array([$this, $method], [$query]);
}

return call_user_func_array([$this, $method], [$query]);
if (method_exists($this->module, $method)) {
return call_user_func_array([$this->module, $method], [$query]);
}

throw new Exception(sprintf('Don\'t know how to export to %s format.', $format));
}

/**
Expand Down Expand Up @@ -113,6 +116,12 @@ public function toCSV(Builder $query)
return $this->sendDownloadResponse($file, 'csv', ['Content-Type' => 'text/csv']);
}

/**
* @param Builder $query
* @return mixed
* @throws Exception
* @throws \Throwable
*/
public function toPDF(Builder $query)
{
if (!app()->has('dompdf.wrapper')) {
Expand All @@ -134,8 +143,8 @@ public function toPDF(Builder $query)
])->render();

return $pdf->loadHTML($html)
->setPaper('a4', 'landscape')
->download(app('scaffold.module')->url().'.pdf');
->setPaper('a4', 'landscape')
->download(app('scaffold.module')->url().'.pdf');
}

/**
Expand Down Expand Up @@ -274,6 +283,9 @@ protected function exportableColumns(): array
->all();
}

/**
* @return mixed
*/
protected function exportableView()
{
return app('scaffold.template')->layout('exportable');
Expand Down
4 changes: 2 additions & 2 deletions tests/Collection/MutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public function it_creates_a_group_of_elements()
public function it_joins_two_or_more_elements_into_a_group_using_literal_position()
{
$this->collection
->join(['first', 'third'], 'group', 'after:second');
->stack(['first', 'third'], 'group', 'after:second');

$this->assertCount(2, $this->collection);

Expand All @@ -279,7 +279,7 @@ public function it_joins_two_or_more_elements_into_a_group_using_literal_positio
public function it_joins_two_or_more_elements_into_a_group()
{
$this->collection
->join(['first', 'third'], 'group');
->stack(['first', 'third'], 'group');

$this->assertCount(2, $this->collection);

Expand Down

0 comments on commit aedcf2c

Please sign in to comment.