Skip to content

Commit

Permalink
Merge pull request #61 from SlovakNationalGallery/GMBUVP-129
Browse files Browse the repository at this point in the history
fix: airtable flavored markdown
  • Loading branch information
rastislav-chynoransky authored Jul 16, 2024
2 parents 0abea53 + ebadd2b commit 317c1ef
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
40 changes: 20 additions & 20 deletions app/Models/Artwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public static function getStats()
'artworks.stats',
fn() => [
'lastUpdate' => optional(
self::orderByDesc('updated_at')->first()
self::orderByDesc('updated_at')->first(),
)->updated_at,
'count' => self::published()->count(),
'locations' => Location::selectRaw(
'count(id) as count, borough'
'count(id) as count, borough',
)
->current()
->whereHas('artworks', function (Builder $query) {
Expand All @@ -42,7 +42,7 @@ public static function getStats()
->groupBy('district')
->collect() // Turn to Laravel collection
->except(''), // Filter out locations with empty district
]
],
);
}

Expand All @@ -65,7 +65,7 @@ public function scopeSelectCurrentLocationDistance($query, $otherArtwork)
'artwork_location',
'artwork_location.artwork_id',
'=',
'artworks.id'
'artworks.id',
)
->join('locations', function (JoinClause $join) {
$join
Expand All @@ -75,50 +75,50 @@ public function scopeSelectCurrentLocationDistance($query, $otherArtwork)
})
->selectRaw(
'ST_Distance_Sphere(point(?, ?), point(locations.gps_lon, locations.gps_lat)) as distance',
[$otherArtworkLocation->gps_lon, $otherArtworkLocation->gps_lat]
[
$otherArtworkLocation->gps_lon,
$otherArtworkLocation->gps_lat,
],
);
}

// default scope for those artworks that we can actually display
public function scopePresentable($query)
{
$query
->published()
->has('coverPhotoMedia')
->has('locations');
$query->published()->has('coverPhotoMedia')->has('locations');
}

public function scopeFilteredBySearchRequest($query, Request $request)
{
$request->whenFilled('boroughs', function ($boroughs) use ($query) {
$query->whereHas('locations', function (Builder $query) use (
$boroughs
$boroughs,
) {
$query->current()->whereIn('borough', $boroughs);
});
});

$request->whenFilled('authors', function ($authorIds) use ($query) {
$query->whereHas('authorsAndCoauthors', function (
Builder $query
Builder $query,
) use ($authorIds) {
$query->whereIn('id', $authorIds);
});
});

$request->whenFilled('categories', function ($categoryIds) use (
$query
$query,
) {
$query->whereHas('categories', function (Builder $query) use (
$categoryIds
$categoryIds,
) {
$query->whereIn('id', $categoryIds);
});
});

$request->whenFilled('keywords', function ($keywordIds) use ($query) {
$query->whereHas('keywords', function (Builder $query) use (
$keywordIds
$keywordIds,
) {
$query->whereIn('id', $keywordIds);
});
Expand Down Expand Up @@ -245,7 +245,7 @@ public function coverPhotoMedia()
{
return $this->hasOneDeepFromRelations(
$this->photos(),
(new Photo())->media()
(new Photo())->media(),
)
->orderBy('artwork_photo.order')
->orderBy('media.order_column')
Expand All @@ -258,8 +258,8 @@ public function defunct(): Attribute
fn() => $this->conditions->contains(
fn(Condition $condition) => str($condition->name)
->trim()
->exactly('zničené, odstránené')
)
->exactly('zničené, odstránené'),
),
);
}

Expand All @@ -268,7 +268,7 @@ public function descriptionHtml(): Attribute
return Attribute::get(
fn() => Str::of($this->description)
->replace("\n", "\n\n")
->markdown()
->airtableMarkdown(),
);
}

Expand Down Expand Up @@ -299,8 +299,8 @@ public function photoMediaForCarousel(): Attribute
'url' => $m->getUrl(),
'description' => $photo->description,
'source' => $photo->source,
]
)
],
),
);
});
}
Expand Down
14 changes: 14 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use App\Http\Resources\ArtworkMapPointCollection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;

class AppServiceProvider extends ServiceProvider
{
Expand All @@ -30,5 +32,17 @@ public function boot()
}

ArtworkMapPointCollection::withoutWrapping();

Str::macro('airtableMarkdown', function ($value) {
return Str::of($value)
->replaceMatches('/\*\*(\s*)(.*?)(\s*)\*\*/', '$1**$2**$3')
->replaceMatches('/_(\s*)(.*?)(\s*)_/', '$1_$2_$3')
->replaceMatches('/~~(\s*)(.*?)(\s*)~~/', '$1~~$2~~$3')
->markdown();
});

Stringable::macro('airtableMarkdown', function () {
return Str::airtableMarkdown($this->value);
});
}
}

0 comments on commit 317c1ef

Please sign in to comment.