Merge pull request #3057 from consul/backport-make-milestones-polymorphic

[Backport] Make milestones polymorphic
This commit is contained in:
Javier Martín
2018-12-05 12:12:04 +01:00
committed by GitHub
87 changed files with 741 additions and 443 deletions

View File

@@ -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

View File

@@ -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

View 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
View 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

View File

@@ -1,4 +1,4 @@
class Budget::Investment::Status < ActiveRecord::Base
class Milestone::Status < ActiveRecord::Base
acts_as_paranoid column: :hidden_at
has_many :milestones