Skip to content

Commit 6a0ed4a

Browse files
committed
#78 #71 Add model to config
1 parent 52a6572 commit 6a0ed4a

File tree

5 files changed

+31
-18
lines changed

5 files changed

+31
-18
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Article::withAnyTag(['Gardening','Cooking'])->get(); // fetch articles with any
6464

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

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

6969
Article::existingTags(); // return collection of all existing tags on any articles
7070
```

config/tagging.php

+3
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,7 @@
2020

2121
// Auto-delete unused tags from the 'tags' database table (when they are used zero times)
2222
'delete_unused_tags'=>true,
23+
24+
// Model to use to store the tags in the database
25+
'tag_model'=>'\Conner\Tagging\Model\Tag',
2326
);

docs/usage-examples.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Article::withAllTags('Gardening, Cooking')->get(); // only fetch articles with a
3636
Article::withAllTags(['Gardening', 'Cooking'])->get();
3737
Article::withAllTags('Gardening', 'Cooking')->get();
3838

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

4141
Article::existingTags(); // return collection of all existing tags on any articles
4242
```

src/Model/Tagged.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,31 @@
55
/**
66
* Copyright (C) 2014 Robert Conner
77
*/
8-
class Tagged extends Eloquent {
9-
8+
class Tagged extends Eloquent
9+
{
1010
protected $table = 'tagging_tagged';
1111
public $timestamps = false;
1212
protected $fillable = ['tag_name', 'tag_slug'];
1313

1414
/**
1515
* Morph to the tag
16-
*
16+
*
1717
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
1818
*/
19-
public function taggable()
19+
public function taggable()
2020
{
2121
return $this->morphTo();
2222
}
2323

2424
/**
2525
* Get instance of tag linked to the tagged value
26-
*
26+
*
2727
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
2828
*/
2929
public function tag()
3030
{
31-
return $this->belongsTo('Conner\Tagging\Model\Tag', 'tag_slug', 'slug');
31+
$model = Util::tagModelString();
32+
return $this->belongsTo($model, 'tag_slug', 'slug');
3233
}
3334

3435
}

src/Util.php

+19-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php namespace Conner\Tagging;
22

33
use Conner\Tagging\Contracts\TaggingUtility;
4-
use Conner\Tagging\Model\Tag;
54

65
/**
76
* Utility functions to help with various tagging functionality.
@@ -10,15 +9,15 @@
109
*
1110
* Copyright (C) 2014 Robert Conner
1211
*/
13-
class Util implements TaggingUtility {
14-
12+
class Util implements TaggingUtility
13+
{
1514
/**
1615
* Converts input into array
1716
*
1817
* @param $tagName string or array
1918
* @return array
2019
*/
21-
public function makeTagArray($tagNames)
20+
public function makeTagArray($tagNames)
2221
{
2322
if(is_array($tagNames) && count($tagNames) == 1) {
2423
$tagNames = $tagNames[0];
@@ -161,8 +160,9 @@ public static function slug($str)
161160
public function incrementCount($tagString, $tagSlug, $count)
162161
{
163162
if($count <= 0) { return; }
163+
$model = $this->tagModelString();
164164

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

167167
if(!$tag) {
168168
$tag = new Tag;
@@ -185,14 +185,15 @@ public function incrementCount($tagString, $tagSlug, $count)
185185
public function decrementCount($tagString, $tagSlug, $count)
186186
{
187187
if($count <= 0) { return; }
188+
$model = $this->tagModelString();
188189

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

191192
if($tag) {
192193
$tag->count = $tag->count - $count;
193194
if($tag->count < 0) {
194195
$tag->count = 0;
195-
\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.");
196+
\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.");
196197
}
197198
$tag->save();
198199
}
@@ -201,12 +202,20 @@ public function decrementCount($tagString, $tagSlug, $count)
201202
/**
202203
* Look at the tags table and delete any tags that are no londer in use by any taggable database rows.
203204
* Does not delete tags where 'suggest' is true
204-
*
205+
*
205206
* @return int
206207
*/
207208
public function deleteUnusedTags()
208209
{
209-
return Tag::deleteUnused();
210+
$model = $this->tagModelString();
211+
return $model::deleteUnused();
212+
}
213+
214+
/**
215+
* @return string
216+
*/
217+
public static function tagModelString()
218+
{
219+
return config('tagging.tag_model', '\Conner\Tagging\Model\Tag');
210220
}
211-
212221
}

0 commit comments

Comments
 (0)