diff --git a/app/controllers/admin/budget_phases_controller.rb b/app/controllers/admin/budget_phases_controller.rb index d0eef18d8..3dd08675b 100644 --- a/app/controllers/admin/budget_phases_controller.rb +++ b/app/controllers/admin/budget_phases_controller.rb @@ -1,4 +1,5 @@ class Admin::BudgetPhasesController < Admin::BaseController + include Translatable before_action :load_phase, only: [:edit, :update] @@ -21,8 +22,8 @@ class Admin::BudgetPhasesController < Admin::BaseController end def budget_phase_params - valid_attributes = [:starts_at, :ends_at, :summary, :description, :enabled] - params.require(:budget_phase).permit(*valid_attributes) + valid_attributes = [:starts_at, :ends_at, :enabled] + params.require(:budget_phase).permit(*valid_attributes, translation_params(Budget::Phase)) end end diff --git a/app/models/budget/phase.rb b/app/models/budget/phase.rb index 432c4b609..de089413a 100644 --- a/app/models/budget/phase.rb +++ b/app/models/budget/phase.rb @@ -6,20 +6,22 @@ class Budget SUMMARY_MAX_LENGTH = 1000 DESCRIPTION_MAX_LENGTH = 2000 + translates :summary, touch: true + translates :description, touch: true + include Globalizable + belongs_to :budget belongs_to :next_phase, class_name: 'Budget::Phase', foreign_key: :next_phase_id has_one :prev_phase, class_name: 'Budget::Phase', foreign_key: :next_phase_id + validates_translation :summary, length: { maximum: SUMMARY_MAX_LENGTH } + validates_translation :description, length: { maximum: DESCRIPTION_MAX_LENGTH } validates :budget, presence: true validates :kind, presence: true, uniqueness: { scope: :budget }, inclusion: { in: PHASE_KINDS } - validates :summary, length: { maximum: SUMMARY_MAX_LENGTH } - validates :description, length: { maximum: DESCRIPTION_MAX_LENGTH } validate :invalid_dates_range? validate :prev_phase_dates_valid? validate :next_phase_dates_valid? - before_validation :sanitize_description - after_save :adjust_date_ranges after_save :touch_budget @@ -87,8 +89,5 @@ class Budget end end - def sanitize_description - self.description = WYSIWYGSanitizer.new.sanitize(description) - end end end diff --git a/app/models/budget/phase/translation.rb b/app/models/budget/phase/translation.rb new file mode 100644 index 000000000..53390143a --- /dev/null +++ b/app/models/budget/phase/translation.rb @@ -0,0 +1,9 @@ +class Budget::Phase::Translation < Globalize::ActiveRecord::Translation + before_validation :sanitize_description + + private + + def sanitize_description + self.description = WYSIWYGSanitizer.new.sanitize(description) + end +end diff --git a/app/views/admin/budget_phases/_form.html.erb b/app/views/admin/budget_phases/_form.html.erb index a962353af..5bcd5ed3b 100644 --- a/app/views/admin/budget_phases/_form.html.erb +++ b/app/views/admin/budget_phases/_form.html.erb @@ -1,4 +1,8 @@ -<%= form_for [:admin, @phase.budget, @phase] do |f| %> +<%= render "admin/shared/globalize_locales", resource: @phase %> + +<%= translatable_form_for [:admin, @phase.budget, @phase] do |f| %> + + <%= render 'shared/errors', resource: @phase %>
<%= f.label :starts_at, t("admin.budget_phases.edit.start_date") %> @@ -18,32 +22,34 @@
- <%= f.label :description, t("admin.budget_phases.edit.description") %> + <%= f.translatable_fields do |translations_form| %> - - <%= t("admin.budget_phases.edit.description_help_text") %> - + <%= f.label :description, t("admin.budget_phases.edit.description") %> - <%= f.cktext_area :description, - maxlength: Budget::Phase::DESCRIPTION_MAX_LENGTH, - ckeditor: { language: I18n.locale }, - label: false %> + + <%= t("admin.budget_phases.edit.description_help_text") %> + + +
+ <%= translations_form.cktext_area :description, + maxlength: Budget::Phase::DESCRIPTION_MAX_LENGTH, + label: false %> +
+ + <%= f.label :summary, t("admin.budget_phases.edit.summary") %> + + + <%= t("admin.budget_phases.edit.summary_help_text") %> + + +
+ <%= translations_form.cktext_area :summary, + maxlength: Budget::Phase::SUMMARY_MAX_LENGTH, + label: false%> +
+ <% end %>
-
- <%= f.label :summary, t("admin.budget_phases.edit.summary") %> - - - <%= t("admin.budget_phases.edit.summary_help_text") %> - - - <%= f.cktext_area :summary, - maxlength: Budget::Phase::SUMMARY_MAX_LENGTH, - ckeditor: { language: I18n.locale }, - label: false %> -
- -
<%= f.check_box :enabled, label: t("admin.budget_phases.edit.enabled") %> diff --git a/config/initializers/routes_hierarchy.rb b/config/initializers/routes_hierarchy.rb index 06a7acc74..d2a95e395 100644 --- a/config/initializers/routes_hierarchy.rb +++ b/config/initializers/routes_hierarchy.rb @@ -5,7 +5,7 @@ module ActionDispatch::Routing::UrlFor def resource_hierarchy_for(resource) case resource.class.name - when "Budget::Investment" + when "Budget::Investment", "Budget::Phase" [resource.budget, resource] when "Milestone" [*resource_hierarchy_for(resource.milestoneable), resource] diff --git a/db/dev_seeds/budgets.rb b/db/dev_seeds/budgets.rb index 7bbc8cc10..72fb523cc 100644 --- a/db/dev_seeds/budgets.rb +++ b/db/dev_seeds/budgets.rb @@ -39,6 +39,18 @@ section "Creating Budgets" do phase: "accepting" ) + Budget.find_each do |budget| + budget.phases.each do |phase| + random_locales.map do |locale| + Globalize.with_locale(locale) do + phase.description = "Description for locale #{locale}" + phase.summary = "Summary for locale #{locale}" + phase.save! + end + end + end + end + Budget.all.each do |budget| city_group = budget.groups.create!(name: I18n.t('seeds.budgets.groups.all_city')) city_group.headings.create!(name: I18n.t('seeds.budgets.groups.all_city'), diff --git a/db/migrate/20190119160418_add_budget_phase_translations.rb b/db/migrate/20190119160418_add_budget_phase_translations.rb new file mode 100644 index 000000000..45fff7db1 --- /dev/null +++ b/db/migrate/20190119160418_add_budget_phase_translations.rb @@ -0,0 +1,17 @@ +class AddBudgetPhaseTranslations < ActiveRecord::Migration + + def self.up + Budget::Phase.create_translation_table!( + { + description: :text, + summary: :text + }, + { migrate_data: true } + ) + end + + def self.down + Budget::Phase.drop_translation_table! + end + +end diff --git a/db/schema.rb b/db/schema.rb index e49f135de..02e3db15b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -242,6 +242,18 @@ ActiveRecord::Schema.define(version: 20190205131722) do add_index "budget_investments", ["heading_id"], name: "index_budget_investments_on_heading_id", using: :btree add_index "budget_investments", ["tsv"], name: "index_budget_investments_on_tsv", using: :gin + create_table "budget_phase_translations", force: :cascade do |t| + t.integer "budget_phase_id", null: false + t.string "locale", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.text "description" + t.text "summary" + end + + add_index "budget_phase_translations", ["budget_phase_id"], name: "index_budget_phase_translations_on_budget_phase_id", using: :btree + add_index "budget_phase_translations", ["locale"], name: "index_budget_phase_translations_on_locale", using: :btree + create_table "budget_phases", force: :cascade do |t| t.integer "budget_id" t.integer "next_phase_id" diff --git a/spec/features/admin/budget_phases_spec.rb b/spec/features/admin/budget_phases_spec.rb index c22037936..b647b4a79 100644 --- a/spec/features/admin/budget_phases_spec.rb +++ b/spec/features/admin/budget_phases_spec.rb @@ -10,13 +10,19 @@ feature 'Admin budget phases' do login_as(admin.user) end - scenario 'Update phase' do + it_behaves_like "translatable", + "budget_phase", + "edit_admin_budget_budget_phase_path", + [], + { "description" => :ckeditor, "summary" => :ckeditor } + + scenario "Update phase", :js do visit edit_admin_budget_budget_phase_path(budget, budget.current_phase) fill_in 'start_date', with: Date.current + 1.days fill_in 'end_date', with: Date.current + 12.days - fill_in 'budget_phase_summary', with: 'This is the summary of the phase.' - fill_in 'budget_phase_description', with: 'This is the description of the phase.' + fill_in_translatable_ckeditor "summary", :en, with: "New summary of the phase." + fill_in_translatable_ckeditor "description", :en, with: "New description of the phase." uncheck 'budget_phase_enabled' click_button 'Save changes' @@ -25,8 +31,8 @@ feature 'Admin budget phases' do expect(budget.current_phase.starts_at.to_date).to eq((Date.current + 1.days).to_date) expect(budget.current_phase.ends_at.to_date).to eq((Date.current + 12.days).to_date) - expect(budget.current_phase.summary).to eq('This is the summary of the phase.') - expect(budget.current_phase.description).to eq('This is the description of the phase.') + expect(budget.current_phase.summary).to include("New summary of the phase.") + expect(budget.current_phase.description).to include("New description of the phase.") expect(budget.current_phase.enabled).to be(false) end end diff --git a/spec/shared/features/translatable.rb b/spec/shared/features/translatable.rb index 1f9c25928..b76467a9c 100644 --- a/spec/shared/features/translatable.rb +++ b/spec/shared/features/translatable.rb @@ -32,7 +32,16 @@ shared_examples "translatable" do |factory_name, path_name, input_fields, textar fields - optional_fields end - let(:translatable) { create(factory_name, attributes) } + let(:translatable) do + if factory_name == "budget_phase" + budget = create(:budget) + budget.phases.first.update attributes + budget.phases.first + else + create(factory_name, attributes) + end + end + let(:path) { send(path_name, *resource_hierarchy_for(translatable)) } before { login_as(create(:administrator).user) }