roda-component 0.1.20 → 0.1.21

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c498627e08894de678599a4c6f1e5ee3c2c51a23
4
- data.tar.gz: ba8977a923ef51aa5378fcd14760c5811d4f0537
3
+ metadata.gz: a16bb2b329a369806cff4a67ac5988eac549382c
4
+ data.tar.gz: 4cf66e8d5cd2579d3949cb2629dc10eeef204dbd
5
5
  SHA512:
6
- metadata.gz: 7cd6097f479d63615a6b6b2c393dc5191f166e7b6da8e6e84ad00a732622f4c586326152f1980b9ed78420c0b3596a9c35304cfb8d25707325a07caf8fe7e021
7
- data.tar.gz: b653b8d9c78622090a362e23b9a53429de4434157103ebc251753971301a3e7097ca7e86ae1dc926d8d159d982b34adc3eb96288720e45ff37b8ec3f74b8bb86
6
+ metadata.gz: dd5d13d7a64980b0b8e894b2eb34674a349cd492e44efd2d3bdf7816ff7720e9f0acbcda0738a06f17af2467c958d3cc631fcd30bb588c6c19724a8d9f7732cc
7
+ data.tar.gz: 99af32ec110135bd65a7970600d62811490201879099baea649a6b2d40461c502a453b70d9d47b85a1b522c893d1497acf3d5bed3d6ad1aee0c7bd9a5dbaa094
@@ -68,7 +68,6 @@ class Roda
68
68
  evt.prevent_default
69
69
 
70
70
  params = {}
71
- el.find('.field-error').remove
72
71
 
73
72
  # loop through all the forum values
74
73
  el.serialize_array.each do |row|
@@ -97,6 +96,8 @@ class Roda
97
96
  form = form_klass.new params_obj, opts
98
97
  end
99
98
 
99
+ el.find(opts[:error_selector] || '.field-error').remove
100
+
100
101
  Component::Instance.new(component(comp), scope).instance_exec form, evt.current_target, evt, &block
101
102
  end
102
103
  else
@@ -10,8 +10,10 @@ class Roda
10
10
  @_attributes = []
11
11
 
12
12
  atts.each do |key, val|
13
- send(:"#{key}=", val)
14
- @_attributes << key
13
+ if respond_to?("#{key}=")
14
+ send(:"#{key}=", val)
15
+ @_attributes << key
16
+ end
15
17
  end
16
18
  end
17
19
 
@@ -67,17 +69,38 @@ class Roda
67
69
  # post = Post.new(edit.attributes)
68
70
  # post.save
69
71
  def initialize(atts, options = {})
72
+ @_data = atts
73
+ @_data = atts.to_obj if atts.is_a? Hash
70
74
  @_options = options
71
75
 
72
76
  # @_attributes = Class.new(Attributes).new
73
77
  @_attributes = Attributes.new
74
78
  @_attributes.set_attr_accessors _attr_accessors
75
- @_attributes.set_values atts
79
+ @_attributes.set_values _data
80
+
81
+ _form.each do |key, klass|
82
+ opts = {}
83
+ opts[key] = _data.send(key) if _data.respond_to?(key)
84
+ @_attributes.set_values opts
85
+ end
76
86
  end
77
87
 
78
88
  def self.attr_accessor(*vars)
79
89
  @_attr_accessors ||= []
80
- @_attr_accessors.concat vars
90
+ @_form ||= {}
91
+
92
+ vars.each do |v|
93
+ if !v.is_a? Hash
94
+ @_attr_accessors << v unless @_attr_accessors.include? v
95
+ else
96
+ v = v.first
97
+
98
+ unless @_attr_accessors.include? v.first
99
+ @_attr_accessors << v.first
100
+ @_form[v.first] = v.last
101
+ end
102
+ end
103
+ end
81
104
  end
82
105
 
83
106
  def method_missing method, *args, &block
@@ -94,7 +117,7 @@ class Roda
94
117
  Hash.new.tap do |atts|
95
118
  _attributes.instance_variables.each do |ivar|
96
119
  # todo: figure out why it's setting @constructor and @toString
