active_delegate 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8b25f154bc3bf1b847b20e9b4b410523881d47f1
4
- data.tar.gz: 99f6ddd155f8d6e52fc72d0de2fab400604366a0
3
+ metadata.gz: 1b9615c5b90ac00644880da87395a7540564480f
4
+ data.tar.gz: eb3b41a9fb3433b2a607e05b60561eedd89dc46c
5
5
  SHA512:
6
- metadata.gz: a37d3dc37ab0f5a6bc55ba414179758c470e73cd8dbf95df474e8783f7b262abd2d59b69cb961582de5aad7dd997b84ac46b62549e5f96d76c35dba65959490f
7
- data.tar.gz: f8fe8f1bcec56fda65430bc519d05d5092c66b46dfa0ecaea64a70f2c17ad7ba66825fee08591e89c4b6c309344a7fbc4f9817fc227e5cbae8ed8e8fa5b2dfdb
6
+ metadata.gz: 963a209b92c4c07ef31784e7d3fdd47420e40c3982309f477034cf9fc714acb74c84ae8b45dc2376bc6c018c15100e627107e6152d85d44e9f21ae04486cc025
7
+ data.tar.gz: 2df53eb000aa943ac0683e661b7fc38e1e40773f67db68ec1a61cee2913691b797ba331a901c46547a21d512845649861db124a8ab22379e73cc4f5dbfd598f3
@@ -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
- dl_method = :"#{@options[:to]}_attribute_names"
115
+ dl_atable = association_reflection.klass.table_name
116
+ dl_method = :"#{dl_atable}_attribute_names"
117
+
123
118
  delegated = prefix_attributes(delegatable_attributes)
124
- delegated = @model.try(dl_method).to_a.concat(delegated)
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 = :"#{@options[:to]}_localized_attribute_names"
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
- attributes.each do |attrib|
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
- cast_type = association_class.attribute_types["#{attr_name}"]
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
- @model.attribute(attrib, cast_type) unless cast_type.nil?
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
@@ -1,3 +1,3 @@
1
1
  module ActiveDelegate
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  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
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-10-20 00:00:00.000000000 Z
11
+ date: 2017-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord