From 5ec40595143500053fda156e0b0e69221137f373 Mon Sep 17 00:00:00 2001 From: Brice Sanchez Date: Thu, 30 Apr 2015 14:33:54 -0400 Subject: [PATCH] Add ability to customize and translate resource name --- changelog.md | 1 + .../refinery/admin/resources_controller.rb | 4 ++-- .../refinery/admin/resources_helper.rb | 11 +++++++++ resources/app/models/refinery/resource.rb | 4 +++- .../refinery/admin/resources/_form.html.erb | 13 ++++++++++- .../admin/resources/_resource.html.erb | 23 +++++++++++++++++-- resources/config/locales/en.yml | 2 ++ resources/config/locales/fr.yml | 2 ++ ...0180930_add_title_to_refinery_resources.rb | 7 ++++++ ...0430180959_translate_refinery_resources.rb | 13 +++++++++++ resources/lib/refinery/resources.rb | 4 ++++ resources/refinerycms-resources.gemspec | 1 + .../features/refinery/admin/resources_spec.rb | 4 ++-- 13 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 resources/app/helpers/refinery/admin/resources_helper.rb create mode 100644 resources/db/migrate/20150430180930_add_title_to_refinery_resources.rb create mode 100644 resources/db/migrate/20150430180959_translate_refinery_resources.rb diff --git a/changelog.md b/changelog.md index fc7fadbc34..dde2579897 100644 --- a/changelog.md +++ b/changelog.md @@ -13,6 +13,7 @@ * Limited the jquery-ui assets loaded in the backend to the ones we use in the core. [#2735](https://github.com/refinery/refinerycms/pull/2735). [Philip Arndt](https://github.com/parndt) * Moved the tabs to the left hand side of the screen. [#2734](https://github.com/refinery/refinerycms/pull/2734). [Isaac Freeman](https://github.com/isaacfreeman) & [Philip Arndt](https://github.com/parndt) & [Brice Sanchez](https://github.com/bricesanchez) * Add extra fields partial in Admin Pages form advanced options [#2943](https://github.com/refinery/refinerycms/pull/2943). [Brice Sanchez](https://github.com/bricesanchez) +* Added ability to customize and translate filename. [#2966](https://github.com/refinery/refinerycms/pull/2966). [Brice Sanchez](https://github.com/bricesanchez) * [See full list](https://github.com/refinery/refinerycms/compare/2-1-stable...master) diff --git a/resources/app/controllers/refinery/admin/resources_controller.rb b/resources/app/controllers/refinery/admin/resources_controller.rb index e2c53ec902..6fce6baa87 100644 --- a/resources/app/controllers/refinery/admin/resources_controller.rb +++ b/resources/app/controllers/refinery/admin/resources_controller.rb @@ -84,9 +84,9 @@ def paginate_resources(conditions = {}) def resource_params # update only supports a single file, create supports many. if action_name == 'update' - params.require(:resource).permit(:file) + params.require(:resource).permit(:resource_title, :file) else - params.require(:resource).permit(:file => []) + params.require(:resource).permit(:resource_title, :file => []) end end diff --git a/resources/app/helpers/refinery/admin/resources_helper.rb b/resources/app/helpers/refinery/admin/resources_helper.rb new file mode 100644 index 0000000000..dd74b5eadb --- /dev/null +++ b/resources/app/helpers/refinery/admin/resources_helper.rb @@ -0,0 +1,11 @@ +module Refinery + module Admin + module ResourcesHelper + # We show the title from the next available locale + # if there is no title for the current locale + def resource_title_with_translations(resource) + resource.title.presence || resource.translations.detect { |t| t.title.present?}.title + end + end + end +end \ No newline at end of file diff --git a/resources/app/models/refinery/resource.rb b/resources/app/models/refinery/resource.rb index b6886656d7..84e733d2f6 100644 --- a/resources/app/models/refinery/resource.rb +++ b/resources/app/models/refinery/resource.rb @@ -4,6 +4,8 @@ module Refinery class Resource < Refinery::Core::BaseModel include Resources::Validators + translates :resource_title + dragonfly_accessor :file, :app => :refinery_resources validates :file, :presence => true @@ -25,7 +27,7 @@ def type_of_content # Returns a titleized version of the filename # my_file.pdf returns My File def title - CGI::unescape(file_name.to_s).gsub(/\.\w+$/, '').titleize + resource_title.presence || CGI::unescape(file_name.to_s).gsub(/\.\w+$/, '').titleize end def update_index diff --git a/resources/app/views/refinery/admin/resources/_form.html.erb b/resources/app/views/refinery/admin/resources/_form.html.erb index 6e3c121986..102688913a 100644 --- a/resources/app/views/refinery/admin/resources/_form.html.erb +++ b/resources/app/views/refinery/admin/resources/_form.html.erb @@ -6,6 +6,8 @@ :object => @resource, :include_object_name => false %> + <%= render '/refinery/admin/locale_picker', :current_locale => Globalize.locale if @resource.persisted? %> +
<% if action_name =~ /(edit)|(update)/ %> <%= action_label :download, @resource.url, t('.download_current') %> @@ -23,12 +25,21 @@
+
+ + <%= f.label :resource_title, t('.resource_title') %> + <%= refinery_help_tag t('.resource_title_help') %> + + <%= f.text_field :resource_title, placeholder: t('.resource_title') %> +
+ <%= render '/refinery/admin/form_actions', :f => f, :continue_editing => false, :hide_cancel => (@app_dialog or action_name == 'insert' or from_dialog?), :delete_title => t('delete', :scope => 'refinery.admin.resources'), :delete_confirmation => (t('message', :scope => 'refinery.admin.delete', - :title => @resource.title) if @resource.persisted?) %> + :title => @resource.title) if @resource.persisted?), + cancel_url: refinery.admin_resources_path -%> <% if @app_dialog -%> diff --git a/resources/app/views/refinery/admin/resources/_resource.html.erb b/resources/app/views/refinery/admin/resources/_resource.html.erb index f43ba45c8d..832c59d45f 100644 --- a/resources/app/views/refinery/admin/resources/_resource.html.erb +++ b/resources/app/views/refinery/admin/resources/_resource.html.erb @@ -6,8 +6,27 @@
  • - <%= resource.title %><%= ".#{resource.ext}" if resource.ext %> - - <%= number_to_human_size(resource.size) %> + <%= resource_title_with_translations resource %> + + + <% if Refinery::I18n.frontend_locales.many? %> + + <% resource.translations.sort_by{ |t| Refinery::I18n.frontend_locales.index(t.locale)}.each do |translation| %> + <% if translation.resource_title.present? %> + <%= link_to refinery.edit_admin_resource_path(resource, switch_locale: translation.locale), + class: 'locale', title: translation.locale.upcase do %> + +
    + <%= locale_text_icon(translation.locale.upcase) %> +
    + <% end %> + <% end %> + <% end %> +
    + <% end %> + + + <%= resource.file_name %> - <%= number_to_human_size(resource.size) %> diff --git a/resources/config/locales/en.yml b/resources/config/locales/en.yml index 332fc8c93f..d8f7cd6ca2 100644 --- a/resources/config/locales/en.yml +++ b/resources/config/locales/en.yml @@ -13,6 +13,8 @@ en: or: or replace: " replace it with this one..." maximum_file_size: The maximum file size is %{bytes}. + resource_title: Title + resource_title_help: Extra information about the file resource: download: Download this file (%{size}) actions: diff --git a/resources/config/locales/fr.yml b/resources/config/locales/fr.yml index d786bee86d..07c081b390 100644 --- a/resources/config/locales/fr.yml +++ b/resources/config/locales/fr.yml @@ -13,6 +13,8 @@ fr: or: ou replace: " le remplacer par celui-ci..." maximum_file_size: "La taille du fichier ne doit pas excéder %{bytes}." + resource_title: Titre + resource_title_help: Information complémentaire sur le fichier resource: download: Télécharger ce fichier (%{size}) actions: diff --git a/resources/db/migrate/20150430180930_add_title_to_refinery_resources.rb b/resources/db/migrate/20150430180930_add_title_to_refinery_resources.rb new file mode 100644 index 0000000000..fb769223b8 --- /dev/null +++ b/resources/db/migrate/20150430180930_add_title_to_refinery_resources.rb @@ -0,0 +1,7 @@ +class AddTitleToRefineryResources < ActiveRecord::Migration + def change + change_table :refinery_resources do |t| + t.string :resource_title + end + end +end diff --git a/resources/db/migrate/20150430180959_translate_refinery_resources.rb b/resources/db/migrate/20150430180959_translate_refinery_resources.rb new file mode 100644 index 0000000000..0c2a4cff54 --- /dev/null +++ b/resources/db/migrate/20150430180959_translate_refinery_resources.rb @@ -0,0 +1,13 @@ +class TranslateRefineryResources < ActiveRecord::Migration + def self.up + Refinery::Resource.create_translation_table!({ + resource_title: :string + }, { + :migrate_data => true + }) + end + + def self.down + Refinery::Resource.drop_translation_table! migrate_data: true + end +end \ No newline at end of file diff --git a/resources/lib/refinery/resources.rb b/resources/lib/refinery/resources.rb index 298d950647..fd82058f4a 100644 --- a/resources/lib/refinery/resources.rb +++ b/resources/lib/refinery/resources.rb @@ -22,3 +22,7 @@ def factory_paths end end end + +ActiveSupport.on_load(:active_record) do + require 'globalize' +end \ No newline at end of file diff --git a/resources/refinerycms-resources.gemspec b/resources/refinerycms-resources.gemspec index a950cfbb5f..3ff0aa2195 100644 --- a/resources/refinerycms-resources.gemspec +++ b/resources/refinerycms-resources.gemspec @@ -21,6 +21,7 @@ Gem::Specification.new do |s| s.add_dependency 'acts_as_indexed', '~> 0.8.0' s.add_dependency 'dragonfly', '~> 1.0.0' + s.add_dependency 'globalize', ['>= 4.0.0', '< 5.2'] s.add_dependency 'refinerycms-core', version s.required_ruby_version = Refinery::Version.required_ruby_version diff --git a/resources/spec/features/refinery/admin/resources_spec.rb b/resources/spec/features/refinery/admin/resources_spec.rb index bf09b0f526..d88cc8736f 100644 --- a/resources/spec/features/refinery/admin/resources_spec.rb +++ b/resources/spec/features/refinery/admin/resources_spec.rb @@ -32,7 +32,7 @@ module Admin click_button ::I18n.t('save', :scope => 'refinery.admin.form_actions') end - expect(page).to have_content("Refinery Is Awesome.txt") + expect(page).to have_content("Refinery Is Awesome") expect(Refinery::Resource.count).to eq(1) end @@ -78,7 +78,7 @@ module Admin it "updates file" do visit refinery.admin_resources_path - expect(page).to have_content("Refinery Is Awesome.txt") + expect(page).to have_content("Refinery Is Awesome") expect(page).to have_selector("a[href='/refinery/resources/#{resource.id}/edit']") click_link "Edit this file"