Skip to content

Commit

Permalink
Version Patch
Browse files Browse the repository at this point in the history
- Update dependency to support Laravel 8
- Add [CHANGELOG.md](CHANGELOG.md)
- Add composer scripts for testing and code fixing
- Add ignore of .php_cs.cache
- Apply PSR style formatting
  • Loading branch information
cyrillkalita committed Sep 26, 2020
1 parent 5625212 commit 8e2cc37
Show file tree
Hide file tree
Showing 13 changed files with 388 additions and 337 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
composer.lock
/vendor
.phpintel/*
.phpunit.result.cache
.phpunit.result.cache
.php_cs.cache
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Changelog

All notable changes to `rtconner/laravel-likeable` will be documented in this file

## 2020-09-26

- Update dependency to support Laravel 8
- Add [CHANGELOG.md](CHANGELOG.md)
- Add composer scripts for testing and code fixing
- Add ignore of .php_cs.cache
- Apply PSR style formatting
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ Important Note: As of version 1.2 I renamed `Conner\Likeable\LikeableTrait` to `

Trait for Laravel Eloquent models to allow easy implementation of a "like" or "favorite" or "remember" feature.

[Laravel 5/6/7 Documentation](https://github.com/rtconner/laravel-likeable/tree/laravel-7)
[Laravel 5/6/7/8 Documentation](https://github.com/rtconner/laravel-likeable/tree/laravel-7)
[Laravel 4 Documentation](https://github.com/rtconner/laravel-likeable/tree/laravel-4)

#### Composer Install (for Laravel 5)
#### Composer Install

composer require rtconner/laravel-likeable "~3.0"
composer require rtconner/laravel-likeable

#### Then run the migrations

Expand All @@ -26,7 +26,7 @@ php artisan migrate

```php
class Article extends \Illuminate\Database\Eloquent\Model {
use \Conner\Likeable\LikeableTrait;
use \Conner\Likeable\Likeable;
}
```

Expand All @@ -43,7 +43,7 @@ $article->unlike(0); // remove likes from the count -- does not check for user

$article->likeCount; // get count of likes

$article->likes; // Iterable Illuminate\Database\Eloquent\Collection of existing likes
$article->likes; // Iterable Illuminate\Database\Eloquent\Collection of existing likes

$article->liked(); // check if currently logged in user liked the article
$article->liked($myUserId);
Expand Down
19 changes: 15 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
],
"require": {
"php": ">=5.6.0",
"illuminate/database": ">=5.0|^6.0|^7.0",
"illuminate/support": ">=5.0|^6.0|^7.0"
"illuminate/database": ">=5.0|^6.0|^7.0|^8.0",
"illuminate/support": ">=5.0|^6.0|^7.0|^8.0"
},
"require-dev": {
"orchestra/testbench": "5.*",
"phpunit/phpunit": "8.*",
"orchestra/testbench": "5.*|6.*",
"phpunit/phpunit": "8.*|9.*",
"mockery/mockery": "1.*"
},
"autoload": {
Expand All @@ -30,6 +30,17 @@
"Conner\\Tests\\Likeable\\": "tests/"
}
},
"scripts": {
"test": "vendor/bin/phpunit --color=always",
"check": [
"php-cs-fixer fix --ansi --dry-run --diff .",
"phpcs --report-width=200 --report-summary --report-full src/ tests/ --standard=PSR2 -n",
"phpmd src/,tests/ text ./phpmd.xml.dist"
],
"fix": [
"php-cs-fixer fix --ansi ."
]
},
"extra": {
"laravel": {
"providers": [
Expand Down
49 changes: 24 additions & 25 deletions migrations/2016_02_07_000000_create_likeable_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,29 @@

class CreateLikeableTables extends Migration
{
public function up()
{
Schema::create('likeable_likes', function(Blueprint $table) {
$table->bigIncrements('id');
$table->string('likeable_id', 36);
$table->string('likeable_type', 255);
$table->string('user_id', 36)->index();
$table->timestamps();
$table->unique(['likeable_id', 'likeable_type', 'user_id'], 'likeable_likes_unique');
});

Schema::create('likeable_like_counters', function(Blueprint $table) {
$table->bigIncrements('id');
$table->string('likeable_id', 36);
$table->string('likeable_type', 255);
$table->unsignedBigInteger('count')->default(0);
$table->unique(['likeable_id', 'likeable_type'], 'likeable_counts');
});

}
public function up()
{
Schema::create('likeable_likes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('likeable_id', 36);
$table->string('likeable_type', 255);
$table->string('user_id', 36)->index();
$table->timestamps();
$table->unique(['likeable_id', 'likeable_type', 'user_id'], 'likeable_likes_unique');
});

Schema::create('likeable_like_counters', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('likeable_id', 36);
$table->string('likeable_type', 255);
$table->unsignedBigInteger('count')->default(0);
$table->unique(['likeable_id', 'likeable_type'], 'likeable_counts');
});
}

public function down()
{
Schema::drop('likeable_likes');
Schema::drop('likeable_like_counters');
}
public function down()
{
Schema::drop('likeable_likes');
Schema::drop('likeable_like_counters');
}
}
23 changes: 23 additions & 0 deletions phpmd.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8" ?>
<ruleset
name="ProxyManager rules"
xmlns="http://pmd.sf.net/ruleset/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd"
xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd"
>
<rule ref="rulesets/codesize.xml"/>
<rule ref="rulesets/unusedcode.xml"/>
<rule ref="rulesets/design.xml">
<!-- eval is needed to generate runtime classes -->
<exclude name="EvalExpression"/>
</rule>
<rule ref="rulesets/naming.xml">
<exclude name="LongVariable"/>
</rule>
<rule ref="rulesets/naming.xml/LongVariable">
<properties>
<property name="minimum">40</property>
</properties>
</rule>
</ruleset>
16 changes: 8 additions & 8 deletions src/Like.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
*/
class Like extends Eloquent
{
protected $table = 'likeable_likes';
public $timestamps = true;
protected $fillable = ['likeable_id', 'likeable_type', 'user_id'];
protected $table = 'likeable_likes';
public $timestamps = true;
protected $fillable = ['likeable_id', 'likeable_type', 'user_id'];

/**
* @access private
*/
public function likeable()
{
return $this->morphTo();
}
}
public function likeable()
{
return $this->morphTo();
}
}
49 changes: 24 additions & 25 deletions src/LikeCounter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,38 @@
*/
class LikeCounter extends Eloquent
{
protected $table = 'likeable_like_counters';
public $timestamps = false;
protected $fillable = ['likeable_id', 'likeable_type', 'count'];
protected $table = 'likeable_like_counters';
public $timestamps = false;
protected $fillable = ['likeable_id', 'likeable_type', 'count'];

/**
* @access private
*/
public function likeable()
{
return $this->morphTo();
}
public function likeable()
{
return $this->morphTo();
}

/**
* Delete all counts of the given model, and recount them and insert new counts
*
* @param $modelClass
*/
public static function rebuild($modelClass)
{
if(empty($modelClass)) {
public static function rebuild($modelClass)
{
if (empty($modelClass)) {
throw new \Exception('$modelClass cannot be empty/null. Maybe set the $morphClass variable on your model.');
}

$builder = Like::query()
->select(\DB::raw('count(*) as count, likeable_type, likeable_id'))
->where('likeable_type', $modelClass)
->groupBy('likeable_id');

$results = $builder->get();

$inserts = $results->toArray();

\DB::table((new static)->table)->insert($inserts);
}

}
}

$builder = Like::query()
->select(\DB::raw('count(*) as count, likeable_type, likeable_id'))
->where('likeable_type', $modelClass)
->groupBy('likeable_id');

$results = $builder->get();

$inserts = $results->toArray();

\DB::table((new static)->table)->insert($inserts);
}
}
Loading

0 comments on commit 8e2cc37

Please sign in to comment.