dao 5.0.0 → 5.1.1
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.
- data/dao.gemspec +1 -1
- data/lib/dao.rb +1 -1
- data/lib/dao/form.rb +12 -2
- data/lib/dao/mode.rb +19 -12
- data/lib/dao/rails/lib/generators/dao/dao_generator.rb +7 -0
- data/lib/dao/rails/lib/generators/dao/templates/dao_helper.rb +44 -16
- data/test/api_test.rb +58 -44
- data/test/form_test.rb +1 -1
- metadata +2 -2
data/dao.gemspec
CHANGED
data/lib/dao.rb
CHANGED
data/lib/dao/form.rb
CHANGED
@@ -358,6 +358,10 @@ module Dao
|
|
358
358
|
values = attributes.get(*key) if attributes.has?(*key)
|
359
359
|
end
|
360
360
|
|
361
|
+
if options[:multiple]
|
362
|
+
name += '[]'
|
363
|
+
end
|
364
|
+
|
361
365
|
list = Array(values).map{|value| value.dup rescue value} # ensure list is dup'd
|
362
366
|
|
363
367
|
case list.first
|
@@ -384,9 +388,15 @@ module Dao
|
|
384
388
|
if options.has_key?(:selected)
|
385
389
|
options.delete(:selected)
|
386
390
|
else
|
387
|
-
|
391
|
+
attributes.get(keys)
|
388
392
|
end
|
389
393
|
|
394
|
+
selected_values = {}
|
395
|
+
|
396
|
+
Array(selected_value).flatten.compact.each do |val|
|
397
|
+
selected_values[val.to_s] = true
|
398
|
+
end
|
399
|
+
|
390
400
|
select_(options_for(options, :name => name, :class => klass, :id => id, :data_error => error)){
|
391
401
|
if blank
|
392
402
|
content = blank.first || ''
|
@@ -423,7 +433,7 @@ module Dao
|
|
423
433
|
end
|
424
434
|
|
425
435
|
if selected.nil?
|
426
|
-
selected = (value.to_s
|
436
|
+
selected = selected_values.has_key?(value.to_s)
|
427
437
|
end
|
428
438
|
|
429
439
|
opts[:value] = (value.nil? ? content : value)
|
data/lib/dao/mode.rb
CHANGED
@@ -39,29 +39,36 @@ module Dao
|
|
39
39
|
|
40
40
|
# instance methods
|
41
41
|
#
|
42
|
-
def
|
43
|
-
@
|
42
|
+
def aliases
|
43
|
+
@aliases ||= []
|
44
|
+
end
|
45
|
+
|
46
|
+
def Mode.alias(a, b)
|
47
|
+
a, b = Mode.for(a), Mode.for(b)
|
48
|
+
a.aliases.push(b) unless a.aliases.include?(b)
|
49
|
+
b.aliases.push(a) unless b.aliases.include?(a)
|
50
|
+
(a.aliases + b.aliases).uniq
|
44
51
|
end
|
45
52
|
|
46
53
|
def case_of?(other)
|
47
|
-
|
54
|
+
a, b = self, Mode.for(other)
|
55
|
+
a == b or a.aliases.include?(b) or b.aliases.include?(a)
|
48
56
|
end
|
49
57
|
|
50
58
|
def ===(other)
|
51
59
|
case_of?(other)
|
52
60
|
end
|
53
61
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
Http.each do |verb|
|
58
|
-
Mode.add(verb)
|
59
|
-
end
|
62
|
+
# setup mode singletons and their aliases
|
63
|
+
#
|
64
|
+
HTTP = ( READ = %w[ get options head ] ) + ( WRITE = %w[ post put delete trace connect ] )
|
60
65
|
|
61
66
|
Mode.add(:read)
|
62
|
-
Read.each{|m| Mode.read.cases.push(Mode.send(m))}
|
63
|
-
|
64
67
|
Mode.add(:write)
|
65
|
-
|
68
|
+
|
69
|
+
HTTP.each{|verb| Mode.add(verb)}
|
70
|
+
|
71
|
+
Mode.alias(:read, :get)
|
72
|
+
Mode.alias(:write, :post)
|
66
73
|
end
|
67
74
|
end
|
@@ -12,6 +12,9 @@ class DaoGenerator < Rails::Generators::NamedBase
|
|
12
12
|
when /system/
|
13
13
|
generate_system!
|
14
14
|
|
15
|
+
when /helper/
|
16
|
+
generate_helper!
|
17
|
+
|
15
18
|
when /api/
|
16
19
|
generate_system!
|
17
20
|
|
@@ -30,6 +33,10 @@ protected
|
|
30
33
|
template "conducer.rb", "app/conducers/#{ @conducer_name.underscore }.rb"
|
31
34
|
end
|
32
35
|
|
36
|
+
def generate_helper!
|
37
|
+
copy_file("dao_helper.rb", "app/helpers/dao_helper.rb")
|
38
|
+
end
|
39
|
+
|
33
40
|
def generate_system!
|
34
41
|
FileUtils.mkdir_p(File.join(Rails.root, 'app/conducers'))
|
35
42
|
|
@@ -1,17 +1,12 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
2
|
module DaoHelper
|
3
3
|
def dao_form_for(*args, &block)
|
4
|
+
# grok the model, or build one on the fly
|
5
|
+
#
|
4
6
|
model = args.flatten.select{|arg| arg.respond_to?(:persisted?)}.last
|
5
7
|
|
6
8
|
options = args.extract_options!.to_options!
|
7
9
|
|
8
|
-
options[:builder] = Dao::Form::Builder
|
9
|
-
|
10
|
-
if options[:post] or model.blank?
|
11
|
-
options[:url] ||= (options.delete(:post) || request.fullpath)
|
12
|
-
options[:method] ||= :post
|
13
|
-
end
|
14
|
-
|
15
10
|
args.push(options)
|
16
11
|
|
17
12
|
if model.blank?
|
@@ -20,22 +15,55 @@ module DaoHelper
|
|
20
15
|
args.unshift(model)
|
21
16
|
end
|
22
17
|
|
18
|
+
# build urls to *relative to the current controller* (with respect to the
|
19
|
+
# resource's state) unless specified...
|
20
|
+
#
|
21
|
+
html = dao_form_attrs(options.delete(:html) || {})
|
22
|
+
|
23
|
+
if model
|
24
|
+
url = options.delete(:url)
|
25
|
+
method = options.delete(:method) || html.delete(:method)
|
26
|
+
|
27
|
+
if model.persisted?
|
28
|
+
method ||= :put
|
29
|
+
else
|
30
|
+
method ||= :post
|
31
|
+
end
|
32
|
+
|
33
|
+
url ||=
|
34
|
+
case method
|
35
|
+
when /post/
|
36
|
+
url_for(:action => :create)
|
37
|
+
when /put/
|
38
|
+
url_for(:action => :update)
|
39
|
+
else
|
40
|
+
'./'
|
41
|
+
end
|
42
|
+
|
43
|
+
options[:url] = url
|
44
|
+
options[:html] = html.merge(:method => method)
|
45
|
+
end
|
46
|
+
|
47
|
+
# use a dao form builder...
|
48
|
+
#
|
49
|
+
options[:builder] = Dao::Form::Builder
|
50
|
+
|
51
|
+
# delegate the rest of th magick to rails...
|
52
|
+
#
|
23
53
|
form_for(*args, &block)
|
24
54
|
end
|
25
55
|
alias_method(:dao_form, :dao_form_for)
|
26
56
|
|
27
57
|
def dao_form_attrs(*args)
|
28
58
|
args.flatten!
|
59
|
+
|
29
60
|
options = args.extract_options!.to_options!.dup
|
30
|
-
|
31
|
-
options[:class] =
|
32
|
-
|
33
|
-
|
34
|
-
options[:class].flatten!
|
35
|
-
options[:class].compact!
|
36
|
-
options[:class].uniq!
|
37
|
-
options[:class] = options[:class].join(' ')
|
61
|
+
|
62
|
+
options[:class] =
|
63
|
+
[args, options.delete(:class)].join(' ').scan(%r/[^\s]+/).push(' dao ').uniq.join(' ')
|
64
|
+
|
38
65
|
options[:enctype] ||= "multipart/form-data"
|
66
|
+
|
39
67
|
options
|
40
68
|
end
|
41
69
|
|
@@ -69,7 +97,7 @@ module DaoHelper
|
|
69
97
|
@dao.route = request.fullpath
|
70
98
|
|
71
99
|
unless options[:error!] == false
|
72
|
-
@dao.error! unless(@dao.status =~ or @dao.status == 420)
|
100
|
+
@dao.error! unless(@dao.status =~ 200 or @dao.status == 420)
|
73
101
|
end
|
74
102
|
|
75
103
|
block ? block.call(@dao) : @dao
|
data/test/api_test.rb
CHANGED
@@ -48,88 +48,102 @@ Testing Dao do
|
|
48
48
|
end
|
49
49
|
|
50
50
|
testing 'that an api can be called with different modes' do
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
Dao::Mode.list.each do |mode|
|
58
|
-
send(mode){ data.modes.push(mode) }
|
51
|
+
Dao::Mode.list.each do |mode|
|
52
|
+
api_class =
|
53
|
+
assert{
|
54
|
+
Dao.api do
|
55
|
+
call(:foo) do
|
56
|
+
send(mode){ data.update(:mode => mode) }
|
59
57
|
end
|
60
58
|
end
|
61
|
-
|
62
|
-
}
|
63
|
-
api = assert{ api_class.new }
|
59
|
+
}
|
60
|
+
api = assert{ api_class.new }
|
64
61
|
|
65
|
-
|
66
|
-
result = api.mode(mode).call(:foo)
|
67
|
-
assert{ result.data.modes.include?(mode) }
|
62
|
+
assert{ api.mode(mode).call(:foo).data[:mode] == mode }
|
68
63
|
end
|
69
64
|
end
|
70
65
|
|
71
|
-
testing 'that
|
72
|
-
read_mode = assert{ Dao::Mode.read }
|
73
|
-
|
66
|
+
testing 'that read==get' do
|
74
67
|
api_class =
|
75
68
|
assert{
|
76
69
|
Dao.api do
|
77
70
|
call(:foo) do
|
78
|
-
data.update :
|
79
|
-
|
71
|
+
read { data.update :answer => 42 }
|
72
|
+
end
|
73
|
+
|
74
|
+
call(:bar) do
|
75
|
+
get { data.update :answer => 42.0 }
|
80
76
|
end
|
81
77
|
end
|
82
78
|
}
|
83
79
|
api = assert{ api_class.new }
|
84
80
|
|
85
|
-
|
86
|
-
|
87
|
-
assert{ result.data.modes == [read_mode] }
|
88
|
-
end
|
89
|
-
end
|
81
|
+
assert{ api.read.call(:foo).data.answer == 42 }
|
82
|
+
assert{ api.get.call(:foo).data.answer == 42 }
|
90
83
|
|
91
|
-
|
92
|
-
|
84
|
+
assert{ api.read.call(:bar).data.answer == 42.0 }
|
85
|
+
assert{ api.get.call(:bar).data.answer == 42.0 }
|
86
|
+
end
|
93
87
|
|
88
|
+
testing 'that write==post' do
|
94
89
|
api_class =
|
95
90
|
assert{
|
96
91
|
Dao.api do
|
97
92
|
call(:foo) do
|
98
|
-
data.update :
|
99
|
-
|
93
|
+
write { data.update :answer => 42 }
|
94
|
+
end
|
95
|
+
|
96
|
+
call(:bar) do
|
97
|
+
post { data.update :answer => 42.0 }
|
100
98
|
end
|
101
99
|
end
|
102
100
|
}
|
103
101
|
api = assert{ api_class.new }
|
104
102
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
103
|
+
assert{ api.write.call(:foo).data.answer == 42 }
|
104
|
+
assert{ api.post.call(:foo).data.answer == 42 }
|
105
|
+
|
106
|
+
assert{ api.write.call(:bar).data.answer == 42.0 }
|
107
|
+
assert{ api.post.call(:bar).data.answer == 42.0 }
|
109
108
|
end
|
110
109
|
|
111
|
-
testing 'that
|
110
|
+
testing 'that aliases are re-defined in scope' do
|
112
111
|
api_class =
|
113
112
|
assert{
|
114
113
|
Dao.api do
|
115
114
|
call(:foo) do
|
116
|
-
data.update :
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
115
|
+
data.update :a => mode
|
116
|
+
read { data.update :b => mode }
|
117
|
+
write { data.update :b => mode }
|
118
|
+
end
|
119
|
+
|
120
|
+
call(:bar) do
|
121
|
+
data.update :a => mode
|
122
|
+
get { data.update :b => mode }
|
123
|
+
post { data.update :b => mode }
|
121
124
|
end
|
122
125
|
end
|
123
126
|
}
|
124
127
|
api = assert{ api_class.new }
|
125
128
|
|
126
|
-
read = Dao::Mode.read
|
127
|
-
result = assert{ api.mode(read).call(:foo) }
|
128
|
-
assert{ result.data.modes == [read] }
|
129
129
|
|
130
|
-
|
131
|
-
result = assert{ api.
|
132
|
-
assert{ result.data
|
130
|
+
[:foo, :bar].each do |call|
|
131
|
+
result = assert{ api.read.call(call) }
|
132
|
+
assert{ result.data[:a] = Dao::Mode.read }
|
133
|
+
assert{ result.data[:b] = Dao::Mode.read }
|
134
|
+
|
135
|
+
result = assert{ api.get.call(call) }
|
136
|
+
assert{ result.data[:a] = Dao::Mode.read }
|
137
|
+
assert{ result.data[:b] = Dao::Mode.get }
|
138
|
+
|
139
|
+
|
140
|
+
result = assert{ api.write.call(call) }
|
141
|
+
assert{ result.data[:a] = Dao::Mode.write }
|
142
|
+
assert{ result.data[:b] = Dao::Mode.write }
|
143
|
+
|
144
|
+
result = assert{ api.post.call(call) }
|
145
|
+
assert{ result.data[:a] = Dao::Mode.write }
|
146
|
+
assert{ result.data[:b] = Dao::Mode.post }
|
133
147
|
end
|
134
148
|
end
|
135
149
|
|
data/test/form_test.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dao
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 5.
|
4
|
+
version: 5.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-29 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|