Skip to content

Commit e286922

Browse files
committed
Refactor model
1 parent 1ed6910 commit e286922

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

model.gb

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,38 +30,41 @@ class ListItem
3030
end
3131

3232
def check
33-
self.class.plugin_db.exec('UPDATE list_items SET checked = true WHERE id = $1', @id)
33+
db.exec('UPDATE list_items SET checked = true WHERE id = $1', @id)
3434
@checked = true
3535
end
3636

3737
def uncheck
38-
self.class.plugin_db.exec('UPDATE list_items SET checked = false WHERE id = $1', @id)
38+
db.exec('UPDATE list_items SET checked = false WHERE id = $1', @id)
3939
@checked = false
4040
end
4141

4242
def update_title(title)
43-
self.class.plugin_db.exec('UPDATE list_items SET title = $1 WHERE id = $2', title, @id)
43+
db.exec('UPDATE list_items SET title = $1 WHERE id = $2', title, @id)
4444
end
4545

4646
def destroy
47-
self.class.plugin_db.exec('DELETE FROM list_items WHERE id = $1', @id)
47+
db.exec('DELETE FROM list_items WHERE id = $1', @id)
4848
end
4949

5050
def valid?
5151
@error.nil?
5252
end
5353

54+
def db
55+
self.class.db
56+
end
5457

55-
def self.plugin_db
58+
def self.db
5659
PG
5760
end
5861

5962
def self.all
60-
plugin_db.query("SELECT * FROM list_items ORDER BY id DESC")
63+
db.query("SELECT * FROM list_items ORDER BY id DESC")
6164
end
6265

6366
def self.find(id)
64-
result = plugin_db.query("SELECT * FROM list_items WHERE id = $1", id).first
67+
result = db.query("SELECT * FROM list_items WHERE id = $1", id).first
6568
if result
6669
new({ id: result[:id], title: result[:title], checked: result[:checked] })
6770
end
@@ -72,7 +75,7 @@ class ListItem
7275
if result[:error].nil?
7376
title = params[:title]
7477
checked = params[:checked].to_i == 1
75-
resultID = self.plugin_db.exec("INSERT INTO list_items (title, checked) VALUES ($1, $2)", title, checked)
78+
resultID = db.exec("INSERT INTO list_items (title, checked) VALUES ($1, $2)", title, checked)
7679
new({ id: resultID, title: title, checked: checked })
7780
else
7881
new({ error: result[:error] })

0 commit comments

Comments
 (0)