Changeset 21

Show
Ignore:
Timestamp:
10/19/06 09:00:01 (2 years ago)
Author:
olivier
Message:

Load ./for-1.1/ into trunk.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/init.rb

    r1 r21  
    2929require "globalize/rails/action_mailer" 
    3030require "globalize/rails/date_helper" 
     31require "globalize/rails/active_record_helper" 
  • trunk/lib/globalize/localization/db_translate.rb

    r1 r21  
    3737            Locale.set("es_ES") 
    3838            product.name -> guitarra 
     39             
     40        The standard ActiveRecord +find+ method has been tweaked to work with Globalize. 
     41        Use it in the exact same way you would the regular find, except for the  
     42        following provisos: 
     43 
     44        1. At this point, it will not work with the <tt>:include</tt> option... 
     45        1. However, there is a replacement: <tt>:include_translated</tt>, which will  
     46         be described below. 
     47        1. The <tt>:select</tt> option is prohibited. 
     48 
     49        +find+ returns the retreived models, with all translated fields correctly 
     50        loaded, depending on the active language. 
     51             
     52        <tt>:include_translated</tt> works as follows:  
     53        any model specified in the <tt>:include_translated</tt> option 
     54        will be eagerly loaded and added to the current model as attributes, 
     55        prefixed with the name of the associated model. This is often referred 
     56        to as _piggybacking_. 
     57 
     58        Example: 
     59          class Product < ActiveRecord::Base 
     60            belongs_to :manufacturer 
     61            belongs_to :category 
     62          end 
     63 
     64          class Category < ActiveRecord::Base 
     65            has_many :products 
     66            translates :name 
     67          end 
     68 
     69          prods = Product.find(:all, :include_translated => [ :manufacturer, :category ]) 
     70          prods.first.category_name -> "batedeira"             
    3971=end 
    4072      def translates(*facets) 
    41  
    4273        # parse out options hash 
    4374        options = facets.pop if facets.last.kind_of? Hash 
     
    4576 
    4677        facets_string = "[" + facets.map {|facet| ":#{facet}"}.join(", ") + "]" 
    47         class_eval <<-HERE 
     78        class_eval <<-HERE    
    4879          @@facet_options = {} 
    4980          attr_writer :fully_loaded 
     
    74105              @@postload_facets ||= @@globalize_facets - @@preload_facets 
    75106            end 
    76             alias_method :globalize_old_find, :find unless 
    77               respond_to? :globalize_old_find 
     107            alias_method :globalize_old_find_every, :find_every unless 
     108              respond_to? :globalize_old_find_every 
    78109          end 
    79110          alias_method :globalize_old_reload,   :reload 
    80111          alias_method :globalize_old_destroy,  :destroy 
    81112          alias_method :globalize_old_create_or_update, :create_or_update 
    82          
     113          alias_method :globalize_old_update, :update         
     114           
    83115          include Globalize::DbTranslate::TranslateObjectMethods 
    84116          extend  Globalize::DbTranslate::TranslateClassMethods         
     
    237269 
    238270        def create_or_update 
    239           globalize_old_create_or_update 
    240           update_translation if Locale.active 
     271          result = globalize_old_create_or_update 
     272          update_translation if Locale.active && result 
     273          result 
     274        end 
     275         
     276        def update 
     277          status = true 
     278          status = globalize_old_update unless attributes_with_quotes(false).empty? 
     279          status 
    241280        end 
    242281         
     
    277316 
    278317    module TranslateClassMethods 
    279        
    280 =begin rdoc 
    281       This is a replacement for the standard ActiveRecord +find+ method. 
    282       Use it in the exact same way you would the regular find, except for the  
    283       following provisos: 
    284  
    285       1. At this point, it will not work with the <tt>:include</tt> option... 
    286       1. However, there is a replacement: <tt>:include_translated</tt>, which will  
    287        be described below. 
    288       1. The <tt>:select</tt> option is prohibited. 
    289  
    290       +find+ returns the retreived models, with all translated fields correctly 
    291       loaded, depending on the active language. 
    292            
    293       <tt>:include_translated</tt> works as follows:  
    294       any model specified in the <tt>:include_translated</tt> option 
    295       will be eagerly loaded and added to the current model as attributes, 
    296       prefixed with the name of the associated model. This is often referred 
    297       to as _piggybacking_. 
    298  
    299       Example: 
    300         class Product < ActiveRecord::Base 
    301           belongs_to :manufacturer 
    302           belongs_to :category 
    303         end 
    304  
    305         class Category < ActiveRecord::Base 
    306           has_many :products 
    307           translates :name 
    308         end 
    309  
    310         prods = Product.find(:all, :include_translated => [ :manufacturer, :category ]) 
    311         prods.first.category_name -> "batedeira" 
    312 =end 
    313       def find(*args) 
    314         options = args.last.is_a?(Hash) ? args.last : {} 
    315  
    316         return globalize_old_find(*args) if options[:untranslated] 
    317  
    318         find_type = args.first 
    319         if find_type == :first 
    320           options[:translate_all] = true 
    321           return globalize_old_find(:first, options) 
    322         elsif find_type != :all 
    323           return globalize_old_find(*args) 
    324         end 
    325  
    326         raise StandardError,  
    327           ":select option not allowed on translatable models " + 
    328           "(#{options[:select]})" if options[:select] && !options[:select].empty? 
    329  
    330         # do quick version if base language is active 
    331         if Locale.base? && !options.has_key?(:include_translated)  
    332           results = untranslated_find(*args)  
    333           results.each {|result| 
    334             result.set_original_language 
    335           } 
    336           return results 
    337         end 
    338  
    339         options[:conditions] = fix_conditions(options[:conditions]) if options[:conditions] 
    340  
    341         # there will at least be an +id+ field here 
    342         select_clause = untranslated_fields.map {|f| "#{table_name}.#{f}" }.join(", ") 
    343  
    344         joins_clause = options[:joins].nil? ? "" : options[:joins].dup 
    345         joins_args = [] 
    346         load_full = options[:translate_all] 
    347         facets = load_full ? globalize_facets : preload_facets 
    348  
    349         if Locale.base? 
    350           select_clause <<  ', ' << facets.map {|f| "#{table_name}.#{f}" }.join(", ") 
    351         else 
    352           language_id = Locale.active.language.id 
    353           load_full = options[:translate_all] 
    354           facets = load_full ? globalize_facets : preload_facets 
    355            
    356 =begin 
    357         There's a bug in sqlite that messes up sorting when aliasing fields,  
    358         see: <http://www.sqlite.org/cvstrac/tktview?tn=1521,33>. 
    359  
    360         Since I want to use sqlite, and sorting, I'm hacking this to make it work. 
    361         This involves renaming order by fields and adding them to the SELECT part.  
    362         It's a sucky hack, but hopefully sqlite will fix the bug soon. 
    363 =end 
    364  
    365           # sqlite bug hack           
    366           select_position = untranslated_fields.size 
    367  
    368           # initialize where tweaking 
    369           if options[:conditions].nil? 
    370             where_clause = "" 
    371           else 
    372             if options[:conditions].kind_of? Array           
    373               conditions_is_array = true 
    374               where_clause = options[:conditions].shift 
    375             else 
    376               where_clause = options[:conditions] 
    377             end 
    378           end 
    379  
    380           facets.each do |facet|  
    381             facet = facet.to_s 
    382             facet_table_alias = "t_#{facet}" 
    383  
    384             # sqlite bug hack           
    385             select_position += 1 
    386             options[:order].sub!(/\b#{facet}\b/, select_position.to_s) if options[:order] && sqlite? 
    387  
    388             select_clause << ", COALESCE(#{facet_table_alias}.text, #{table_name}.#{facet}) AS #{facet}, "  
    389             select_clause << " #{facet_table_alias}.text AS #{facet}_not_base "  
    390             joins_clause  << " LEFT OUTER JOIN globalize_translations AS #{facet_table_alias} " + 
    391               "ON #{facet_table_alias}.table_name = ? " + 
    392               "AND #{table_name}.#{primary_key} = #{facet_table_alias}.item_id " + 
    393               "AND #{facet_table_alias}.facet = ? AND #{facet_table_alias}.language_id = ? " 
    394             joins_args << table_name << facet << language_id             
    395              
    396             #for translated fields inside WHERE clause substitute corresponding COALESCE string 
    397             where_clause.gsub!(/((((#{table_name}\.)|\W)#{facet})|^#{facet})\W/, " COALESCE(#{facet_table_alias}.text, #{table_name}.#{facet}) ")           
    398           end 
    399            
    400           options[:conditions] = sanitize_sql(  
    401             conditions_is_array ? [ where_clause ] + options[:conditions] : where_clause  
    402           ) unless options[:conditions].nil?           
    403         end 
    404  
    405         # add in associations (of :belongs_to nature) if applicable 
    406         associations = options[:include_translated] || [] 
    407         associations = [ associations ].flatten 
    408         associations.each do |assoc| 
    409           rfxn = reflect_on_association(assoc) 
    410           assoc_type = rfxn.macro 
    411           raise StandardError,  
    412             ":include_translated associations must be of type :belongs_to;" + 
    413             "#{assoc} is #{assoc_type}" if assoc_type != :belongs_to 
    414           klass = rfxn.klass 
    415           assoc_facets = klass.preload_facets 
    416           included_table = klass.table_name 
    417           included_fk = klass.primary_key 
    418           fk = rfxn.options[:foreign_key] || "#{assoc}_id" 
    419           assoc_facets.each do |facet| 
    420             facet_table_alias = "t_#{assoc}_#{facet}" 
    421  
    422            if Locale.base? 
    423               select_clause << ", #{included_table}.#{facet} AS #{assoc}_#{facet} " 
    424             else             
    425               select_clause << ", COALESCE(#{facet_table_alias}.text, #{included_table}.#{facet}) " + 
    426                 "AS #{assoc}_#{facet} " 
    427               joins_clause << " LEFT OUTER JOIN globalize_translations AS #{facet_table_alias} " + 
    428                 "ON #{facet_table_alias}.table_name = ? " + 
    429                 "AND #{table_name}.#{fk} = #{facet_table_alias}.item_id " + 
    430                 "AND #{facet_table_alias}.facet = ? AND #{facet_table_alias}.language_id = ? " 
    431               joins_args << klass.table_name << facet.to_s << language_id                         
    432             end                         
    433           end 
    434           joins_clause << "LEFT OUTER JOIN #{included_table} " +  
    435               "ON #{table_name}.#{fk} = #{included_table}.#{included_fk} " 
    436         end 
    437  
    438         options[:select] = select_clause 
    439         options[:readonly] = false 
    440  
    441         sanitized_joins_clause = sanitize_sql( [ joins_clause, *joins_args ] )         
    442         options[:joins] = sanitized_joins_clause 
    443         results = globalize_old_find(:all, options) 
    444  
    445         results.each {|result| 
    446           result.set_original_language 
    447           result.fully_loaded = true if load_full 
    448         } 
    449       end 
    450318 
    451319      # Use this instead of +find+ if you want to bypass the translation 
     
    456324        options[:untranslated] = true 
    457325        args << options if !has_options 
    458         globalize_old_find(*args) 
    459       end 
    460  
    461  
     326        find(*args) 
     327      end 
     328       
    462329      protected 
    463         def validate_find_options(options) # :nodoc: 
    464           options.assert_valid_keys [ :conditions, :group, :include, :include_translated,  
    465             :group, :joins, :limit, :offset, :order, :select, :readonly, :translate_all, 
    466             :untranslated ] 
    467         end 
    468  
     330        # FIX: figure out how to use default rails VALID_FIND_OPTIONS constant 
     331        VALID_FIND_OPTIONS = [ :conditions, :include, :joins, :limit, :offset, 
     332                               :order, :select, :readonly, :group, :from,  
     333                               :untranslated, :include_translated ] 
     334 
     335        def validate_find_options(options) #:nodoc: 
     336          options.assert_valid_keys(VALID_FIND_OPTIONS) 
     337        end 
     338       
    469339      private 
     340        def find_every(options) 
     341          return globalize_old_find_every(options) if options[:untranslated] 
     342          raise StandardError,  
     343            ":select option not allowed on translatable models " + 
     344            "(#{options[:select]})" if options[:select] && !options[:select].empty? 
     345 
     346          # do quick version if base language is active 
     347          if Locale.base? && !options.has_key?(:include_translated)  
     348            results = globalize_old_find_every(options)  
     349            results.each {|result| 
     350              result.set_original_language 
     351            } 
     352            return results 
     353          end 
     354 
     355          options[:conditions] = fix_conditions(options[:conditions]) if options[:conditions] 
     356 
     357          # there will at least be an +id+ field here 
     358          select_clause = untranslated_fields.map {|f| "#{table_name}.#{f}" }.join(", ") 
     359 
     360          joins_clause = options[:joins].nil? ? "" : options[:joins].dup 
     361          joins_args = [] 
     362          load_full = options[:translate_all] 
     363          facets = load_full ? globalize_facets : preload_facets 
     364 
     365          if Locale.base? 
     366            select_clause <<  ', ' << facets.map {|f| "#{table_name}.#{f}" }.join(", ") 
     367          else 
     368            language_id = Locale.active.language.id 
     369            load_full = options[:translate_all] 
     370            facets = load_full ? globalize_facets : preload_facets 
     371             
     372=begin 
     373          There's a bug in sqlite that messes up sorting when aliasing fields,  
     374          see: <http://www.sqlite.org/cvstrac/tktview?tn=1521,33>. 
     375 
     376          Since I want to use sqlite, and sorting, I'm hacking this to make it work. 
     377          This involves renaming order by fields and adding them to the SELECT part.  
     378          It's a sucky hack, but hopefully sqlite will fix the bug soon. 
     379=end 
     380 
     381            # sqlite bug hack           
     382            select_position = untranslated_fields.size 
     383 
     384            # initialize where tweaking 
     385            if options[:conditions].nil? 
     386              where_clause = "" 
     387            else 
     388              if options[:conditions].kind_of? Array           
     389                conditions_is_array = true 
     390                where_clause = options[:conditions].shift 
     391              else 
     392                where_clause = options[:conditions] 
     393              end 
     394            end 
     395 
     396            facets.each do |facet|  
     397              facet = facet.to_s 
     398              facet_table_alias = "t_#{facet}" 
     399 
     400              # sqlite bug hack           
     401              select_position += 1 
     402              options[:order].sub!(/\b#{facet}\b/, select_position.to_s) if options[:order] && sqlite? 
     403 
     404              select_clause << ", COALESCE(#{facet_table_alias}.text, #{table_name}.#{facet}) AS #{facet}, "  
     405              select_clause << " #{facet_table_alias}.text AS #{facet}_not_base "  
     406              joins_clause  << " LEFT OUTER JOIN globalize_translations AS #{facet_table_alias} " + 
     407                "ON #{facet_table_alias}.table_name = ? " + 
     408                "AND #{table_name}.#{primary_key} = #{facet_table_alias}.item_id " + 
     409                "AND #{facet_table_alias}.facet = ? AND #{facet_table_alias}.language_id = ? " 
     410              joins_args << table_name << facet << language_id             
     411               
     412              #for translated fields inside WHERE clause substitute corresponding COALESCE string 
     413              where_clause.gsub!(/((((#{table_name}\.)|\W)#{facet})|^#{facet})\W/, " COALESCE(#{facet_table_alias}.text, #{table_name}.#{facet}) ")           
     414            end 
     415             
     416            options[:conditions] = sanitize_sql(  
     417              conditions_is_array ? [ where_clause ] + options[:conditions] : where_clause  
     418            ) unless options[:conditions].nil?           
     419          end 
     420 
     421          # add in associations (of :belongs_to nature) if applicable 
     422          associations = options[:include_translated] || [] 
     423          associations = [ associations ].flatten 
     424          associations.each do |assoc| 
     425            rfxn = reflect_on_association(assoc) 
     426            assoc_type = rfxn.macro 
     427            raise StandardError,  
     428              ":include_translated associations must be of type :belongs_to;" + 
     429              "#{assoc} is #{assoc_type}" if assoc_type != :belongs_to 
     430            klass = rfxn.klass 
     431            assoc_facets = klass.preload_facets 
     432            included_table = klass.table_name 
     433            included_fk = klass.primary_key 
     434            fk = rfxn.options[:foreign_key] || "#{assoc}_id" 
     435            assoc_facets.each do |facet| 
     436              facet_table_alias = "t_#{assoc}_#{facet}" 
     437 
     438             if Locale.base? 
     439                select_clause << ", #{included_table}.#{facet} AS #{assoc}_#{facet} " 
     440              else             
     441                select_clause << ", COALESCE(#{facet_table_alias}.text, #{included_table}.#{facet}) " + 
     442                  "AS #{assoc}_#{facet} " 
     443                joins_clause << " LEFT OUTER JOIN globalize_translations AS #{facet_table_alias} " + 
     444                  "ON #{facet_table_alias}.table_name = ? " + 
     445                  "AND #{table_name}.#{fk} = #{facet_table_alias}.item_id " + 
     446                  "AND #{facet_table_alias}.facet = ? AND #{facet_table_alias}.language_id = ? " 
     447                joins_args << klass.table_name << facet.to_s << language_id                         
     448              end                         
     449            end 
     450            joins_clause << "LEFT OUTER JOIN #{included_table} " +  
     451                "ON #{table_name}.#{fk} = #{included_table}.#{included_fk} " 
     452          end 
     453 
     454          options[:select] = select_clause 
     455          options[:readonly] = false 
     456 
     457          sanitized_joins_clause = sanitize_sql( [ joins_clause, *joins_args ] )         
     458          options[:joins] = sanitized_joins_clause 
     459          results = globalize_old_find_every(options) 
     460 
     461          results.each {|result| 
     462            result.set_original_language 
     463            result.fully_loaded = true if load_full 
     464          } 
     465           
     466          return results 
     467        end 
    470468 
    471469        # properly scope conditions to table 
  • trunk/lib/globalize/models/currency.rb

    r1 r21  
    150150        raise ArgumentError, "Not an amount (#{num})" if num.delete("^0-9").empty? 
    151151        _dollars, _cents = num.delete("^0-9.").split('.', 2) 
    152         _cents = 0 if !_cents 
     152        _cents = _cents ? _cents[0,2] : 0 
    153153        Currency.new(_dollars.to_i * 100 + _cents.to_i) 
    154154      when num.is_a?(Numeric) 
  • trunk/lib/globalize/rails/action_mailer.rb

    r1 r21  
    2525      initialize_defaults(method_name) 
    2626      send(method_name, *parameters) 
    27  
     27       
    2828      # If an explicit, textual body has not been set, we check assumptions. 
    2929      unless String === @body 
     
    4040          end 
    4141        end 
    42  
     42         
    4343        # Then, if there were such templates, we check to see if we ought to 
    4444        # also render a "normal" template (without the content type). If a 
     
    6262      # build the mail object itself 
    6363      @mail = create_mail 
     64 
    6465    end 
    6566 
     
    8283            type_sections = code ? sections[2..-1] : sections[1..-1] 
    8384            type = type_sections.join("/") 
     85             
    8486            next if type.empty? 
    8587            @parts << Part.new(:content_type => type, 
     
    8789              :body => render_message(sections.join('.'), @body)) 
    8890          end 
     91           
    8992 
    9093          # if we found templates at this stage, no need to continue to defaults 
  • trunk/lib/globalize/rails/action_view.rb

    r1 r21  
    33  class Base 
    44    alias_method :globalize_old_render_file, :render_file 
    5  
     5     
     6    # Name of file extensions which are handled internally in rails. Other types 
     7    # like liquid has to register through register_handler. 
     8    @@re_extension = /\.(rjs|rhtml|rxml)$/ 
     9     
    610    @@globalize_path_cache = {} 
    711 
     
    913      if Globalize::Locale.active? 
    1014        localized_path = locate_globalize_path(template_path, use_full_path) 
    11  
    1215        # don't use_full_path -- we've already expanded the path 
    1316        globalize_old_render_file(localized_path, false, local_assigns) 
     
    1619      end 
    1720    end 
    18  
     21     
    1922    private 
     23     
     24      # Override because the original version is too minimalist 
     25      def path_and_extension(template_path) #:nodoc: 
     26        template_path_without_extension = template_path.sub(@@re_extension, '') 
     27        [ template_path_without_extension, $1 ] 
     28      end 
     29       
    2030      def locate_globalize_path(template_path, use_full_path) 
     31       
    2132        active_locale = Globalize::Locale.active 
    2233        locale_code = active_locale.code 
     
    2738 
    2839        if use_full_path 
    29                                         template_extension = pick_template_extension(template_path).to_s 
    30           template_file_name = full_template_path(template_path, template_extension) 
     40          template_path_without_extension, template_extension = path_and_extension(template_path) 
     41           
     42          if template_extension 
     43            template_file_name = full_template_path(template_path_without_extension, template_extension) 
     44          else 
     45            template_extension = pick_template_extension(template_path).to_s 
     46            template_file_name = full_template_path(template_path, template_extension) 
     47          end 
    3148        else 
    3249          template_file_name = template_path 
  • trunk/lib/globalize/rails/date_helper.rb

    r1 r21  
    7676        end 
    7777 
    78         select_html(options[:field_name] || 'month', month_options, options[:prefix], options[:include_blank], options[:discard_type], options[:disabled]) 
     78        select_html(options[:field_name] || 'month', month_options, options[:prefix], options[:include_blank], options[:discard_type], options[:disabled], options[:id], options[:class]) 
     79      end 
     80 
     81      def select_year(date, options = {}) 
     82        year_options = [] 
     83        y = date ? (date.kind_of?(Fixnum) ? (y = (date == 0) ? Date.today.year : date) : date.year) : Date.today.year 
     84 
     85        start_year, end_year = (options[:start_year] || y-5), (options[:end_year] || y+5) 
     86        step_val = start_year < end_year ? 1 : -1 
     87 
     88        start_year.step(end_year, step_val) do |year| 
     89          year_options << ((date && (date.kind_of?(Fixnum) ? date : date.year) == year) ? 
     90            %(<option value="#{year}" selected="selected">#{year}</option>\n) : 
     91            %(<option value="#{year}">#{year}</option>\n) 
     92          ) 
     93        end 
     94 
     95        select_html(options[:field_name] || 'year', year_options, options[:prefix], options[:include_blank], options[:discard_type], options[:disabled], options[:id], options[:class]) 
     96      end 
     97       
     98      private 
     99        def select_html(type, options, prefix = nil, include_blank = false, discard_type = false, disabled = false, id = nil, klass = nil) 
     100          select_html  = %(<select name="#{prefix || DEFAULT_PREFIX}) 
     101          select_html << "[#{type}]" unless discard_type 
     102          select_html << %(") 
     103          select_html << %( disabled="disabled") if disabled 
     104          select_html << %( id="#{id}") if id 
     105          select_html << %( class="#{klass}") if klass 
     106          select_html << %(>\n) 
     107          select_html << %(<option value=""></option>\n) if include_blank 
     108          select_html << options.to_s 
     109          select_html << "</select>\n" 
    79110      end 
    80111    end 
  • trunk/tasks/data.rake

    r1 r21  
    11# Reads a CSV file from the data/ dir. 
     2require 'csv' 
     3 
    24def csv_file filename 
    35  path = File.join File.dirname( __FILE__ ), '../data', "#{filename}.csv" 
     
    6163      t.column :item_id,                :integer 
    6264      t.column :facet,                  :string 
     65      t.column :built_in,               :boolean, :default => true 
    6366      t.column :language_id,            :integer 
    6467      t.column :pluralization_index,    :integer 
     
    8790    ActiveRecord::Base.connection.add_index :globalize_languages, :iso_639_2   
    8891    ActiveRecord::Base.connection.add_index :globalize_languages, :iso_639_3   
    89     ActiveRecord::Base.connection.add_index :globalize_languages, :rfc_3066 
    90      
    91     # Dirty hack to force initalization of pg indexes 
    92     if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL' 
    93       ActiveRecord::Base.connection.execute "SELECT nextval('public.globalize_countries_id_seq')" 
    94       ActiveRecord::Base.connection.execute "SELECT nextval('public.globalize_translations_id_seq')" 
    95       ActiveRecord::Base.connection.execute "SELECT nextval('public.globalize_languages_id_seq')" 
    96     end 
     92    ActiveRecord::Base.connection.add_index :globalize_languages, :rfc_3066  
    9793  end 
    9894   
     
    108104  desc 'Load locale data' 
    109105  task :load_locale_data => :environment do 
     106    # This needs to be called here, so that we can load the structure without 
     107    # the data. It's needed for using currval() as used in loading 
     108    if ActiveRecord::Base.connection.adapter_name == 'PostgreSQL' 
     109      ActiveRecord::Base.connection.execute "SELECT nextval('globalize_countries_id_seq')" 
     110      ActiveRecord::Base.connection.execute "SELECT nextval('globalize_translations_id_seq')" 
     111      ActiveRecord::Base.connection.execute "SELECT nextval('globalize_languages_id_seq')" 
     112    end 
    110113    load_from_csv 'globalize_countries',    csv_file( :country_data ) 
    111114    load_from_csv 'globalize_languages',    csv_file( :language_data ) 
     
    119122    Globalize::Translation.destroy_all 
    120123  end 
     124   
     125  desc 'Run Globalize tests' 
     126  Rake::TestTask.new do |t| 
     127    t.test_files = FileList["#{File.dirname( __FILE__ )}/../test/*_test.rb"] 
     128    t.verbose = true 
     129  end 
    121130end 
  • trunk/test/action_mailer_test.rb

    r1 r21  
    4040  end 
    4141 
    42   def test_en 
     42  def test_he 
    4343    Locale.set('he') 
    4444    mail = GlobalizeMailer.create_test 
  • trunk/test/config/database.yml

    r1 r21  
    1010 
    1111test: 
    12   adapter: sqlite3 
    13   dbfile: vendor/plugins/globalize/test/db/test.sqlite3.db 
     12  adapter: mysql 
     13  database: globalize_test 
     14  username: root 
     15  password: root 
     16  encoding: utf8 
  • trunk/test/currency_test.rb

    r1 r21  
    9191  end 
    9292 
     93  def test_parse2 
     94    m1 = Currency.parse("$134.5483726") 
     95    assert_equal 13454, m1.cents   
     96  end 
     97 
     98  def test_parse2 
     99    m1 = Currency.parse('54') 
     100    assert_equal 5400, m1.cents   
     101  end 
     102   
    93103  def test_format 
    94104    m1 = Currency.new(1234567) 
  • trunk/test/db/schema.rb

    r1 r21  
    11ActiveRecord::Schema.define do 
     2 
     3  create_table :globalize_simples, :force => true do |t| 
     4    t.column :name, :string 
     5    t.column :description, :string 
     6  end 
    27 
    38  create_table :globalize_products, :force => true do |t| 
     
    5863  end 
    5964 
    60   add_index :globalize_translations, [ :tr_key, :language_id ] 
    61   add_index :globalize_translations, [ :table_name, :item_id, :language_id ] 
     65  add_index :globalize_translations, [ :tr_key, :language_id ], :name => 'tr_key' 
     66  add_index :globalize_translations, [ :table_name, :item_id, :language_id ], :name => 'table_name' 
    6267 
    6368  create_table :globalize_languages, :force => true do |t| 
  • trunk/test/db_translation_test.rb

    r1 r21  
    55  fixtures :globalize_languages, :globalize_translations, :globalize_countries,  
    66    :globalize_products, :globalize_manufacturers, :globalize_categories,  
    7     :globalize_categories_products 
     7    :globalize_categories_products, :globalize_simples 
    88 
    99  class Product < ActiveRecord::Base 
     
    3131  end 
    3232 
     33  class Simple < ActiveRecord::Base 
     34    set_table_name "globalize_simples" 
     35 
     36    translates :name, :description 
     37  end 
     38 
    3339  def setup 
     40    Globalize::Locale.set_base_language("en-US") 
    3441    Globalize::Locale.set("en-US") 
    35     Globalize::Locale.set_base_language("en-US") 
    36   end 
    37  
     42  end 
     43 
     44  def test_simple 
     45    simp = Simple.find(1) 
     46    assert_equal "first", simp.name 
     47    assert_equal "This is a description of the first simple", simp.description    
     48     
     49    Globalize::Locale.set 'he-IL' 
     50    simp = Simple.find(1) 
     51    assert_equal "זהו השם הךאשון", simp.name 
     52    assert_equal "זהו התיאוך הךאשון", simp.description    
     53  end 
     54 
     55  def test_simple_save 
     56    simp = Simple.find(1) 
     57    simp.name = '1st' 
     58    simp.save! 
     59     
     60    Globalize::Locale.set 'he-IL' 
     61    simp = Simple.find(1) 
     62    simp.name = 'ה-1' 
     63    simp.save! 
     64  end 
     65 
     66  def test_simple_create 
     67    simp = Simple.new 
     68    simp.name = '1st' 
     69    simp.save! 
     70     
     71    Globalize::Locale.set 'he-IL' 
     72    simp = Simple.new 
     73    simp.name = 'ה-1' 
     74    simp.save! 
     75  end 
     76   
    3877  def test_native_language 
    3978    heb = Globalize::Language.pick("he") 
  • trunk/test/fixtures/globalize_translations.yml

    r1 r21  
    100100  type: ViewTranslation 
    101101  language_id: 2 
    102   tr_key: "Specs is too long (max is %d characters)" 
     102  tr_key: "Specs is too long (maximum is %d characters)" 
    103103  pluralization_index: 1 
    104104  text: "המ׀ךט א׹וך מדי (המקסימום הוא תו אחד)" 
     
    107107  type: ViewTranslation 
    108108  language_id: 2 
    109   tr_key: "Specs is too long (max is %d characters)" 
     109  tr_key: "Specs is too long (maximum is %d characters)" 
    110110  pluralization_index: 2 
    111111  text: "המ׀ךט א׹וך מדי (המקסימום הוא %d תווים)" 
     
    334334  pluralization_index: 1 
    335335  text: "דשמב׹" 
     336 
     337model_simple_name_1: 
     338  id: 50 
     339  table_name: globalize_simples 
     340  item_id: 1 
     341  facet: name 
     342  language_id: 2 
     343  text: זהו השם הךאשון 
     344  type: ModelTranslation 
     345 
     346model_simple_desc_1: 
     347  id: 51 
     348  table_name: globalize_simples 
     349  item_id: 1 
     350  facet: description 
     351  language_id: 2 
     352  text: זהו התיאוך הךאשון 
     353  type: ModelTranslation 
     354   
  • trunk/test/validation_test.rb

    r1 r21  
    2525    prod = Product.find(3) 
    2626    assert !prod.valid? 
    27     assert_equal "Name is too short (min is 5 characters)", prod.errors.full_messages.first  
     27    assert_equal "Name is too short (minimum is 5 characters)", prod.errors.full_messages.first  
    2828  end 
    2929end