Merge pull request #3057 from consul/backport-make-milestones-polymorphic
[Backport] Make milestones polymorphic
This commit is contained in:
@@ -24,6 +24,7 @@ class Budget
|
||||
include Notifiable
|
||||
include Filterable
|
||||
include Flaggable
|
||||
include Milestoneable
|
||||
|
||||
belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id'
|
||||
belongs_to :heading
|
||||
@@ -40,8 +41,6 @@ class Budget
|
||||
has_many :comments, -> {where(valuation: false)}, as: :commentable, class_name: 'Comment'
|
||||
has_many :valuations, -> {where(valuation: true)}, as: :commentable, class_name: 'Comment'
|
||||
|
||||
has_many :milestones
|
||||
|
||||
validates :title, presence: true
|
||||
validates :author, presence: true
|
||||
validates :description, presence: true
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
class Budget
|
||||
class Investment
|
||||
class Milestone < ActiveRecord::Base
|
||||
include Imageable
|
||||
include Documentable
|
||||
documentable max_documents_allowed: 3,
|
||||
max_file_size: 3.megabytes,
|
||||
accepted_content_types: [ "application/pdf" ]
|
||||
|
||||
translates :title, :description, touch: true
|
||||
include Globalizable
|
||||
|
||||
belongs_to :investment
|
||||
belongs_to :status, class_name: 'Budget::Investment::Status'
|
||||
|
||||
validates :investment, presence: true
|
||||
validates :publication_date, presence: true
|
||||
validate :description_or_status_present?
|
||||
|
||||
scope :order_by_publication_date, -> { order(publication_date: :asc, created_at: :asc) }
|
||||
scope :published, -> { where("publication_date <= ?", Date.current) }
|
||||
scope :with_status, -> { where("status_id IS NOT NULL") }
|
||||
|
||||
def self.title_max_length
|
||||
80
|
||||
end
|
||||
|
||||
def description_or_status_present?
|
||||
unless description.present? || status_id.present?
|
||||
errors.add(:description)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
7
app/models/concerns/milestoneable.rb
Normal file
7
app/models/concerns/milestoneable.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
module Milestoneable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
has_many :milestones, as: :milestoneable, dependent: :destroy
|
||||
end
|
||||
end
|
||||
31
app/models/milestone.rb
Normal file
31
app/models/milestone.rb
Normal file
@@ -0,0 +1,31 @@
|
||||
class Milestone < ActiveRecord::Base
|
||||
include Imageable
|
||||
include Documentable
|
||||
documentable max_documents_allowed: 3,
|
||||
max_file_size: 3.megabytes,
|
||||
accepted_content_types: [ "application/pdf" ]
|
||||
|
||||
translates :title, :description, touch: true
|
||||
include Globalizable
|
||||
|
||||
belongs_to :milestoneable, polymorphic: true
|
||||
belongs_to :status
|
||||
|
||||
validates :milestoneable, presence: true
|
||||
validates :publication_date, presence: true
|
||||
validate :description_or_status_present?
|
||||
|
||||
scope :order_by_publication_date, -> { order(publication_date: :asc, created_at: :asc) }
|
||||
scope :published, -> { where("publication_date <= ?", Date.current) }
|
||||
scope :with_status, -> { where("status_id IS NOT NULL") }
|
||||
|
||||
def self.title_max_length
|
||||
80
|
||||
end
|
||||
|
||||
def description_or_status_present?
|
||||
unless description.present? || status_id.present?
|
||||
errors.add(:description)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,4 +1,4 @@
|
||||
class Budget::Investment::Status < ActiveRecord::Base
|
||||
class Milestone::Status < ActiveRecord::Base
|
||||
acts_as_paranoid column: :hidden_at
|
||||
|
||||
has_many :milestones
|
||||
Reference in New Issue
Block a user