4

I am using Ruby on Rails (3.2.2), globalize3 (0.2.0) and batch_translations (0.1.2) ruby-gems. I would like to validate translated data handled by globalize3 as well as it should be. That is, for example, if...

... in my ROOT_RAILS/app/models/article.rb file I have:

class Article < ActiveRecord::Base
  translates :title, :content

  # This is needed to make the batch_translations to work.
  attr_accessible :translations_attributes
  accepts_nested_attributes_for :translations

  validates :title,
    :presence => true,
    :length   => ...
  validates :content,
    :presence => true,
    :length   => ...

  ...
end

... in my ROOT_RAILS/app/views/articles/_form.html.erb file I have:

<%= form_for(@article) do |f| %>
    English translation:
    <%= f.text_field :title %>
    <%= f.text_field :content %>

    Italiano translation:
    <%= f.globalize_fields_for :it do |g| %>
      <%= g.text_field :title %>
      <%= f.text_field :content %>
    <% end %>
<% end %>

I would like to validate data for title and content values when submitting the form and the current I18n.locale is not the I18n.default_locale. In other words, in order to store in the database correct information also for translations, I would like to validate title and content attributes when an user submit translation data for a locale language that is not the default English language.

Is it possible? If so, how?

Note: I took example from this question.

Community
  • 1
  • 1
Backo
  • 18,291
  • 27
  • 103
  • 170

1 Answers1

0

If I'm understanding you correctly, there's a few ways you could go about this.

1) A controller level test, where you test that a form submitted with the locale included persists to the correct localized content table. One way to do that would be something like this:

    it "persists localized content for the Italian locale" do
       Article.should_receive(:create).with(:locale => 'it', :title => 'title goes here', :content => 'this is content')
       post :create, :locale => 'it', :title => 'title goes here', :content => 'this is content'
    end

This basically says that the Article model should receive the create message with these arguments. I'm not entirely clear on how the batch_translations API works so you'll want to verify that this is how the parameters come in from that style form.

At that point, I think you'd be adequately testing that the data got persisted to the database correctly. Anything further would be testing that the Globalize gem works, which you want to avoid. Try and test the boundaries.

Dan
  • 181
  • 1
  • 8