aboutsummaryrefslogtreecommitdiff
path: root/interfaces/ruby/models/comment.rb
diff options
context:
space:
mode:
authorJohn Ankarström <john@ankarstrom.se>2021-03-03 14:32:43 +0100
committerJohn Ankarström <john@ankarstrom.se>2021-03-03 14:34:55 +0100
commitd40c681d2f9d56e01cd5f1ff3e586b79ea4397f9 (patch)
tree5050c91c05807507cae5cc144037fb051d41b1f0 /interfaces/ruby/models/comment.rb
parentd072b503f5ccc5d08f279aac463d37837cbbb576 (diff)
downloadcomb-d40c681d2f9d56e01cd5f1ff3e586b79ea4397f9.tar.gz
Add 'user' model
Diffstat (limited to 'interfaces/ruby/models/comment.rb')
-rw-r--r--interfaces/ruby/models/comment.rb18
1 files changed, 13 insertions, 5 deletions
diff --git a/interfaces/ruby/models/comment.rb b/interfaces/ruby/models/comment.rb
index b3977f6..f7f9e13 100644
--- a/interfaces/ruby/models/comment.rb
+++ b/interfaces/ruby/models/comment.rb
@@ -1,18 +1,26 @@
class Comment < ActiveRecord::Base
belongs_to :post
+ belongs_to :user, optional: true
+
validates_presence_of :date
- validates_presence_of :author
- validates_presence_of :email
validates_format_of :email, with: /@/
validates_presence_of :body
+ validate :user_or_guest
+
+private
+
+ def user_or_guest
+ user.present? or (author.present? and email.present?)
+ end
end
class CreateCommentTable < ActiveRecord::Migration[6.0]
def change
create_table :comments do |t|
- t.references :post, foreign_key: true, index: true
- t.string :author, null: false
- t.string :email, null: false
+ t.references :post, foreign_key: true, index: true, null: false
+ t.references :user, foreign_key: true
+ t.string :author
+ t.string :email
t.string :body, null: false
t.timestamps
end