diff --git a/app/controllers/admin/budget_groups_controller.rb b/app/controllers/admin/budget_groups_controller.rb index 9c5a54b98..fb84c686c 100644 --- a/app/controllers/admin/budget_groups_controller.rb +++ b/app/controllers/admin/budget_groups_controller.rb @@ -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 -end \ No newline at end of file + def load_budget + @budget = Budget.find_by(slug: params[:budget_id]) || Budget.find_by(id: params[:budget_id]) + end + +end diff --git a/app/helpers/admin_helper.rb b/app/helpers/admin_helper.rb index 40c5f8b84..b1f07dd05 100644 --- a/app/helpers/admin_helper.rb +++ b/app/helpers/admin_helper.rb @@ -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 diff --git a/app/models/budget/group.rb b/app/models/budget/group.rb index dd96cdb95..6dc7ba61d 100644 --- a/app/models/budget/group.rb +++ b/app/models/budget/group.rb @@ -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 diff --git a/app/views/admin/budget_groups/update.js.erb b/app/views/admin/budget_groups/update.js.erb new file mode 100644 index 000000000..1bb7a7509 --- /dev/null +++ b/app/views/admin/budget_groups/update.js.erb @@ -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 %> diff --git a/app/views/admin/budgets/_group.html.erb b/app/views/admin/budgets/_group.html.erb index c3ce314aa..67d0aa587 100644 --- a/app/views/admin/budgets/_group.html.erb +++ b/app/views/admin/budgets/_group.html.erb @@ -2,8 +2,10 @@ - <%= 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}" } %> diff --git a/app/views/admin/budgets/_group_form.html.erb b/app/views/admin/budgets/_group_form.html.erb new file mode 100644 index 000000000..792511b73 --- /dev/null +++ b/app/views/admin/budgets/_group_form.html.erb @@ -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") %> + +
+ <%= f.text_field :name, + label: false, + maxlength: 50, + placeholder: t("admin.budgets.form.group"), + class: "input-group-field" %> +
+ <%= f.submit t(button_title), class: "button success" %> +
+
+<% end %> diff --git a/app/views/admin/budgets/_groups.html.erb b/app/views/admin/budgets/_groups.html.erb index e98c98334..0bc36b517 100644 --- a/app/views/admin/budgets/_groups.html.erb +++ b/app/views/admin/budgets/_groups.html.erb @@ -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") %> - -
- <%= f.text_field :name, - label: false, - maxlength: 50, - placeholder: t("admin.budgets.form.group"), - class: "input-group-field" %> -
- <%= f.submit t("admin.budgets.form.create_group"), class: "button success" %> -
-
-<% 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| %>
diff --git a/config/locales/en/admin.yml b/config/locales/en/admin.yml index 2058b917d..baa678996 100644 --- a/config/locales/en/admin.yml +++ b/config/locales/en/admin.yml @@ -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 diff --git a/config/locales/es/admin.yml b/config/locales/es/admin.yml index 399eda7bb..9fb9fae63 100644 --- a/config/locales/es/admin.yml +++ b/config/locales/es/admin.yml @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 612ca8e1e..dbc3134e1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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"