Hi everybody,

I'm having problems on a testcase that I'm working on. I hope that I can
explain the problem properly and that some of you can help me out
becouse I'm realy stuck on this one.

Ok, I have a Page model that needs to be multi lingual. Therefore I have
used a small but nice plugin: http://github.com/janne/model_translations
. Basically, I had to create a new model PageTranslation that conatains
the i18n attributes. Let me draw a small example:

Page
----
id
parent_id

PageTranslation
---------------
title

Now I want to use these object for the base of other objects. So I used
polymophisme on the page and page translation. Becouse of the
inheritance, I have created an abstract model for Page and
PageTranslation where other objects can inherit from. As an example
object, I'll use a new object StaticPage. This is what I have so far:

class Page < ActiveRecord::Base
  has_many :page_translations
  belongs_to :content, :polymorphic => true
  belongs_to :parent, :class_name => 'Page'
  has_many :children, :class_name => 'Page', :foreign_key => 'parent_id'
end

class PageTranslation < ActiveRecord::Base
  belongs_to :page
  belongs_to :content_translation, :polymorphic => true
end

class PageModel < ActiveRecord::Base

  self.abstract_class = true

  has_one :page, :as => :content, :dependent => :destroy

  validates_presence_of :page
  validates_associated :page

  after_save :save_page

  translates :locale, :slug, :keywords, :description, :title, :content

  Page.column_names.each do |attribute|
    if attribute != "id"
      define_method "#{attribute}=".to_sym do |value|
        init_page
        self.page["#{attribute}"] = value
      end

      define_method "#{attribute}".to_sym do
        self.page[attribute] if self.page
      end
    end
  end

  def children
    self.page.children if self.page
  end

  private

  def init_page
    self.page = Page.new unless self.page
  end

  def save_page
    self.page.save(false)
  end
end

class PageTranslationModel < ActiveRecord::Base
  self.abstract_class = true

  has_one :page_translation, :as => :content_translation, :dependent =>
:destroy

  validates_presence_of :page_translation
  validates_associated :page_translation

  after_save :save_page_translation

  PageTranslation.column_names.each do |attribute|
    if attribute != "id"
      define_method "#{attribute}=".to_sym do |value|
        init_page_translation
        self.page_translation["#{attribute}"] = value
      end

      define_method "#{attribute}".to_sym do
        self.page_translation[attribute] if self.page_translation
      end
    end
  end



  private

  def init_page_translation
    self.page_translation = PageTranslation.new unless
self.page_translation
  end

  def save_page_translation
    self.page_translation.save(false)
  end
end

class StaticPage < PageModel
end

class StaticPageTranslation < PageTranslationModel
end


For testing, I've created a simple form like this:

<% form_for(@static_page, :url => static_pages_path , :html => { :id =>
"static-page-form" }) do |f| %>
    <input type="text" name="static_page[parent_id]" value="1" />
<input type="submit" value="save" />
<% end %>

This test works like a charm. The static page gets saved and the content
get synchronized with the page table. So that part works.

The part I can't get to work, is the part with the translations.
I have used the model translation plugin on a simpler test case and got
it to work by adding the following to a model:
  has_many :test_translations
  after_update :save_translations

  def new_translation_attributes=(translation_attributes)
    translation_attributes.each do |attributes|
      test_translations.build(attributes)
    end
  end

  def existing_translation_attributes=(translation_attributes)
    test_translations.reject(&:new_record?).each do |translation|
      attributes = translation_attributes[translation.id.to_s]
      if attributes
        test_translation.attributes = attributes
      else
        test_translations.destroy(translation)
      end
    end
  end

  def save_translations
    test_translations.each do |translation|
      translation.save(false)
    end
  end
And have my form fields like this:

<input type="text" name="test[existing_translation_attributes][][name]"
value=""/>

But I don't seem to be able to implement this pattern in my abstract
PageModel.
It would be great if someone could help me out with this one, becouse I
don't think I would be able to solve this without any help.

Thank you in advance
-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to