Changeset 21
- Timestamp:
- 10/19/06 09:00:01 (2 years ago)
- Files:
-
- trunk/init.rb (modified) (1 diff)
- trunk/lib/globalize/localization/db_translate.rb (modified) (6 diffs)
- trunk/lib/globalize/models/currency.rb (modified) (1 diff)
- trunk/lib/globalize/rails/action_mailer.rb (modified) (5 diffs)
- trunk/lib/globalize/rails/action_view.rb (modified) (4 diffs)
- trunk/lib/globalize/rails/active_record_helper.rb (added)
- trunk/lib/globalize/rails/date_helper.rb (modified) (1 diff)
- trunk/tasks/data.rake (modified) (5 diffs)
- trunk/test/action_mailer_test.rb (modified) (1 diff)
- trunk/test/config/database.yml (modified) (1 diff)
- trunk/test/config/database.yml.default (added)
- trunk/test/config/database.yml.example (added)
- trunk/test/currency_test.rb (modified) (1 diff)
- trunk/test/db/schema.rb (modified) (2 diffs)
- trunk/test/db_translation_test.rb (modified) (2 diffs)
- trunk/test/fixtures/globalize_simples.yml (added)
- trunk/test/fixtures/globalize_translations.yml (modified) (3 diffs)
- trunk/test/mime_responds_test.rb (added)
- trunk/test/validation_test.rb (modified) (1 diff)
- trunk/test/views/layouts (added)
- trunk/test/views/layouts/standard.rhtml (added)
- trunk/test/views/respond_to (added)
- trunk/test/views/respond_to/all_types_with_layout.rhtml (added)
- trunk/test/views/respond_to/all_types_with_layout.rjs (added)
- trunk/test/views/respond_to/using_defaults.en.rhtml (added)
- trunk/test/views/respond_to/using_defaults.en.rjs (added)
- trunk/test/views/respond_to/using_defaults.en.rxml (added)
- trunk/test/views/respond_to/using_defaults.fr.rhtml (added)
- trunk/test/views/respond_to/using_defaults.fr.rjs (added)
- trunk/test/views/respond_to/using_defaults.fr.rxml (added)
- trunk/test/views/respond_to/using_defaults.rhtml (added)
- trunk/test/views/respond_to/using_defaults.rjs (added)
- trunk/test/views/respond_to/using_defaults.rxml (added)
- trunk/test/views/respond_to/using_defaults_with_type_list.en.rhtml (added)
- trunk/test/views/respond_to/using_defaults_with_type_list.en.rjs (added)
- trunk/test/views/respond_to/using_defaults_with_type_list.en.rxml (added)
- trunk/test/views/respond_to/using_defaults_with_type_list.fr.rhtml (added)
- trunk/test/views/respond_to/using_defaults_with_type_list.fr.rjs (added)
- trunk/test/views/respond_to/using_defaults_with_type_list.fr.rxml (added)
- trunk/test/views/respond_to/using_defaults_with_type_list.rhtml (added)
- trunk/test/views/respond_to/using_defaults_with_type_list.rjs (added)
- trunk/test/views/respond_to/using_defaults_with_type_list.rxml (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/init.rb
r1 r21 29 29 require "globalize/rails/action_mailer" 30 30 require "globalize/rails/date_helper" 31 require "globalize/rails/active_record_helper" trunk/lib/globalize/localization/db_translate.rb
r1 r21 37 37 Locale.set("es_ES") 38 38 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" 39 71 =end 40 72 def translates(*facets) 41 42 73 # parse out options hash 43 74 options = facets.pop if facets.last.kind_of? Hash … … 45 76 46 77 facets_string = "[" + facets.map {|facet| ":#{facet}"}.join(", ") + "]" 47 class_eval <<-HERE 78 class_eval <<-HERE 48 79 @@facet_options = {} 49 80 attr_writer :fully_loaded … … 74 105 @@postload_facets ||= @@globalize_facets - @@preload_facets 75 106 end 76 alias_method :globalize_old_find , :findunless77 respond_to? :globalize_old_find 107 alias_method :globalize_old_find_every, :find_every unless 108 respond_to? :globalize_old_find_every 78 109 end 79 110 alias_method :globalize_old_reload, :reload 80 111 alias_method :globalize_old_destroy, :destroy 81 112 alias_method :globalize_old_create_or_update, :create_or_update 82 113 alias_method :globalize_old_update, :update 114 83 115 include Globalize::DbTranslate::TranslateObjectMethods 84 116 extend Globalize::DbTranslate::TranslateClassMethods … … 237 269 238 270 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 241 280 end 242 281 … … 277 316 278 317 module TranslateClassMethods 279 280 =begin rdoc281 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 the283 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 will287 be described below.288 1. The <tt>:select</tt> option is prohibited.289 290 +find+ returns the retreived models, with all translated fields correctly291 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> option295 will be eagerly loaded and added to the current model as attributes,296 prefixed with the name of the associated model. This is often referred297 to as _piggybacking_.298 299 Example:300 class Product < ActiveRecord::Base301 belongs_to :manufacturer302 belongs_to :category303 end304 305 class Category < ActiveRecord::Base306 has_many :products307 translates :name308 end309 310 prods = Product.find(:all, :include_translated => [ :manufacturer, :category ])311 prods.first.category_name -> "batedeira"312 =end313 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.first319 if find_type == :first320 options[:translate_all] = true321 return globalize_old_find(:first, options)322 elsif find_type != :all323 return globalize_old_find(*args)324 end325 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 active331 if Locale.base? && !options.has_key?(:include_translated)332 results = untranslated_find(*args)333 results.each {|result|334 result.set_original_language335 }336 return results337 end338 339 options[:conditions] = fix_conditions(options[:conditions]) if options[:conditions]340 341 # there will at least be an +id+ field here342 select_clause = untranslated_fields.map {|f| "#{table_name}.#{f}" }.join(", ")343 344 joins_clause = options[:joins].nil? ? "" : options[:joins].dup345 joins_args = []346 load_full = options[:translate_all]347 facets = load_full ? globalize_facets : preload_facets348 349 if Locale.base?350 select_clause << ', ' << facets.map {|f| "#{table_name}.#{f}" }.join(", ")351 else352 language_id = Locale.active.language.id353 load_full = options[:translate_all]354 facets = load_full ? globalize_facets : preload_facets355 356 =begin357 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 =end364 365 # sqlite bug hack366 select_position = untranslated_fields.size367 368 # initialize where tweaking369 if options[:conditions].nil?370 where_clause = ""371 else372 if options[:conditions].kind_of? Array373 conditions_is_array = true374 where_clause = options[:conditions].shift375 else376 where_clause = options[:conditions]377 end378 end379 380 facets.each do |facet|381 facet = facet.to_s382 facet_table_alias = "t_#{facet}"383 384 # sqlite bug hack385 select_position += 1386 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_id395 396 #for translated fields inside WHERE clause substitute corresponding COALESCE string397 where_clause.gsub!(/((((#{table_name}\.)|\W)#{facet})|^#{facet})\W/, " COALESCE(#{facet_table_alias}.text, #{table_name}.#{facet}) ")398 end399 400 options[:conditions] = sanitize_sql(401 conditions_is_array ? [ where_clause ] + options[:conditions] : where_clause402 ) unless options[:conditions].nil?403 end404 405 # add in associations (of :belongs_to nature) if applicable406 associations = options[:include_translated] || []407 associations = [ associations ].flatten408 associations.each do |assoc|409 rfxn = reflect_on_association(assoc)410 assoc_type = rfxn.macro411 raise StandardError,412 ":include_translated associations must be of type :belongs_to;" +413 "#{assoc} is #{assoc_type}" if assoc_type != :belongs_to414 klass = rfxn.klass415 assoc_facets = klass.preload_facets416 included_table = klass.table_name417 included_fk = klass.primary_key418 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 else425 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_id432 end433 end434 joins_clause << "LEFT OUTER JOIN #{included_table} " +435 "ON #{table_name}.#{fk} = #{included_table}.#{included_fk} "436 end437 438 options[:select] = select_clause439 options[:readonly] = false440 441 sanitized_joins_clause = sanitize_sql( [ joins_clause, *joins_args ] )442 options[:joins] = sanitized_joins_clause443 results = globalize_old_find(:all, options)444 445 results.each {|result|446 result.set_original_language447 result.fully_loaded = true if load_full448 }449 end450 318 451 319 # Use this instead of +find+ if you want to bypass the translation … … 456 324 options[:untranslated] = true 457 325 args << options if !has_options 458 globalize_old_find(*args) 459 end 460 461 326 find(*args) 327 end 328 462 329 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 469 339 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 470 468 471 469 # properly scope conditions to table trunk/lib/globalize/models/currency.rb
r1 r21 150 150 raise ArgumentError, "Not an amount (#{num})" if num.delete("^0-9").empty? 151 151 _dollars, _cents = num.delete("^0-9.").split('.', 2) 152 _cents = 0 if !_cents152 _cents = _cents ? _cents[0,2] : 0 153 153 Currency.new(_dollars.to_i * 100 + _cents.to_i) 154 154 when num.is_a?(Numeric) trunk/lib/globalize/rails/action_mailer.rb
r1 r21 25 25 initialize_defaults(method_name) 26 26 send(method_name, *parameters) 27 27 28 28 # If an explicit, textual body has not been set, we check assumptions. 29 29 unless String === @body … … 40 40 end 41 41 end 42 42 43 43 # Then, if there were such templates, we check to see if we ought to 44 44 # also render a "normal" template (without the content type). If a … … 62 62 # build the mail object itself 63 63 @mail = create_mail 64 64 65 end 65 66 … … 82 83 type_sections = code ? sections[2..-1] : sections[1..-1] 83 84 type = type_sections.join("/") 85 84 86 next if type.empty? 85 87 @parts << Part.new(:content_type => type, … … 87 89 :body => render_message(sections.join('.'), @body)) 88 90 end 91 89 92 90 93 # if we found templates at this stage, no need to continue to defaults trunk/lib/globalize/rails/action_view.rb
r1 r21 3 3 class Base 4 4 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 6 10 @@globalize_path_cache = {} 7 11 … … 9 13 if Globalize::Locale.active? 10 14 localized_path = locate_globalize_path(template_path, use_full_path) 11 12 15 # don't use_full_path -- we've already expanded the path 13 16 globalize_old_render_file(localized_path, false, local_assigns) … … 16 19 end 17 20 end 18 21 19 22 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 20 30 def locate_globalize_path(template_path, use_full_path) 31 21 32 active_locale = Globalize::Locale.active 22 33 locale_code = active_locale.code … … 27 38 28 39 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 31 48 else 32 49 template_file_name = template_path trunk/lib/globalize/rails/date_helper.rb
r1 r21 76 76 end 77 77 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" 79 110 end 80 111 end trunk/tasks/data.rake
r1 r21 1 1 # Reads a CSV file from the data/ dir. 2 require 'csv' 3 2 4 def csv_file filename 3 5 path = File.join File.dirname( __FILE__ ), '../data', "#{filename}.csv" … … 61 63 t.column :item_id, :integer 62 64 t.column :facet, :string 65 t.column :built_in, :boolean, :default => true 63 66 t.column :language_id, :integer 64 67 t.column :pluralization_index, :integer … … 87 90 ActiveRecord::Base.connection.add_index :globalize_languages, :iso_639_2 88 91 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 97 93 end 98 94 … … 108 104 desc 'Load locale data' 109 105 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 110 113 load_from_csv 'globalize_countries', csv_file( :country_data ) 111 114 load_from_csv 'globalize_languages', csv_file( :language_data ) … … 119 122 Globalize::Translation.destroy_all 120 123 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 121 130 end trunk/test/action_mailer_test.rb
r1 r21 40 40 end 41 41 42 def test_ en42 def test_he 43 43 Locale.set('he') 44 44 mail = GlobalizeMailer.create_test trunk/test/config/database.yml
r1 r21 10 10 11 11 test: 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 91 91 end 92 92 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 93 103 def test_format 94 104 m1 = Currency.new(1234567) trunk/test/db/schema.rb
r1 r21 1 1 ActiveRecord::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 2 7 3 8 create_table :globalize_products, :force => true do |t| … … 58 63 end 59 64 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' 62 67 63 68 create_table :globalize_languages, :force => true do |t| trunk/test/db_translation_test.rb
r1 r21 5 5 fixtures :globalize_languages, :globalize_translations, :globalize_countries, 6 6 :globalize_products, :globalize_manufacturers, :globalize_categories, 7 :globalize_categories_products 7 :globalize_categories_products, :globalize_simples 8 8 9 9 class Product < ActiveRecord::Base … … 31 31 end 32 32 33 class Simple < ActiveRecord::Base 34 set_table_name "globalize_simples" 35 36 translates :name, :description 37 end 38 33 39 def setup 40 Globalize::Locale.set_base_language("en-US") 34 41 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 38 77 def test_native_language 39 78 heb = Globalize::Language.pick("he") trunk/test/fixtures/globalize_translations.yml
r1 r21 100 100 type: ViewTranslation 101 101 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)" 103 103 pluralization_index: 1 104 104 text: "×××€×š× ×ך×× ××× (××קס×××× ××× ×ª× ×××)" … … 107 107 type: ViewTranslation 108 108 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)" 110 110 pluralization_index: 2 111 111 text: "×××€×š× ×ך×× ××× (××קס×××× ××× %d ת××××)" … … 334 334 pluralization_index: 1 335 335 text: "×׊××ך" 336 337 model_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 346 model_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 25 25 prod = Product.find(3) 26 26 assert !prod.valid? 27 assert_equal "Name is too short (min is 5 characters)", prod.errors.full_messages.first27 assert_equal "Name is too short (minimum is 5 characters)", prod.errors.full_messages.first 28 28 end 29 29 end
