Skip to content

Commit

Permalink
Merge pull request #90 from jakejohns/feature/autotagprop
Browse files Browse the repository at this point in the history
Add Auto Tagging from Property
  • Loading branch information
rtconner committed Jan 6, 2016
2 parents a4b5963 + 4e6a90f commit ce6f25e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
4 changes: 2 additions & 2 deletions config/tagging.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +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',
);
);
57 changes: 56 additions & 1 deletion src/Taggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ trait Taggable
{
/** @var \Conner\Tagging\Contracts\TaggingUtility **/
static $taggingUtility;

/**
* Temp storage for auto tag
*
* @var mixed
* @access protected
*/
protected $autoTagTmp;

/**
* Track if auto tag has been manually set
*
* @var boolean
* @access protected
*/
protected $autoTagSet = false;

/**
* Boot the soft taggable trait for a model.
Expand All @@ -27,7 +43,11 @@ public static function bootTaggable()
$model->untag();
});
}


static::saved(function ($model) {
$model->autoTagPostSave();
});

static::$taggingUtility = app(TaggingUtility::class);
}

Expand Down Expand Up @@ -288,4 +308,39 @@ public static function shouldDeleteUnused()
return Config::get('tagging.delete_unused_tags');
}

/**
* Set tag names to be set on save
*
* @param mixed $value Data for retag
*
* @return void
*
* @access public
*/
public function setTagNamesAttribute($value)
{
$this->autoTagTmp = $value;
$this->autoTagSet = true;
}

/**
* AutoTag post-save hook
*
* Tags model based on data stored in tmp property, or untags if manually
* set to falsey value
*
* @return void
*
* @access public
*/
public function autoTagPostSave()
{
if ($this->autoTagSet) {
if ($this->autoTagTmp) {
$this->retag($this->autoTagTmp);
} else {
$this->untag();
}
}
}
}

0 comments on commit ce6f25e

Please sign in to comment.