diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-07-06 15:42:33 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-07-06 15:42:33 -0400 |
commit | 2586fba3b8a181289c597071733774b3a406f675 (patch) | |
tree | b7f1d64b3caf617ae36a20b44cf20a24d0b14dfc /app | |
parent | c8d2050d147f65b141e8b8fcd229524de3842e7a (diff) | |
download | thoughts-2586fba3b8a181289c597071733774b3a406f675.tar.gz thoughts-2586fba3b8a181289c597071733774b3a406f675.tar.bz2 thoughts-2586fba3b8a181289c597071733774b3a406f675.zip |
Abstracted Recordable concern
Blogs, streams, and updates are all recordable, and there's no need to repeat code in the models for this.
Diffstat (limited to 'app')
-rw-r--r-- | app/models/blog.rb | 4 | ||||
-rw-r--r-- | app/models/concerns/recordable.rb | 9 | ||||
-rw-r--r-- | app/models/stream.rb | 5 | ||||
-rw-r--r-- | app/models/update.rb | 5 |
4 files changed, 14 insertions, 9 deletions
diff --git a/app/models/blog.rb b/app/models/blog.rb index 322a808..5742879 100644 --- a/app/models/blog.rb +++ b/app/models/blog.rb | |||
@@ -1,12 +1,10 @@ | |||
1 | class Blog < ApplicationRecord | 1 | class Blog < ApplicationRecord |
2 | has_many :records, as: :recordable, inverse_of: :recordable | 2 | include Recordable |
3 | 3 | ||
4 | validates :title, presence: true | 4 | validates :title, presence: true |
5 | validates :body, presence: true, if: :published | 5 | validates :body, presence: true, if: :published |
6 | validates :slug, presence: true, format: /\A[-a-z0-9]+\z/, if: :published | 6 | validates :slug, presence: true, format: /\A[-a-z0-9]+\z/, if: :published |
7 | 7 | ||
8 | accepts_nested_attributes_for :records, allow_destroy: true | ||
9 | |||
10 | before_validation :set_draft_title | 8 | before_validation :set_draft_title |
11 | before_save :set_published_at | 9 | before_save :set_published_at |
12 | 10 | ||
diff --git a/app/models/concerns/recordable.rb b/app/models/concerns/recordable.rb new file mode 100644 index 0000000..bbbb582 --- /dev/null +++ b/app/models/concerns/recordable.rb | |||
@@ -0,0 +1,9 @@ | |||
1 | module Recordable | ||
2 | extend ActiveSupport::Concern | ||
3 | |||
4 | included do | ||
5 | has_many :records, as: :recordable, inverse_of: :recordable | ||
6 | |||
7 | accepts_nested_attributes_for :records, allow_destroy: true | ||
8 | end | ||
9 | end | ||
diff --git a/app/models/stream.rb b/app/models/stream.rb index 7faa370..1398b75 100644 --- a/app/models/stream.rb +++ b/app/models/stream.rb | |||
@@ -1,12 +1,11 @@ | |||
1 | class Stream < ApplicationRecord | 1 | class Stream < ApplicationRecord |
2 | has_many :records, as: :recordable, inverse_of: :recordable | 2 | include Recordable |
3 | |||
3 | has_many :updates | 4 | has_many :updates |
4 | 5 | ||
5 | validates :title, presence: true | 6 | validates :title, presence: true |
6 | validates :slug, presence: true, format: /\A[-a-z0-9]+\z/ | 7 | validates :slug, presence: true, format: /\A[-a-z0-9]+\z/ |
7 | 8 | ||
8 | accepts_nested_attributes_for :records, allow_destroy: true | ||
9 | |||
10 | def path | 9 | def path |
11 | "/thinks/#{slug}" | 10 | "/thinks/#{slug}" |
12 | end | 11 | end |
diff --git a/app/models/update.rb b/app/models/update.rb index 41cc453..73c4911 100644 --- a/app/models/update.rb +++ b/app/models/update.rb | |||
@@ -1,11 +1,10 @@ | |||
1 | class Update < ApplicationRecord | 1 | class Update < ApplicationRecord |
2 | has_many :records, as: :recordable, inverse_of: :recordable | 2 | include Recordable |
3 | |||
3 | belongs_to :stream | 4 | belongs_to :stream |
4 | 5 | ||
5 | validates :stream, :body, presence: true | 6 | validates :stream, :body, presence: true |
6 | 7 | ||
7 | accepts_nested_attributes_for :records, allow_destroy: true | ||
8 | |||
9 | def path | 8 | def path |
10 | "/thinks/#{stream.slug}\#update-#{id}" | 9 | "/thinks/#{stream.slug}\#update-#{id}" |
11 | end | 10 | end |