roda-component 0.1.7 → 0.1.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/roda/component.rb +1 -1
- data/lib/roda/component/events.rb +39 -7
- data/lib/roda/component/form.rb +46 -2
- data/lib/roda/component/version.rb +1 -1
- data/test/dummy/components/chat.rb +6 -0
- data/test/dummy/components/login.rb +18 -65
- data/test/dummy/config.ru +1 -0
- data/test/dummy/dummy.db +0 -0
- 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: 4d2295718996694bcaf5a8f826f77ee2f488b415
|
4
|
+
data.tar.gz: d4232e47a4af010f6cbec1f8d3231fc8975b9f2d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1404cb9742440c6f27210f56e2904c01543240aad4a675a8c0e6f8b11773fb2b165390886ec2fe46b7f66333cf58ea94955b5d1e548cfc994710be73cbe71d9
|
7
|
+
data.tar.gz: 327320da0c95ea40a015a8516627c730cc6e7fb2e7020d5bb78e556b2d9036cc371c5815272e3dbbddb629e272fe0e30e44a1a7891a895ba12a14f5617cc9337
|
data/lib/roda/component.rb
CHANGED
@@ -40,7 +40,7 @@ class Roda
|
|
40
40
|
$faye.subscribe "/components/#{self.class._name}" do |msg|
|
41
41
|
msg = Native(msg)
|
42
42
|
|
43
|
-
trigger :"#{msg[:type]}", msg unless $faye.public_id == msg[:public_id]
|
43
|
+
trigger :"#{msg[:type]}", msg['local'], msg unless $faye.public_id == msg[:public_id]
|
44
44
|
end
|
45
45
|
|
46
46
|
$faye.on 'transport:up' do
|
@@ -1,12 +1,12 @@
|
|
1
1
|
class Roda
|
2
2
|
class Component
|
3
3
|
class Events < Struct.new(:klass, :component_opts, :scope, :request)
|
4
|
-
def on name, options = {}, &block
|
4
|
+
def on name, options = {}, extra_opts = false, &block
|
5
5
|
if client? && options.is_a?(String)
|
6
6
|
class_name = klass._name
|
7
7
|
class_events = (events[class_name] ||= {})
|
8
8
|
event = (class_events[:_jquery_events] ||= [])
|
9
|
-
event << [block, class_name, options, name]
|
9
|
+
event << [block, class_name, options, extra_opts, name]
|
10
10
|
elsif options.is_a?(Hash)
|
11
11
|
limit_if = options.delete(:if) || []
|
12
12
|
limit_if = [limit_if] unless limit_if.is_a? Array
|
@@ -28,16 +28,48 @@ class Roda
|
|
28
28
|
return unless e = events[klass._name]
|
29
29
|
|
30
30
|
(e[:_jquery_events] || []).each do |event|
|
31
|
-
block, comp, selector, name = event
|
31
|
+
block, comp, selector, opts, name = event
|
32
32
|
|
33
33
|
name = name.to_s
|
34
34
|
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
case name.to_s
|
36
|
+
when 'ready'
|
37
|
+
el = Element.find(selector)
|
38
|
+
|
39
|
+
Component::Instance.new(component(comp), scope).instance_exec el, &block
|
40
|
+
when 'form'
|
41
|
+
klass = opts.delete :class
|
42
|
+
warn 'missing form class option' unless klass
|
43
|
+
|
44
|
+
el = DOM.new Element.find(selector)
|
45
|
+
|
46
|
+
el.on :submit do |evt|
|
47
|
+
evt.prevent_default
|
48
|
+
|
49
|
+
params = {}
|
50
|
+
el.find('.field-error').remove
|
51
|
+
|
52
|
+
# loop through all the forum values
|
53
|
+
el.serialize_array.each do |row|
|
54
|
+
field, _ = row
|
55
|
+
|
56
|
+
# we need to make it native to access it like ruby
|
57
|
+
field = Native(field)
|
58
|
+
name = field['name']
|
59
|
+
value = field['value']
|
60
|
+
|
61
|
+
params[name] = value
|
62
|
+
end
|
63
|
+
|
64
|
+
form = klass.new params, dom: el
|
65
|
+
|
66
|
+
Component::Instance.new(component(comp), scope).instance_exec form, evt.current_target, evt, &block
|
38
67
|
end
|
39
68
|
else
|
40
|
-
|
69
|
+
Document.on name, selector do |evt|
|
70
|
+
el = evt.current_target
|
71
|
+
Component::Instance.new(component(comp), scope).instance_exec el, evt, &block
|
72
|
+
end
|
41
73
|
end
|
42
74
|
end
|
43
75
|
end
|
data/lib/roda/component/form.rb
CHANGED
@@ -34,7 +34,9 @@ class Roda
|
|
34
34
|
# # Now it's safe to initialize the model.
|
35
35
|
# post = Post.new(edit.attributes)
|
36
36
|
# post.save
|
37
|
-
def initialize(atts)
|
37
|
+
def initialize(atts, options = {})
|
38
|
+
@_options = options
|
39
|
+
|
38
40
|
atts.each do |key, val|
|
39
41
|
send(:"#{key}=", val)
|
40
42
|
end
|
@@ -44,7 +46,8 @@ class Roda
|
|
44
46
|
def attributes
|
45
47
|
Hash.new.tap do |atts|
|
46
48
|
instance_variables.each do |ivar|
|
47
|
-
|
49
|
+
# todo: figure out why it's setting @constructor and @toString
|
50
|
+
next if ivar == :@errors || ivar == :@_options || ivar == :@_dom || ivar == :@constructor || ivar == :@toString
|
48
51
|
|
49
52
|
att = ivar[1..-1].to_sym
|
50
53
|
atts[att] = send(att)
|
@@ -59,6 +62,47 @@ class Roda
|
|
59
62
|
end
|
60
63
|
end
|
61
64
|
end
|
65
|
+
|
66
|
+
def display_errors options = {}
|
67
|
+
if extra_errors = options.delete(:errors)
|
68
|
+
extra_errors.each do |key, value|
|
69
|
+
errors[key] = value
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
errors.each do |key, error|
|
74
|
+
error = error.first
|
75
|
+
field_error_dom = options.delete :tmpl
|
76
|
+
field_error_dom = DOM.new('<span class="field-error"><span>') unless field_error_dom
|
77
|
+
field_error_dom.html _error_name(key, error)
|
78
|
+
|
79
|
+
field = _dom.find("input[name='#{key}']")
|
80
|
+
field.before field_error_dom.dom
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
protected
|
85
|
+
|
86
|
+
def _options
|
87
|
+
@_options
|
88
|
+
end
|
89
|
+
|
90
|
+
def _dom
|
91
|
+
@_dom ||= @_options[:dom]
|
92
|
+
end
|
93
|
+
|
94
|
+
def _error_name key, error
|
95
|
+
case error.to_sym
|
96
|
+
when :not_email
|
97
|
+
'Email Isn\'t Valid.'
|
98
|
+
when :not_present
|
99
|
+
'Required.'
|
100
|
+
when :not_equal
|
101
|
+
'Password does not match.'
|
102
|
+
else
|
103
|
+
error
|
104
|
+
end
|
105
|
+
end
|
62
106
|
end
|
63
107
|
end
|
64
108
|
end
|
@@ -50,76 +50,29 @@ class LoginComponent < Roda::Component
|
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
53
|
-
on :
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
params[name] = value
|
70
|
-
end
|
71
|
-
|
72
|
-
form = Forms::Login.new params
|
73
|
-
|
74
|
-
if form.valid?
|
75
|
-
login_with params do |res|
|
76
|
-
if res['success']
|
77
|
-
# redirect them to the chatroom
|
78
|
-
`window.location.replace("/")`
|
53
|
+
on :form, 'form.main-form', class: Forms::Login do |form, el, evt|
|
54
|
+
if form.valid?
|
55
|
+
login_with form.attributes do |res|
|
56
|
+
if res['success']
|
57
|
+
# redirect them to the chatroom
|
58
|
+
`window.location.replace("/")`
|
59
|
+
else
|
60
|
+
case res['reason']
|
61
|
+
when /doesn't exist/
|
62
|
+
# Add fields for them to signup with
|
63
|
+
container = el.find('.field-container')
|
64
|
+
container.prepend('<input name="signup" type="hidden" value=true>')
|
65
|
+
container.prepend('<input name="last_name" type="text" placeholder="Last Name">')
|
66
|
+
container.prepend('<input name="first_name" type="text" placeholder="First Name">')
|
67
|
+
container.find("[name='password']").after('<input name="password_confirmation" type="password" placeholder="Confirm Password">')
|
79
68
|
else
|
80
|
-
|
81
|
-
|
82
|
-
# Add fields for them to signup with
|
83
|
-
container = el.find('.field-container')
|
84
|
-
container.prepend('<input name="signup" type="hidden" value=true>')
|
85
|
-
container.prepend('<input name="last_name" type="text" placeholder="Last Name">')
|
86
|
-
container.prepend('<input name="first_name" type="text" placeholder="First Name">')
|
87
|
-
container.find("[name='password']").after('<input name="password_confirmation" type="password" placeholder="Confirm Password">')
|
88
|
-
else
|
89
|
-
# Show server errors
|
90
|
-
display_errors res['errors']
|
91
|
-
end
|
69
|
+
# Show server errors
|
70
|
+
form.display_errors tmpl: tmpl(:field_error), errors: res['errors']
|
92
71
|
end
|
93
72
|
end
|
94
|
-
else
|
95
|
-
display_errors form.errors
|
96
73
|
end
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
protected
|
101
|
-
|
102
|
-
def display_errors errors
|
103
|
-
errors.each do |key, error|
|
104
|
-
error = error.first
|
105
|
-
field_error = tmpl :field_error
|
106
|
-
field_error.html error_name(key, error)
|
107
|
-
|
108
|
-
field = dom.find("input[name='#{key}']")
|
109
|
-
field.before field_error.dom
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
def error_name key, error
|
114
|
-
case error.to_sym
|
115
|
-
when :not_email
|
116
|
-
'Email Isn\'t Valid.'
|
117
|
-
when :not_present
|
118
|
-
'Required.'
|
119
|
-
when :not_equal
|
120
|
-
'Password does not match.'
|
121
74
|
else
|
122
|
-
|
75
|
+
form.display_errors tmpl: tmpl(:field_error)
|
123
76
|
end
|
124
77
|
end
|
125
78
|
end
|
data/test/dummy/config.ru
CHANGED
@@ -11,6 +11,7 @@ Unreloader.require './models'
|
|
11
11
|
Unreloader.require '../../lib/roda/component/dom.rb'
|
12
12
|
Unreloader.require '../../lib/roda/component/faye.rb'
|
13
13
|
Unreloader.require '../../lib/roda/component/form.rb'
|
14
|
+
Unreloader.require '../../lib/roda/component/events.rb'
|
14
15
|
Unreloader.require '../../lib/roda/component/form/validations.rb'
|
15
16
|
Unreloader.require '../../lib/roda/component.rb'
|
16
17
|
Unreloader.require '../../lib/roda/plugins/component.rb'
|
data/test/dummy/dummy.db
CHANGED
Binary file
|
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.
|
4
|
+
version: 0.1.8
|
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-
|
11
|
+
date: 2015-01-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: opal
|