Skip to content

Commit

Permalink
Add ability to translate image_title and image_alt fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Brice Sanchez committed Jul 24, 2015
1 parent a747bfe commit b22eab5
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 17 deletions.
1 change: 1 addition & 0 deletions changelog.md
Expand Up @@ -16,6 +16,7 @@
* 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)
* Added ability to translate images title and alt attributes. [#2965](https://github.com/refinery/refinerycms/pull/2965). [Brice Sanchez](https://github.com/bricesanchez)

* [See full list](https://github.com/refinery/refinerycms/compare/2-1-stable...master)

Expand Down
2 changes: 2 additions & 0 deletions images/app/models/refinery/image.rb
Expand Up @@ -2,6 +2,8 @@

module Refinery
class Image < Refinery::Core::BaseModel
translates :image_title, :image_alt

dragonfly_accessor :image, :app => :refinery_images

include Images::Validators
Expand Down
5 changes: 4 additions & 1 deletion images/app/views/refinery/admin/images/_form.html.erb
Expand Up @@ -5,6 +5,8 @@
object: @image,
include_object_name: false %>
<%= render '/refinery/admin/locale_picker', :current_locale => Globalize.locale if @image.persisted? %>

<div class="field">
<% if action_name =~ /(edit)|(update)/ %>
<p>
Expand Down Expand Up @@ -45,7 +47,8 @@
continue_editing: false,
hide_cancel: (@app_dialog or action_name == 'insert' or from_dialog?),
delete_title: t('delete', scope: 'refinery.admin.images'),
delete_confirmation: t('message', scope: 'refinery.admin.delete', title: @image.image_name) -%>
delete_confirmation: t('message', scope: 'refinery.admin.delete', title: @image.image_name),
cancel_url: refinery.admin_images_path -%>
<% if @app_dialog %>
<input type="hidden" name="app_dialog" value="<%= @app_dialog %>" />
Expand Down
21 changes: 17 additions & 4 deletions images/app/views/refinery/admin/images/_list_view_image.html.erb
Expand Up @@ -2,11 +2,24 @@
<span class="title">
<%= list_view_image.title %>
</span>
<span class='alt'>
<%= action_icon :info, '#', "Title: #{list_view_image.title} Alt text: #{list_view_image.alt}" %>
</span>

<span class="preview">&nbsp;</span>
<% if Refinery::I18n.frontend_locales.many? %>
<span class='locales'>
<% list_view_image.translations.sort_by{ |t| Refinery::I18n.frontend_locales.index(t.locale)}.each do |translation| %>
<% if translation.image_title.present? %>
<%= link_to refinery.edit_admin_image_path(list_view_image, 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"><%= list_view_image.image_name %></span>
<span class="actions">

<%= action_icon :preview, list_view_image.url, t('view_live_html', scope: 'refinery.admin.images') %>
Expand Down
4 changes: 4 additions & 0 deletions images/config/locales/fr.yml
Expand Up @@ -15,6 +15,10 @@ fr:
replace_image: " la remplacer par celle-ci..."
current_image: Image actuelle
maximum_image_size: "La taille de l'image ne doit pas excéder %{bytes}."
image_title: Titre
image_title_help: Information complémentaire sur l'image
image_alt: Alt
image_alt_help: Texte utilise si l'image ne peut pas être affichée.
actions:
create_new_image: Ajouter une image
records:
Expand Down
14 changes: 14 additions & 0 deletions images/db/migrate/20150430171341_translate_refinery_images.rb
@@ -0,0 +1,14 @@
class TranslateRefineryImages < ActiveRecord::Migration
def self.up
Refinery::Image.create_translation_table!({
image_alt: :string,
image_title: :string
}, {
:migrate_data => true
})
end

def self.down
Refinery::Image.drop_translation_table! migrate_data: true
end
end
4 changes: 4 additions & 0 deletions images/lib/refinery/images.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 images/refinerycms-images.gemspec
Expand Up @@ -20,6 +20,7 @@ Gem::Specification.new do |s|
s.test_files = `git ls-files -- spec/*`.split("\n")

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
1 change: 1 addition & 0 deletions images/spec/features/refinery/admin/images_spec.rb
Expand Up @@ -25,6 +25,7 @@ module Refinery
it_has_behaviour 'deletes an image'
it_has_behaviour 'edits an image'
it_has_behaviour 'uploads images'
it_has_behaviour 'translates an image'
end

context 'When there are many images' do
Expand Down
18 changes: 9 additions & 9 deletions images/spec/support/shared contexts/admin_images_tab.rb
@@ -1,17 +1,17 @@
shared_context 'admin images tab' do

let(:open_upload_dialog) { find('a', text: ::I18n.t('create_new_image', scope: 'refinery.admin.images.actions')).trigger(:click) }
let(:select_upload) {}
let(:initialize_context) {}
let(:index_in_frame) {false}
let(:dialog_frame_id) {'dialog_iframe'}
let(:open_upload_dialog) { find('a', text: ::I18n.t('create_new_image', scope: 'refinery.admin.images.actions')).trigger(:click) }
let(:select_upload) {}
let(:initialize_context) {}
let(:index_in_frame) {false}
let(:dialog_frame_id) {'dialog_iframe'}
let(:initial_path) { refinery.admin_images_path(view: %w(grid list).sample) }

let(:index_item_selector) {'#records li'}
let(:gridview_img_selector) {' > img'}
let(:index_item_selector) {'#records li'}
let(:gridview_img_selector) {' > img'}
let(:gridview_title_selector) {'[tooltip]'}
let(:gridview_alt_selector) {'[alt]'}
let(:gridview_alt_selector) {'[alt]'}
let(:listview_title_selector) {' > span.title'}
let(:listview_alt_selector) {' > span.alt'}
let(:listview_filename_selector) {' > span.preview'}

end
2 changes: 1 addition & 1 deletion images/spec/support/shared examples/image_editor.rb
@@ -1,3 +1,3 @@
shared_examples 'edits an image' do
pending
end
end
4 changes: 2 additions & 2 deletions images/spec/support/shared examples/image_indexer.rb
Expand Up @@ -61,8 +61,8 @@
expect(page).to have_selector(index_item_selector << listview_title_selector, count: image_count)
end

it 'makes the alt attribute of each image available' do
expect(page).to have_selector(index_item_selector << listview_alt_selector, count: image_count)
it 'makes the filename of each image available' do
expect(page).to have_selector(index_item_selector << listview_filename_selector, count: image_count)
end

it 'has an option to switch to grid view' do
Expand Down
31 changes: 31 additions & 0 deletions images/spec/support/shared examples/image_translator.rb
@@ -0,0 +1,31 @@
shared_examples 'translates an image' do
before do
allow(Refinery::I18n).to receive(:frontend_locales).and_return([:en, :fr])
ensure_on(initial_path)
end

context "when in list view" do
before do
ensure_on(current_path + "?view=list")
end

it "can have a second locale added to it", focus: true do
expect(page).to have_content("Beach")
expect(page).to have_selector("a[href='/refinery/images/#{image.id}/edit']")

click_link "Edit this image"

within "#switch_locale_picker" do
click_link "FR"
end

fill_in "Title", :with => "Titre de la première image"
fill_in "Alt", :with => "Texte alternatif de la première image"

click_button "Save"

expect(page).to have_content("'Titre de la première image' was successfully updated.")
expect(Refinery::Image.translation_class.count).to eq(1)
end
end
end

0 comments on commit b22eab5

Please sign in to comment.