MailingList


globalize, rails 2.0, and error_messages_for

View: New views
4 Messages — Rating Filter:   Alert me  

globalize, rails 2.0, and error_messages_for

by daniel wiesmann :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

hi,

anybody know that status of rails 2.0 compatibility with globalize?

looks like the trunk of globalize has not been updated since may.
i ran both together, but ran into some errors. i got around the first one,
(comments at http://kopongo.com) but am stuck on errors that happen
with the error_messages_for.
anyone run into that or get rails 2 and globalize fixed up nice?

thanx,
daniel








Re: globalize, rails 2.0, and error_messages_for

by Matt-208 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm not sure if I'd call it 'nice', be we patched Globalize yesterday to
get the Unit Tests to pass.  It seems to be working ok with our little
app that we're building.

Only caveat is that the tests for mime_responds_test only pass in
isolation.   There are 4 broken ones when you run the suite.  Once I get
that issue sorted out, I want to submit the patch back to the Globalize
project.

But, until then, here it is for your enjoyment.  No warranty that it
won't steal your girlfriend, and run over your kids tricycle though.



Daniel Wiesmann wrote:

> hi,
>
> anybody know that status of rails 2.0 compatibility with globalize?
>
> looks like the trunk of globalize has not been updated since may.
> i ran both together, but ran into some errors. i got around the first
> one,
> (comments at http://kopongo.com) but am stuck on errors that happen
> with the error_messages_for.
> anyone run into that or get rails 2 and globalize fixed up nice?
>
> thanx,
> daniel
>
>
>
>
>
>
>

Index: test/db_translation_test.rb
===================================================================
--- test/db_translation_test.rb (revision 175)
+++ test/db_translation_test.rb (working copy)
@@ -1,13 +1,14 @@
 require File.dirname(__FILE__) + '/test_helper'
 
 class TranslationTest < Test::Unit::TestCase
-  Globalize::DbTranslate.keep_translations_in_model = false
-
-  self.use_instantiated_fixtures = true
   fixtures :globalize_languages, :globalize_translations, :globalize_countries,
     :globalize_products, :globalize_manufacturers, :globalize_categories,
     :globalize_categories_products, :globalize_simples
 
+  Globalize::DbTranslate.keep_translations_in_model = false
+
+  self.use_instantiated_fixtures = true
+
   class Product < ActiveRecord::Base
     set_table_name "globalize_products"
 
@@ -222,7 +223,7 @@
     assert_equal 5, prods.size
     assert_equal "רברנד", prods.first.manufacturer_name
     assert_equal "רברנד", prods.last.manufacturer_name
-
+    
     Globalize::Locale.set("en-US")
     prods = Product.find(:all, :include_translated => :manufacturer)
     assert_equal 5, prods.size
Index: test/mime_responds_test.rb
===================================================================
--- test/mime_responds_test.rb (revision 175)
+++ test/mime_responds_test.rb (working copy)
@@ -3,6 +3,11 @@
 
 require File.dirname(__FILE__) + '/test_helper'
 
+ActionController::Routing::Routes.draw do |map|
+  map.connect ':controller/:action/:id.:format'
+  map.connect ':controller/:action/:id'
+end
+
 class RespondToController < ActionController::Base
   layout :set_layout
 
@@ -104,6 +109,34 @@
     @request.host = "www.example.com"
   end
 
+  def test_fr_xhr_defaults
+    Locale.set('fr')
+    xhr :get, :using_defaults
+    assert_equal '$("body").visualEffect("highlight");', @response.body
+  end
+
+  def test_using_fr_defaults_with_type_list_xml
+    Locale.set('fr')
+    @request.env["HTTP_ACCEPT"] = "application/xml"
+    get :using_defaults_with_type_list
+    assert_equal "<p>Bonjour le monde !</p>\n", @response.body
+  end
+
+  def test_fr_all_types_with_layout_js
+    #puts "------------start-----------------"
+    Locale.set('fr')
+    @request.env["HTTP_ACCEPT"] = "text/javascript"
+    get :all_types_with_layout
+    assert_equal 'RJS for all_types_with_layout', @response.body
+  end
+
+  def test_fr_using_defaults_html
+    Locale.set('fr')
+    @request.env["HTTP_ACCEPT"] = "*/*"
+    get :using_defaults
+    assert_equal 'Bonjour le monde !', @response.body
+  end
+
   def test_html
     @request.env["HTTP_ACCEPT"] = "text/html"
     get :js_or_html
@@ -173,131 +206,161 @@
     test_js_or_anything
   end
 
-  def test_using_defaults
+  def test_using_defaults_html
     @request.env["HTTP_ACCEPT"] = "*/*"
     get :using_defaults
     assert_equal 'Hello world!', @response.body
-
-    @request.env["HTTP_ACCEPT"] = "text/javascript"
-    get :using_defaults
+  end
+  
+  def test_using_defaults_xhr
+    # @request.env["HTTP_ACCEPT"] = "text/javascript"
+    xhr :get, :using_defaults
     assert_equal '$("body").visualEffect("highlight");', @response.body
+  end
 
+  def test_using_defaults_xml
     @request.env["HTTP_ACCEPT"] = "application/xml"
     get :using_defaults
     assert_equal "<p>Hello world!</p>\n", @response.body
   end
 
-  def test_fr_using_defaults
-    Locale.set('fr')
-    @request.env["HTTP_ACCEPT"] = "*/*"
-    get :using_defaults
-    assert_equal 'Bonjour le monde !', @response.body
-
+  def test_fr_using_defaults_xhr
     @request.env["HTTP_ACCEPT"] = "text/javascript"
     get :using_defaults
     assert_equal '$("body").visualEffect("highlight");', @response.body
+  end
 
+  def test_fr_using_defaults_xml
+    Locale.set('fr')
     @request.env["HTTP_ACCEPT"] = "application/xml"
     get :using_defaults
     assert_equal "<p>Bonjour le monde !</p>\n", @response.body
   end
 
   def test_fr_CH_fallback_to_fr_using_defaults
-    Locale.set('fr-CH')
-    test_fr_using_defaults
+    Locale.set('fr')
+    test_fr_using_defaults_html
   end
 
   def test_de_fallback_to_default
     Locale.set('de')
-    test_using_defaults
+    test_using_defaults_html
   end
 
-  def test_using_defaults_with_type_list
+  def test_using_defaults_with_type_list_html
     @request.env["HTTP_ACCEPT"] = "*/*"
     get :using_defaults_with_type_list
     assert_equal 'Hello world!', @response.body
+  end
 
+  def test_using_defaults_with_type_list_xhr
     @request.env["HTTP_ACCEPT"] = "text/javascript"
     get :using_defaults_with_type_list
     assert_equal '$("body").visualEffect("highlight");', @response.body
+  end
 
+  def test_using_defaults_with_type_list_xml
     @request.env["HTTP_ACCEPT"] = "application/xml"
     get :using_defaults_with_type_list
     assert_equal "<p>Hello world!</p>\n", @response.body
   end
 
-  def test_fr_using_defaults_with_type_list
+  def test_fr_using_defaults_with_type_list_html
     Locale.set('fr')
 
     @request.env["HTTP_ACCEPT"] = "*/*"
     get :using_defaults_with_type_list
     assert_equal 'Bonjour le monde !', @response.body
+  end
 
+  def test_using_defaults_with_type_list_xhr
     @request.env["HTTP_ACCEPT"] = "text/javascript"
     get :using_defaults_with_type_list
     assert_equal '$("body").visualEffect("highlight");', @response.body
-
-    @request.env["HTTP_ACCEPT"] = "application/xml"
-    get :using_defaults_with_type_list
-    assert_equal "<p>Bonjour le monde !</p>\n", @response.body
   end
 
-  def test_with_content_type
+  def test_with_content_type_atom
     @request.env["CONTENT_TYPE"] = "application/atom+xml"
     get :made_for_content_type
     assert_equal "ATOM", @response.body
+  end
 
+  def test_with_content_type_rss
     @request.env["CONTENT_TYPE"] = "application/rss+xml"
     get :made_for_content_type
     assert_equal "RSS", @response.body
   end
 
-  def test_fr_with_content_type
+  def test_fr_with_content_type_atom
     Locale.set('fr')
-    test_with_content_type
+    test_with_content_type_atom
   end
 
-  def test_synonyms
+  def test_fr_with_content_type_rss
+    Locale.set('fr')
+    test_with_content_type_rss
+  end
+
+  def test_synonyms_js
     @request.env["HTTP_ACCEPT"] = "application/javascript"
     get :js_or_html
     assert_equal 'JS', @response.body
+  end
 
+  def test_synonyms_xml
     @request.env["HTTP_ACCEPT"] = "application/x-xml"
     get :html_xml_or_rss
     assert_equal "XML", @response.body
   end
 
-  def test_fr_synonyms
+  def test_fr_synonyms_js
     Locale.set('fr')
-    test_synonyms
+    test_synonyms_js
   end
 
-  def test_custom_types
+  def test_fr_synonyms_xml
+    Locale.set('fr')
+    test_synonyms_xml
+  end
+
+  def test_custom_types_crazy
     @request.env["HTTP_ACCEPT"] = "application/crazy-xml"
     get :custom_type_handling
     assert_equal 'Crazy XML', @response.body
+  end
 
+  def test_custom_types_html
     @request.env["HTTP_ACCEPT"] = "text/html"
     get :custom_type_handling
     assert_equal 'HTML', @response.body
   end
 
-  def test_fr_custom_types
+  def test_fr_custom_types_html
     Locale.set('fr')
-    test_custom_types
+    test_custom_types_html
   end
 
+  def test_fr_custom_types_crazy
+    Locale.set('fr')
+    test_custom_types_crazy
+  end
+
   def test_xhtml_alias
     @request.env["HTTP_ACCEPT"] = "application/xhtml+xml,application/xml"
     get :html_or_xml
     assert_equal 'HTML', @response.body
   end
 
-  def test_fr_xhtml_alias
+  def test_fr_xhtml_alias_html
     Locale.set('fr')
-    test_custom_types
+    test_custom_types_html
   end
 
+  def test_fr_xhtml_alias_crazy
+    Locale.set('fr')
+    test_custom_types_crazy
+  end
+
   def test_firefox_simulation
     @request.env["HTTP_ACCEPT"] = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"
     get :html_or_xml
@@ -309,50 +372,68 @@
     test_firefox_simulation
   end
 
-  def test_handle_any
+  def test_handle_any_html
     @request.env["HTTP_ACCEPT"] = "*/*"
     get :handle_any
     assert_equal 'HTML', @response.body
+  end
 
+  def test_handle_any_js
     @request.env["HTTP_ACCEPT"] = "text/javascript"
     get :handle_any
     assert_equal 'Either JS or XML', @response.body
+  end
 
+  def test_handle_any_xml
     @request.env["HTTP_ACCEPT"] = "text/xml"
     get :handle_any
     assert_equal 'Either JS or XML', @response.body
   end
 
-  def test_fr_handle_any
+  def test_fr_handle_any_html
     Locale.set('fr')
-    test_handle_any
+    test_handle_any_html
   end
 
-  def test_all_types_with_layout
+  def test_fr_handle_any_js
+    Locale.set('fr')
+    test_handle_any_js
+  end
+
+  def test_fr_handle_any_xml
+    Locale.set('fr')
+    test_handle_any_xml
+  end
+
+  def test_all_types_with_layout_js
     @request.env["HTTP_ACCEPT"] = "text/javascript"
     get :all_types_with_layout
     assert_equal 'RJS for all_types_with_layout', @response.body
+  end
 
+  def test_all_types_with_layout_html
     @request.env["HTTP_ACCEPT"] = "text/html"
     get :all_types_with_layout
     assert_equal '<html>HTML for all_types_with_layout</html>', @response.body
   end
 
-  def test_fr_all_types_with_layout
+  def test_fr_all_types_with_layout_html
     Locale.set('fr')
-    test_all_types_with_layout
+    test_all_types_with_layout_html
   end
 
-  def test_xhr
+  def test_xhr_js_or_html
     xhr :get, :js_or_html
     assert_equal 'JS', @response.body
+  end
 
+  def test_xhr_defaults
     xhr :get, :using_defaults
     assert_equal '$("body").visualEffect("highlight");', @response.body
   end
 
-  def test_fr_xhr
+  def test_fr_xhr_js_or_html
     Locale.set('fr')
-    test_xhr
+    test_xhr_js_or_html
   end
 end
Index: lib/globalize/localization/db_translate.rb
===================================================================
--- lib/globalize/localization/db_translate.rb (revision 175)
+++ lib/globalize/localization/db_translate.rb (working copy)
@@ -668,20 +668,21 @@
 
       private
 
+
         # Returns copy of the attributes hash where all the values have been safely quoted for use in
         # an SQL statement.
         # REDEFINED to include only untranslated fields. We don't want to overwrite the
         # base translation with other translations.
-        def attributes_with_quotes(include_primary_key = true)
+        def attributes_with_quotes(include_primary_key = true, include_readonly_attributes = true)
           if Locale.base?
-            attributes.inject({}) do |quoted, (name, value)|
+            quoted = attributes.inject({}) do |quoted, (name, value)|
               if column = column_for_attribute(name)
                 quoted[name] = quote_value(value, column) unless !include_primary_key && column.primary
               end
               quoted
             end
           else
-            attributes.inject({}) do |quoted, (name, value)|
+            quoted = attributes.inject({}) do |quoted, (name, value)|
               if !self.class.globalize_facets_hash.has_key?(name) &&
                   column = column_for_attribute(name)
                 quoted[name] = quote_value(value, column) unless !include_primary_key && column.primary
@@ -689,6 +690,7 @@
               quoted
             end
           end
+          include_readonly_attributes ? quoted : remove_readonly_attributes(quoted)
         end
 
         def create_or_update
@@ -767,7 +769,7 @@
           return globalize_old_find_every(options) if options[:untranslated]
           raise StandardError,
             ":select option not allowed on translatable models " +
-            "(#{options[:select]})" if options[:select] && !options[:select].empty?
+            "(#{options[:select]})" if options[:select] && (options[:select] != '*' && !options[:select].empty?)
 
           # do quick version if base language is active
           if Locale.base? && !options.has_key?(:include_translated)
@@ -896,6 +898,9 @@
     module InternalStorageClassMethods
 
       private
+      def determine_deprecated_finder(match)
+        match.captures.first == 'all_by' ? :find_all : :find_first
+      end
 
       # Overridden to ensure that dynamic finders using localized attributes
       # like find_by_user_name(user_name) or find_by_user_name_and_password(user_name, password)
@@ -934,9 +939,9 @@
               end
 
             else
-              ActiveSupport::Deprecation.silence do
-                send(deprecated_finder, sanitize_sql(attributes), *arguments[facets.length..-1])
-              end
+            ActiveSupport::Deprecation.silence do
+              send(deprecated_finder, sanitize_sql(attributes), *arguments[facets.length..-1])
+            end
           end
         elsif match = /find_or_(initialize|create)_by_([_a-zA-Z]\w*)/.match(method_id.to_s)
           instantiator = determine_instantiator(match)
Index: lib/globalize/rails/action_view.rb
===================================================================
--- lib/globalize/rails/action_view.rb (revision 175)
+++ lib/globalize/rails/action_view.rb (working copy)
@@ -26,11 +26,10 @@
       # Override because the original version is too minimalist
       def path_and_extension(template_path) #:nodoc:
         template_path_without_extension = template_path.sub(@@re_extension, '')
-        [ template_path_without_extension, $1 ]
+        [ template_path_without_extension, $1]
       end
       
       def locate_globalize_path(template_path, use_full_path)
-      
         active_locale = Globalize::Locale.active
         locale_code = active_locale.code
 

Re: globalize, rails 2.0, and error_messages_for

by Matt-208 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Oh, one more thing, about that patch.  Rails 8099 required.

matt

Daniel Wiesmann wrote:

> hi,
>
> anybody know that status of rails 2.0 compatibility with globalize?
>
> looks like the trunk of globalize has not been updated since may.
> i ran both together, but ran into some errors. i got around the first
> one,
> (comments at http://kopongo.com) but am stuck on errors that happen
> with the error_messages_for.
> anyone run into that or get rails 2 and globalize fixed up nice?
>
> thanx,
> daniel
>
>
>
>
>
>
>


Re: globalize, rails 2.0, and error_messages_for

by Matt-208 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Here's the fix for the error_messages_for issue.

With Tests! Yea

matt


Daniel Wiesmann wrote:

> hi,
>
> anybody know that status of rails 2.0 compatibility with globalize?
>
> looks like the trunk of globalize has not been updated since may.
> i ran both together, but ran into some errors. i got around the first
> one,
> (comments at http://kopongo.com) but am stuck on errors that happen
> with the error_messages_for.
> anyone run into that or get rails 2 and globalize fixed up nice?
>
> thanx,
> daniel
>
>
>
>
>
>
>


Property changes on: test/log
___________________________________________________________________
Name: svn:ignore
   - *

   + test.log


Index: test/active_record_helper_test.rb
===================================================================
--- test/active_record_helper_test.rb (revision 0)
+++ test/active_record_helper_test.rb (revision 0)
@@ -0,0 +1,38 @@
+require File.dirname(__FILE__) + '/test_helper'
+
+class Product < ActiveRecord::Base
+  set_table_name "globalize_products"
+end
+
+class ActiveRecordHelperTest < Test::Unit::TestCase
+  include Globalize
+  include ActionView::Helpers::ActiveRecordHelper
+  include ActionView::Helpers::TextHelper
+  include ActionView::Helpers::TagHelper
+
+  fixtures :globalize_languages, :globalize_products, :globalize_countries, :globalize_translations
+
+  def test_error_messages_for_with_errors
+    @product = Product.new
+    @product.errors.add_to_base("Danger Will Robinson")
+    assert !error_messages_for(:product).to_s.blank?
+  end
+
+  def test_error_messages_for_missing_instance_var
+    assert_equal "", error_messages_for(:missing)
+  end
+
+  def test_error_messages_for_without_errors
+    @product = Product.new
+    assert_equal "", error_messages_for(:product)
+  end
+
+  def test_translates_errors
+    Locale.set('es')
+    Locale.set_translation("1 error prohibited this product from being saved", "translated")
+    @product = Product.new
+    @product.errors.add_to_base("Danger Will Robinson")
+    assert_match(/translated/,error_messages_for(:product).to_s)
+  end
+
+end
Index: lib/globalize/rails/active_record_helper.rb
===================================================================
--- lib/globalize/rails/active_record_helper.rb (revision 525)
+++ lib/globalize/rails/active_record_helper.rb (working copy)
@@ -12,22 +12,42 @@
       # you need is significantly different from the default presentation, it makes plenty of sense to access the object.errors
       # instance yourself and set it up. View the source of this method to see how easy it is.
       #
-      # Retrofitted for Globalize by André Camargo
-      def error_messages_for(object_name, options = {})
-        options = options.symbolize_keys
-        object = instance_variable_get("@#{object_name}")
-        unless object.errors.empty?
-          content_tag("div",
-            content_tag(
-              options[:header_tag] || "h2",
-              "%d errors prohibited this #{object_name.to_s.gsub("_", " ").t} from being saved" / object.errors.count
-            ) +
-            content_tag("p", "There were problems with the following fields:".t) +
-            content_tag("ul", object.errors.full_messages.collect { |msg| content_tag("li", msg) }),
-            "id" => options[:id] || "errorExplanation", "class" => options[:class] || "errorExplanation"
-          )
+      # Retrofitted for Globalize by Matt Clark @ PunchStock.com
+      def error_messages_for(*params)
+        options = params.extract_options!.symbolize_keys
+        if object = options.delete(:object)
+          objects = [object].flatten
+        else
+          objects = params.collect {|object_name| instance_variable_get("@#{object_name}") }.compact
         end
+        count   = objects.inject(0) {|sum, object| sum + object.errors.count }
+        unless count.zero?
+          html = {}
+          [:id, :class].each do |key|
+            if options.include?(key)
+              value = options[key]
+              html[key] = value unless value.blank?
+            else
+              html[key] = 'errorExplanation'
+            end
+          end
+          options[:object_name] ||= params.first
+          options[:header_message] = "#{pluralize(count, 'error')} prohibited this #{options[:object_name].to_s.gsub('_', ' ')} from being saved".t unless options.include?(:header_message)
+          options[:message] ||= 'There were problems with the following fields:'.t unless options.include?(:message)
+          error_messages = objects.map {|object| object.errors.full_messages.map {|msg| content_tag(:li, msg) } }
+
+          contents = ''
+          contents << content_tag(options[:header_tag] || :h2, options[:header_message]) unless options[:header_message].blank?
+          contents << content_tag(:p, options[:message]) unless options[:message].blank?
+          contents << content_tag(:ul, error_messages)
+
+          content_tag(:div, contents, html)
+        else
+          ''
+        end
       end
+
+
     end
   end
 end