Module: Globalize::ActiveRecord::ClassMethods
- Defined in:
- lib/globalize/active_record/class_methods.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *arguments, &block) ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
# File 'lib/globalize/active_record/class_methods.rb', line 89
def method_missing(method_id, *arguments, &block)
match, attribute_names, translated_attributes, untranslated_attributes = supported_on_missing?(method_id)
return super unless match
scope = scoped
translated_attributes.each do |attr|
scope = scope.with_translated_attribute(attr, arguments[attribute_names.index(attr)])
end
untranslated_attributes.each do |unt|
index = attribute_names.index(unt)
raise StandarError unless index
scope = scope.send(:"scoped_by_#{unt}", arguments[index])
end
return scope.send(match.finder) if match.is_a?(::ActiveRecord::DynamicFinderMatch)
return scope
end
|
Instance Method Details
#required_attributes ⇒ Object
37
38
39
|
# File 'lib/globalize/active_record/class_methods.rb', line 37
def required_attributes
validators.map { |v| v.attributes if v.is_a?(ActiveModel::Validations::PresenceValidator) }.flatten
end
|
#required_translated_attributes ⇒ Object
41
42
43
|
# File 'lib/globalize/active_record/class_methods.rb', line 41
def required_translated_attributes
translated_attribute_names & required_attributes
end
|
#respond_to?(method_id, *args, &block) ⇒ Boolean
66
67
68
|
# File 'lib/globalize/active_record/class_methods.rb', line 66
def respond_to?(method_id, *args, &block)
supported_on_missing?(method_id) || super
end
|
#respond_to_missing?(method_id, include_private = false) ⇒ Boolean
70
71
72
|
# File 'lib/globalize/active_record/class_methods.rb', line 70
def respond_to_missing?(method_id, include_private = false)
supported_on_missing?(method_id) || super
end
|
#supported_on_missing?(method_id) ⇒ Boolean
75
76
77
78
79
80
81
82
83
84
85
86
87
|
# File 'lib/globalize/active_record/class_methods.rb', line 75
def supported_on_missing?(method_id)
return super unless RUBY_VERSION < '1.9' || respond_to?(:translated_attribute_names)
match = ::ActiveRecord::DynamicFinderMatch.match(method_id) || ::ActiveRecord::DynamicScopeMatch.match(method_id)
return false if match.nil?
attribute_names = match.attribute_names.map(&:to_sym)
translated_attributes = attribute_names & translated_attribute_names
return false if translated_attributes.empty?
untranslated_attributes = attribute_names - translated_attributes
return false if untranslated_attributes.any?{|unt| ! respond_to?(:"scoped_by_#{unt}")}
return [match, attribute_names, translated_attributes, untranslated_attributes]
end
|
#translated?(name) ⇒ Boolean
33
34
35
|
# File 'lib/globalize/active_record/class_methods.rb', line 33
def translated?(name)
translated_attribute_names.include?(name.to_sym)
end
|
#translated_column_name(name) ⇒ Object
61
62
63
|
# File 'lib/globalize/active_record/class_methods.rb', line 61
def translated_column_name(name)
"#{translation_class.table_name}.#{name}"
end
|
#translation_class ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/globalize/active_record/class_methods.rb', line 45
def translation_class
@translation_class ||= begin
klass = self.const_get(:Translation) rescue nil
if klass.nil? || klass.class_name != (self.class_name + "Translation")
klass = self.const_set(:Translation, Class.new(Globalize::ActiveRecord::Translation))
end
klass.belongs_to name.underscore.gsub('/', '_')
klass
end
end
|
#translations_table_name ⇒ Object
57
58
59
|
# File 'lib/globalize/active_record/class_methods.rb', line 57
def translations_table_name
translation_class.table_name
end
|
#with_locale(locale) ⇒ Object
6
7
8
|
# File 'lib/globalize/active_record/class_methods.rb', line 6
def with_locale(locale)
scoped.where(:"is_locale_#{locale}" => true)
end
|
#with_locales(*locales) ⇒ Object
10
11
12
|
# File 'lib/globalize/active_record/class_methods.rb', line 10
def with_locales(*locales)
scoped.merge(translation_class.with_locales(*locales))
end
|
#with_required_attributes ⇒ Object
19
20
21
22
23
|
# File 'lib/globalize/active_record/class_methods.rb', line 19
def with_required_attributes
required_translated_attributes.inject(scoped) do |scope, name|
scope.where("#{translated_column_name(name)} IS NOT NULL")
end
end
|
#with_translated_attribute(name, value, locales = nil) ⇒ Object
25
26
27
28
29
30
31
|
# File 'lib/globalize/active_record/class_methods.rb', line 25
def with_translated_attribute(name, value, locales = nil)
locales ||= Globalize.fallbacks
with_translations.where(
translated_column_name(name) => value,
translated_column_name(:locale) => Array(locales).map(&:to_s)
)
end
|
#with_translations(*locales) ⇒ Object
14
15
16
17
|
# File 'lib/globalize/active_record/class_methods.rb', line 14
def with_translations(*locales)
locales = translated_locales if locales.empty?
includes(:translations).with_locales(locales).with_required_attributes
end
|