Skip to content

Commit

Permalink
Merge branch '1.x' into lun-156-unable-to-create-order
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson authored Sep 17, 2024
2 parents a3fb640 + 9eac4c7 commit 3089660
Show file tree
Hide file tree
Showing 28 changed files with 51 additions and 40 deletions.
2 changes: 1 addition & 1 deletion packages/admin/src/Filament/Resources/ProductResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public static function getBaseNameFormComponent(): Component
{
$nameType = Attribute::whereHandle('name')
->whereAttributeType(
(new (static::getModel()))->getMorphClass()
static::getModel()::morphName()
)
->first()?->type ?: TranslatedText::class;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static function createRecord(array $data, string $model): Model
$currency = Currency::getDefault();

$nameAttribute = Attribute::whereAttributeType(
(new $model)->getMorphClass()
$model::morphName()
)
->whereHandle('name')
->first()
Expand Down
4 changes: 2 additions & 2 deletions packages/admin/src/Filament/Resources/ProductTypeResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ public static function getDefaultForm(Forms\Form $form): Forms\Form
Forms\Components\Tabs\Tab::make(__('lunarpanel::producttype.tabs.product_attributes.label'))
->schema([
AttributeSelector::make('mappedAttributes')
->withType((new Product)->getMorphClass())
->withType(Product::morphName())
->relationship(name: 'mappedAttributes')
->label('')
->columnSpan(2),
]),
Forms\Components\Tabs\Tab::make(__('lunarpanel::producttype.tabs.variant_attributes.label'))
->schema([
AttributeSelector::make('mappedAttributes')
->withType((new ProductVariant)->getMorphClass())
->withType(ProductVariant::morphName())
->relationship(name: 'mappedAttributes')
->label('')
->columnSpan(2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function setUp(): void
$this->success();
});

$attribute = Attribute::where('attribute_type', '=', (new Collection)->getMorphClass())
$attribute = Attribute::where('attribute_type', '=', Collection::morphName())
->where('handle', '=', 'name')->first();

$formInput = TextInput::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function setUp(): void

$record = $this->process(function (array $data) {
$attribute = Attribute::whereHandle('name')->whereAttributeType(
(new Collection)->getMorphClass()
Collection::morphName()
)->first()->type;

return Collection::create([
Expand Down Expand Up @@ -58,7 +58,7 @@ public function setUp(): void
$this->success();
});

$attribute = Attribute::where('attribute_type', '=', (new Collection)->getMorphClass())
$attribute = Attribute::where('attribute_type', '=', Collection::morphName())
->where('handle', '=', 'name')->first();

$formInput = TextInput::class;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function createChildCollection(Collection $parent, array|string $name)
DB::beginTransaction();

$attribute = Attribute::whereHandle('name')->whereAttributeType(
(new Collection)->getMorphClass()
Collection::morphName()
)->first()->type;

$parent->appendNode(Collection::create([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public function getAttributeGroups()
$this->getRelationship()->getParent()
);

if ($type === (new ProductType)->getMorphClass()) {
$type = (new Product)->getMorphClass();
if ($type === ProductType::morphName()) {
$type = Product::morphName();
}

if ($this->attributableType) {
Expand Down
6 changes: 3 additions & 3 deletions packages/admin/src/Support/Forms/Components/Attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ protected function setUp(): void

$productTypeId = null;

$morphMap = (new $modelClass)->getMorphClass();
$morphMap = $modelClass::morphName();

$attributeQuery = Attribute::where('attribute_type', $morphMap);

// Products are unique in that they use product types to map attributes, so we need
// to try and find the product type ID
if ($morphMap == (new Product)->getMorphClass()) {
if ($morphMap == Product::morphName()) {
$productTypeId = $record?->product_type_id ?: ProductType::first()->id;

// If we have a product type, the attributes should be based off that.
Expand All @@ -41,7 +41,7 @@ protected function setUp(): void
}
}

if ($morphMap == (new ProductVariant)->getMorphClass()) {
if ($morphMap == ProductVariant::morphName()) {
$productTypeId = $record?->product?->product_type_id ?: ProductType::first()->id;
// If we have a product type, the attributes should be based off that.
if ($productTypeId) {
Expand Down
2 changes: 1 addition & 1 deletion packages/admin/src/Support/Resources/BaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ protected static function applyGlobalSearchAttributeConstraints(Builder $query,
protected static function mapSearchableAttributes(array &$map)
{
$attributes = Attribute::whereAttributeType(
(new (static::getModel()))->getMorphClass()
static::getModel()::morphName()
)
->whereSearchable(true)
->get();
Expand Down
2 changes: 1 addition & 1 deletion packages/core/database/factories/CartLineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function definition(): array
return [
'cart_id' => Cart::factory(),
'quantity' => $this->faker->numberBetween(0, 1000),
'purchasable_type' => (new ProductVariant)->getMorphClass(),
'purchasable_type' => ProductVariant::morphName(),
'purchasable_id' => ProductVariant::factory(),
'meta' => null,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public function definition(): array
{
return [
'purchasable_id' => ProductVariant::factory(),
'purchasable_type' => (new ProductVariant)->getMorphClass(),
'purchasable_type' => ProductVariant::morphName(),
];
}
}
2 changes: 1 addition & 1 deletion packages/core/database/factories/OrderLineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function definition(): array
{
return [
'order_id' => Order::factory(),
'purchasable_type' => (new ProductVariant)->getMorphClass(),
'purchasable_type' => ProductVariant::morphName(),
'purchasable_id' => ProductVariant::factory(),
'type' => 'physical',
'description' => $this->faker->sentence,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/database/factories/UrlFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function definition(): array
'slug' => $this->faker->slug,
'default' => true,
'language_id' => Language::factory(),
'element_type' => (new Product)->getMorphClass(),
'element_type' => Product::morphName(),
'element_id' => 1,
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public function run()

DB::table("{$prefix}attributes")
->whereAttributeType(
(new ProductType)->getMorphClass()
ProductType::morphName()
)
->update([
'attribute_type' => 'product',
]);

DB::table("{$prefix}attribute_groups")
->whereAttributableType(
(new ProductType)->getMorphClass()
ProductType::morphName()
)
->update([
'attributable_type' => 'product',
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Base/Traits/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function getAttributableClassnameAttribute()

public function getAttributableMorphMapAttribute()
{
return (new self)->getMorphClass();
return self::morphName();
}

/**
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/Base/Traits/HasModelExtending.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ public static function modelClass(): string
return ModelManifest::get($contractClass) ?? static::class;
}

/**
* Returns the model alias registered in the model relation morph map.
*/
public static function morphName():string{
return (new (static::modelClass()))->getMorphClass();
}

public function getMorphClass(): string
{
$morphMap = Relation::morphMap();
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/Console/InstallLunar.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function handle(): void
$this->components->info('Setting up initial attributes');

$group = AttributeGroup::create([
'attributable_type' => (new Product)->getMorphClass(),
'attributable_type' => Product::morphName(),
'name' => collect([
'en' => 'Details',
]),
Expand All @@ -149,7 +149,7 @@ public function handle(): void
]);

$collectionGroup = AttributeGroup::create([
'attributable_type' => (new Collection)->getMorphClass(),
'attributable_type' => Collection::morphName(),
'name' => collect([
'en' => 'Details',
]),
Expand Down Expand Up @@ -251,7 +251,7 @@ public function handle(): void

$type->mappedAttributes()->attach(
Attribute::whereAttributeType(
(new Product)->getMorphClass()
Product::morphName()
)->get()->pluck('id')
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/DiscountTypes/BuyXGetY.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function apply(Cart $cart): Cart
// Get all purchasables that are eligible.
$conditions = $cart->lines->reject(function ($line) {
return ! $this->discount->purchasableConditions->first(function ($item) use ($line) {
return $item->purchasable_type == (new Product)->getMorphClass() &&
return $item->purchasable_type == Product::morphName() &&
$item->purchasable_id == $line->purchasable->product->id;
});
});
Expand Down Expand Up @@ -89,7 +89,7 @@ public function apply(Cart $cart): Cart
// Get the reward lines and sort by cheapest first.
$rewardLines = $cart->lines->filter(function ($line) {
return $this->discount->purchasableRewards->first(function ($item) use ($line) {
return $item->purchasable_type == (new Product)->getMorphClass() &&
return $item->purchasable_type == Product::morphName() &&
$item->purchasable_id == $line->purchasable->product->id;
});
})->sortBy('subTotal.value');
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/Models/Discount.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public function scopeProducts(Builder $query, iterable $productIds = [], array|s
fn ($subQuery) => $subQuery->whereDoesntHave('purchasables', fn ($query) => $query->when($types, fn ($query) => $query->whereIn('type', $types)))
->orWhereHas('purchasables',
fn ($relation) => $relation->whereIn('purchasable_id', $productIds)
->wherePurchasableType((new Product)->getMorphClass())
->wherePurchasableType(Product::morphName())
->when(
$types,
fn ($query) => $query->whereIn('type', $types)
Expand All @@ -200,7 +200,7 @@ public function scopeProductVariants(Builder $query, iterable $variantIds = [],
fn ($subQuery) => $subQuery->whereDoesntHave('purchasables', fn ($query) => $query->when($types, fn ($query) => $query->whereIn('type', $types)))
->orWhereHas('purchasables',
fn ($relation) => $relation->whereIn('purchasable_id', $variantIds)
->wherePurchasableType((new ProductVariant)->getMorphClass())
->wherePurchasableType(ProductVariant::morphName())
->when(
$types,
fn ($query) => $query->whereIn('type', $types)
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/Models/ProductType.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public function mappedAttributes(): MorphToMany
public function productAttributes(): MorphToMany
{
return $this->mappedAttributes()->whereAttributeType(
(new Product)->getMorphClass()
Product::morphName()
);
}

public function variantAttributes(): MorphToMany
{
return $this->mappedAttributes()->whereAttributeType(
(new ProductVariant)->getMorphClass()
ProductVariant::morphName()
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function definition(): array
{
return [
'purchasable_id' => 1,
'purchasable_type' => (new Product)->getMorphClass(),
'purchasable_type' => Product::morphName(),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function resolve(ShippingOptionRequest $shippingOptionRequest): ?Shipping

$hasExclusions = $shippingZone->shippingExclusions()
->whereHas('exclusions', function ($query) use ($productIds) {
$query->wherePurchasableType((new Product)->getMorphClass())
$query->wherePurchasableType(Product::morphName())
->whereIn('purchasable_id', $productIds);
})->exists();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function resolve(ShippingOptionRequest $shippingOptionRequest): ?Shipping

$hasExclusions = $shippingZone->shippingExclusions()
->whereHas('exclusions', function ($query) use ($productIds) {
$query->wherePurchasableType((new Product)->getMorphClass())
$query->wherePurchasableType(Product::morphName())
->whereIn('purchasable_id', $productIds);
})->exists();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function resolve(ShippingOptionRequest $shippingOptionRequest): ?Shipping

$hasExclusions = $shippingZone->shippingExclusions()
->whereHas('exclusions', function ($query) use ($productIds) {
$query->wherePurchasableType((new Product)->getMorphClass())
$query->wherePurchasableType(Product::morphName())
->whereIn('purchasable_id', $productIds);
})->exists();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function resolve(ShippingOptionRequest $shippingOptionRequest): ?Shipping

$hasExclusions = $shippingZone->shippingExclusions()
->whereHas('exclusions', function ($query) use ($productIds) {
$query->wherePurchasableType((new Product)->getMorphClass())
$query->wherePurchasableType(Product::morphName())
->whereIn('purchasable_id', $productIds);
})->exists();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
->assertHasNoFormErrors();

$this->assertDatabaseHas((new ProductType)->mappedAttributes()->getTable(), [
'attributable_type' => (new ProductType)->getMorphClass(),
'attributable_type' => ProductType::morphName(),
'attributable_id' => $component->get('record')->id,
]);
});
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@
->assertHasNoFormErrors();

$this->assertDatabaseHas((new ProductType)->mappedAttributes()->getTable(), [
'attributable_type' => (new ProductType)->getMorphClass(),
'attributable_type' => ProductType::morphName(),
'attributable_id' => $component->get('record')->id,
'attribute_id' => $attributeA->id,
]);

$this->assertDatabaseHas((new ProductType)->mappedAttributes()->getTable(), [
'attributable_type' => (new ProductType)->getMorphClass(),
'attributable_type' => ProductType::morphName(),
'attributable_id' => $component->get('record')->id,
'attribute_id' => $attributeB->id,
]);
Expand All @@ -46,13 +46,13 @@
->assertHasNoFormErrors();

$this->assertDatabaseHas((new ProductType)->mappedAttributes()->getTable(), [
'attributable_type' => (new ProductType)->getMorphClass(),
'attributable_type' => ProductType::morphName(),
'attributable_id' => $component->get('record')->id,
'attribute_id' => $attributeA->id,
]);

$this->assertDatabaseMissing((new ProductType)->mappedAttributes()->getTable(), [
'attributable_type' => (new ProductType)->getMorphClass(),
'attributable_type' => ProductType::morphName(),
'attributable_id' => $component->get('record')->id,
'attribute_id' => $attributeB->id,
]);
Expand Down
4 changes: 4 additions & 0 deletions tests/core/Unit/Base/Traits/HasModelExtendingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ function () {
);

expect((new \Lunar\Tests\Core\Stubs\Models\CustomProduct)->getMorphClass())
->toBe('product')
->and(\Lunar\Tests\Core\Stubs\Models\CustomProduct::morphName())
->toBe('product')
->and((new Product)->getMorphClass())
->toBe('product')
->and(Product::morphName())
->toBe('product');
});

0 comments on commit 3089660

Please sign in to comment.