Make Milestones general, and not specific to Budget Investments

Generalize the Budget::Investment::Milestone model to a
polymorphic Milestone model so it can be used for entities
other than Budget::Investment.
This commit is contained in:
Marko Lovic
2018-07-16 15:00:56 +02:00
committed by Javi Martín
parent 81f516efd7
commit c0f6fa182f
23 changed files with 176 additions and 122 deletions

View File

@@ -2,19 +2,19 @@ class Admin::BudgetInvestmentMilestonesController < Admin::BaseController
include Translatable
before_action :load_budget_investment, only: [:index, :new, :create, :edit, :update, :destroy]
before_action :load_budget_investment_milestone, only: [:edit, :update, :destroy]
before_action :load_milestone, only: [:edit, :update, :destroy]
before_action :load_statuses, only: [:index, :new, :create, :edit, :update]
def index
end
def new
@milestone = Budget::Investment::Milestone.new
@milestone = Milestone.new
end
def create
@milestone = Budget::Investment::Milestone.new(milestone_params)
@milestone.investment = @investment
@milestone = Milestone.new(milestone_params)
@milestone.milestoneable = @investment
if @milestone.save
redirect_to admin_budget_budget_investment_path(@investment.budget, @investment),
notice: t('admin.milestones.create.notice')
@@ -47,22 +47,22 @@ class Admin::BudgetInvestmentMilestonesController < Admin::BaseController
image_attributes = [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy]
documents_attributes = [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy]
attributes = [:publication_date, :budget_investment_id, :status_id,
translation_params(Budget::Investment::Milestone),
translation_params(Milestone),
image_attributes: image_attributes, documents_attributes: documents_attributes]
params.require(:budget_investment_milestone).permit(*attributes)
params.require(:milestone).permit(*attributes)
end
def load_budget_investment
@investment = Budget::Investment.find(params[:budget_investment_id])
end
def load_budget_investment_milestone
def load_milestone
@milestone = get_milestone
end
def get_milestone
Budget::Investment::Milestone.find(params[:id])
Milestone.find(params[:id])
end
def resource

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: 'Milestone::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

@@ -13,7 +13,7 @@
<%= f.translatable_fields do |translations_form| %>
<%= translations_form.hidden_field :title, value: l(Time.current, format: :datetime),
maxlength: Budget::Investment::Milestone.title_max_length %>
maxlength: Milestone.title_max_length %>
<%= translations_form.text_area :description,
rows: 5,

View File

@@ -18,7 +18,7 @@
<td class="text-center"><%= milestone.id %></td>
<td>
<%= link_to milestone.title,
edit_admin_budget_budget_investment_budget_investment_milestone_path(@investment.budget,
edit_admin_budget_budget_investment_milestone_path(@investment.budget,
@investment,
milestone) %>
</td>
@@ -46,7 +46,7 @@
</td>
<td class="small-2">
<%= link_to t("admin.milestones.index.delete"),
admin_budget_budget_investment_budget_investment_milestone_path(@investment.budget,
admin_budget_budget_investment_milestone_path(@investment.budget,
@investment,
milestone),
method: :delete,

View File

@@ -63,6 +63,6 @@
<p>
<%= link_to t("admin.budget_investments.show.new_milestone"),
new_admin_budget_budget_investment_budget_investment_milestone_path(@budget, @investment),
new_admin_budget_budget_investment_milestone_path(@budget, @investment),
class: "button hollow" %>
</p>