97
- next if ivar == :@constructor || ivar == :@toString || ivar == :@_attributes
120
+ next if ivar == :@constructor || ivar == :@toString || ivar == :@_attributes || ivar == :@_data || ivar == :@_forms
98
121
 
99
122
  att = ivar[1..-1].to_sym
100
123
  atts[att] = _attributes.send(att)
@@ -155,25 +178,35 @@ class Roda
155
178
  def render_values dom = false, key = false, data = false
156
179
  dom = _options[:dom] unless dom
157
180
  key = _options[:key] if !key && _options.key?(:key)
158
- data = attributes unless data
159
181
 
160
- data.each do |k, v|
161
- k = "#{key}[#{k}]" if !key.nil?
162
- if v.is_a?(Hash)
163
- render_values dom, k, v
164
- else
165
- dom.find('input, select') do |element|
166
- if element['name'] == k
167
- case element.name
168
- when 'select'
169
- element.find('option').each do |x|
170
- x['selected'] = true if x['value']==v.to_s
171
- end
172
- when 'input'
173
- element['value'] = v.to_s
174
- end
182
+ dom.find('input, select') do |element|
183
+ name = element['name']
184
+ name = name.gsub(/\A#{key}/, '') if key
185
+ keys = name.gsub(/\A\[/, '').gsub(/[^a-z0-9_]/, '|').gsub(/\|\|/, '|').gsub(/\|$/, '').split('|')
186
+ value = false
187
+
188
+ keys.each do |k|
189
+ begin
190
+ value = value ? value.send(k) : send(k)
191
+
192
+ if klass = _form[k.to_s.to_sym]
193
+ options = {}
194
+ options[:key] = _options[:key] if _options.key? :key
195
+
196
+ value = klass.new(value, options)
175
197
  end
198
+ rescue
199
+ value = ''
200
+ end
201
+ end
202
+
203
+ case element.name
204
+ when 'select'
205
+ element.find('option') do |x|
206
+ x['selected'] = true if x['value'] == value.to_s
176
207
  end
208
+ when 'input'
209
+ element['value'] = value.to_s
177
210
  end
178
211
  end
179
212
  end
@@ -184,10 +217,22 @@ class Roda
184
217
  @_attr_accessors
185
218
  end
186
219
 
220
+ def self._form
221
+ @_form
222
+ end
223
+
187
224
  def _attributes
188
225
  @_attributes ||= []
189
226
  end
190
227
 
228
+ def _form
229
+ self.class._form
230
+ end
231
+
232
+ def _data
233
+ @_data ||= []
234
+ end
235
+
191
236
  def _attr_accessors
192
237
  self.class._attr_accessors
193
238
  end
@@ -107,19 +107,16 @@ class Roda
107
107
  # @param [Symbol] att The attribute you wish to verify the presence of.
108
108
  # @param [Array<Symbol, Symbol>] error The error that should be returned
109
109
  # when the validation fails.
110
- def assert_present(att, error = [att, :not_present], klass = false)
111
- if error.is_a? Class
112
- error_new = klass.dup
113
- klass = error
114
- error = error_new || [att, :not_present]
115
-
110
+ def assert_present(att, error = [att, :not_present])
111
+ if klass = _form[att]
116
112
  options = {}
117
113
  options[:key] = _options[:key] if _options.key? :key
118
114
 
119
- f = klass.new(_attributes.send(att), options)
115
+ f = klass.new(send(att), options)
120
116
  assert(f.valid?, [att, f.errors])
121
117
  else
122
- assert(!_attributes.send(att).to_s.empty?, error)
118
+ binding.pry if att.to_s == 'line1'
119
+ assert(!send(att).to_s.empty?, error)
123
120
  end
124
121
  end
125
122
 
@@ -1,5 +1,5 @@
1
1
  class Roda
2
2
  class Component
3
- VERSION = "0.1.20"
3
+ VERSION = "0.1.21"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: roda-component
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.20
4
+ version: 0.1.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - cj
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-29 00:00:00.000000000 Z
11
+ date: 2015-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: opal