Skip to content

Commit

Permalink
transfer labels from the rockspec into the newly created module #188
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Feb 28, 2025
1 parent cdc1c7f commit b35c7ec
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
11 changes: 11 additions & 0 deletions models/modules.moon
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import Model from require "lapis.db.model"
import concat from table
import safe_insert from require "helpers.models"

types = require "lapis.validate.types"

-- Generated schema dump: (do not edit)
--
-- CREATE TABLE modules (
Expand Down Expand Up @@ -87,6 +89,15 @@ class Modules extends Model
modules_count: db.raw "modules_count + 1"
}, timestamp: false


-- Transfer labels from rockspec if present
if spec.labels
labels = types.array_of(types.truncated_text(128))\transform spec.labels
if labels and next labels
-- take the first 10
labels = [l for l in *labels[1,10]]
mod\set_labels labels

mod

@search: (query, manifest_ids) =>
Expand Down
60 changes: 59 additions & 1 deletion spec/models/modules_spec.moon
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe "models.modules", ->

other_user\refresh!
assert.same 1, other_user.modules_count

describe "labels", ->
it "sets labels to something", ->
mod = factory.Modules!
Expand All @@ -96,6 +96,64 @@ describe "models.modules", ->
mod\set_labels {"one", "- -", " ", "two"}
assert.same {"one", "two"}, mod.labels

it "creates module with labels from rockspec", ->
user = factory.Users!
spec = {
package: "my_module"
version: "1.0.0"
description: {
summary: "A test module"
detailed: "A more detailed description"
homepage: "http://example.com"
license: "MIT"
}
labels: {"Test", "MoonScript", "Hello World"}
}

mod = Modules\create spec, user
assert.truthy mod
assert.same {"test", "moonscript", "hello-world"}, mod.labels

it "handles invalid type for labels", ->
user = factory.Users!
spec = {
package: "my_module"
version: "1.0.0"
description: {}
labels: "this is not an array"
}

mod = Modules\create spec, user
assert.truthy mod
assert.nil mod.labels

it "handles empty array for labels", ->
user = factory.Users!
spec = {
package: "my_module"
version: "1.0.0"
description: {}
labels: {}
}

mod = Modules\create spec, user
assert.truthy mod
assert.nil mod.labels

it "only takes the first 10 labels", ->
user = factory.Users!
spec = {
package: "my_module"
version: "1.0.0"
description: {}
labels: {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve"}
}

mod = Modules\create spec, user
assert.truthy mod
assert.same {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"}, mod.labels


describe "parse labels", ->
it "parses labels", ->
assert.same {"hello-world", "yeah", "okay-zone"}, Modules\parse_labels "hello world, yeah, okay_zone"
Expand Down

0 comments on commit b35c7ec

Please sign in to comment.