Skip to content

Commit

Permalink
Merge branch 'development' of github.com:Webiny/webiny-js into develo…
Browse files Browse the repository at this point in the history
…pment
  • Loading branch information
Pavel910 committed Mar 1, 2019
2 parents a2a1edb + 28a6cb9 commit fbab80b
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 9 deletions.
18 changes: 13 additions & 5 deletions packages/webiny-app/src/plugins/imagePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ const sanitizeTransformArgs = (args: ?Object): Object => {
};

const getSrcSetAutoSizes = (max: ?number) => {
const maxWidth = max ? getSupportedImageResizeWidth(max) : 2500;
max = isFixedImageWidth(max) ? parseInt(max) : 2500;
const maxWidth = getSupportedImageResizeWidth(max);
return SUPPORTED_IMAGE_RESIZE_WIDTHS.filter((supportedWidth: number) => {
return supportedWidth <= maxWidth;
});
Expand All @@ -62,6 +63,17 @@ const convertTransformToQueryParams = (transform: Object): string => {
.join("&");
};

const isFixedImageWidth = width => {
if (Number.isFinite(width)) {
return true;
}

if (typeof width === "string" && width.endsWith("px")) {
return true;
}
return false;
};

const imagePlugin: ImageComponentPluginType = {
name: "image-component",
type: "image-component",
Expand Down Expand Up @@ -95,10 +107,6 @@ const imagePlugin: ImageComponentPluginType = {

// Check if image width was forced, and additionally if width was set as pixels, with "px" in the value.
let forcedWidth = props.width || (props.style && props.style.width);
if (typeof forcedWidth === "string" && forcedWidth.endsWith("px")) {
forcedWidth = parseInt(forcedWidth);
}

const srcSetAutoWidths = getSrcSetAutoSizes(forcedWidth);
srcSetAutoWidths.forEach(width => {
srcSet[width + "w"] = imagePlugin.getImageSrc({
Expand Down
103 changes: 102 additions & 1 deletion packages/webiny-entity/__tests__/toStorage.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import User from "./entities/user";
import { Entity, Driver, EntityModel, EntityAttributesContainer } from "webiny-entity";
import { BooleanAttribute } from "webiny-model";
import { Model, BooleanAttribute } from "webiny-model";

describe("toStorage test", () => {
test("should return the same values, except dynamic attribute", async () => {
Expand All @@ -22,6 +22,107 @@ describe("toStorage test", () => {
expect(data["enabled"]).toBe(true);
});

test("should return the same values, except dynamic attribute (including nested models)", async () => {
class C extends Model {
constructor() {
super();
this.attr("attr1").char();
this.attr("attr2").char();
this.attr("attr3")
.integer()
.setDynamic(() => {
return "attr3DynValue";
});
}
}

class B extends Model {
constructor() {
super();
this.attr("attr1").char();
this.attr("attr2").char();
this.attr("attr3")
.integer()
.setDynamic(() => {
return "attr3DynValue";
});

this.attr("attr4").model(C);
this.attr("attr5").models(C);
this.attr("attr6")
.model(C)
.setDynamic(() => {
return "attr6DynValue";
});
this.attr("attr7")
.models(C)
.setDynamic(() => {
return "attr7DynValue";
});
}
}

class A extends Entity {
constructor() {
super();
this.attr("attr1").char();
this.attr("attr2").char();
this.attr("attr3")
.integer()
.setDynamic(() => {
return "attr3DynValue";
});

this.attr("attr4").model(B);
}
}

const a = new A();
a.populate({
attr1: "attr1",
attr2: "attr2",
attr3: 333,
attr4: {
attr1: "attr1",
attr2: "attr2",
attr3: 333,
attr4: {
attr1: "attr1",
attr2: "attr2",
attr3: 333
},
attr5: [
{ attr1: "0.attr1", attr2: "0.attr2", attr3: 333 },
{ attr1: "1.attr1", attr2: "1.attr2", attr3: 333 }
],
attr6: {
attr1: "attr1",
attr2: "attr2",
attr3: 333
},
attr7: [
{ attr1: "0.attr1", attr2: "0.attr2", attr3: 333 },
{ attr1: "1.attr1", attr2: "1.attr2", attr3: 333 }
]
}
});

const data = await a.toStorage();
expect(data).toEqual({
attr1: "attr1",
attr2: "attr2",
attr4: {
attr1: "attr1",
attr2: "attr2",
attr4: { attr1: "attr1", attr2: "attr2" },
attr5: [
{ attr1: "0.attr1", attr2: "0.attr2" },
{ attr1: "1.attr1", attr2: "1.attr2" }
]
}
});
});

test("should return 1 or 0 instead true or false respectively", async () => {
class CustomBooleanAttribute extends BooleanAttribute {
setStorageValue(value) {
Expand Down
2 changes: 1 addition & 1 deletion packages/webiny-model/src/attributes/modelAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ModelAttribute extends Attribute {
for (let name in value.getAttributes()) {
const attribute = value.getAttribute(name);
// $FlowFixMe - we can be sure we have attribute because it's pulled from list of attributes, using getAttributes() method.
if (attribute.getToStorage()) {
if (attribute.getToStorage() && !attribute.getDynamic()) {
// $FlowFixMe - we can be sure we have attribute because it's pulled from list of attributes, using getAttributes() method.
json[name] = await attribute.getStorageValue();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/webiny-model/src/attributes/modelsAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class ModelsAttribute extends Attribute {
for (let name in model.getAttributes()) {
const attribute = model.getAttribute(name);
// $FlowFixMe - we can be sure we have attribute because it's pulled from list of attributes, using getAttributes() method.
if (attribute.getToStorage()) {
if (attribute.getToStorage() && !attribute.getDynamic()) {
// $FlowFixMe - we can be sure we have attribute because it's pulled from list of attributes, using getAttributes() method.
json[name] = await attribute.getStorageValue();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/webiny-model/src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ class Model {
for (let name in this.getAttributes()) {
const attribute = this.getAttribute(name);
// $FlowFixMe - we can be sure we have attribute because it's pulled from list of attributes, using getAttributes() method.
if (attribute.getToStorage() && attribute.value.isDirty()) {
if (attribute.getToStorage() && attribute.value.isDirty() && !attribute.getDynamic()) {
// $FlowFixMe - we can be sure we have attribute because it's pulled from list of attributes, using getAttributes() method.
json[name] = await attribute.getStorageValue();
}
Expand Down

0 comments on commit fbab80b

Please sign in to comment.