Skip to content

Commit

Permalink
Another slightly less ugly way for backward compatibility
Browse files Browse the repository at this point in the history
Instead of using a constructor, add a special check in the boot-method for locations.
This seems to fit better in the system and does hopefully not break the existing tests.

Signed-off-by: Tobias Regnery <[email protected]>
  • Loading branch information
Toreg87 committed Nov 4, 2024
1 parent 1318dc6 commit 651d1c7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
9 changes: 8 additions & 1 deletion app/Models/CompanyableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ trait CompanyableTrait
*/
public static function bootCompanyableTrait()
{
static::addGlobalScope(new CompanyableScope);
// In Version 7.0 and before locations weren't scoped by companies, so add a check for the backward compatibility setting
if (__CLASS__ != 'App\Models\Location') {
static::addGlobalScope(new CompanyableScope);
} else {
if (Setting::getSettings()->scope_locations_fmcs == 1) {
static::addGlobalScope(new CompanyableScope);
}
}
}
}
11 changes: 1 addition & 10 deletions app/Models/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,8 @@

class Location extends SnipeModel
{
function __construct() {
parent::__construct();
// This is a workaround for backward compatibility with older versions where locations doesn't get scoped.
// Normaly we would only add 'use CompanyableTrait;', but this has to be conditional on the setting.
// So instead of using the trait, add the scope directly if no backward compatibility is used
if (Setting::getSettings()->scope_locations_fmcs) {
static::addGlobalScope(new CompanyableScope);
}
}

use HasFactory;
use CompanyableTrait;

protected $presenter = \App\Presenters\LocationPresenter::class;
use Presentable;
Expand Down

0 comments on commit 651d1c7

Please sign in to comment.