Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Etiene committed May 6, 2016
1 parent a69197b commit e5ec6ff
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 26 deletions.
4 changes: 2 additions & 2 deletions app.moon
Expand Up @@ -26,7 +26,7 @@ import
Users
Versions
DownloadsDaily
Labels
ModuleLabels
from require "models"

import
Expand Down Expand Up @@ -91,7 +91,7 @@ class MoonRocks extends lapis.Application
@popular_modules = Modules\select "order by downloads desc limit 5"
Users\include_in @popular_modules, "user_id"

@labels = Labels\select "order by name"
@labels = ModuleLabels\select "order by name"

@downloads_daily = DownloadsDaily\fetch true, 30
render: true
Expand Down
10 changes: 5 additions & 5 deletions applications/modules.moon
Expand Up @@ -25,7 +25,7 @@ import
Rocks
Dependencies
Modules
Labels
ModuleLabels
LabelsModules
from require "models"

Expand Down Expand Up @@ -216,7 +216,7 @@ class MoonRocksModules extends lapis.Application
redirect_to: @url_for @module

[modules_label: "/label/modules/:label"]: capture_errors_404 =>
label = assert_error Labels\find(name: @params.label), "Invalid label"
label = assert_error ModuleLabels\find(name: @params.label), "Invalid label"

@title = "All modules in #{label.name}"

Expand All @@ -237,7 +237,7 @@ class MoonRocksModules extends lapis.Application
before: =>
load_module @
assert_editable @, @module
@label = Labels\find @params.label_id
@label = ModuleLabels\find @params.label_id
return unless @label

assert_error LabelsModules\find({
Expand All @@ -264,7 +264,7 @@ class MoonRocksModules extends lapis.Application
@title = "Add Label to Module"

already_in = { l.id, true for l in *@module\get_labels! }
@labels = for l in *Labels\select "order by name"
@labels = for l in *ModuleLabels\select "order by name"
continue if already_in[l.id]
l

Expand All @@ -278,7 +278,7 @@ class MoonRocksModules extends lapis.Application
{ "label_id", is_integer: true }
}

label = assert_error Labels\find(id: @params.label_id), "Invalid label id"
label = assert_error ModuleLabels\find(id: @params.label_id), "Invalid label id"

assert_error LabelsModules\create label_id: label.id, module_id: @module.id
redirect_to: @url_for("module", @)
Expand Down
6 changes: 3 additions & 3 deletions helpers/toolbox.moon
Expand Up @@ -10,7 +10,7 @@ import

import
Modules
Labels
ModuleLabels
LabelsModules
Followings
from require "models"
Expand All @@ -22,14 +22,14 @@ _modules = {m.id,m.name for m in *modules}
class Toolbox
create_labels_from_dump: =>
for l in *labels
Labels\create name: l.name
ModuleLabels\create name: l.name

apply_labels_to_modules: =>
for m in *modules
mod = Modules\find name: m.name
if mod
for l in *m.labels
label = Labels\find name: _labels[tonumber l]
label = ModuleLabels\find name: _labels[tonumber l]
if label
LabelsModules\create module_id: mod.id, label_id: label.id

Expand Down
15 changes: 14 additions & 1 deletion migrations.moon
Expand Up @@ -231,7 +231,18 @@ import
add_column "versions", "archived", boolean default: false

[1457699498]: =>
create_table "labels", {
create_table "labels_modules", {
{"label_id", foreign_key}
{"module_id", foreign_key}
{"created_at", time}
{"updated_at", time}
"PRIMARY KEY (label_id, module_id)"
}

create_index "labels_modules", "module_id"

[1462567085]: =>
create_table "module_labels", {
{"id", serial}
{"name", varchar}
{"created_at", time}
Expand All @@ -247,6 +258,8 @@ import
"PRIMARY KEY (label_id, module_id)"
}

create_index "labels_modules", "module_id"



}
2 changes: 1 addition & 1 deletion models/labels_modules.moon
Expand Up @@ -7,7 +7,7 @@ class LabelsModules extends Model
@timestamp: true

@relations: {
{"label", belongs_to: "Labels"}
{"label", belongs_to: "ModuleLabels"}
{"module", belongs_to: "Modules"}
}

Expand Down
3 changes: 1 addition & 2 deletions models/labels.moon → models/module_labels.moon
Expand Up @@ -3,6 +3,5 @@ import Model from require "lapis.db.model"
import generate_key from require "helpers.models"


class Labels extends Model
class ModuleLabels extends Model
@timestamp: true

6 changes: 3 additions & 3 deletions spec/helpers/toolbox_spec.moon
Expand Up @@ -3,7 +3,7 @@ import truncate_tables from require "lapis.spec.db"

factory = require "spec.factory"

import Modules, Labels from require "models"
import Modules, ModuleLabels from require "models"

toolbox = require "helpers.toolbox"

Expand All @@ -19,11 +19,11 @@ describe "helpers.toolbox", ->
use_test_server!

setup ->
truncate_tables Labels
truncate_tables ModuleLabels

it "imports labels", ->
toolbox\create_labels_from_dump!
count = Labels\count!
count = ModuleLabels\count!
assert.equal count, #labels

it "applies labels", ->
Expand Down
6 changes: 4 additions & 2 deletions views/module.moon
Expand Up @@ -78,11 +78,13 @@ class Module extends require "widgets.page"
h3 "Labels"
for i,l in ipairs @labels
div class: "label_row", ->
a href: "/label/modules/#{l.name}", l.name
a href: @url_for("modules_label", label: l.name), l.name
if can_edit
span class: "sub", ->
text " ("
a href: "/label/remove/#{@user.slug}/#{@module.name}/#{l.id}", "remove"
a href:
@url_for("remove_label",user: @user.slug, module: @module.name, label_id: l.id),
"remove"
text ")"


Expand Down
2 changes: 1 addition & 1 deletion views/modules_label.moon
@@ -1,4 +1,4 @@
class Modules extends require "widgets.page"
class ModulesLabel extends require "widgets.page"
inner_content: =>
h2 ->
text @title
Expand Down
10 changes: 5 additions & 5 deletions views/remove_label.moon
@@ -1,18 +1,18 @@
class RemoveLabel require "widgets.page"
class RemoveLabel extends require "widgets.page"
inner_content: =>
h2 "Remove Label: #{@module.name}"
@render_modules { @module }
@render_errors!

form action: @req.cmd_url, method: "POST", ->
input type: "hidden", name: "csrf_token", value: @csrf_token
form method: "POST", ->
@csrf_input!
div ->
text "Are you sure you want to remove this label from "
a href: "", @module.name
a href: @url_for(@module), @module.name
text "? "
input type: "submit", value: "Yes, Remove"

div ->
a href: @url_for("module", @), ->
a href: @url_for(@module), ->
raw "« No, Return to module"

2 changes: 1 addition & 1 deletion views/user_settings/import_toolbox.moon
Expand Up @@ -21,7 +21,7 @@ class UserSettingsImportToolbox extends require "widgets.user_settings_page"

else
p ->
test "No endorsements were imported."
text "No endorsements were imported."
else
form method: "POST", class: "form", ->
@csrf_input!
Expand Down

0 comments on commit e5ec6ff

Please sign in to comment.