Skip to content

Commit

Permalink
Fix #18
Browse files Browse the repository at this point in the history
Added polymorphic relation to subscription, this allows multipe subscribable models at the same time.
  • Loading branch information
gerardojbaez committed Nov 15, 2017
1 parent a1db78c commit d4d2ebc
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
### Added
- Fix #18

## [1.0.0] - 2016-03-05
### Added
- This change log file
Expand Down
2 changes: 1 addition & 1 deletion src/LaraPlans/Contracts/PlanSubscriptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

interface PlanSubscriptionInterface
{
public function user();
public function subscribable();
public function plan();
public function usage();
public function getStatusAttribute();
Expand Down
7 changes: 3 additions & 4 deletions src/LaraPlans/Models/PlanSubscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class PlanSubscription extends Model implements PlanSubscriptionInterface
* @var array
*/
protected $fillable = [
'user_id',
'plan_id',
'name',
'trial_ends_at',
Expand Down Expand Up @@ -77,13 +76,13 @@ protected static function boot()
}

/**
* Get user.
* Get the subscribable of the model.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
public function subscribable()
{
return $this->belongsTo(config('auth.providers.users.model'));
return $this->morphTo();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/LaraPlans/Traits/PlanSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function subscription($name = 'default')
*/
public function subscriptions()
{
return $this->hasMany(config('laraplans.models.plan_subscription'));
return $this->morphMany(config('laraplans.models.plan_subscription'), 'subscribable');
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/database/factories/PlanSubscriptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

$factory->define(PlanSubscription::class, function (Faker\Generator $faker) {
return [
'user_id' => factory(User::class)->create()->id,
'subscribable_id' => factory(User::class)->create()->id,
'subscribable_type' => User::class,
'plan_id' => factory(Plan::class)->create()->id,
'name' => $faker->word
];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

// @codingStandardsIgnoreFile

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

Expand All @@ -14,7 +16,8 @@ public function up()
{
Schema::create('plan_subscriptions', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('subscribable_id')->unsigned()->index();
$table->string('subscribable_type')->index();
$table->integer('plan_id')->unsigned();
$table->string('name');
$table->timestamp('trial_ends_at')->nullable();
Expand All @@ -23,7 +26,6 @@ public function up()
$table->timestamp('canceled_at')->nullable();
$table->timestamps();

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('plan_id')->references('id')->on('plans')->onDelete('cascade');
});
}
Expand Down
6 changes: 4 additions & 2 deletions tests/integration/Models/PlanSubscriptionTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

// @codingStandardsIgnoreFile

namespace Gerarodjbaez\LaraPlans\Tests\Integration\Models;

use Config;
Expand Down Expand Up @@ -71,9 +73,9 @@ public function setUp()
* @test
* @return void
*/
public function it_can_get_subscription_user()
public function it_gets_subscribable_model_instance()
{
$this->assertInstanceOf(config('auth.providers.users.model'), $this->subscription->user);
$this->assertInstanceOf(User::class, $this->subscription->subscribable);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/integration/SubscriptionAbilityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function it_can_check_feature_usage()
new PlanFeature(['code' => 'pictures_per_listing', 'value' => 10]),
new PlanFeature(['code' => 'listing_title_bold', 'value' => 'N']),
new PlanFeature(['code' => 'listing_video', 'value' => 'Y']),
new PlanFeature(['code' => 'non_positive_word', 'value' => 'I']), // test for non-positive words
]);

// Create Subscription
Expand All @@ -49,5 +50,6 @@ public function it_can_check_feature_usage()
$this->assertEquals('N', $user->subscription('main')->ability()->value('listing_title_bold'));
$this->assertFalse($user->subscription('main')->ability()->enabled('listing_title_bold'));
$this->assertTrue($user->subscription('main')->ability()->enabled('listing_video'));
$this->assertFalse($user->subscription('main')->ability()->canUse('non_positive_word'));
}
}

1 comment on commit d4d2ebc

@josteph
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you forgot to change the scopeByUser method:

   /**
     * Find by user id.
     *
     * @param  \Illuminate\Database\Eloquent\Builder
     * @param  int $user_id
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeByUser($query, $id)
    {
        return $query->where('subscribable_id', $id); // previously was user_id
    }

Please sign in to comment.