From 13de2852294a911c24cee79c961ac4011fd13e88 Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Fri, 6 Jun 2014 18:15:38 +0300 Subject: Third commit --- README.rst | 4 +- app.rb | 110 ++++++++++++++++++++++++++++---------------------- views/edit_object.erb | 24 ++++++++++- views/layout.erb | 6 ++- views/view_object.erb | 4 +- 5 files changed, 92 insertions(+), 56 deletions(-) diff --git a/README.rst b/README.rst index b26626f..b1cc26a 100644 --- a/README.rst +++ b/README.rst @@ -3,6 +3,6 @@ Membrain is an app for remembering things. The idea is very simple - you enter d ## DB Schema ``` -create table objects (id integer primary key autoincrement, title text, icon text, updated datetime); -create table properties (id integer primary key autoincrement, object_id integer, key text, value text); +CREATE TABLE objects (id integer primary key autoincrement, title text, icon text, updated datetime); +CREATE TABLE properties (id integer primary key autoincrement, object_id integer, key text, value text, type text); ``` diff --git a/app.rb b/app.rb index 55f00c8..aa3d2ea 100755 --- a/app.rb +++ b/app.rb @@ -5,71 +5,83 @@ require "sinatra" class MBObject - attr_reader :id, :title - - @@db = SQLite3::Database.new "membrain.db" - - def initialize - @id = 0 - @keys = [] - @values = [] - @title = "" - end - - def init_with_row(row) - @id = row[0] - @title = row[1] - @updated = row[2] - end - - def self.create(name) - @@db.execute("INSERT INTO objects (title, updated) VALUES (?, DATETIME())", name) - @@db.execute("SELECT last_insert_rowid()")[0][0] - end - - def self.all - all = [] - @@db.execute("SELECT id, title, updated FROM objects") do | row | - o = MBObject.new - all << o.init_with_row(row) + attr_writer :title + attr_reader :id, :title + + @@db = SQLite3::Database.new "membrain.db" + + def initialize + @id = 0 + @title = "" + @properties = {} + end + + def save + @@db.execute("UPDATE objects SET title = ? WHERE id = ?", @title, @id) + end + + def init_with_row(row) + @id = row[0] + @title = row[1] + @updated = row[2] + end + + def self.create(name) + @@db.execute("INSERT INTO objects (title, updated) VALUES (?, DATETIME())", name) + @@db.execute("SELECT last_insert_rowid()")[0][0] + end + + def self.all + all = [] + @@db.execute("SELECT id, title, updated FROM objects") do | row | + o = MBObject.new + o.init_with_row(row) + all << o + end + return all end - return all - end - - def self.get(id) - o = MBObject.new - @@db.execute("SELECT id, icon, updated FROM objects WHERE id = ?", id) do | row | - o = MBObject.new - o.init_with_row(row) + + def self.get(id) + o = MBObject.new + @@db.execute("SELECT id, title, icon, updated FROM objects WHERE id = ?", id) do | row | + o.init_with_row(row) + end + return o end - return o - end - def get_url - "/#{@id}/edit" - end + def get_url + "/#{@id}/edit" + end end get '/' do - @data = MBObject.all - erb :index + @data = MBObject.all + erb :index end get '/add' do - erb :form + erb :form end post '/add' do - i = MBObject.create(params[:name]) - redirect "/#{i}" + i = MBObject.create(params[:name]) + redirect "/#{i}" end get '/:id' do - @data = MBObject.get(params[:id]) - erb :view_object + @object = MBObject.get(params[:id]) + erb :view_object end get '/:id/edit' do - @o = MBObject.get(params[:id]) - erb :edit_object + @o = MBObject.get(params[:id]) + erb :edit_object +end + +post '/:id/edit' do + @o = MBObject.get(params[:id]) + @o.title = params[:title] + @o.save + p params[:keys] params[:values] + redirect @o.get_url end diff --git a/views/edit_object.erb b/views/edit_object.erb index aa44acc..9c066aa 100644 --- a/views/edit_object.erb +++ b/views/edit_object.erb @@ -1,7 +1,29 @@
- + +
+
+
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
diff --git a/views/layout.erb b/views/layout.erb index a4d059b..386dad0 100644 --- a/views/layout.erb +++ b/views/layout.erb @@ -5,8 +5,10 @@ -
- <%= yield %> +
+
+ <%= yield %> +
diff --git a/views/view_object.erb b/views/view_object.erb index 20a58d7..24c4833 100644 --- a/views/view_object.erb +++ b/views/view_object.erb @@ -1,3 +1,3 @@ -<%= @data.title %> +

<%= @object.title %>

-Edit +Edit -- cgit v1.2.3