Skip to content

Commit ba62fc8

Browse files
committed
Code for nevernote app (MVP)
1 parent d384548 commit ba62fc8

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'datamapper'
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
addressable (2.3.7)
5+
bcrypt (3.1.10)
6+
bcrypt-ruby (3.1.5)
7+
bcrypt (>= 3.1.3)
8+
datamapper (1.2.0)
9+
dm-aggregates (~> 1.2.0)
10+
dm-constraints (~> 1.2.0)
11+
dm-core (~> 1.2.0)
12+
dm-migrations (~> 1.2.0)
13+
dm-serializer (~> 1.2.0)
14+
dm-timestamps (~> 1.2.0)
15+
dm-transactions (~> 1.2.0)
16+
dm-types (~> 1.2.0)
17+
dm-validations (~> 1.2.0)
18+
dm-aggregates (1.2.0)
19+
dm-core (~> 1.2.0)
20+
dm-constraints (1.2.0)
21+
dm-core (~> 1.2.0)
22+
dm-core (1.2.1)
23+
addressable (~> 2.3)
24+
dm-migrations (1.2.0)
25+
dm-core (~> 1.2.0)
26+
dm-serializer (1.2.2)
27+
dm-core (~> 1.2.0)
28+
fastercsv (~> 1.5)
29+
json (~> 1.6)
30+
json_pure (~> 1.6)
31+
multi_json (~> 1.0)
32+
dm-timestamps (1.2.0)
33+
dm-core (~> 1.2.0)
34+
dm-transactions (1.2.0)
35+
dm-core (~> 1.2.0)
36+
dm-types (1.2.2)
37+
bcrypt-ruby (~> 3.0)
38+
dm-core (~> 1.2.0)
39+
fastercsv (~> 1.5)
40+
json (~> 1.6)
41+
multi_json (~> 1.0)
42+
stringex (~> 1.4)
43+
uuidtools (~> 2.1)
44+
dm-validations (1.2.0)
45+
dm-core (~> 1.2.0)
46+
fastercsv (1.5.5)
47+
json (1.8.2)
48+
json_pure (1.8.2)
49+
multi_json (1.10.1)
50+
stringex (1.5.1)
51+
uuidtools (2.1.5)
52+
53+
PLATFORMS
54+
ruby
55+
56+
DEPENDENCIES
57+
datamapper
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require 'data_mapper'
2+
3+
# DataMapper::Logger.new($stdout, :debug)
4+
5+
DataMapper.setup(:default, 'sqlite:nevernote.db')
6+
7+
class User
8+
include DataMapper::Resource
9+
10+
property :id, Serial
11+
property :username, String, unique: true, required: true
12+
property :password, BCryptHash, required: true
13+
14+
has n, :notebooks
15+
has n, :notes, through: :notebooks
16+
end
17+
18+
class Note
19+
include DataMapper::Resource
20+
21+
property :id, Serial
22+
property :title, String, required: true
23+
property :content, Text, default: "Blank note"
24+
25+
belongs_to :notebook
26+
has 1, :user, through: :notebook
27+
28+
has n, :taggings # SELECT * FROM taggings WHERE note_id = self.id;
29+
has n, :tags, through: :taggings
30+
end
31+
32+
class Notebook
33+
include DataMapper::Resource
34+
35+
property :id, Serial
36+
property :name, String, required: true
37+
38+
# validate uniqueness of user-notebook name combination
39+
40+
belongs_to :user
41+
has n, :notes
42+
end
43+
44+
class Tagging
45+
include DataMapper::Resource
46+
47+
property :id, Serial
48+
belongs_to :note
49+
belongs_to :tag
50+
end
51+
52+
class Tag
53+
include DataMapper::Resource
54+
55+
property :id, Serial
56+
property :name, String, length: 30, required: true, unique: true
57+
58+
has n, :taggings
59+
has n, :notes, through: :taggings
60+
end
61+
62+
DataMapper.finalize
63+
DataMapper.auto_migrate!
Binary file not shown.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require './models.rb'
2+
3+
Note.all.destroy # start from a blanks slate
4+
User.all.destroy
5+
Notebook.all.destroy
6+
Tag.all.destroy
7+
Tagging.all.destroy
8+
9+
joe = User.create(username: "Joe", password: "beans")
10+
11+
p User.count == 1
12+
13+
recipes = Notebook.create(name: "Recipes", user: joe)
14+
15+
16+
attrs = {name: "Recipes", user: joe}
17+
n = Notebook.new
18+
n.name = attrs[:name]
19+
n.user = attrs[:user]
20+
n.save
21+
22+
p Notebook.count == 1
23+
24+
# As a user, I want to keep a store of notes.
25+
bkfst = Note.new(title: "Breakfast ideas", content: "truffle oil egg sandwich")
26+
bkfst.notebook = recipes
27+
bkfst.save
28+
29+
p Note.count == 1
30+
31+
# As a user, I want to search for notes by their title.
32+
p Note.all(:title.like => "Breakfast %").first == bkfst
33+
34+
# As a user, I want to organize notes into notebooks.
35+
p bkfst.notebook == recipes
36+
37+
# As a user, I want to add tags to notes.
38+
incomplete = Tag.create(name: "incomplete")
39+
bkfst.tags << incomplete
40+
bkfst.save
41+
42+
p joe.notebooks
43+
p joe.notes
44+
p joe.notes.first.tags
45+
# As a user, I want to see all notes in a notebook.
46+
# p recipes.notes
47+
48+
# As a user, I want to see all notes that have a particular tag.
49+
# p incomplete.notes

0 commit comments

Comments
 (0)