lib/globalize/localization/db_translate.rb
# Use this instead of +find+ if you want to bypass the translation
# code for any reason.
#
# Note: <i>Use when Globalize::DbTranslate.keep_translations_in_model is false</i>
Use this instead of +find+ if you want to bypass the translation
code for any reason.


Use this instead of + find + if you want to bypass the translation code for any reason .
def find_every(options)
          return globalize_old_find_every(options) if options[:untranslated]
          if Locale.base? && !options.has_key?(:include_translated)
            results = globalize_old_find_every(options)
            results.each {|result|
              result.set_original_language
            }
            return results
          end
          options[:conditions] = sanitize_sql(options[:conditions]) if options[:conditions]
          joins_clause = options[:joins].nil? ? "" : options[:joins].dup
          joins_args = []
          load_full = options[:translate_all]
          facets = load_full ? globalize_facets : preload_facets
          if options[:select].nil? || options[:select] = '*'
            surrounding_clause = '%s'
          else
            surrounding_clause = options[:select]
            re_select = Regexp.new("#{table_name}.*")
            if surrounding_clause =~ re_select
              surrounding_clause = surrounding_clause.gsub(re_select, '%s')
            else
              raise StandardError,
              "this :select option format is not allowed on translatable models " +
              "(#{options[:select]})"
            end
          end
          select_clause = untranslated_fields.map {|f| "#{table_name}.#{f}" }.join(", ")
          if Locale.base?
            select_clause <<  ', ' << facets.map {|f| "#{table_name}.#{f}" }.join(", ")
          else
            language_id = Locale.active.language.id
            load_full = options[:translate_all]
            facets = load_full ? globalize_facets : preload_facets
            select_position = untranslated_fields.size
            if options[:conditions].nil?
              where_clause = ""
            else
              if options[:conditions].kind_of? Array
                conditions_is_array = true
                where_clause = options[:conditions].shift
              else
                where_clause = options[:conditions]
              end
            end
            facets.each do |facet|
              facet = facet.to_s
              facet_table_alias = "t_#{facet}"
              select_position += 1
              options[:order].sub!(/\b#{facet}\b/, select_position.to_s) if options[:order] && sqlite?
              select_clause << ", COALESCE(#{facet_table_alias}.text, #{table_name}.#{facet}) AS #{facet}, "
              select_clause << " #{facet_table_alias}.text AS #{facet}_not_base "
              joins_clause  << " LEFT OUTER JOIN globalize_translations AS #{facet_table_alias} " +
                "ON #{facet_table_alias}.table_name = ? " +
                "AND #{table_name}.#{primary_key} = #{facet_table_alias}.item_id " +
                "AND #{facet_table_alias}.facet = ? AND #{facet_table_alias}.language_id = ? "
              joins_args << table_name << facet << language_id
              where_clause.gsub!(/((((#{table_name}\.)|\W)#{facet})|^#{facet})\W/, " COALESCE(#{facet_table_alias}.text, #{table_name}.#{facet}) ")
            end
            options[:conditions] = sanitize_sql(
              conditions_is_array ? [ where_clause ] + options[:conditions] : where_clause
            ) unless options[:conditions].nil?
          end
          associations = options[:include_translated] || []
          associations = [ associations ].flatten
          associations.each do |assoc|
            rfxn = reflect_on_association(assoc)
            assoc_type = rfxn.macro
            raise StandardError,
              ":include_translated associations must be of type :belongs_to;" +
              "#{assoc} is #{assoc_type}" if assoc_type != :belongs_to
            klass = rfxn.klass
            assoc_facets = klass.preload_facets
            included_table = klass.table_name
            included_fk = klass.primary_key
            fk = rfxn.options[:foreign_key] || "#{assoc}_id"
            assoc_facets.each do |facet|
              facet_table_alias = "t_#{assoc}_#{facet}"
             if Locale.base?
                select_clause << ", #{included_table}.#{facet} AS #{assoc}_#{facet} "
              else
                select_clause << ", COALESCE(#{facet_table_alias}.text, #{included_table}.#{facet}) " +
                  "AS #{assoc}_#{facet} "
                joins_clause << " LEFT OUTER JOIN globalize_translations AS #{facet_table_alias} " +
                  "ON #{facet_table_alias}.table_name = ? " +
                  "AND #{table_name}.#{fk} = #{facet_table_alias}.item_id " +
                  "AND #{facet_table_alias}.facet = ? AND #{facet_table_alias}.language_id = ? "
                joins_args << klass.table_name << facet.to_s << language_id
              end
            end
            joins_clause << "LEFT OUTER JOIN #{included_table} " +
                "ON #{table_name}.#{fk} = #{included_table}.#{included_fk} "
          end
          options[:select] = surrounding_clause % select_clause
          options[:readonly] = false
          sanitized_joins_clause = sanitize_sql( [ joins_clause, *joins_args ] )
          options[:joins] = sanitized_joins_clause
          results = globalize_old_find_every(options)
          results.each {|result|
            result.set_original_language
            result.fully_loaded = true if load_full
          }
          return results
        end
def find_every ( options ) return globalize_old_find_every ( options ) if options [ :untranslated ] if Locale . base? && ! options . has_key? ( :include_translated ) results = globalize_old_find_every ( options ) results . each { | result | result . set_original_language } return results end options [ :conditions ] = sanitize_sql ( options [ :conditions ] ) if options [ :conditions ] joins_clause = options [ :joins ] . nil? ? "" : options [ :joins ] . dup joins_args = [ ] load_full = options [ :translate_all ] facets = load_full ? globalize_facets : preload_facets if options [ :select ] . nil? || options [ :select ] = '*' surrounding_clause = '%s' else surrounding_clause = options [ :select ] re_select = Regexp . new ( "#{table_name}.*" ) if surrounding_clause =~ re_select surrounding_clause = surrounding_clause . gsub ( re_select , '%s' ) else raise StandardError , "this :select option format is not allowed on translatable models " + "(#{options[:select]})" end end select_clause = untranslated_fields . map { | f | "#{table_name}.#{f}" } . join ( ", " ) if Locale . base? select_clause << ', ' << facets . map { | f | "#{table_name}.#{f}" } . join ( ", " ) else language_id = Locale . active . language . id load_full = options [ :translate_all ] facets = load_full ? globalize_facets : preload_facets select_position = untranslated_fields . size if options [ :conditions ] . nil? where_clause = "" else if options [ :conditions ] . kind_of? Array conditions_is_array = true where_clause = options [ :conditions ] . shift else where_clause = options [ :conditions ] end end facets . each do | facet | facet = facet . to_s facet_table_alias = "t_#{facet}" select_position += 1 options [ :order ] . sub! ( / \b #{ facet } \b / , select_position . to_s ) if options [ :order ] && sqlite? select_clause << ", COALESCE(#{facet_table_alias}.text, #{table_name}.#{facet}) AS #{facet}, " select_clause << " #{facet_table_alias}.text AS #{facet}_not_base " joins_clause << " LEFT OUTER JOIN globalize_translations AS #{facet_table_alias} " + "ON #{facet_table_alias}.table_name = ? " + "AND #{table_name}.#{primary_key} = #{facet_table_alias}.item_id " + "AND #{facet_table_alias}.facet = ? AND #{facet_table_alias}.language_id = ? " joins_args << table_name << facet << language_id where_clause . gsub! ( / (((( #{ table_name } \. )| \W ) #{ facet } )|^ #{ facet } ) \W / , " COALESCE(#{facet_table_alias}.text, #{table_name}.#{facet}) " ) end options [ :conditions ] = sanitize_sql ( conditions_is_array ? [ where_clause ] + options [ :conditions ] : where_clause ) unless options [ :conditions ] . nil? end associations = options [ :include_translated ] || [ ] associations = [ associations ] . flatten associations . each do | assoc | rfxn = reflect_on_association ( assoc ) assoc_type = rfxn . macro raise StandardError , ":include_translated associations must be of type :belongs_to;" + "#{assoc} is #{assoc_type}" if assoc_type != :belongs_to klass = rfxn . klass assoc_facets = klass . preload_facets included_table = klass . table_name included_fk = klass . primary_key fk = rfxn . options [ :foreign_key ] || "#{assoc}_id" assoc_facets . each do | facet | facet_table_alias = "t_#{assoc}_#{facet}" if Locale . base? select_clause << ", #{included_table}.#{facet} AS #{assoc}_#{facet} " else select_clause << ", COALESCE(#{facet_table_alias}.text, #{included_table}.#{facet}) " + "AS #{assoc}_#{facet} " joins_clause << " LEFT OUTER JOIN globalize_translations AS #{facet_table_alias} " + "ON #{facet_table_alias}.table_name = ? " + "AND #{table_name}.#{fk} = #{facet_table_alias}.item_id " + "AND #{facet_table_alias}.facet = ? AND #{facet_table_alias}.language_id = ? " joins_args << klass . table_name << facet . to_s << language_id end end joins_clause << "LEFT OUTER JOIN #{included_table} " + "ON #{table_name}.#{fk} = #{included_table}.#{included_fk} " end options [ :select ] = surrounding_clause % select_clause options [ :readonly ] = false sanitized_joins_clause = sanitize_sql ( [ joins_clause , * joins_args ] ) options [ :joins ] = sanitized_joins_clause results = globalize_old_find_every ( options ) results . each { | result | result . set_original_language result . fully_loaded = true if load_full } return results end
Use this instead of +find+ if you want to bypass the translation
code for any reason.
Use this instead of + find + if you want to bypass the translation code for any reason .
# do quick version if base language is active # there will at least be an +id+ field here =begin
          There's a bug in sqlite that messes up sorting when aliasing fields,
          see: <http://www.sqlite.org/cvstrac/tktview?tn=1521,33>.

          Since I want to use sqlite, and sorting, I'm hacking this to make it work.
          This involves renaming order by fields and adding them to the SELECT part.
          It's a sucky hack, but hopefully sqlite will fix the bug soon.
=end # sqlite bug hack # initialize where tweaking # sqlite bug hack #for translated fields inside WHERE clause substitute corresponding COALESCE string # add in associations (of :belongs_to nature) if applicable
options
<not_specific>