active_delegate 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/active_delegate.rb +9 -1
- data/lib/active_delegate/attributes.rb +18 -14
- data/lib/active_delegate/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1b9615c5b90ac00644880da87395a7540564480f
|
4
|
+
data.tar.gz: eb3b41a9fb3433b2a607e05b60561eedd89dc46c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 963a209b92c4c07ef31784e7d3fdd47420e40c3982309f477034cf9fc714acb74c84ae8b45dc2376bc6c018c15100e627107e6152d85d44e9f21ae04486cc025
|
7
|
+
data.tar.gz: 2df53eb000aa943ac0683e661b7fc38e1e40773f67db68ec1a61cee2913691b797ba331a901c46547a21d512845649861db124a8ab22379e73cc4f5dbfd598f3
|
data/lib/active_delegate.rb
CHANGED
@@ -17,7 +17,15 @@ module ActiveDelegate
|
|
17
17
|
|
18
18
|
# Delegate attributes
|
19
19
|
def delegate_attributes(*args)
|
20
|
-
options = args.extract_options
|
20
|
+
options = args.extract_options!.except(:single, :cast_type, :alias)
|
21
|
+
Attributes.new(self, options)
|
22
|
+
end
|
23
|
+
|
24
|
+
# Delegate attribute
|
25
|
+
def delegate_attribute(attribute, cast_type, options={})
|
26
|
+
options = options.except(:only, :except)
|
27
|
+
options = options.merge(only: [attribute], single: true, cast_type: cast_type)
|
28
|
+
|
21
29
|
Attributes.new(self, options)
|
22
30
|
end
|
23
31
|
end
|
@@ -48,18 +48,11 @@ module ActiveDelegate
|
|
48
48
|
[:id, :created_at, :updated_at] + poly_attr.to_a
|
49
49
|
end
|
50
50
|
|
51
|
-
# Get method attributes
|
52
|
-
def association_valid_methods(attributes)
|
53
|
-
assoc_methods = association_class.public_instance_methods
|
54
|
-
attributes.to_a.select { |a| a.in? assoc_methods }
|
55
|
-
end
|
56
|
-
|
57
51
|
# Get delegatable attributes
|
58
52
|
def delegatable_attributes
|
59
53
|
attributes = association_attribute_names.map(&:to_sym)
|
60
54
|
attributes = attributes & @options[:only].to_a if @options[:only].present?
|
61
55
|
attributes = attributes - @options[:except].to_a if @options[:except].present?
|
62
|
-
attributes = attributes + association_valid_methods(@options[:only])
|
63
56
|
attributes = attributes - default_excluded_attributes
|
64
57
|
|
65
58
|
attributes.map(&:to_sym)
|
@@ -119,16 +112,18 @@ module ActiveDelegate
|
|
119
112
|
|
120
113
|
# Save delagated attributes in model class
|
121
114
|
def save_delegated_attributes
|
122
|
-
|
115
|
+
dl_atable = association_reflection.klass.table_name
|
116
|
+
dl_method = :"#{dl_atable}_attribute_names"
|
117
|
+
|
123
118
|
delegated = prefix_attributes(delegatable_attributes)
|
124
|
-
|
119
|
+
define_attribute_names_and_types(delegated)
|
125
120
|
|
121
|
+
delegated = @model.try(dl_method).to_a.concat(delegated)
|
126
122
|
@model.send(:define_singleton_method, dl_method) { delegated }
|
127
|
-
define_attribute_names_and_types(delegated)
|
128
123
|
|
129
124
|
if @options[:localized].present?
|
130
125
|
localized = prefix_attributes(localized_attributes)
|
131
|
-
lc_method = :"#{
|
126
|
+
lc_method = :"#{dl_atable}_localized_attribute_names"
|
132
127
|
|
133
128
|
@model.send(:define_singleton_method, lc_method) { localized }
|
134
129
|
end
|
@@ -136,11 +131,20 @@ module ActiveDelegate
|
|
136
131
|
|
137
132
|
# Define attribute names and types
|
138
133
|
def define_attribute_names_and_types(attributes)
|
139
|
-
|
134
|
+
existing = association_attribute_names.map(&:to_sym)
|
135
|
+
undefined = attributes.reject { |a| a.in? existing }
|
136
|
+
|
137
|
+
undefined.each do |attrib|
|
140
138
|
attr_name = attrib.to_s.sub("#{attribute_prefix}_", '')
|
141
|
-
|
139
|
+
attr_deft = @options[:default] || association_class.column_defaults["#{attr_name}"]
|
140
|
+
cast_type = @options[:cast_type] || association_class.attribute_types["#{attr_name}"]
|
141
|
+
|
142
|
+
@model.attribute(attrib, cast_type, default: attr_deft)
|
142
143
|
|
143
|
-
@
|
144
|
+
if @options[:alias].present?
|
145
|
+
@model.attribute(@options[:alias], cast_type, default: attr_deft)
|
146
|
+
@model.alias_attribute(@options[:alias], attrib)
|
147
|
+
end
|
144
148
|
end
|
145
149
|
end
|
146
150
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_delegate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonian Guveli
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-11-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|