Added interface and methods for edit the name of a Budget::Group if the budget is still inthe drafting phase

This commit is contained in:
Raúl Fuentes
2018-02-13 13:51:47 +01:00
committed by Bertocq
parent 089cef8ead
commit 6d8d44a5e3
10 changed files with 62 additions and 19 deletions

View File

@@ -8,10 +8,22 @@ class Admin::BudgetGroupsController < Admin::BaseController
@groups = @budget.groups.includes(:headings)
end
def update
@group = Budget::Group.by_slug(params[:id]).first
if @group.generate_slug?
params[:id] = @group.generate_slug
end
@group.update(budget_group_params)
end
private
def budget_group_params
params.require(:budget_group).permit(:name)
end
def load_budget
@budget = Budget.find_by(slug: params[:budget_id]) || Budget.find_by(id: params[:budget_id])
end
end

View File

@@ -72,6 +72,10 @@ module AdminHelper
user_roles(user).join(", ")
end
def display_budget_goup_form(group)
group.errors.messages.size > 0 ? "" : "display:none"
end
private
def namespace

View File

@@ -10,14 +10,27 @@ class Budget
validates :name, presence: true, uniqueness: { scope: :budget }
validates :slug, presence: true, format: /\A[a-z0-9\-_]+\z/
scope :by_slug, ->(slug) { where(slug: slug) }
before_save :strip_name
def to_param
slug
end
def single_heading_group?
headings.count == 1
end
private
def generate_slug?
slug.nil? || budget.drafting?
end
private
def strip_name
self.name = self.name.strip
end
end
end

View File

@@ -0,0 +1,6 @@
<% if @group.errors.any? %>
$("#group-form-<%= @group.id %>").html('<%= j render("admin/budgets/group_form", group: @group, budget: @group.budget, button_title: "admin.budgets.form.submit", id: "group-form-#{@group.id}", css_class: "group-toggle-#{@group.id}" ) %>');
<% else %>
$("#group-name-<%= @group.id %>").html('<%= @group.name %>')
$(".group-toggle-<%= @group.id %>").toggle()
<% end %>

View File

@@ -2,8 +2,10 @@
<thead>
<tr>
<th colspan="4" class="with-button">
<%= group.name %>
<%= content_tag(:span, group.name, class:"group-toggle-#{group.id}", id:"group-name-#{group.id}") %>
<%= render 'admin/budgets/group_form', budget: @budget, group: group, id: "group-form-#{group.id}", button_title: "admin.budgets.form.submit", css_class: "group-toggle-#{group.id}" %>
<%= link_to t("admin.budgets.form.add_heading"), "#", class: "button float-right js-toggle-link", data: { "toggle-selector" => "#group-#{group.id}-new-heading-form" } %>
<%= link_to t("admin.budgets.form.edit_group"), "#", class: "button float-right js-toggle-link", data: { "toggle-selector" => ".group-toggle-#{group.id}" } %>
</th>
</tr>

View File

@@ -0,0 +1,15 @@
<%= form_for [:admin, budget, group], html: {id: id, style: display_budget_goup_form(group), class: css_class}, remote: true do |f| %>
<%= f.label :name, t("admin.budgets.form.group") %>
<div class="input-group">
<%= f.text_field :name,
label: false,
maxlength: 50,
placeholder: t("admin.budgets.form.group"),
class: "input-group-field" %>
<div class="input-group-button">
<%= f.submit t(button_title), class: "button success" %>
</div>
</div>
<% end %>

View File

@@ -10,21 +10,7 @@
<%= link_to t("admin.budgets.form.add_group"), "#", class: "button float-right js-toggle-link", data: { "toggle-selector" => "#new-group-form" } %>
<% end %>
<%= form_for [:admin, @budget, Budget::Group.new], html: {id: "new-group-form", style: "display:none"}, remote: true do |f| %>
<%= f.label :name, t("admin.budgets.form.group") %>
<div class="input-group">
<%= f.text_field :name,
label: false,
maxlength: 50,
placeholder: t("admin.budgets.form.group"),
class: "input-group-field" %>
<div class="input-group-button">
<%= f.submit t("admin.budgets.form.create_group"), class: "button success" %>
</div>
</div>
<% end %>
<%= render 'admin/budgets/group_form', budget: @budget, group: Budget::Group.new, id: "new-group-form", button_title: "admin.budgets.form.create_group", css_class: '' %>
<% groups.each do |group| %>
<div id="<%= dom_id(group) %>">

View File

@@ -106,6 +106,8 @@ en:
no_groups: No groups created yet. Each user will be able to vote in only one heading per group.
add_group: Add new group
create_group: Create group
edit_group: Edit group
submit: Save group
heading: Heading name
add_heading: Add heading
amount: Amount

View File

@@ -106,6 +106,8 @@ es:
no_groups: No hay grupos creados todavía. Cada usuario podrá votar en una sola partida de cada grupo.
add_group: Añadir nuevo grupo
create_group: Crear grupo
edit_group: Editar grupo
submit: Guardar grupo
heading: Nombre de la partida
add_heading: Añadir partida
amount: Cantidad

View File

@@ -903,6 +903,7 @@ ActiveRecord::Schema.define(version: 20180220211105) do
t.integer "related_content_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "flags_count", default: 0
t.datetime "hidden_at"
t.integer "related_content_scores_count", default: 0
t.integer "author_id"