Skip to content

Commit cf2fbd4

Browse files
committed
AASM and more models
1 parent eb5c614 commit cf2fbd4

File tree

7 files changed

+40
-7
lines changed

7 files changed

+40
-7
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ gem 'bcrypt', '~> 3.1.7'
55
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
66
gem 'pg'
77
gem 'bootsnap'
8+
gem 'aasm'
89

910
group :development, :test do
1011
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
GEM
22
remote: https://rubygems.org/
33
specs:
4+
aasm (4.12.2)
5+
concurrent-ruby (~> 1.0)
46
actioncable (5.1.3)
57
actionpack (= 5.1.3)
68
nio4r (~> 2.0)
@@ -136,6 +138,7 @@ PLATFORMS
136138
ruby
137139

138140
DEPENDENCIES
141+
aasm
139142
bcrypt (~> 3.1.7)
140143
bootsnap
141144
byebug

app/models/book.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
class Book < ApplicationRecord
22
has_and_belongs_to_many :authors
3+
4+
has_many :reviews
35
end

app/models/review.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Review < ApplicationRecord
2+
3+
belongs_to :book
4+
belongs_to :reviewer
5+
6+
include AASM
7+
8+
aasm do
9+
state :draft, initial: true
10+
state :published
11+
12+
event :publish do
13+
transitions from: [:draft], to: :published
14+
end
15+
end
16+
17+
end

app/models/reviewer.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Reviewer < ApplicationRecord
2+
3+
has_many :reviews
4+
5+
end

db/migrate/20170822122707_create_everything.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ def change
2222
end
2323

2424
create_table :reviews do |t|
25-
t.string :state
25+
t.string :aasm_state, null: false
26+
t.boolean :featured, null: false, default: false
27+
t.text :content, null: false
2628
t.references :book, null: false, foreign_key: true
2729
t.references :reviewer, null: false, foreign_key: true
2830
t.timestamps

lib/tasks/populate.rake

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
namespace :db do
22
desc "Populate database"
33
task populate: :environment do
4-
Book.create!(title: "Brothers Karamazov")
5-
Author.create!(name: "Fyodor Dostoevsky")
6-
Book.last.authors << Author.last
4+
reviewer_fred = Reviewer.create!(name: "Fred Fredson")
5+
6+
brothers_k = Book.create!(title: "Brothers Karamazov")
7+
fyodor_d = Author.create!(name: "Fyodor Dostoevsky")
8+
brothers_k.authors << fyodor_d
9+
Review.create!(book: brothers_k, reviewer: reviewer_fred, content: "a classic")
710

8-
Book.create!(title: "The Fatal Eggs")
9-
Author.create!(name: "Mikhail Bulgakov")
10-
Book.last.authors << Author.last
11+
fatal_e = Book.create!(title: "The Fatal Eggs")
12+
mikhail_b = Author.create!(name: "Mikhail Bulgakov")
13+
fatal_e.authors << mikhail_b
1114

1215
end
1316
end

0 commit comments

Comments
 (0)