diff --git a/changelog.md b/changelog.md index 271cc4df55..5c5aac3dd3 100644 --- a/changelog.md +++ b/changelog.md @@ -15,6 +15,7 @@ * Remove required_label formhelper. [#2954](https://github.com/refinery/refinerycms/pull/2954). [Johan Bruning](https://github.com/johanb) * Added ability to create page part title different form slug. [#2875](https://github.com/refinery/refinerycms/pull/2875). [Brice Sanchez](https://github.com/bricesanchez) & [Philip Arndt](https://github.com/parndt) & [Josef Šimánek](https://github.com/simi) * Deprecated `part_with_title` method in `Refinery#Page` and `title_matches?` method in `Refinery#PagePart`. [#2875](https://github.com/refinery/refinerycms/pull/2875). [Brice Sanchez](https://github.com/bricesanchez) & [Philip Arndt](https://github.com/parndt) & [Josef Šimánek](https://github.com/simi) +* 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 464fbcf29b..4ac17589c8 100644 --- a/resources/app/controllers/refinery/admin/resources_controller.rb +++ b/resources/app/controllers/refinery/admin/resources_controller.rb @@ -83,9 +83,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/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..19df63fb23 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 %> + + + <% 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..1138e98bd8 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: The title visible to users. resource: download: Download this file (%{size}) actions: diff --git a/resources/config/locales/fr.yml b/resources/config/locales/fr.yml index d786bee86d..82a69a069a 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: Le titre visible pour les utilisateurs. resource: download: Télécharger ce fichier (%{size}) actions: diff --git a/resources/db/migrate/20150430180959_add_translated_title_to_refinery_resources.rb b/resources/db/migrate/20150430180959_add_translated_title_to_refinery_resources.rb new file mode 100644 index 0000000000..f0efe1867f --- /dev/null +++ b/resources/db/migrate/20150430180959_add_translated_title_to_refinery_resources.rb @@ -0,0 +1,11 @@ +class AddTranslatedTitleToRefineryResources < ActiveRecord::Migration + def self.up + Refinery::Resource.create_translation_table!({ + resource_title: :string + }) + end + + def self.down + Refinery::Resource.drop_translation_table! + 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 24c184424c..8028ce5e4f 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" @@ -92,6 +92,30 @@ module Admin expect(page).to have_content("Refinery Is Awesome2") expect(Refinery::Resource.count).to eq(1) end + + describe "translate", focus: true do + before do + allow(Refinery::I18n).to receive(:frontend_locales).and_return([:en, :fr]) + end + + it "can have a second locale added to it" do + visit refinery.admin_resources_path + 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" + + within "#switch_locale_picker" do + click_link "FR" + end + + fill_in "Title", :with => "Premier fichier" + click_button "Save" + + expect(page).to have_content("'Premier fichier' was successfully updated.") + expect(Resource.translation_class.count).to eq(1) + end + end end context "destroy" do diff --git a/resources/spec/models/refinery/resource_spec.rb b/resources/spec/models/refinery/resource_spec.rb index 85f99c211e..bb1c8e5829 100644 --- a/resources/spec/models/refinery/resource_spec.rb +++ b/resources/spec/models/refinery/resource_spec.rb @@ -3,6 +3,7 @@ module Refinery describe Resource, :type => :model do let(:resource) { FactoryGirl.create(:resource) } + let(:titled_resource) { FactoryGirl.create(:resource, resource_title: 'Resource Title')} context "with valid attributes" do it "should create successfully" do @@ -31,8 +32,15 @@ module Refinery end describe "#title" do - it "returns a titleized version of the filename" do - expect(resource.title).to eq("Refinery Is Awesome") + context 'when a specific title has not been given' do + it "returns a titleized version of the filename" do + expect(resource.title).to eq("Refinery Is Awesome") + end + end + context 'when a specific title has been given' do + it 'returns that title' do + expect(titled_resource.title).to eq('Resource Title') + end end end