Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
R
rankable
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
CI / CD
CI / CD
Pipelines
Schedules
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
terranet
rankable
Commits
19a489be
Commit
19a489be
authored
Oct 17, 2016
by
endi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix create/update methods
parent
435456cb
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
31 deletions
+28
-31
HasRankableField.php
src/Terranet/Rankable/HasRankableField.php
+24
-29
Rankable.php
src/Terranet/Rankable/Rankable.php
+3
-1
RankableScope.php
src/Terranet/Rankable/RankableScope.php
+1
-1
No files found.
src/Terranet/Rankable/HasRankableField.php
View file @
19a489be
<?php
namespace
Terranet\Rankable
;
<?php
namespace
Terranet\Rankable
;
trait
HasRankableField
{
...
...
@@ -42,13 +44,10 @@ trait HasRankableField
*/
public
function
syncRanking
(
array
$ranking
=
[])
{
foreach
((
array
)
$ranking
as
$id
=>
$value
)
{
foreach
((
array
)
$ranking
as
$id
=>
$value
)
{
$this
->
where
(
$this
->
getKeyName
(),
'='
,
$id
)
->
update
([
$this
->
getRankableColumn
()
=>
(
int
)
$value
]);
->
update
([
$this
->
getRankableColumn
()
=>
(
int
)
$value
]);
}
}
...
...
@@ -56,51 +55,47 @@ trait HasRankableField
{
static
::
addGlobalScope
(
new
RankableScope
);
static
::
updating
(
function
(
Rankable
$model
)
{
if
(
$model
->
isDirty
(
$model
->
getRankableColumn
())
&&
$oldValue
=
$model
->
getOriginal
(
$model
->
getRankableColumn
()))
{
static
::
updating
(
function
(
Rankable
$model
)
{
if
(
$model
->
isDirty
(
$model
->
getRankableColumn
())
&&
$oldValue
=
$model
->
getOriginal
(
$model
->
getRankableColumn
()))
{
// switch ranks in conflicts
$query
=
$model
->
newQuery
();
$query
=
$model
->
applyRankGrouping
(
$query
,
$model
);
$query
=
$model
->
rankableQuery
(
$model
);
$query
->
where
(
$model
->
getRankableColumn
(),
'='
,
(
int
)
$model
->
getAttribute
(
$model
->
getRankableColumn
()));
$query
->
update
([
$model
->
getRankableColumn
()
=>
$oldValue
]);
$query
->
update
([
$model
->
getRankableColumn
()
=>
$oldValue
]);
}
});
static
::
creating
(
function
(
Rankable
$model
)
{
static
::
creating
(
function
(
Rankable
$model
)
{
$field
=
$model
->
getRankableColumn
();
if
(
!
$model
->
{
$field
})
{
$query
=
$model
->
newQuery
();
$query
=
$model
->
applyRankGrouping
(
$query
,
$model
);
if
(
!
$model
->
{
$field
})
{
$query
=
$model
->
rankableQuery
(
$model
);
$value
=
(
int
)
$query
->
max
(
$field
);
$model
->
{
$field
}
=
(
$value
+
$model
->
getRankableIncrementValue
());
$model
->
setAttribute
(
$field
,
$value
+
$model
->
getRankableIncrementValue
());
}
});
}
protected
function
applyRankGrouping
(
$query
,
$model
)
{
if
(
$columns
=
(
array
)
$model
->
getRankableGroupByColumn
())
{
foreach
(
$columns
as
$column
)
{
if
(
$columns
=
(
array
)
$model
->
getRankableGroupByColumn
())
{
foreach
(
$columns
as
$column
)
{
$query
->
where
(
$column
,
'='
,
$model
->
{
$column
});
}
}
return
$query
;
}
/**
* @param Rankable $model
* @return mixed
*/
protected
function
rankableQuery
(
Rankable
$model
)
{
return
$model
->
applyRankGrouping
(
$model
->
newQueryWithoutScopes
(),
$model
);
}
}
\ No newline at end of file
src/Terranet/Rankable/Rankable.php
View file @
19a489be
<?php
namespace
Terranet\Rankable
;
<?php
namespace
Terranet\Rankable
;
interface
Rankable
{
...
...
src/Terranet/Rankable/RankableScope.php
View file @
19a489be
...
...
@@ -2,7 +2,7 @@
use
Illuminate\Database\Eloquent\Builder
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\ScopeInterface
;
use
Illuminate\Database\Eloquent\Scope
as
Scope
Interface
;
class
RankableScope
implements
ScopeInterface
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment