From 6478bb70c2a7a09b14df1bdc01ffa9c99d873d26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 10 Oct 2018 14:37:43 +0200 Subject: [PATCH] Update polls translatable fields The `:name` attribute is still allowed in the controller because some forks use it when creating a poll from a budget. --- .../admin/poll/polls_controller.rb | 6 ++--- app/models/poll.rb | 5 +++- app/views/admin/poll/polls/_form.html.erb | 23 +++++++++++-------- config/locales/en/activerecord.yml | 4 ++++ spec/features/admin/poll/polls_spec.rb | 8 +++---- 5 files changed, 29 insertions(+), 17 deletions(-) diff --git a/app/controllers/admin/poll/polls_controller.rb b/app/controllers/admin/poll/polls_controller.rb index e93a82d47..863f8769c 100644 --- a/app/controllers/admin/poll/polls_controller.rb +++ b/app/controllers/admin/poll/polls_controller.rb @@ -61,10 +61,10 @@ class Admin::Poll::PollsController < Admin::Poll::BaseController def poll_params image_attributes = [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy] - attributes = [:name, :starts_at, :ends_at, :geozone_restricted, :summary, :description, - :results_enabled, :stats_enabled, geozone_ids: [], + attributes = [:name, :starts_at, :ends_at, :geozone_restricted, :results_enabled, + :stats_enabled, geozone_ids: [], image_attributes: image_attributes] - params.require(:poll).permit(*attributes, *translation_params(Poll)) + params.require(:poll).permit(*attributes, translation_params(Poll)) end def search_params diff --git a/app/models/poll.rb b/app/models/poll.rb index 14b102260..8b5dad7dd 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -8,6 +8,7 @@ class Poll < ActiveRecord::Base translates :summary, touch: true translates :description, touch: true globalize_accessors + accepts_nested_attributes_for :translations, allow_destroy: true RECOUNT_DURATION = 1.week @@ -24,7 +25,9 @@ class Poll < ActiveRecord::Base has_and_belongs_to_many :geozones belongs_to :author, -> { with_hidden }, class_name: 'User', foreign_key: 'author_id' - validates :name, presence: true + translation_class.instance_eval do + validates :name, presence: true + end validate :date_range diff --git a/app/views/admin/poll/polls/_form.html.erb b/app/views/admin/poll/polls/_form.html.erb index 1f7520547..49939e8fd 100644 --- a/app/views/admin/poll/polls/_form.html.erb +++ b/app/views/admin/poll/polls/_form.html.erb @@ -1,9 +1,7 @@ <%= render "admin/shared/globalize_locales", resource: @poll %> <%= translatable_form_for [:admin, @poll] do |f| %> -
- <%= f.translatable_text_field :name %> -
+ <%= render "shared/errors", resource: @poll %>
@@ -19,13 +17,20 @@
-
- <%=f.translatable_text_area :summary, rows: 4%> -
-
- <%=f.translatable_text_area :description, rows: 8%> -
+ <%= f.translatable_fields do |translations_form| %> +
+ <%= translations_form.text_field :name %> +
+ +
+ <%= translations_form.text_area :summary, rows: 4 %> +
+ +
+ <%= translations_form.text_area :description, rows: 8 %> +
+ <% end %>
<%= render 'images/admin_image', imageable: @poll, f: f %> diff --git a/config/locales/en/activerecord.yml b/config/locales/en/activerecord.yml index 053020463..9bd206502 100644 --- a/config/locales/en/activerecord.yml +++ b/config/locales/en/activerecord.yml @@ -185,6 +185,10 @@ en: geozone_restricted: "Restricted by geozone" summary: "Summary" description: "Description" + poll/translation: + name: "Name" + summary: "Summary" + description: "Description" poll/question: title: "Question" summary: "Summary" diff --git a/spec/features/admin/poll/polls_spec.rb b/spec/features/admin/poll/polls_spec.rb index b28aff019..fad14a0c2 100644 --- a/spec/features/admin/poll/polls_spec.rb +++ b/spec/features/admin/poll/polls_spec.rb @@ -60,11 +60,11 @@ feature 'Admin polls' do start_date = 1.week.from_now end_date = 2.weeks.from_now - fill_in "poll_name_en", with: "Upcoming poll" + fill_in "Name", with: "Upcoming poll" fill_in 'poll_starts_at', with: start_date.strftime("%d/%m/%Y") fill_in 'poll_ends_at', with: end_date.strftime("%d/%m/%Y") - fill_in 'poll_summary_en', with: "Upcoming poll's summary. This poll..." - fill_in 'poll_description_en', with: "Upcomming poll's description. This poll..." + fill_in 'Summary', with: "Upcoming poll's summary. This poll..." + fill_in 'Description', with: "Upcomming poll's description. This poll..." expect(page).not_to have_css("#poll_results_enabled") expect(page).not_to have_css("#poll_stats_enabled") @@ -88,7 +88,7 @@ feature 'Admin polls' do expect(page).to have_css("img[alt='#{poll.image.title}']") - fill_in "poll_name_en", with: "Next Poll" + fill_in "Name", with: "Next Poll" fill_in 'poll_ends_at', with: end_date.strftime("%d/%m/%Y") click_button "Update poll"