Merge pull request #3083 from consul/backport-milestone_polymorphic_path

Make milestones controller polymorphic
This commit is contained in:
Javier Martín
2018-12-10 18:37:15 +01:00
committed by GitHub
9 changed files with 90 additions and 88 deletions

View File

@@ -1,76 +1,8 @@
class Admin::BudgetInvestmentMilestonesController < Admin::BaseController
include Translatable
before_action :load_budget_investment, only: [:index, :new, :create, :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 = Milestone.new
end
def create
@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')
else
render :new
end
end
def edit
end
def update
if @milestone.update(milestone_params)
redirect_to admin_budget_budget_investment_path(@investment.budget, @investment),
notice: t('admin.milestones.update.notice')
else
render :edit
end
end
def destroy
@milestone.destroy
redirect_to admin_budget_budget_investment_path(@investment.budget, @investment),
notice: t('admin.milestones.delete.notice')
end
class Admin::BudgetInvestmentMilestonesController < Admin::MilestonesController
private
def milestone_params
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(Milestone),
image_attributes: image_attributes, documents_attributes: documents_attributes]
params.require(:milestone).permit(*attributes)
def milestoneable
Budget::Investment.find(params[:budget_investment_id])
end
def load_budget_investment
@investment = Budget::Investment.find(params[:budget_investment_id])
end
def load_milestone
@milestone = get_milestone
end
def get_milestone
Milestone.find(params[:id])
end
def resource
get_milestone
end
def load_statuses
@statuses = Milestone::Status.all
end
end

View File

@@ -0,0 +1,72 @@
class Admin::MilestonesController < Admin::BaseController
include Translatable
before_action :load_milestoneable, only: [:index, :new, :create, :edit, :update, :destroy]
before_action :load_milestone, only: [:edit, :update, :destroy]
before_action :load_statuses, only: [:index, :new, :create, :edit, :update]
helper_method :milestoneable_path
def index
end
def new
@milestone = @milestoneable.milestones.new
end
def create
@milestone = @milestoneable.milestones.new(milestone_params)
if @milestone.save
redirect_to milestoneable_path, notice: t('admin.milestones.create.notice')
else
render :new
end
end
def edit
end
def update
if @milestone.update(milestone_params)
redirect_to milestoneable_path, notice: t('admin.milestones.update.notice')
else
render :edit
end
end
def destroy
@milestone.destroy
redirect_to milestoneable_path, notice: t('admin.milestones.delete.notice')
end
private
def milestone_params
image_attributes = [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy]
documents_attributes = [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy]
attributes = [:publication_date, :status_id,
translation_params(Milestone),
image_attributes: image_attributes, documents_attributes: documents_attributes]
params.require(:milestone).permit(*attributes)
end
def load_milestoneable
@milestoneable = milestoneable
end
def milestoneable
raise "Implement in subclass"
end
def load_milestone
@milestone = @milestoneable.milestones.find(params[:id])
end
def load_statuses
@statuses = Milestone::Status.all
end
def milestoneable_path
polymorphic_path([:admin, *resource_hierarchy_for(@milestone.milestoneable)])
end
end

View File

@@ -1,7 +0,0 @@
<%= back_link_to admin_budget_budget_investment_path(@investment.budget, @investment) %>
<h2><%= t("admin.milestones.edit.title") %></h2>
<div class="milestone-edit">
<%= render '/admin/budget_investment_milestones/form' %>
</div>

View File

@@ -18,9 +18,8 @@
<td class="text-center"><%= milestone.id %></td>
<td>
<%= link_to milestone.title,
edit_admin_budget_budget_investment_milestone_path(@investment.budget,
@investment,
milestone) %>
polymorphic_path([:admin, *resource_hierarchy_for(milestone)],
action: :edit) %>
</td>
<td class="small small-5"><%= milestone.description %></td>
<td class="small">
@@ -46,9 +45,7 @@
</td>
<td class="small-2">
<%= link_to t("admin.milestones.index.delete"),
admin_budget_budget_investment_milestone_path(@investment.budget,
@investment,
milestone),
polymorphic_path([:admin, *resource_hierarchy_for(milestone)]),
method: :delete,
class: "button hollow alert expanded" %>
</td>

View File

@@ -63,6 +63,7 @@
<p>
<%= link_to t("admin.budget_investments.show.new_milestone"),
new_admin_budget_budget_investment_milestone_path(@budget, @investment),
polymorphic_path([:admin, *resource_hierarchy_for(@investment.milestones.new)],
action: :new),
class: "button hollow" %>
</p>

View File

@@ -1,6 +1,6 @@
<%= render "admin/shared/globalize_locales", resource: @milestone %>
<%= translatable_form_for [:admin, @investment.budget, @investment, @milestone] do |f| %>
<%= translatable_form_for [:admin, *resource_hierarchy_for(@milestone)] do |f| %>
<div class="small-12 medium-6 margin-bottom">
<%= f.select :status_id,

View File

@@ -0,0 +1,7 @@
<%= back_link_to milestoneable_path %>
<h2><%= t("admin.milestones.edit.title") %></h2>
<div class="milestone-edit">
<%= render "form" %>
</div>

View File

@@ -1,7 +1,7 @@
<div class="milestone-new row">
<div class="small-12 column">
<%= back_link_to admin_budget_budget_investment_path(@investment.budget, @investment) %>
<%= back_link_to milestoneable_path %>
<h1><%= t("admin.milestones.new.creating") %></h1>

View File

@@ -8,7 +8,7 @@ module ActionDispatch::Routing::UrlFor
when "Budget::Investment"
[resource.budget, resource]
when "Milestone"
[resource.milestoneable.budget, resource.milestoneable, resource]
[*resource_hierarchy_for(resource.milestoneable), resource]
when "Legislation::Annotation"
[resource.draft_version.process, resource.draft_version, resource]
when "Legislation::Proposal", "Legislation::Question", "Legislation::DraftVersion"