Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
guidocella committed Jan 1, 2017
1 parent 4508fca commit ff035ec
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
8 changes: 5 additions & 3 deletions src/ModelPopulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,12 @@ public function run(array $insertedPKs, $persist, $keepTimestamps = false)

$this->model = new $this->model;

// Creating the translations before filling the model allows setting
// custom attributes for the main model in the form of attribute:locale,
// Creating the translations before filling the model allows setting custom
// attributes for the main model in the form of attribute:locale,
// e.g. name:de, without having them overwritten.
$this->translate($insertedPKs);
if ($this->shouldTranslate()) {
$this->translate($insertedPKs);
}

$this->fillModel($this->model, $insertedPKs);

Expand Down
18 changes: 7 additions & 11 deletions src/PopulatesTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,13 @@ public function translationStates(...$states)
}

/**
* Determine if a model is the main model or its translation.
* Determine if the model should be translated.
*
* @param Model $model
* @return bool
*/
protected function isTranslation(Model $model)
protected function shouldTranslate()
{
return $model !== $this->model;
return $this->locales && in_array(Translatable::class, class_uses($this->model));
}

/**
Expand All @@ -95,10 +94,6 @@ protected function isTranslation(Model $model)
*/
protected function translate(array $insertedPKs)
{
if (!$this->shouldTranslate()) {
return;
}

// We'll first set the translations relation to a new collection
// through an instance of the translation model, just in case its newCollection() method is overridden,
// but we're not gonna use Translatable::getNewTranslation()
Expand Down Expand Up @@ -129,13 +124,14 @@ protected function translate(array $insertedPKs)
}

/**
* Determine if the model should be translated.
* Determine if a model is the main model or its translation.
*
* @param Model $model
* @return bool
*/
protected function shouldTranslate()
protected function isTranslation(Model $model)
{
return $this->locales && in_array(Translatable::class, class_uses($this->model));
return $model !== $this->model;
}

/**
Expand Down

0 comments on commit ff035ec

Please sign in to comment.