Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to customize and translate resource name #2966

Merged
merged 1 commit into from Jul 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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 %>
</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
28 changes: 26 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 All @@ -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
Expand Down
12 changes: 10 additions & 2 deletions resources/spec/models/refinery/resource_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down