Skip to content

Commit

Permalink
Dev (#26)
Browse files Browse the repository at this point in the history
* refactor defaults for custom attributes

* add remove default test

* remove dead code
  • Loading branch information
dvicklund authored Mar 6, 2022
1 parent 994f7b1 commit adfa14e
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 196 deletions.
14 changes: 1 addition & 13 deletions src/Models/CustomAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CustomAttribute extends BaseModel
'reserved',
'display_type',
'has_default',
'default_value',
];

/**
Expand All @@ -43,17 +44,4 @@ public function customAttributeValues()
{
return $this->hasMany(CustomAttributeValue::class, 'custom_attribute_id');
}

/**
* TODO: REMOVE THIS CLASS
* TODO: bake this functionality into the CustomAttribute model & table
*
* The belongsToMany attributeDefaultValue relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function customAttributeDefault()
{
return $this->hasMany(CustomAttributeDefault::class, 'custom_attribute_id');
}
}
44 changes: 0 additions & 44 deletions src/Models/CustomAttributeDefault.php

This file was deleted.

13 changes: 0 additions & 13 deletions src/Models/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,21 +123,8 @@ public function customAttributes()
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/

public function customAttributeValues()
{
return $this->hasMany(CustomAttributeValue::class, 'inventory_id');
}

/**
* TODO: Remove this relationship and bake into the CustomAttribute model & class
* The hasMany attributeValues relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/

public function customAttributeDefaults()
{
return $this->hasMany(CustomAttributeDefault::class, 'inventory_id');
}
}
117 changes: 28 additions & 89 deletions src/Traits/CustomAttributeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@ trait CustomAttributeTrait
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
abstract public function customAttributeValues();

/**
* TODO: refactor this relationship out in favor of CustomAttribute->default_val
* The hasMany customAttributeDefaults relationship.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
abstract public function customAttributeDefaults();

/**
* Returns true if item has given custom attribute, or
Expand Down Expand Up @@ -98,17 +90,6 @@ public function getCustomAttributes()
return $this->customAttributes()->get();
}

/**
* Returns the list of all custom attribute value
* models for this inventory item
*
* @return Collection
*/
public function getCustomAttributeValueObjects()
{
return $this->customAttributeValues()->get();
}

/**
* Returns a formatted list of all custom attribute
* values for this inventory item with the format
Expand Down Expand Up @@ -309,6 +290,7 @@ private function createCustomAttribute($name, $displayName, $rawType, $type, $ha
'reserved' => false,
'display_type' => $type,
'has_default' => $hasDefault,
'default_value' => $defaultValue,
'required' => $required,
'rule' => $rule,
'rule_desc' => $ruleDesc,
Expand Down Expand Up @@ -337,9 +319,6 @@ private function createCustomAttribute($name, $displayName, $rawType, $type, $ha
*/
public function setCustomAttribute($attr, $value, $type = null)
{
/**
* TODO: Ability to add existing custom attribute to inventory item that does not have it assigned already
*/
try {
if ($attr instanceof Model) {
// do nothing
Expand Down Expand Up @@ -440,37 +419,7 @@ public function getCustomAttributeValue($attr)
}

/**
* TODO: this may not be necessary without custom_attribute_default
*
* Gets a customAttributeDefault object by custom_attribute_id and
* inventory_id
*
* @param int|string|Model $attr
*
* @return mixed
*
* @throws InvalidCustomAttributeException
*/
public function getCustomAttributeDefaultObj($attr)
{
try {
$attrObj = $this->resolveCustomAttributeObject($attr);

$defaultObj = $this->customAttributeDefaults()
->where('custom_attribute_id', $attrObj->getKey())
->where('inventory_id', $this->getKey())
->get()->first();

if ($defaultObj) {return $defaultObj;}
else throw new \Exception;
} catch (\Exception $e) {
throw new InvalidCustomAttributeException('Could not get custom attribute default object with key "'.$attr.'"');
}
}

/**
* Gets a custom attribute default based on the custom attribute id
* and inventory item's id
* Gets a custom attribute default from the custom attribute's model
*
* @param int|string $attr
*
Expand All @@ -483,13 +432,8 @@ public function getCustomAttributeDefault($attr)
try {
$attrObj = $this->getCustomAttribute($attr);

$attrDefaultObj = $this->getCustomAttributeDefaultObj($attrObj);

$type = $attrObj->value_type;

$key = $type . '_val';

return $attrDefaultObj->$key;
if (is_numeric($attrObj->default_value)) return filter_var($attrObj->default_value, FILTER_SANITIZE_NUMBER_FLOAT);
else return $attrObj->default_value;
} catch (\Exception $e) {
throw new InvalidCustomAttributeException('Could not get custom attribute with key "'.$attr.'"');
}
Expand Down Expand Up @@ -551,47 +495,42 @@ public function removeCustomAttribute($attr)
}
}

/**
* TODO: refactor this method to set the default on the CustomAttribute model itself
*
/**
* Sets the given custom attribute default for this item
*
* @param string|int|Model $attr
* @param string|int|date $value
*
* @return Model|boolean
* @return Model
*/
public function setCustomAttributeDefault($attr, $value) {
$attrObj = $this->resolveCustomAttributeObject($attr);
$attrObj = $this->getCustomAttribute($attr);

$id = $attrObj->id;
$attrObj->default_value = $value;

$type = $attrObj->value_type;
if (!$attrObj->has_default) $attrObj->has_default = true;

try {
$existingAttrDefaultObj = $this->getCustomAttributeDefaultObj($id);
$attrObj->save();

$valKey = $type . '_val';

$existingAttrDefaultObj->$valKey = $value;

return $existingAttrDefaultObj->save();
} catch (\Exception $e) {
$itemKey = $this->getKey();

$attrDefault = [
'custom_attribute_id' => $id,
'inventory_id' => $itemKey,
'string_val' => $type == 'string' ? $value : null,
'num_val' => $type == 'num' ? $value : null,
'date_val' => $type == 'date' ? $value : null,
];

$attrObj->has_default = true;
$attrObj->save();
return $attrObj;
}

return $this->customAttributeDefaults()->create($attrDefault);
}
/**
* Sets the given custom attribute default for this item
*
* @param string|int|Model $attr
*
* @return Model
*/
public function removeCustomAttributeDefault($attr) {
$attrObj = $this->getCustomAttribute($attr);

$attrObj->default_value = null;

if ($attrObj->has_default) $attrObj->has_default = false;

$attrObj->save();

return $attrObj;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,6 @@ public function up()

$table->unique(['inventory_id', 'custom_attribute_id'], 'values_inventory_attribute_id_unique');
});

// TODO: take this out and modify custom_attributes instead
Schema::create('custom_attribute_defaults', function(Blueprint $table) {
$table->id();
$table->foreignId('inventory_id');
$table->foreignId('custom_attribute_id');
$table->string('string_val', 8191)->nullable();
$table->decimal('num_val', 16, 4)->nullable(); // 123,456,789,012.3456
$table->dateTime('date_val')->nullable();

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

$table->unique(['inventory_id', 'custom_attribute_id'], 'defaults_inventory_attribute_id_unique');
});
}

