Skip to content

Commit

Permalink
add HasCamelCasing trait to models: UserGroupStage, UserUserGroup, an…
Browse files Browse the repository at this point in the history
…d InvitationModel
  • Loading branch information
taslangraham committed Jul 28, 2024
1 parent 165984b commit 202fa9f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 128 deletions.
75 changes: 12 additions & 63 deletions classes/invitation/models/InvitationModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\InteractsWithTime;
use PKP\invitation\core\enums\InvitationStatus;
use Eloquence\Behaviours\HasCamelCasing;

class InvitationModel extends Model
{
use InteractsWithTime;
use HasCamelCasing;

/**
* Model's database table
Expand Down Expand Up @@ -74,70 +76,17 @@ class InvitationModel extends Model
];

protected $visible = [
'invitation_id',
'status',
'createdAt',
'id',
'status',
'createdAt',
'updatedAt',
'user_id',
'context_id',
'expiry_date',
'userId',
'contextId',
'expiryDate',
'email',
];

public function keyHash(): Attribute
{
return Attribute::make(
get: fn ($user, $attributes) => $attributes['key_hash'],
set: fn ($value) => ['key_hash' => $value]
);
}

public function userId(): Attribute
{
return Attribute::make(
get: fn ($user, $attributes) => $attributes['user_id'],
set: fn ($value) => ['user_id' => $value]
);
}

public function expiryDate(): Attribute
{
return Attribute::make(
get: fn ($user, $attributes) => new Carbon($attributes['expiry_date']),
set: fn ($value) => ['expiry_date' => $value]
);
}

public function updatedAt(): Attribute
{
return Attribute::make(
get: fn ($user, $attributes) => new Carbon($attributes['updated_at']),
set: fn ($value) => ['updated_at' => $value]
);
}

public function createdAt(): Attribute
{
return Attribute::make(
get: fn ($user, $attributes) => new Carbon($attributes['created_at']),
set: fn ($value) => ['created_at' => $value]
);
}

public function contextId(): Attribute
{
return Attribute::make(
get: fn ($user, $attributes) => $attributes['context_id'],
set: fn ($value) => ['context_id' => $value]
);
}
public function className(): Attribute
{
return Attribute::make(
get: fn ($user, $attributes) => $attributes['class_name'],
set: fn ($value) => ['class_name' => $value]
);
}
public function id(): Attribute
{
return Attribute::make(
Expand Down Expand Up @@ -249,22 +198,22 @@ public function scopeStillActive(Builder $query): Builder
{
// Apply the NotExpired scope
$query->notExpired();

// Apply the NotHandled scope
return $query->notHandled();
}

public function markAs(InvitationStatus $status): bool
{
$this->status = $status;
$this->updated_at = Carbon::now();
$this->updatedAt = Carbon::now();

return $this->save();
}

/**
* Mark all invitations with a given status.
*
*
*/
public static function markAllAs(InvitationStatus $status, Collection $ids): int
{
Expand Down
27 changes: 3 additions & 24 deletions classes/userGroup/relationships/UserGroupStage.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
use APP\facades\Repo;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Eloquence\Behaviours\HasCamelCasing;

class UserGroupStage extends \Illuminate\Database\Eloquent\Model
{
use HasCamelCasing;

public $timestamps = false;
public $incrementing = false;
protected $primaryKey = null;
Expand All @@ -40,30 +43,6 @@ public function userGroup(): Attribute
);
}

public function stageId(): Attribute
{
return Attribute::make(
get: fn ($value, $attributes) => $attributes['stage_id'],
set: fn ($value) => ['stage_id' => $value]
);
}

public function userGroupId(): Attribute
{
return Attribute::make(
get: fn ($userGroup, $attributes) => $attributes['user_group_id'],
set: fn ($value) => ['user_group_id' => $value]
);
}

public function contextId(): Attribute
{
return Attribute::make(
get: fn ($value, $attributes) => $attributes['context_id'],
set: fn ($value) => ['context_id' => $value]
);
}

public function scopeWithStageId(Builder $query, int $stageId): Builder
{
return $query->where('stage_id', $stageId);
Expand Down
44 changes: 3 additions & 41 deletions classes/userGroup/relationships/UserUserGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
use APP\facades\Repo;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Facades\DB;
use Eloquence\Behaviours\HasCamelCasing;
use PKP\core\Core;

class UserUserGroup extends \Illuminate\Database\Eloquent\Model
{
use HasCamelCasing;

public $timestamps = false;
public $incrementing = false;
protected $primaryKey = null;
Expand All @@ -43,46 +45,6 @@ public function userGroup(): Attribute
);
}

public function userId(): Attribute
{
return Attribute::make(
get: fn ($user, $attributes) => $attributes['user_id'],
set: fn ($value) => ['user_id' => $value]
);
}

public function userGroupId(): Attribute
{
return Attribute::make(
get: fn ($userGroup, $attributes) => $attributes['user_group_id'],
set: fn ($value) => ['user_group_id' => $value]
);
}

public function dateStart(): Attribute
{
return Attribute::make(
get: fn ($userGroup, $attributes) => $attributes['date_start'],
set: fn ($value) => ['date_start' => $value]
);
}

public function dateEnd(): Attribute
{
return Attribute::make(
get: fn ($userGroup, $attributes) => $attributes['date_end'],
set: fn ($value) => ['date_end' => $value]
);
}

public function masthead(): Attribute
{
return Attribute::make(
get: fn ($userGroup, $attributes) => $attributes['masthead'],
set: fn ($value) => ['masthead' => $value]
);
}

public function scopeWithUserId(Builder $query, int $userId): Builder
{
return $query->where('user_user_groups.user_id', $userId);
Expand Down

0 comments on commit 202fa9f

Please sign in to comment.