summaryrefslogtreecommitdiff
path: root/vendor/plugins/globalize2/lib
diff options
context:
space:
mode:
authorerdgeist <erdgeist@erdgeist.org>2026-06-27 22:52:50 +0200
committererdgeist <erdgeist@erdgeist.org>2026-06-27 22:52:50 +0200
commit9a19a0494ef51cdac9a78e24d517ca48ba44c453 (patch)
tree8eaae12d8047a40e29d3ea7ff3116b5c869e04bd /vendor/plugins/globalize2/lib
parent85a01e35274b8d4d4165a7b26bd7986e211246bb (diff)
parent1853082fcd8c067390c246f9daa01a9b47387497 (diff)
Migration from Rails 2.3.5 to Rails 8.1 successful.
Merging dev branch.
Diffstat (limited to 'vendor/plugins/globalize2/lib')
-rw-r--r--vendor/plugins/globalize2/lib/globalize.rb15
-rw-r--r--vendor/plugins/globalize2/lib/globalize/active_record.rb194
-rw-r--r--vendor/plugins/globalize2/lib/globalize/active_record/adapter.rb80
-rw-r--r--vendor/plugins/globalize2/lib/globalize/active_record/attributes.rb25
-rw-r--r--vendor/plugins/globalize2/lib/globalize/active_record/migration.rb40
-rw-r--r--vendor/plugins/globalize2/lib/i18n/missing_translations_log_handler.rb41
-rw-r--r--vendor/plugins/globalize2/lib/i18n/missing_translations_raise_handler.rb25
7 files changed, 0 insertions, 420 deletions
diff --git a/vendor/plugins/globalize2/lib/globalize.rb b/vendor/plugins/globalize2/lib/globalize.rb
deleted file mode 100644
index 67c1878..0000000
--- a/vendor/plugins/globalize2/lib/globalize.rb
+++ /dev/null
@@ -1,15 +0,0 @@
1module Globalize
2 autoload :ActiveRecord, 'globalize/active_record'
3
4 class << self
5 def fallbacks?
6 I18n.respond_to?(:fallbacks)
7 end
8
9 def fallbacks(locale)
10 fallbacks? ? I18n.fallbacks[locale] : [locale.to_sym]
11 end
12 end
13end
14
15ActiveRecord::Base.send(:include, Globalize::ActiveRecord)
diff --git a/vendor/plugins/globalize2/lib/globalize/active_record.rb b/vendor/plugins/globalize2/lib/globalize/active_record.rb
deleted file mode 100644
index d2a843f..0000000
--- a/vendor/plugins/globalize2/lib/globalize/active_record.rb
+++ /dev/null
@@ -1,194 +0,0 @@
1module Globalize
2 class MigrationError < StandardError; end
3 class MigrationMissingTranslatedField < MigrationError; end
4 class BadMigrationFieldType < MigrationError; end
5
6 module ActiveRecord
7 autoload :Adapter, 'globalize/active_record/adapter'
8 autoload :Attributes, 'globalize/active_record/attributes'
9 autoload :Migration, 'globalize/active_record/migration'
10
11 def self.included(base)
12 base.extend ActMacro
13 end
14
15 class << self
16 def build_translation_class(target, options)
17 options[:table_name] ||= "#{target.table_name.singularize}_translations"
18
19 klass = target.const_defined?(:Translation) ?
20 target.const_get(:Translation) :
21 target.const_set(:Translation, Class.new(::ActiveRecord::Base))
22
23 klass.class_eval do
24 set_table_name(options[:table_name])
25 belongs_to target.name.underscore.gsub('/', '_')
26 def locale; read_attribute(:locale).to_sym; end
27 def locale=(locale); write_attribute(:locale, locale.to_s); end
28 end
29
30 klass
31 end
32 end
33
34 module ActMacro
35 def locale
36 (defined?(@@locale) && @@locale)
37 end
38
39 def locale=(locale)
40 @@locale = locale
41 end
42
43 def translates(*attr_names)
44 return if translates?
45 options = attr_names.extract_options!
46
47 class_inheritable_accessor :translation_class, :translated_attribute_names
48 self.translation_class = ActiveRecord.build_translation_class(self, options)
49 self.translated_attribute_names = attr_names.map(&:to_sym)
50
51 include InstanceMethods
52 extend ClassMethods, Migration
53
54 after_save :save_translations!
55 has_many :translations, :class_name => translation_class.name,
56 :foreign_key => class_name.foreign_key,
57 :dependent => :delete_all,
58 :extend => HasManyExtensions
59
60 named_scope :with_translations, lambda { |locale|
61 conditions = required_attributes.map do |attribute|
62 "#{quoted_translation_table_name}.#{attribute} IS NOT NULL"
63 end
64 conditions << "#{quoted_translation_table_name}.locale = ?"
65 { :include => :translations, :conditions => [conditions.join(' AND '), locale] }
66 }
67
68 attr_names.each { |attr_name| translated_attr_accessor(attr_name) }
69 end
70
71 def translates?
72 included_modules.include?(InstanceMethods)
73 end
74 end
75
76 module HasManyExtensions
77 def by_locale(locale)
78 first(:conditions => { :locale => locale.to_s })
79 end
80
81 def by_locales(locales)
82 all(:conditions => { :locale => locales.map(&:to_s) })
83 end
84 end
85
86 module ClassMethods
87 delegate :set_translation_table_name, :to => :translation_class
88
89 def with_locale(locale)
90 previous_locale, self.locale = self.locale, locale
91 result = yield
92 self.locale = previous_locale
93 result
94 end
95
96 def translation_table_name
97 translation_class.table_name
98 end
99
100 def quoted_translation_table_name
101 translation_class.quoted_table_name
102 end
103
104 def required_attributes
105 validations = reflect_on_all_validations.select do |validation|
106 validation.macro == :validates_presence_of
107 end.map(&:name)
108 end
109
110 def respond_to?(method, *args, &block)
111 method.to_s =~ /^find_by_(\w+)$/ && translated_attribute_names.include?($1.to_sym) || super
112 end
113
114 def method_missing(method, *args)
115 if method.to_s =~ /^find_by_(\w+)$/ && translated_attribute_names.include?($1.to_sym)
116 find_first_by_translated_attr_and_locales($1, args.first)
117 else
118 super
119 end
120 end
121
122 protected
123
124 def find_first_by_translated_attr_and_locales(name, value)
125 query = "#{translated_attr_name(name)} = ? AND #{translated_attr_name('locale')} IN (?)"
126 locales = Globalize.fallbacks(locale || I18n.locale).map(&:to_s)
127 find(:first, :joins => :translations, :conditions => [query, value, locales])
128 end
129
130 def translated_attr_accessor(name)
131 define_method "#{name}=", lambda { |value|
132 globalize.write(self.class.locale || I18n.locale, name, value)
133 self[name] = value
134 }
135 define_method name, lambda { |*args|
136 globalize.fetch(args.first || self.class.locale || I18n.locale, name)
137 }
138 alias_method "#{name}_before_type_cast", name
139 end
140
141 def translated_attr_name(name)
142 "#{translation_class.table_name}.#{name}"
143 end
144 end
145
146 module InstanceMethods
147 def globalize
148 @globalize ||= Adapter.new self
149 end
150
151 def attributes=(attributes, *args)
152 if attributes.respond_to?(:delete) && locale = attributes.delete(:locale)
153 self.class.with_locale(locale) { super }
154 else
155 super
156 end
157 end
158
159 def available_locales
160 translations.scoped(:select => 'DISTINCT locale').map(&:locale)
161 end
162
163 def translated_locales
164 translations.map(&:locale)
165 end
166
167 def translated_attributes
168 translated_attribute_names.inject({}) do |attributes, name|
169 attributes.merge(name => send(name))
170 end
171 end
172
173 def set_translations(options)
174 options.keys.each do |locale|
175 translation = translations.find_by_locale(locale.to_s) ||
176 translations.build(:locale => locale.to_s)
177 translation.update_attributes!(options[locale])
178 end
179 end
180
181 def reload(options = nil)
182 translated_attribute_names.each { |name| @attributes.delete(name.to_s) }
183 globalize.reset
184 super(options)
185 end
186
187 protected
188
189 def save_translations!
190 globalize.save_translations!
191 end
192 end
193 end
194end
diff --git a/vendor/plugins/globalize2/lib/globalize/active_record/adapter.rb b/vendor/plugins/globalize2/lib/globalize/active_record/adapter.rb
deleted file mode 100644
index 12f89ec..0000000
--- a/vendor/plugins/globalize2/lib/globalize/active_record/adapter.rb
+++ /dev/null
@@ -1,80 +0,0 @@
1module Globalize
2 module ActiveRecord
3 class Adapter
4 # The cache caches attributes that already were looked up for read access.
5 # The stash keeps track of new or changed values that need to be saved.
6 attr_reader :record, :cache, :stash
7
8 def initialize(record)
9 @record = record
10 @cache = Attributes.new
11 @stash = Attributes.new
12 end
13
14 def fetch(locale, attr_name)
15 cache.contains?(locale, attr_name) ?
16 cache.read(locale, attr_name) :
17 cache.write(locale, attr_name, fetch_attribute(locale, attr_name))
18 end
19
20 def write(locale, attr_name, value)
21 stash.write(locale, attr_name, value)
22 cache.write(locale, attr_name, value)
23 end
24
25 def save_translations!
26 stash.each do |locale, attrs|
27 translation = record.translations.find_or_initialize_by_locale(locale.to_s)
28 attrs.each { |attr_name, value| translation[attr_name] = value }
29 translation.save!
30 end
31 stash.clear
32 end
33
34 def reset
35 cache.clear
36 # stash.clear
37 end
38
39 protected
40
41 def fetch_translation(locale)
42 locale = locale.to_sym
43 record.translations.loaded? ? record.translations.detect { |t| t.locale == locale } :
44 record.translations.by_locale(locale)
45 end
46
47 def fetch_translations(locale)
48 # only query if not already included with :include => translations
49 record.translations.loaded? ? record.translations :
50 record.translations.by_locales(Globalize.fallbacks(locale))
51 end
52
53 def fetch_attribute(locale, attr_name)
54 translations = fetch_translations(locale)
55 value, requested_locale = nil, locale
56
57 Globalize.fallbacks(locale).each do |fallback|
58 translation = translations.detect { |t| t.locale == fallback }
59 value = translation && translation.send(attr_name)
60 locale = fallback && break if value
61 end
62
63 set_metadata(value, :locale => locale, :requested_locale => requested_locale)
64 value
65 end
66
67 def set_metadata(object, metadata)
68 if object.respond_to?(:translation_metadata)
69 object.translation_metadata.merge!(meta_data)
70 end
71 end
72
73 def translation_metadata_accessor(object)
74 return if obj.respond_to?(:translation_metadata)
75 class << object; attr_accessor :translation_metadata end
76 object.translation_metadata ||= {}
77 end
78 end
79 end
80end
diff --git a/vendor/plugins/globalize2/lib/globalize/active_record/attributes.rb b/vendor/plugins/globalize2/lib/globalize/active_record/attributes.rb
deleted file mode 100644
index 7bd923c..0000000
--- a/vendor/plugins/globalize2/lib/globalize/active_record/attributes.rb
+++ /dev/null
@@ -1,25 +0,0 @@
1# Helper class for storing values per locale. Used by Globalize::Adapter
2# to stash and cache attribute values.
3module Globalize
4 module ActiveRecord
5 class Attributes < Hash
6 def [](locale)
7 locale = locale.to_sym
8 self[locale] = {} unless has_key?(locale)
9 self.fetch(locale)
10 end
11
12 def contains?(locale, attr_name)
13 self[locale].has_key?(attr_name)
14 end
15
16 def read(locale, attr_name)
17 self[locale][attr_name]
18 end
19
20 def write(locale, attr_name, value)
21 self[locale][attr_name] = value
22 end
23 end
24 end
25end
diff --git a/vendor/plugins/globalize2/lib/globalize/active_record/migration.rb b/vendor/plugins/globalize2/lib/globalize/active_record/migration.rb
deleted file mode 100644
index ebff3b6..0000000
--- a/vendor/plugins/globalize2/lib/globalize/active_record/migration.rb
+++ /dev/null
@@ -1,40 +0,0 @@
1module Globalize
2 module ActiveRecord
3 module Migration
4 def create_translation_table!(fields)
5 translated_attribute_names.each do |f|
6 raise MigrationMissingTranslatedField, "Missing translated field #{f}" unless fields[f]
7 end
8
9 fields.each do |name, type|
10 if translated_attribute_names.include?(name) && ![:string, :text].include?(type)
11 raise BadMigrationFieldType, "Bad field type for #{name}, should be :string or :text"
12 end
13 end
14
15 self.connection.create_table(translation_table_name) do |t|
16 t.references self.table_name.singularize
17 t.string :locale
18 fields.each do |name, type|
19 t.column name, type
20 end
21 t.timestamps
22 end
23
24 self.connection.add_index(translation_table_name, "#{self.table_name.singularize}_id", :name => translation_index_name)
25 end
26
27 def translation_index_name
28 require 'digest/sha1'
29 # FIXME what's the max size of an index name?
30 index_name = "index_#{translation_table_name}_on_#{self.table_name.singularize}_id"
31 index_name.size < 50 ? index_name : "index_#{Digest::SHA1.hexdigest(index_name)}"
32 end
33
34 def drop_translation_table!
35 self.connection.remove_index(translation_table_name, :name => translation_index_name) rescue nil
36 self.connection.drop_table(translation_table_name)
37 end
38 end
39 end
40end
diff --git a/vendor/plugins/globalize2/lib/i18n/missing_translations_log_handler.rb b/vendor/plugins/globalize2/lib/i18n/missing_translations_log_handler.rb
deleted file mode 100644
index 24c1089..0000000
--- a/vendor/plugins/globalize2/lib/i18n/missing_translations_log_handler.rb
+++ /dev/null
@@ -1,41 +0,0 @@
1# A simple exception handler that behaves like the default exception handler
2# but additionally logs missing translations to a given log.
3#
4# Useful for identifying missing translations during testing.
5#
6# E.g.
7#
8# require 'globalize/i18n/missing_translations_log_handler'
9# I18n.missing_translations_logger = RAILS_DEFAULT_LOGGER
10# I18n.exception_handler = :missing_translations_log_handler
11#
12# To set up a different log file:
13#
14# logger = Logger.new("#{RAILS_ROOT}/log/missing_translations.log")
15# I18n.missing_translations_logger = logger
16
17module I18n
18 @@missing_translations_logger = nil
19
20 class << self
21 def missing_translations_logger
22 @@missing_translations_logger ||= begin
23 require 'logger' unless defined?(Logger)
24 Logger.new(STDOUT)
25 end
26 end
27
28 def missing_translations_logger=(logger)
29 @@missing_translations_logger = logger
30 end
31
32 def missing_translations_log_handler(exception, locale, key, options)
33 if MissingTranslationData === exception
34 missing_translations_logger.warn(exception.message)
35 return exception.message
36 else
37 raise exception
38 end
39 end
40 end
41end
diff --git a/vendor/plugins/globalize2/lib/i18n/missing_translations_raise_handler.rb b/vendor/plugins/globalize2/lib/i18n/missing_translations_raise_handler.rb
deleted file mode 100644
index 18237b1..0000000
--- a/vendor/plugins/globalize2/lib/i18n/missing_translations_raise_handler.rb
+++ /dev/null
@@ -1,25 +0,0 @@
1# A simple exception handler that behaves like the default exception handler
2# but also raises on missing translations.
3#
4# Useful for identifying missing translations during testing.
5#
6# E.g.
7#
8# require 'globalize/i18n/missing_translations_raise_handler'
9# I18n.exception_handler = :missing_translations_raise_handler
10module I18n
11 class << self
12 def missing_translations_raise_handler(exception, locale, key, options)
13 raise exception
14 end
15 end
16end
17
18I18n.exception_handler = :missing_translations_raise_handler
19
20ActionView::Helpers::TranslationHelper.module_eval do
21 def translate(key, options = {})
22 I18n.translate(key, options)
23 end
24 alias :t :translate
25end