From 8f3fd44986e2433b222e976f11d049d4a407dcbd Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Mon, 23 May 2016 05:20:14 -0400 Subject: add role options --- src/guff.cr | 48 ++++++++++++++++++++++++++++++++++++++++++------ src/views/admin-page.ecr | 4 ++-- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/guff.cr b/src/guff.cr index 61330a1..b0f57d4 100644 --- a/src/guff.cr +++ b/src/guff.cr @@ -970,6 +970,29 @@ module Guff r ? r.to_i64 : nil end end + + class RoleModel < Model + SQL = { + get_roles: " + SELECT role, + name + + FROM roles + + ORDER BY sort + ", + } + + def get_roles : Array(Hash(String, String)) + r = [] of Hash(String, String) + + @context.dbs.ro.all(SQL[:get_roles]) do |row| + r << { "role": row["role"] as String, "name": row["name"] as String } + end + + r + end + end end class ModelSet @@ -985,6 +1008,7 @@ module Guff project: Models::ProjectModel, blog: Models::BlogModel, site: Models::SiteModel, + role: Models::RoleModel, }) end @@ -1356,6 +1380,16 @@ module Guff super(TABS) end + OPTION = "" + + def role_options + @role_options ||= String.build do |io| + @context.models.role.get_roles.each do |row| + io << OPTION % %w{role name}.map { |key| h(row[key]) } + end + end + end + ECR.def_to_s("src/views/admin-page.ecr") end @@ -1993,14 +2027,16 @@ module Guff ), -- user-visible role name - role_name TEXT UNIQUE NOT NULL - CHECK (LENGTH(role_name) > 0) + name TEXT UNIQUE NOT NULL + CHECK (LENGTH(name) > 0), + + sort INTEGER UNIQUE NOT NULL ) }, %{ - INSERT INTO roles(role_id, role, role_name) VALUES - (1, 'viewer', 'Viewer'), - (2, 'editor', 'Editor'), - (3, 'admin', 'Admin') + INSERT INTO roles(role_id, role, name, sort) VALUES + (1, 'viewer', 'Viewer', 1), + (2, 'editor', 'Editor', 2), + (3, 'admin', 'Admin', 3) }, %{ CREATE TABLE users ( user_id INTEGER PRIMARY KEY, diff --git a/src/views/admin-page.ecr b/src/views/admin-page.ecr index 678b8c5..3394096 100644 --- a/src/views/admin-page.ecr +++ b/src/views/admin-page.ecr @@ -268,7 +268,7 @@ class='form-control' title='Choose role of user.' ><%= - # get_roles + role_options %> @@ -362,7 +362,7 @@ class='form-control' title='Choose role of user.' ><%= - # get_roles + role_options %> -- cgit v1.2.3