Module: SingleTableGlobalize3::ActiveRecord::ClassMethods
- Includes:
- WithTranslations
- Defined in:
- lib/single_table_globalize3/active_record/class_methods.rb
Instance Method Summary
collapse
#required_attributes, #required_translated_attributes, #translated?, #translated_column_name, #translation_class, #translations_table_name, #with_translated_attribute, #with_translations
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_id, *arguments, &block) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/single_table_globalize3/active_record/class_methods.rb', line 40
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
if defined?(::ActiveRecord::DynamicFinderMatch) && match.is_a?(::ActiveRecord::DynamicFinderMatch)
if match.instantiator? and scope.blank?
return scope.find_or_instantiator_by_attributes match, attribute_names, *arguments, &block
end
match_finder_method = match.finder.to_s
match_finder_method << "!" if match.bang? && ::ActiveRecord::VERSION::STRING >= "3.1.0"
return scope.send(match_finder_method).tap do |found|
found.is_a?(Array) ? found.map { |f| f.translations.reload } : found.translations.reload unless found.nil?
end
end
return scope
end
|
Instance Method Details
#attribute_names ⇒ Object
17
18
19
20
21
22
23
|
# File 'lib/single_table_globalize3/active_record/class_methods.rb', line 17
def attribute_names
@attribute_names ||= if !abstract_class? && table_exists?
column_names
else
[]
end
end
|
#find_or_instantiator_by_attributes(match, attributes, *args) {|record| ... } ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/single_table_globalize3/active_record/class_methods.rb', line 69
def find_or_instantiator_by_attributes(match, attributes, *args)
options = args.size > 1 && args.last(2).all?{ |a| a.is_a?(Hash) } ? args. : {}
protected_attributes_for_create, unprotected_attributes_for_create = {}, {}
args.each_with_index do |arg, i|
if arg.is_a?(Hash)
protected_attributes_for_create = args[i].with_indifferent_access
else
unprotected_attributes_for_create[attributes[i]] = args[i]
end
end
record = if ::ActiveRecord::VERSION::STRING < "3.1.0"
new do |r|
r.send(:attributes=, protected_attributes_for_create, true) unless protected_attributes_for_create.empty?
r.send(:attributes=, unprotected_attributes_for_create, false) unless unprotected_attributes_for_create.empty?
end
else
new(protected_attributes_for_create, options) do |r|
r.assign_attributes(unprotected_attributes_for_create, :without_protection => true)
end
end
yield(record) if block_given?
record.send(match.bang? ? :save! : :save) if match.instantiator.eql?(:create)
record
end
|
#respond_to?(method_id, *args, &block) ⇒ Boolean
7
8
9
|
# File 'lib/single_table_globalize3/active_record/class_methods.rb', line 7
def respond_to?(method_id, *args, &block)
supported_on_missing?(method_id) || super
end
|
#respond_to_missing?(method_id, include_private = false) ⇒ Boolean
11
12
13
|
# File 'lib/single_table_globalize3/active_record/class_methods.rb', line 11
def respond_to_missing?(method_id, include_private = false)
supported_on_missing?(method_id) || super
end
|
#supported_on_missing?(method_id) ⇒ Boolean
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/single_table_globalize3/active_record/class_methods.rb', line 26
def supported_on_missing?(method_id)
return super unless RUBY_VERSION < '1.9' || respond_to?(:translated_attribute_names)
match = defined?(::ActiveRecord::DynamicFinderMatch) && (::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
|