Skip to content
Browse files

Captions now support translations.

  • Loading branch information...
1 parent 320cb41 commit 8ee14e71301b35d995408fdea9737c12adc931de @mhaylock mhaylock committed May 12, 2011
View
6 app/models/image_page.rb
@@ -2,9 +2,7 @@ class ImagePage < ActiveRecord::Base
belongs_to :image
belongs_to :page
-
- before_save do |image_page|
- image_page.position = (ImagePage.maximum(:position) || -1) + 1
- end
+
+ translates :caption if self.respond_to?(:translates)
end
View
3 app/views/admin/pages/tabs/_images_field.html.erb
@@ -4,9 +4,10 @@
<li id='image_<%= image_form.object.id %>'>
<%= image_form.hidden_field :id %>
<%= image_fu image_form.object, '135x135#c' %>
+ <% index = image_form.object_name.split('[').last.split(']').first.to_i %>
+ <%= hidden_field_tag "#{f.object_name}[images_attributes][#{index}][image_page_id]", f.object.image_page_id_for_image_index(index) %>
<% if RefinerySetting.find_or_set(:page_images_captions, false) %>
<div class='textarea_wrapper_for_wym'>
- <% index = image_form.object_name.split('[').last.split(']').first.to_i %>
<%= text_area_tag "#{f.object_name}[images_attributes][#{index}][caption]",
f.object.caption_for_image_index(index),
:style => 'display: none',
View
22 db/migrate/20110511215016_translate_page_image_captions.rb
@@ -0,0 +1,22 @@
+class TranslatePageImageCaptions < ActiveRecord::Migration
+ def self.up
+ add_column ::ImagePage.table_name, :id, :primary_key
+
+ ::ImagePage.reset_column_information
+ unless defined?(::ImagePage::Translation) && ::ImagePage::Translation.table_exists?
+ ::ImagePage.create_translation_table!({
+ :caption => :string
+ }, {
+ :migrate_data => true
+ })
+ end
+ end
+
+ def self.down
+ ::ImagePage.reset_column_information
+
+ ::ImagePage.drop_translation_table! :migrate_data => true
+
+ remove_column ::ImagePage.table_name, :id
+ end
+end
View
28 lib/refinerycms-page-images.rb
@@ -9,7 +9,7 @@ class Engine < Rails::Engine
config.to_prepare do
Page.module_eval do
- has_many :image_pages
+ has_many :image_pages, :order => 'position ASC'
has_many :images, :through => :image_pages, :order => 'position ASC'
# accepts_nested_attributes_for MUST come before def images_attributes=
# this is because images_attributes= overrides accepts_nested_attributes_for.
@@ -18,23 +18,41 @@ class Engine < Rails::Engine
attr_accessible :images_attributes
def images_attributes=(data)
- ImagePage.delete_all(:page_id => self.id)
-
+ ids_to_delete = data.map{|i, d| d['image_page_id']}.compact
+ self.image_pages.each do |image_page|
+ if ids_to_delete.index(image_page.id.to_s).nil?
+ # Image has been removed, we must delete it
+ self.image_pages.delete(image_page)
+ image_page.destroy
+ end
+ end
+
(0..(data.length-1)).each do |i|
unless (image_data = data[i.to_s]).nil? or image_data['id'].blank?
- image_page = self.image_pages.new(:image_id => image_data['id'].to_i, :position => i)
+ image_page = if image_data['image_page_id'].present?
+ self.image_pages.find(image_data['image_page_id'])
+ else
+ self.image_pages.new(:image_id => image_data['id'].to_i)
+ end
+ image_page.position = i
# Add caption if supported
if RefinerySetting.find_or_set(:page_images_captions, false)
image_page.caption = image_data['caption']
end
- self.image_pages << image_page
+
+ self.image_pages << image_page if image_data['image_page_id'].blank?
+ image_page.save
end
end
end
def caption_for_image_index(index)
self.image_pages[index].try(:caption).presence || ""
end
+
+ def image_page_id_for_image_index(index)
+ self.image_pages[index].try(:id)
+ end
end
end
View
29 public/javascripts/page-image-picker.js
@@ -87,7 +87,7 @@ reset_functionality = function() {
image_added = function(image) {
new_list_item = (current_list_item = $('li.empty')).clone();
image_id = $(image).attr('id').replace('image_', '');
- current_list_item.find('input:hidden').val(image_id);
+ current_list_item.find('input:hidden:first').val(image_id);
$("<img />").attr({
title: $(image).attr('title')
@@ -153,22 +153,15 @@ reindex_images = function() {
$(input).attr('id', $(input).attr('id').replace(/_\d/, '_' + i));
$(input).attr('data-old-id', $(input).attr('data-old-id').replace(/_\d_/, '_'+i+'_').replace(/_\d/, '_' + i));
});
- $('#page_images li input:hidden:not(.caption)').each(function(i, input){
- // make the image's name consistent with its position.
- parts = $(input).attr('name').split(']');
- parts[1] = ('[' + i);
- $(input).attr('name', parts.join(']'));
-
- // make the image's id consistent with its position.
- $(input).attr('id', $(input).attr('id').replace(/_\d_/, '_'+i+'_').replace(/_\d/, '_'+i));
- });
- $('#page_images li input.caption:hidden').each(function(i, input){
- // make the image's name consistent with its position.
- parts = $(input).attr('name').split(']');
- parts[1] = ('[' + i);
- $(input).attr('name', parts.join(']'));
-
- // make the image's id consistent with its position.
- $(input).attr('id', $(input).attr('id').replace(/_\d_/, '_'+i+'_').replace(/_\d/, '_'+i));
+ $('#page_images li').each(function(i, li){
+ $('input:hidden', li).each(function() {
+ // make the image's name consistent with its position.
+ parts = $(this).attr('name').split(']');
+ parts[1] = ('[' + i);
+ $(this).attr('name', parts.join(']'));
+
+ // make the image's id consistent with its position.
+ $(this).attr('id', $(this).attr('id').replace(/_\d_/, '_'+i+'_').replace(/_\d/, '_'+i));
+ });
});
}

0 comments on commit 8ee14e7

Please sign in to comment.
Something went wrong with that request. Please try again.