Skip to content

Commit

Permalink
Merge pull request #35 from tipoff/wolfrednicolas/feature/concise-syn…
Browse files Browse the repository at this point in the history
…tax-relations-model

Concise syntax relations model
  • Loading branch information
drewroberts committed Feb 10, 2021
2 parents 7471385 + 180bd43 commit 9f6e636
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
5 changes: 1 addition & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@
"tipoff/support": "^1.1.2"
},
"require-dev": {
"orchestra/testbench": "^6.0",
"phpunit/phpunit": "^9.3",
"spatie/laravel-ray": "^1.9",
"vimeo/psalm": "^4.4"
"tipoff/test-support": "^1.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion database/factories/ReviewFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function definition()
'reviewed_at' => $this->faker->dateTimeBetween($startDate = '-7 days', $endDate = 'now', $timezone = null),
'reply' => $this->faker->sentences(3, true),
'replied_at' => $this->faker->dateTimeBetween($startDate = '-7 days', $endDate = 'now', $timezone = null),
'location_id' => randomOrCreate(config('tipoff.model_class.location')),
'location_id' => randomOrCreate(app('location')),
];
}
}
2 changes: 1 addition & 1 deletion database/factories/SnapshotFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class SnapshotFactory extends Factory
public function definition()
{
return [
'competitor_id' => randomOrCreate(Competitor::class),
'competitor_id' => randomOrCreate(app('competitor')),
'date' => $this->faker->date,
'reviews' => $this->faker->numberBetween(1, 8),
];
Expand Down
18 changes: 12 additions & 6 deletions src/Models/Competitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function market()

public function snapshots()
{
return $this->hasMany(Snapshot::class);
return $this->hasMany(app('snapshot'));
}

public function location()
Expand All @@ -38,7 +38,9 @@ public function getNameAttribute($value)

public function getReviewsAttribute()
{
$recent = Snapshot::where('competitor_id', $this->id)->orderByDesc('date')->first();
$snapshot = app('snapshot');

$recent = $snapshot::where('competitor_id', $this->id)->orderByDesc('date')->first();

if (! isset($recent)) {
return '?';
Expand All @@ -49,6 +51,8 @@ public function getReviewsAttribute()

public function getWeeklyReviewsAttribute()
{
$snapshot = app('snapshot');

// Snapshots are run every Wednesday morning.
$today = Carbon::now('America/New_York');
if ($today->dayOfWeek == Carbon::WEDNESDAY) {
Expand All @@ -57,8 +61,8 @@ public function getWeeklyReviewsAttribute()
$firstdate = new Carbon('last wednesday');
}

$recent = Snapshot::where('competitor_id', $this->id)->where('date', $firstdate->format('Y-m-d'))->first();
$prior = Snapshot::where('competitor_id', $this->id)->where('date', $firstdate->subDays(7)->format('Y-m-d'))->first();
$recent = $snapshot::where('competitor_id', $this->id)->where('date', $firstdate->format('Y-m-d'))->first();
$prior = $snapshot::where('competitor_id', $this->id)->where('date', $firstdate->subDays(7)->format('Y-m-d'))->first();

if (! isset($recent) || ! isset($prior)) {
return '?';
Expand All @@ -69,11 +73,13 @@ public function getWeeklyReviewsAttribute()

public function getMonthlyReviewsAttribute()
{
$snapshot = app('snapshot');

// Snapshots are run on the 1st of every month.
$firstdate = Carbon::now('America/New_York')->firstOfMonth();

$recent = Snapshot::where('competitor_id', $this->id)->where('date', $firstdate->format('Y-m-d'))->first();
$prior = Snapshot::where('competitor_id', $this->id)->where('date', $firstdate->subMonths(1)->format('Y-m-d'))->first();
$recent = $snapshot::where('competitor_id', $this->id)->where('date', $firstdate->format('Y-m-d'))->first();
$prior = $snapshot::where('competitor_id', $this->id)->where('date', $firstdate->subMonths(1)->format('Y-m-d'))->first();

if (! isset($recent) || ! isset($prior)) {
return '?';
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Review.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Review extends BaseModel

public function location()
{
return $this->belongsTo(config('tipoff.model_class.location'));
return $this->belongsTo(app('location'));
}

public function getTitleAttribute()
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ class Snapshot extends Model

public function competitor()
{
return $this->belongsTo(Competitor::class);
return $this->belongsTo(app('competitor'));
}
}

0 comments on commit 9f6e636

Please sign in to comment.