Skip to content

Commit

Permalink
Add ability to customize and translate resource name
Browse files Browse the repository at this point in the history
  • Loading branch information
Brice Sanchez committed Jul 23, 2015
1 parent 3b3e59a commit d26e204
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 8 deletions.
1 change: 1 addition & 0 deletions changelog.md
Expand Up @@ -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)

Expand Down
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions 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.resource_title.present?}.resource_title
end
end
end
end
4 changes: 3 additions & 1 deletion resources/app/models/refinery/resource.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
13 changes: 12 additions & 1 deletion resources/app/views/refinery/admin/resources/_form.html.erb
Expand Up @@ -6,6 +6,8 @@
:object => @resource,
:include_object_name => false %>
<%= render '/refinery/admin/locale_picker', :current_locale => Globalize.locale if @resource.persisted? %>

<div class="field">
<% if action_name =~ /(edit)|(update)/ %>
<%= action_label :download, @resource.url, t('.download_current') %>
Expand All @@ -23,12 +25,21 @@
</label>
</div>
<div class="field" id="title">
<span class='label_with_help'>
<%= f.label :resource_title, t('.resource_title') %>
<%= refinery_help_tag t('.resource_title_help') %>
</span>
<%= f.text_field :resource_title, placeholder: t('.resource_title') %>
</div>
<%= 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 -%>
<input type="hidden" name="app_dialog" value="<%= @app_dialog %>" />
Expand Down
23 changes: 21 additions & 2 deletions resources/app/views/refinery/admin/resources/_resource.html.erb
Expand Up @@ -6,8 +6,27 @@
<li class="clearfix record <%= cycle('on', 'on-hover') %>">

<span class="title <%= resource.ext.try(:downcase) %>">
<%= resource.title %><%= ".#{resource.ext}" if resource.ext %>
<span class="preview">- <%= number_to_human_size(resource.size) %></span>
<%= resource_title_with_translations resource %>
</span>

<% if Refinery::I18n.frontend_locales.many? %>
<span class='locales'>
<% 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 %>

<div class="<%=translation.locale %> locale_marker">
<%= locale_text_icon(translation.locale.upcase) %>
</div>
<% end %>
<% end %>
<% end %>
</span>
<% end %>

<span class="preview">
<%= resource.file_name %> - <%= number_to_human_size(resource.size) %>
</span>

<span class="actions">
Expand Down
2 changes: 2 additions & 0 deletions resources/config/locales/en.yml
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions resources/config/locales/fr.yml
Expand Up @@ -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:
Expand Down
@@ -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
4 changes: 4 additions & 0 deletions resources/lib/refinery/resources.rb
Expand Up @@ -22,3 +22,7 @@ def factory_paths
end
end
end

ActiveSupport.on_load(:active_record) do
require 'globalize'
end
1 change: 1 addition & 0 deletions resources/refinerycms-resources.gemspec
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions resources/spec/features/refinery/admin/resources_spec.rb
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand Down
36 changes: 36 additions & 0 deletions resources/spec/helpers/refinery/admin/resources_helper_spec.rb
@@ -0,0 +1,36 @@
require "spec_helper"

module Refinery
module Admin
describe ResourcesHelper, :type => :helper do
describe "#resource_title_with_translations" do
let(:resource) { FactoryGirl.build(:resource) }

before do
Globalize.with_locale(:en) do
resource.resource_title = "draft"
resource.save!
end

Globalize.with_locale(:lv) do
resource.resource_title = "melnraksts"
resource.save!
end
end

context 'when the resource has a title in the current locale' do
it "returns the resource title" do
expect(helper.resource_title_with_translations(resource)).to eq("draft")
end
end

context 'when the resource does not have a title in the current locale' do
it 'returns the title from the next locale with a title' do
Resource.translation_class.where(:locale => :en).first.destroy
expect(helper.resource_title_with_translations(resource)).to eq("melnraksts")
end
end
end
end
end
end

0 comments on commit d26e204

Please sign in to comment.