<commit_before><?php

namespace SleepingOwl\Admin\Traits;

use Illuminate\Database\Query\Builder;
use Illuminate\Database\Eloquent\Model;

trait OrderableModel
{
    /**
     * Boot trait.
     */
    protected static function bootOrderableModel()
    {
        static::creating(function (Model $row) {
            $row->updateOrderFieldOnCreate();
        });

        static::deleted(function (Model $row) {
            $row->updateOrderFieldOnDelete();
        });
    }

    /**
     * Get order value.
     * @return int
     */
    public function getOrderValue()
    {
        return $this->{$this->getOrderField()};
    }

    /**
     * Move model up.
     */
    public function moveUp()
    {
        $this->move(1);
    }

    /**
     * Move model down.
     */
    public function moveDown()
    {
        $this->move(-1);
    }

    /**
     * Move model in the $destination.
     *
     * @param $destination -1 (move down) or 1 (move up)
     */
    protected function move($destination)
    {
        if ($previousRow = static::orderModel()->findByPosition($this->getOrderValue() - $destination)->first()) {
            $previousRow->{$this->getOrderField()} += $destination;
            $previousRow->save();
        }

        $this->{$this->getOrderField()} -= $destination;
        $this->save();
    }

    /**
     * Update order field on create.
     */
    protected function updateOrderFieldOnCreate()
    {
        $this->{$this->getOrderField()} = static::orderModel()->count();
    }

    /**
     * Update order field on delete.
     */
    protected function updateOrderFieldOnDelete()
    {
        static::orderModel()
              ->where($this->getOrderField(), '>', $this->getOrderValue())
              ->decrement($this->getOrderField());
    }

    /**
     * Order scope.
     *
     * @param $query
     *
     * @return mixed
     */
    public function scopeOrderModel($query)
    {
        return $query;
    }

    /**
     * @param Builder $query
     * @param int $position
     *
     * @return mixed<commit_msg>Fixed #556<commit_after><?php

namespace SleepingOwl\Admin\Traits;

use Illuminate\Database\Query\Builder;
use Illuminate\Database\Eloquent\Model;

trait OrderableModel
{
    /**
     * Boot trait.
     */
    protected static function bootOrderableModel()
    {
        static::creating(function (Model $row) {
            $row->updateOrderFieldOnCreate();
        });

        static::deleted(function (Model $row) {
            $row->updateOrderFieldOnDelete();
        });

        static::restored(function (Model $row) {
            $row->updateOrderFieldOnRestore();
        });
    }

    /**
     * Get order value.
     * @return int
     */
    public function getOrderValue()
    {
        return $this->{$this->getOrderField()};
    }

    /**
     * Move model up.
     */
    public function moveUp()
    {
        $this->move(1);
    }

    /**
     * Move model down.
     */
    public function moveDown()
    {
        $this->move(-1);
    }

    /**
     * Move model in the $destination.
     *
     * @param $destination -1 (move down) or 1 (move up)
     */
    protected function move($destination)
    {
        if ($previousRow = static::orderModel()->findByPosition($this->getOrderValue() - $destination)->first()) {
            $previousRow->{$this->getOrderField()} += $destination;
            $previousRow->save();
        }

        $this->{$this->getOrderField()} -= $destination;
        $this->save();
    }

    /**
     * Update order field on create.
     */
    protected function updateOrderFieldOnCreate()
    {
        $this->{$this->getOrderField()} = static::orderModel()->count();
    }

    /**
     * Update order field on delete.
     */
    protected function updateOrderFieldOnDelete()
    {
        static::orderModel()
              ->where($this->getOrderField(), '>', $this->getOrderValue())
              ->decrement($this->getOrderField());
    }

    /**
     * Update order field on restore.
     */
    protected function updateOrderFieldOnRestore()
    {
        static::orderModel()
            ->where($this->getOrderField(), '>', $this->getOrderValue())
            ->increment($this->getOrderField());
    }

    /**
     * Order scope.
     *
     * @param $query
     *
     * @return mixed
     */
    public function scopeOrderModel($query)
    {
        return $query;
    }

    /**
     * @param Builder $query
     * @param int $position
     *
     * @return mixed
{'fraction_non_alphanumeric': 0.15314514336519228, 'fraction_numerical': 0.0024449877750611247, 'mean_word_length': 1.9277813923227065, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2203999', 'n_tokens_mistral': 1305, 'n_tokens_neox': 1301, 'n_words': 246}