Add new fields, include tags on the new forum API action

This commit is contained in:
Toby Zerner 2015-06-15 12:20:26 +09:30
parent d5b1d3bdb2
commit 85d7dc8752
5 changed files with 35 additions and 22 deletions

View File

@ -6,13 +6,19 @@ Tag.prototype.id = Model.prop('id');
Tag.prototype.name = Model.prop('name');
Tag.prototype.slug = Model.prop('slug');
Tag.prototype.description = Model.prop('description');
Tag.prototype.color = Model.prop('color');
Tag.prototype.backgroundUrl = Model.prop('backgroundUrl');
Tag.prototype.backgroundMode = Model.prop('backgroundMode');
Tag.prototype.iconUrl = Model.prop('iconUrl');
Tag.prototype.discussionsCount = Model.prop('discussionsCount');
Tag.prototype.position = Model.prop('position');
Tag.prototype.parent = Model.one('parent');
Tag.prototype.defaultSort = Model.prop('defaultSort');
Tag.prototype.isChild = Model.prop('isChild');
Tag.prototype.discussionsCount = Model.prop('discussionsCount');
Tag.prototype.lastTime = Model.prop('lastTime', Model.date);
Tag.prototype.lastDiscussion = Model.one('lastDiscussion');
export default Tag;

View File

@ -1,19 +0,0 @@
<?php namespace Flarum\Tags\Handlers;
use Flarum\Tags\Tag;
use Flarum\Tags\TagSerializer;
use Flarum\Forum\Events\RenderView;
class TagPreloader
{
public function subscribe($events)
{
$events->listen('Flarum\Forum\Events\RenderView', __CLASS__.'@renderForum');
}
public function renderForum(RenderView $event)
{
$serializer = new TagSerializer($event->action->actor, null, ['parent']);
$event->view->data = array_merge($event->view->data, $serializer->collection(Tag::orderBy('position')->get())->toArray());
}
}

View File

@ -5,4 +5,16 @@ use Flarum\Core\Models\Model;
class Tag extends Model
{
protected $table = 'tags';
protected $dates = ['last_time'];
public function parent()
{
return $this->belongsTo('Flarum\Tags\Tag', 'parent_id');
}
public function lastDiscussion()
{
return $this->belongsTo('Flarum\Core\Models\Discussion', 'last_discussion_id');
}
}

View File

@ -25,11 +25,13 @@ class TagSerializer extends BaseSerializer
'slug' => $tag->slug,
'color' => $tag->color,
'backgroundUrl' => $tag->background_path,
'backgroundMode' => $tag->background_mode,
'iconUrl' => $tag->icon_path,
'discussionsCount' => (int) $tag->discussions_count,
'position' => $tag->position === null ? null : (int) $tag->position,
'defaultSort' => $tag->default_sort,
'isChild' => (bool) $tag->parent_id
'isChild' => (bool) $tag->parent_id,
'lastTime' => $tag->last_time ? $tag->last_time->toRFC3339String() : null
];
return $this->extendAttributes($tag, $attributes);
@ -39,4 +41,9 @@ class TagSerializer extends BaseSerializer
{
return $this->hasOne('Flarum\Tags\TagSerializer');
}
protected function lastDiscussion()
{
return $this->hasOne('Flarum\Api\Serializers\DiscussionSerializer');
}
}

View File

@ -27,7 +27,6 @@ class TagsServiceProvider extends ServiceProvider
new EventSubscribers([
'Flarum\Tags\Handlers\DiscussionTaggedNotifier',
'Flarum\Tags\Handlers\TagPreloader',
'Flarum\Tags\Handlers\TagSaver'
]),
@ -39,6 +38,14 @@ class TagsServiceProvider extends ServiceProvider
new ApiInclude(['discussions.index', 'discussions.show'], 'tags', true),
new Relationship('Flarum\Core\Models\Forum', 'tags', function ($model) {
return Tag::query();
}),
new SerializeRelationship('Flarum\Api\Serializers\ForumSerializer', 'hasMany', 'tags', 'Flarum\Tags\TagSerializer'),
new ApiInclude(['forum.show'], ['tags', 'tags.parent', 'tags.lastDiscussion'], true),
(new Permission('discussion.tag'))
->serialize()
->grant(function ($grant, $user) {