From 2586fba3b8a181289c597071733774b3a406f675 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 6 Jul 2018 15:42:33 -0400 Subject: Abstracted Recordable concern Blogs, streams, and updates are all recordable, and there's no need to repeat code in the models for this. --- app/models/blog.rb | 4 +--- app/models/concerns/recordable.rb | 9 +++++++++ app/models/stream.rb | 5 ++--- app/models/update.rb | 5 ++--- 4 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 app/models/concerns/recordable.rb (limited to 'app') 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 @@ class Blog < ApplicationRecord - has_many :records, as: :recordable, inverse_of: :recordable + include Recordable validates :title, presence: true validates :body, presence: true, if: :published validates :slug, presence: true, format: /\A[-a-z0-9]+\z/, if: :published - accepts_nested_attributes_for :records, allow_destroy: true - before_validation :set_draft_title before_save :set_published_at 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 @@ +module Recordable + extend ActiveSupport::Concern + + included do + has_many :records, as: :recordable, inverse_of: :recordable + + accepts_nested_attributes_for :records, allow_destroy: true + end +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 @@ class Stream < ApplicationRecord - has_many :records, as: :recordable, inverse_of: :recordable + include Recordable + has_many :updates validates :title, presence: true validates :slug, presence: true, format: /\A[-a-z0-9]+\z/ - accepts_nested_attributes_for :records, allow_destroy: true - def path "/thinks/#{slug}" 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 @@ class Update < ApplicationRecord - has_many :records, as: :recordable, inverse_of: :recordable + include Recordable + belongs_to :stream validates :stream, :body, presence: true - accepts_nested_attributes_for :records, allow_destroy: true - def path "/thinks/#{stream.slug}\#update-#{id}" end -- cgit 1.4.1