diff --git a/app/controllers/admin/budget_investment_milestones_controller.rb b/app/controllers/admin/budget_investment_milestones_controller.rb index 23ef679cc..f354d42fa 100644 --- a/app/controllers/admin/budget_investment_milestones_controller.rb +++ b/app/controllers/admin/budget_investment_milestones_controller.rb @@ -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 diff --git a/app/controllers/admin/milestones_controller.rb b/app/controllers/admin/milestones_controller.rb new file mode 100644 index 000000000..13a277957 --- /dev/null +++ b/app/controllers/admin/milestones_controller.rb @@ -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 diff --git a/app/views/admin/budget_investment_milestones/edit.html.erb b/app/views/admin/budget_investment_milestones/edit.html.erb deleted file mode 100644 index d15b5a66a..000000000 --- a/app/views/admin/budget_investment_milestones/edit.html.erb +++ /dev/null @@ -1,7 +0,0 @@ -<%= back_link_to admin_budget_budget_investment_path(@investment.budget, @investment) %> - -

<%= t("admin.milestones.edit.title") %>

- -
- <%= render '/admin/budget_investment_milestones/form' %> -
diff --git a/app/views/admin/budget_investments/_milestones.html.erb b/app/views/admin/budget_investments/_milestones.html.erb index 20a870d71..1bba1bc18 100644 --- a/app/views/admin/budget_investments/_milestones.html.erb +++ b/app/views/admin/budget_investments/_milestones.html.erb @@ -18,9 +18,8 @@ <%= milestone.id %> <%= link_to milestone.title, - edit_admin_budget_budget_investment_milestone_path(@investment.budget, - @investment, - milestone) %> + polymorphic_path([:admin, *resource_hierarchy_for(milestone)], + action: :edit) %> <%= milestone.description %> @@ -46,9 +45,7 @@ <%= 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" %> diff --git a/app/views/admin/budget_investments/show.html.erb b/app/views/admin/budget_investments/show.html.erb index cb44745a1..c6462ff52 100644 --- a/app/views/admin/budget_investments/show.html.erb +++ b/app/views/admin/budget_investments/show.html.erb @@ -63,6 +63,7 @@

<%= 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" %>

diff --git a/app/views/admin/budget_investment_milestones/_form.html.erb b/app/views/admin/milestones/_form.html.erb similarity index 93% rename from app/views/admin/budget_investment_milestones/_form.html.erb rename to app/views/admin/milestones/_form.html.erb index c62ba3521..5006b8fa1 100644 --- a/app/views/admin/budget_investment_milestones/_form.html.erb +++ b/app/views/admin/milestones/_form.html.erb @@ -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| %>
<%= f.select :status_id, diff --git a/app/views/admin/milestones/edit.html.erb b/app/views/admin/milestones/edit.html.erb new file mode 100644 index 000000000..297ebccb4 --- /dev/null +++ b/app/views/admin/milestones/edit.html.erb @@ -0,0 +1,7 @@ +<%= back_link_to milestoneable_path %> + +

<%= t("admin.milestones.edit.title") %>

+ +
+ <%= render "form" %> +
diff --git a/app/views/admin/budget_investment_milestones/new.html.erb b/app/views/admin/milestones/new.html.erb similarity index 63% rename from app/views/admin/budget_investment_milestones/new.html.erb rename to app/views/admin/milestones/new.html.erb index 7e065a605..aa55123c9 100644 --- a/app/views/admin/budget_investment_milestones/new.html.erb +++ b/app/views/admin/milestones/new.html.erb @@ -1,7 +1,7 @@
- <%= back_link_to admin_budget_budget_investment_path(@investment.budget, @investment) %> + <%= back_link_to milestoneable_path %>

<%= t("admin.milestones.new.creating") %>

diff --git a/config/initializers/routes_hierarchy.rb b/config/initializers/routes_hierarchy.rb index e9892ef38..0a712f923 100644 --- a/config/initializers/routes_hierarchy.rb +++ b/config/initializers/routes_hierarchy.rb @@ -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"