Class: ActiveDelegate::Attributes
Overview
Delegates attributes to an associated model
Instance Attribute Summary
Attributes inherited from Delegator
#model, #options
Instance Method Summary
collapse
Methods inherited from Delegator
#association_class, #association_name, #association_reflection, #delegation_args, #delegation_options, #delegation_prefix, #initialize
Instance Method Details
#association_table ⇒ Object
31
32
33
|
# File 'lib/active_delegate/attributes.rb', line 31
def association_table
association_class.table_name
end
|
#attribute_options ⇒ Object
26
27
28
29
|
# File 'lib/active_delegate/attributes.rb', line 26
def attribute_options
keys = i[cast_type default define alias writer dirty localized finder scope]
options.select { |k, _v| k.in? keys }.merge(prefix: delegation_prefix)
end
|
#call ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/active_delegate/attributes.rb', line 64
def call
redefine_build_association(association_name)
delegatable_attributes.each do |attribute|
delegate_methods(attribute.delegatable_methods)
define_model_class_methods(attribute)
define_attribute_methods(attribute)
define_attribute_queries(attribute)
end
end
|
#default_options ⇒ Object
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/active_delegate/attributes.rb', line 10
def default_options
{
except: [],
only: [],
writer: true,
dirty: true,
localized: false,
define: true,
finder: false,
scope: false,
to: nil,
prefix: nil,
allow_nil: false
}
end
|
#delegatable_attributes ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
|
# File 'lib/active_delegate/attributes.rb', line 51
def delegatable_attributes
attributes = delegation_args(association_class.attribute_names)
attributes -= excluded_attributes
attributes.map! do |attribute_name|
ActiveDelegate::Attribute::Object.new(
attribute_name, association_class, attribute_options
)
end
attributes.reject { |a| model.has_attribute?(a.prefixed) }
end
|
#excluded_attributes ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/active_delegate/attributes.rb', line 35
def excluded_attributes
excluded = i[created_at updated_at]
excluded << association_reflection.active_record_primary_key.to_sym
for_key = association_reflection.foreign_key
excluded << for_key.to_sym if for_key.present?
sti_col = association_class.inheritance_column
excluded << sti_col.to_sym if sti_col.present?
assoc_as = association_reflection.options[:as]
excluded += [:"#{assoc_as}_type", :"#{assoc_as}_id"] if assoc_as.present?
excluded
end
|