/**
Expand Down
19 changes: 11 additions & 8 deletions tests/CustomAttributeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ public function testCanAddExistingAttributeToItem()
$this->assertEquals($attr1->id, $attr3->id);
}

// TODO: modify this for custom attribute default refactor
public function testCanAddCustomAttributeWithDefault()
{
$item = $this->newInventory();
Expand All @@ -251,7 +250,6 @@ public function testCanAddCustomAttributeWithDefault()
$this->assertEquals('default value', $item->getCustomAttributeValue('fresh_property'));
}

// TODO: modify this for custom attribute default refactor
public function testCanAddDefaultToExistingCustomAttribute()
{
$item = $this->newInventory();
Expand All @@ -267,7 +265,6 @@ public function testCanAddDefaultToExistingCustomAttribute()
$this->assertTrue($attr->has_default);
}

// TODO: modify this for custom attribute default refactor
public function testCanChangeCustomAttributeValueWithDefault()
{
$item = $this->newInventory();
Expand All @@ -282,7 +279,6 @@ public function testCanChangeCustomAttributeValueWithDefault()
$this->assertEquals(strtotime('22-2-2'), strtotime($item->getCustomAttributeDefault($attr->id)));
}

// TODO: modify this for custom attribute default refactor
public function testCanChangeCustomAttributeDefaultValue()
{
$item = $this->newInventory();
Expand All @@ -294,6 +290,17 @@ public function testCanChangeCustomAttributeDefaultValue()
$this->assertEquals(42, $item->getCustomAttributeDefault('number_property'));
}

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

$attr = $item->addCustomAttribute('integer', 'A further number for testing', 20);

$item->removeCustomAttributeDefault($attr);

$this->assertNull($item->getCustomAttributeDefault('a_further_number_for_testing'));
}

public function testCanAddRequiredCustomAttribute()
{
$item = $this->newInventory();
Expand Down Expand Up @@ -463,7 +470,6 @@ public function testCannotGetValueOfNonexistentCustomAttribute()
$item->getCustomAttributeValue('not an attribute even a little');
}

// TODO: maybe refactor this method
public function testCannotGetDefaultValueOfNonexistentCustomAttribute()
{
$item = $this->newInventory();
Expand All @@ -473,7 +479,6 @@ public function testCannotGetDefaultValueOfNonexistentCustomAttribute()
$item->getCustomAttributeDefault('not an attribute at all');
}

// TODO: maybe refactor this as well
public function testCannotCreateNumericCustomAttributeWithInvalidDefault()
{
$item = $this->newInventory();
Expand All @@ -483,7 +488,6 @@ public function testCannotCreateNumericCustomAttributeWithInvalidDefault()
$item->addCustomAttribute('integer', 'Number Property', 'not a number');
}

// TODO: although maybe would be better to just rewrite the backend
public function testCannotCreateDateCustomAttributeWithInvalidDefault()
{
$item = $this->newInventory();
Expand All @@ -493,7 +497,6 @@ public function testCannotCreateDateCustomAttributeWithInvalidDefault()
$item->addCustomAttribute('date', 'Date Property', 'not a date');
}

// TODO: so that it fits with these scenarios
public function testCannotCreateTimeCustomAttributeWithInvalidDefault()
{
$item = $this->newInventory();
Expand Down
Loading

0 comments on commit adfa14e

Please sign in to comment.