Skip to content

Commit

Permalink
#78 #71 Add model to config
Browse files Browse the repository at this point in the history
  • Loading branch information
rtconner committed Oct 19, 2015
1 parent 52a6572 commit 6a0ed4a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -64,7 +64,7 @@ Article::withAnyTag(['Gardening','Cooking'])->get(); // fetch articles with any

Article::withAllTags(['Gardening', 'Cooking'])->get(); // only fetch articles with all the tags

Conner\Tagging\Tag::where('count', '>', 2)->get(); // return all tags used more than twice
Conner\Tagging\Model\Tag::where('count', '>', 2)->get(); // return all tags used more than twice

Article::existingTags(); // return collection of all existing tags on any articles
```
Expand Down
3 changes: 3 additions & 0 deletions config/tagging.php
Expand Up @@ -20,4 +20,7 @@

// Auto-delete unused tags from the 'tags' database table (when they are used zero times)
'delete_unused_tags'=>true,

// Model to use to store the tags in the database
'tag_model'=>'\Conner\Tagging\Model\Tag',
);
2 changes: 1 addition & 1 deletion docs/usage-examples.md
Expand Up @@ -36,7 +36,7 @@ Article::withAllTags('Gardening, Cooking')->get(); // only fetch articles with a
Article::withAllTags(['Gardening', 'Cooking'])->get();
Article::withAllTags('Gardening', 'Cooking')->get();

Conner\Tagging\Tag::where('count', '>', 2)->get(); // return all tags used more than twice
Conner\Tagging\Model\Tag::where('count', '>', 2)->get(); // return all tags used more than twice

Article::existingTags(); // return collection of all existing tags on any articles
```
13 changes: 7 additions & 6 deletions src/Model/Tagged.php
Expand Up @@ -5,30 +5,31 @@
/**
* Copyright (C) 2014 Robert Conner
*/
class Tagged extends Eloquent {

class Tagged extends Eloquent
{
protected $table = 'tagging_tagged';
public $timestamps = false;
protected $fillable = ['tag_name', 'tag_slug'];

/**
* Morph to the tag
*
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function taggable()
public function taggable()
{
return $this->morphTo();
}

/**
* Get instance of tag linked to the tagged value
*
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function tag()
{
return $this->belongsTo('Conner\Tagging\Model\Tag', 'tag_slug', 'slug');
$model = Util::tagModelString();
return $this->belongsTo($model, 'tag_slug', 'slug');
}

}
29 changes: 19 additions & 10 deletions src/Util.php
@@ -1,7 +1,6 @@
<?php namespace Conner\Tagging;

use Conner\Tagging\Contracts\TaggingUtility;
use Conner\Tagging\Model\Tag;

/**
* Utility functions to help with various tagging functionality.
Expand All @@ -10,15 +9,15 @@
*
* Copyright (C) 2014 Robert Conner
*/
class Util implements TaggingUtility {
class Util implements TaggingUtility
{
/**
* Converts input into array
*
* @param $tagName string or array
* @return array
*/
public function makeTagArray($tagNames)
public function makeTagArray($tagNames)
{
if(is_array($tagNames) && count($tagNames) == 1) {
$tagNames = $tagNames[0];
Expand Down Expand Up @@ -161,8 +160,9 @@ public static function slug($str)
public function incrementCount($tagString, $tagSlug, $count)
{
if($count <= 0) { return; }
$model = $this->tagModelString();

$tag = Tag::where('slug', '=', $tagSlug)->first();
$tag = $model::where('slug', '=', $tagSlug)->first();

if(!$tag) {
$tag = new Tag;
Expand All @@ -185,14 +185,15 @@ public function incrementCount($tagString, $tagSlug, $count)
public function decrementCount($tagString, $tagSlug, $count)
{
if($count <= 0) { return; }
$model = $this->tagModelString();

$tag = Tag::where('slug', '=', $tagSlug)->first();
$tag = $model::where('slug', '=', $tagSlug)->first();

if($tag) {
$tag->count = $tag->count - $count;
if($tag->count < 0) {
$tag->count = 0;
\Log::warning("The \Conner\Tagging\Model\Tag count for `$tag->name` was a negative number. This probably means your data got corrupted. Please assess your code and report an issue if you find one.");
\Log::warning("The '.$model.' count for `$tag->name` was a negative number. This probably means your data got corrupted. Please assess your code and report an issue if you find one.");
}
$tag->save();
}
Expand All @@ -201,12 +202,20 @@ public function decrementCount($tagString, $tagSlug, $count)
/**
* Look at the tags table and delete any tags that are no londer in use by any taggable database rows.
* Does not delete tags where 'suggest' is true
*
*
* @return int
*/
public function deleteUnusedTags()
{
return Tag::deleteUnused();
$model = $this->tagModelString();
return $model::deleteUnused();
}

/**
* @return string
*/
public static function tagModelString()
{
return config('tagging.tag_model', '\Conner\Tagging\Model\Tag');
}

}

0 comments on commit 6a0ed4a

Please sign in to comment.