Skip to content

Commit

Permalink
redo relations for custom attribute values and inventory items with p…
Browse files Browse the repository at this point in the history
…ivot, add ability to get all custom attribute values for an inventory item
  • Loading branch information
dvicklund committed Mar 6, 2022
1 parent 04bb053 commit a6c3123
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 14 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"php": ">=7.3.0",
"laravel/framework": "^9.0",
"gazsp/baum": "^2.0"

},
"require-dev": {
"phpunit/phpunit": "^9.5.10",
Expand Down
14 changes: 9 additions & 5 deletions src/Models/CustomAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ class CustomAttribute extends BaseModel
];

/**
* The belongsToMany inventories relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
* The BelongsToMany customAttributes relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function inventories()
{
return $this->belongsToMany(Inventory::class, 'custom_attribute_values', 'custom_attribute_id', 'inventory_id');
return $this->belongsToMany(Inventory::class, 'custom_attribute_values', 'custom_attribute_id', 'inventory_id')
->withPivot("string_val", "num_val", "date_val")
->as("values")
// ->using(CustomAttributeValue::class)
->withTimestamps();
}

/**
Expand Down
16 changes: 8 additions & 8 deletions src/Models/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,17 @@ public function bundles()
}

/**
* The hasManyThrough customAttributes relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
* The BelongsToMany customAttributes relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
// public function customAttributes()
// {
// return $this->hasManyThrough(Attribute::class, AttributeValue::class, 'attribute_id', 'id', 'attribute_id', 'attribute_id');
// }
public function customAttributes()
{
return $this->belongsToMany(CustomAttribute::class, 'custom_attribute_values', 'inventory_id', 'custom_attribute_id');
return $this->belongsToMany(CustomAttribute::class, 'custom_attribute_values', 'inventory_id', 'custom_attribute_id')
->withPivot("string_val", "num_val", "date_val")
->as("values")
// ->using(CustomAttributeValues::class)
->withTimestamps();
}

/**
Expand Down
20 changes: 20 additions & 0 deletions src/Traits/CustomAttributeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,23 @@ public function getCustomAttributes()
return $this->customAttributes()->get();
}

/**
* Returns the list of all custom attribute values
* for this inventory item
*
* @return Collection
*/
public function getCustomAttributeValues()
{
$attrVals = $this->customAttributes()->get()->reduce(function($carry, $item) {
if (!$carry) return [$item->name => $item->values[$item->value_type.'_val']];
else $carry[$item->name] = $item->values[$item->value_type.'_val'];
return $carry;
});

return $attrVals;
}

/**
* Adds a new custom attribute to this inventory item
*
Expand Down Expand Up @@ -538,6 +555,9 @@ public function getCustomAttributeDefault($attr)
*/
public function getCustomAttributeValueObj($attr)
{
// If called with no attribute to find, don't bother looking
// if (!$attr) return false;

try {
$attrObj = $this->resolveCustomAttributeObject($attr);

Expand Down
24 changes: 24 additions & 0 deletions tests/CustomAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,36 @@ public function testCanAddExistingCustomAttributeToNewInventoryItemByModel()
$this->assertEquals('Another value', $item2->getCustomAttributeValue($attr));
}

public function testCanGetAllCustomAttributesForAnInventoryItem() {
$item = $this->newInventory();

$attr1 = $item->addCustomAttribute('string', 'A fresh as heck property');
$item->setCustomAttribute($attr1, 'string value');

$attr2 = $item->addCustomAttribute('integer', 'Another super fresh prop but numeric this time');
$item->setCustomAttribute($attr2, 42);

$attr3 = $item->addCustomAttribute('date', 'The time and day of these most freshest of properties');
$item->setCustomAttribute($attr3, '3-1-2021');

$allAttrs = $item->getCustomAttributes();
$allAttrVals = $item->getCustomAttributeValues();

$this->assertCount(3, $allAttrs);
$this->assertCount(3, $allAttrVals);
$this->assertEquals($allAttrVals['a_fresh_as_heck_property'], 'string value');
$this->assertEquals($allAttrVals['another_super_fresh_prop_but_numeric_this_time'], 42);
$this->assertEquals(strtotime($allAttrVals['the_time_and_day_of_these_most_freshest_of_properties']), strtotime('3-1-2021'));
}



/*
* "Cannot" tests
*/



public function testCannotAddCustomAttributeWithInvalidRegexRule()
{
$item = $this->newInventory();
Expand Down
2 changes: 2 additions & 0 deletions tests/FunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ private static function migrateTables()
$table->string('string_val', 8191)->nullable();
$table->decimal('num_val', 16, 4)->nullable(); // 123,456,789,012.3456
$table->dateTime('date_val')->nullable();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrent()->useCurrentOnUpdate();

$table->foreign('inventory_id')->references('id')->on('inventories')->onUpdate('restrict');
$table->foreign('custom_attribute_id')->references('id')->on('custom_attributes')->onUpdate('restrict');
Expand Down

0 comments on commit a6c3123

Please sign in to comment.