cuca 0.05 → 0.06

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,254 @@
1
+ require 'cuca/stdlib/form'
2
+ require 'cuca/stdlib/formerrors'
3
+ require 'cuca/generator/markaby'
4
+
5
+ # == Form's for ActiveRecord
6
+ # AR Form build a form by providing one model of ActiveRecord.
7
+ # Likly that you want to overwrite the form method to
8
+ # run a custom layout.
9
+ #
10
+ # This Widget will call <form_name>_submit(model) if Form is submitted
11
+ # and validation passed. You still have to ::save the model.
12
+ #
13
+ # = Example:
14
+ #
15
+ # ARForm('user_edit', User.find_by_username('bones'),
16
+ # :disable_on_update => ['username', 'created'])
17
+ #
18
+ class ARFormWidget < FormWidget
19
+
20
+ include Cuca::Generator::Markaby
21
+
22
+
23
+
24
+
25
+ # valid options
26
+ # * :disabled_on_create => ['field_name_1', 'field_name_2', ..]
27
+ # switch off fields on new records
28
+ # * :diabled_on_update => ['field_name_1', 'field_name_2', ..]
29
+ # switch off fields on existing records
30
+ def output(form_name, model, options = {})
31
+ @model = model
32
+ @disabled_on_update = options[:disabled_on_update] || []
33
+ @disabled_on_create = options[:disabled_on_create] || []
34
+ @hidden_on_update = options[:hidden_on_update] || []
35
+ @hidden_on_create = options[:hidden_on_create] || []
36
+ super(form_name, options)
37
+ end
38
+
39
+ def on_submit
40
+ controller.send(@form_name+'_submit', @model) unless controller.nil?
41
+
42
+ # this is to reload the form with the new values in case the formname_submit did
43
+ # save something:
44
+ clear
45
+ # form
46
+ end
47
+
48
+ def validate
49
+ form if @_content.empty? # password fields might write hints to the validator...
50
+ clear
51
+ @form_erros = {}
52
+ p = request_parameters.dup
53
+ p.delete(@submit_name)
54
+
55
+ if @model.new_record? then
56
+ @disabled_on_create.each { |d| p.delete(d) }
57
+ @hidden_on_create.each { |d| p.delete(d) }
58
+ else
59
+ @disabled_on_update.each { |d| p.delete(d) }
60
+ @hidden_on_update.each { |d| p.delete(d) }
61
+ end
62
+
63
+
64
+ # don't save empty passwords!!
65
+ @password_fields ||= []
66
+ @password_fields.each do |pwf|
67
+ p.delete(pwf) if p[pwf].chomp.empty?
68
+ end
69
+ # $stderr.puts p.inspect
70
+ @model.attributes = p
71
+
72
+ return true if @model.valid?
73
+
74
+ @model.errors.each do |k,v|
75
+ @form_errors[k] = v
76
+ end
77
+ end
78
+
79
+ # to make a 2 digit string out a number
80
+ private
81
+ def twodig(n)
82
+ n.to_s.length == 1 ? "0"+n.to_s : n.to_s
83
+ end
84
+
85
+ def field_enable?(fname)
86
+ if @model.new_record? then
87
+ return @disabled_on_create.include?(fname) ? false : true
88
+ else
89
+ return @disabled_on_update.include?(fname) ? false : true
90
+ end
91
+ end
92
+ def field_hidden?(fname)
93
+ if @model.new_record? then
94
+ return @hidden_on_create.include?(fname) ? true : false
95
+ else
96
+ return @hidden_on_update.include?(fname) ? true : false
97
+ end
98
+ end
99
+
100
+
101
+ private
102
+ def fe_text(name, value,enabled)
103
+ "<input type='text' name='#{name}' value='#{value}' #{'disabled' unless enabled}>"
104
+ end
105
+
106
+ private
107
+ def fe_textarea(name, value, enabled, rows=4, cols = 50)
108
+ "<textarea name='#{name}' rows='#{rows}' cols='#{cols}'>#{value}</textarea>"
109
+ end
110
+
111
+
112
+ # the fe-password is special: will never contain a value and will not save the
113
+ # password if nothing was typed into the field
114
+ private
115
+ def fe_password(name,enabled)
116
+ @password_fields ||= []
117
+ @password_fields << name
118
+ r = "<input type='password' name='#{name}' #{'disabled' unless enabled}>"
119
+ if !@model.new_record? then
120
+ r << "<br><small>Leave empty to keep current password</small>"
121
+ end
122
+ return r
123
+ end
124
+
125
+ # this is to build a select box, example:
126
+ # fe_select('gender', [['f', 'female'],['m','Male']], true) or
127
+ # fe_select('gender', ['f','m'], true)
128
+ private
129
+ def fe_select(name, options, enabled)
130
+ r = "<select name='#{name}' #{'disabled' unless enabled}>\n"
131
+ options.each do |o|
132
+ ov = o.instance_of?(Array) ? o[0] : o
133
+ sel = ''
134
+ sel = ' selected' if @model.send(name.intern) == ov
135
+ if o.instance_of?(Array) then
136
+ r+="<option value='#{o[0]}'#{sel}>#{o[1]}</option>\n"
137
+ else
138
+ r+="<option value='#{o}'#{sel}>#{o}</option>\n"
139
+ end
140
+ end
141
+ r+="</option>\n"
142
+ end
143
+
144
+
145
+ def fe_datetime(name, v,enabled)
146
+ v = Time.now unless v
147
+ r = <<-EOS
148
+ <input type='text' name='#{name}' id='i_#{name}' value='#{v.year}/#{twodig(v.month)}/#{twodig(v.day)} #{v.hour}:#{v.min}' #{'disabled' unless enabled}>
149
+ EOS
150
+ if enabled then
151
+ r << <<-EOS
152
+ <input type='submit' id='s_#{name}' value='...'>
153
+ <script type="text/javascript">
154
+ Calendar.setup({
155
+ inputField : "i_#{name}", // id of the input field
156
+ ifFormat : "%Y/%m/%d %H:%M", // format of the input field
157
+ showsTime : true, // will display a time selector
158
+ button : "s_#{name}", // trigger for the calendar (button ID)
159
+ singleClick : true, // double-click mode
160
+ step : 1 // show all years in drop-down boxes (instead of every other year as default)
161
+ });
162
+ </script>
163
+ </input>
164
+ EOS
165
+ end
166
+ return r
167
+ end
168
+
169
+ # select date.. no time
170
+ def fe_date(name, v,enabled)
171
+ v = Time.now unless v
172
+ r = <<-EOS
173
+ <input type='text' name='#{name}' id='i_#{name}' value='#{v.year}/#{twodig(v.month)}/#{twodig(v.day)}' #{'disabled' unless enabled}>
174
+ EOS
175
+ if enabled then
176
+ r << <<-EOS
177
+ <input type='submit' id='s_#{name}' value='...'>
178
+ <script type="text/javascript">
179
+ Calendar.setup({
180
+ inputField : "i_#{name}", // id of the input field
181
+ ifFormat : "%Y/%m/%d", // format of the input field
182
+ showsTime : false, // will display a time selector
183
+ button : "s_#{name}", // trigger for the calendar (button ID)
184
+ singleClick : true, // double-click mode
185
+ step : 1 // show all years in drop-down boxes (instead of every other year as default)
186
+ });
187
+ </script>
188
+ </input>
189
+ EOS
190
+ end
191
+ return r
192
+ end
193
+
194
+ def fe_bool(name,v,enabled)
195
+ r = ''
196
+ r << "<select name='#{name}' #{'disabled' unless enabled}>\n"
197
+ r << "<option #{"selected" if v} value='t'>true</option>\n"
198
+ r << "<option #{"selected" if !v} value='f'>false</option>\n"
199
+ r << "</select>\n"
200
+ end
201
+
202
+ def fe_int(name,v,enabled)
203
+ "<input type='text' name='#{name}' value='#{v}' #{'disabled' unless enabled} size=5>"
204
+ end
205
+
206
+ def fe(type, name, value='')
207
+ return '' if field_hidden?(name)
208
+ enabled = field_enable?(name)
209
+ r = ""
210
+ case(type)
211
+ when :string
212
+ r << fe_text(name,value,enabled)
213
+ when :boolean
214
+ r << fe_bool(name,value,enabled)
215
+ when :integer
216
+ r << fe_int(name,value,enabled)
217
+ when :datetime
218
+ r << fe_datetime(name,value,enabled)
219
+ when :date
220
+ r << fe_date(name,value,enabled)
221
+ when :password
222
+ r << fe_password(name, enabled)
223
+ end
224
+ return r
225
+ end
226
+
227
+ # build a form element for column name
228
+ def fe_for(column_name, hint='')
229
+ col = @model.column_for_attribute(column_name)
230
+ fe(col.type, col.name, @model.send(column_name.intern))
231
+ end
232
+
233
+
234
+ # you might want to replace this method
235
+ public
236
+ def form
237
+ r = mabtext { FormErrors(@form_errors) }
238
+ r << "<form action='#{@post_to}' method='POST'>\n"
239
+ r << "<table>"
240
+ @model.class.columns.each do |col|
241
+ next if field_hidden?(col.name)
242
+ k = col.name
243
+ v = @model.send(k.intern) # this allows us to overwrite accessors
244
+ r << "<tr><td>#{k}</td><td>#{fe(col.type,k,v)}</td></tr>"
245
+ end
246
+ r << "<tr><td><br/></td></tr>"
247
+ r << "<tr><td></td><td><input type='submit' value=#{@model.new_record? ? 'Save' : 'Update'} name='#{@submit_name}'></td></tr>"
248
+ r << "</table>\n</form>\n"
249
+ @_content = r
250
+ end
251
+
252
+
253
+ end
254
+
@@ -11,9 +11,10 @@ module Helpers
11
11
  # init the application. call this from the setup method.
12
12
  def init(app_path = '/', params = {})
13
13
  require 'cuca/cgi_emu'
14
+ require 'cuca/urlmap'
14
15
  @cgi = CGIEmu.new({'PATH_INFO' => app_path, 'QUERY_PARAMS' => params})
15
- @app = Cuca::App.new(@cgi)
16
- @app.load_support_files
16
+ @app = Cuca::App.new
17
+ @app.load_support_files(Cuca::URLMap.new($cuca_path+'/app', app_path))
17
18
  end
18
19
 
19
20
 
data/lib/cuca_console.rb CHANGED
@@ -10,3 +10,5 @@ $app = Cuca::App.new
10
10
  def url(some_path)
11
11
  $app.load_support_files(Cuca::URLMap.new($cuca_path+'/app', some_path))
12
12
  end
13
+
14
+ url '/'
data/tests/controller.rb CHANGED
@@ -101,6 +101,36 @@ class ReturnOtherMimeController < Cuca::Controller
101
101
  end
102
102
 
103
103
 
104
+
105
+ class ChainAController < Cuca::Controller
106
+ before_filter 'a', 1
107
+ def a
108
+ @out ||= ''
109
+ @out << 'a'
110
+ end
111
+ end
112
+
113
+
114
+ class ChainBController < ChainAController
115
+ before_filter 'b',100
116
+ def b
117
+ @out ||= ''
118
+ @out << 'b'
119
+ end
120
+ end
121
+
122
+ class ChainCController < ChainBController
123
+ before_filter 'c',2
124
+ attr :out
125
+ def c
126
+ @out ||= ''
127
+ @out << 'c'
128
+ end
129
+ end
130
+
131
+
132
+
133
+
104
134
  class ControllerTests < Test::Unit::TestCase
105
135
 
106
136
  def test_basic
@@ -160,4 +190,11 @@ class ControllerTests < Test::Unit::TestCase
160
190
  end
161
191
 
162
192
 
193
+ def test_chain_filters
194
+ c = ChainCController.new
195
+ c.run_before_filters
196
+ assert_equal 'acb', c.out
197
+ end
198
+
199
+
163
200
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cuca
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.05"
4
+ version: "0.06"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Boese
@@ -9,11 +9,12 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-20 00:00:00 +00:00
12
+ date: 2009-03-04 00:00:00 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: markaby
17
+ type: :runtime
17
18
  version_requirement:
18
19
  version_requirements: !ruby/object:Gem::Requirement
19
20
  requirements:
@@ -23,6 +24,7 @@ dependencies:
23
24
  version:
24
25
  - !ruby/object:Gem::Dependency
25
26
  name: fcgi
27
+ type: :runtime
26
28
  version_requirement:
27
29
  version_requirements: !ruby/object:Gem::Requirement
28
30
  requirements:
@@ -38,106 +40,110 @@ extensions: []
38
40
 
39
41
  extra_rdoc_files:
40
42
  - CHANGELOG
43
+ - README
41
44
  files:
42
45
  - bin/cuca
43
- - lib/cuca.rb
44
- - lib/cuca_console.rb
45
46
  - lib/cuca
47
+ - lib/cuca/mimetypes.rb
48
+ - lib/cuca/sessionpage.rb
49
+ - lib/cuca/urlmap.rb
50
+ - lib/cuca/layout.rb
51
+ - lib/cuca/widget.rb
46
52
  - lib/cuca/cgi_fix.rb
53
+ - lib/cuca/const.rb
54
+ - lib/cuca/sessionflash.rb
47
55
  - lib/cuca/controller.rb
56
+ - lib/cuca/session.rb
57
+ - lib/cuca/app.rb
48
58
  - lib/cuca/config.rb
59
+ - lib/cuca/test
60
+ - lib/cuca/test/helpers.rb
49
61
  - lib/cuca/stdlib
50
- - lib/cuca/stdlib/arview.rb
51
- - lib/cuca/stdlib/form.rb
52
- - lib/cuca/stdlib/link.rb
53
62
  - lib/cuca/stdlib/slink.rb
63
+ - lib/cuca/stdlib/link.rb
64
+ - lib/cuca/stdlib/form.rb
65
+ - lib/cuca/stdlib/list.rb
66
+ - lib/cuca/stdlib/arview.rb
54
67
  - lib/cuca/stdlib/arform.rb
68
+ - lib/cuca/stdlib/old_arform.rb
55
69
  - lib/cuca/stdlib/listwidget
56
- - lib/cuca/stdlib/listwidget/querydef.rb
70
+ - lib/cuca/stdlib/listwidget/list.rb
57
71
  - lib/cuca/stdlib/listwidget/dblist.rb
72
+ - lib/cuca/stdlib/listwidget/querydef.rb
58
73
  - lib/cuca/stdlib/listwidget/staticdatalist.rb
59
- - lib/cuca/stdlib/listwidget/list.rb
60
- - lib/cuca/stdlib/list.rb
61
74
  - lib/cuca/stdlib/formerrors.rb
75
+ - lib/cuca/stdlib/formelements.rb
62
76
  - lib/cuca/generator
63
77
  - lib/cuca/generator/markaby.rb
64
78
  - lib/cuca/generator/view.rb
65
- - lib/cuca/sessionflash.rb
66
- - lib/cuca/widget.rb
67
- - lib/cuca/mimetypes.rb
68
- - lib/cuca/test
69
- - lib/cuca/test/helpers.rb
70
- - lib/cuca/sessionpage.rb
71
- - lib/cuca/session.rb
72
79
  - lib/cuca/cgi_emu.rb
73
- - lib/cuca/app.rb
74
- - lib/cuca/urlmap.rb
75
- - lib/cuca/layout.rb
76
- - lib/cuca/const.rb
80
+ - lib/cuca_console.rb
81
+ - lib/cuca.rb
82
+ - tests/mimetypes.rb
83
+ - tests/urlmap.rb
77
84
  - tests/generator_markaby.rb
78
- - tests/controller.rb
85
+ - tests/all.rb
79
86
  - tests/widget.rb
80
- - tests/mimetypes.rb
87
+ - tests/generator_view.rb
88
+ - tests/controller.rb
81
89
  - tests/app.rb
82
90
  - tests/test_app
91
+ - tests/test_app/conf
92
+ - tests/test_app/conf/environment.rb
93
+ - tests/test_app/conf/config.rb
83
94
  - tests/test_app/log
84
95
  - tests/test_app/log/messages
85
96
  - tests/test_app/README
86
97
  - tests/test_app/app
98
+ - tests/test_app/app/agent
99
+ - tests/test_app/app/agent/index.rb
100
+ - tests/test_app/app/agent/list.rb
101
+ - tests/test_app/app/_views
102
+ - tests/test_app/app/_views/test_template.rhtml
87
103
  - tests/test_app/app/user
88
104
  - tests/test_app/app/user/__username
89
105
  - tests/test_app/app/user/__username/index.rb
90
106
  - tests/test_app/app/user/list.rb
91
- - tests/test_app/app/_views
92
- - tests/test_app/app/_views/test_template.rhtml
93
- - tests/test_app/app/agent
94
- - tests/test_app/app/agent/index.rb
95
- - tests/test_app/app/agent/list.rb
96
- - tests/test_app/conf
97
- - tests/test_app/conf/config.rb
98
- - tests/test_app/conf/environment.rb
99
- - tests/urlmap.rb
100
- - tests/generator_view.rb
101
- - tests/all.rb
102
- - application_skeleton/log
103
- - application_skeleton/log/messages
104
- - application_skeleton/log/error.log
105
- - application_skeleton/log/access.log
106
- - application_skeleton/README
107
107
  - application_skeleton/tests
108
108
  - application_skeleton/tests/widgets
109
109
  - application_skeleton/tests/widgets/link.rb
110
- - application_skeleton/app
111
- - application_skeleton/app/index.rb
112
- - application_skeleton/app/demo.rb
113
- - application_skeleton/app/_controllers
114
- - application_skeleton/app/_controllers/application.rb
115
- - application_skeleton/app/_layouts
116
- - application_skeleton/app/_layouts/simple.rb
117
- - application_skeleton/app/_widgets
118
- - application_skeleton/app/_widgets/test.rb
119
- - application_skeleton/app/_widgets/sourcecode.rb
120
- - application_skeleton/app/_views
110
+ - application_skeleton/conf
111
+ - application_skeleton/conf/environment.rb
112
+ - application_skeleton/conf/config.rb
113
+ - application_skeleton/public
114
+ - application_skeleton/public/img
115
+ - application_skeleton/public/img/cuca-seagull.png
116
+ - application_skeleton/public/dispatch.cgi
117
+ - application_skeleton/public/dispatch.fcgi
118
+ - application_skeleton/public/500.html
119
+ - application_skeleton/public/css
120
+ - application_skeleton/public/css/style.css
121
+ - application_skeleton/public/404.html
121
122
  - application_skeleton/scripts
122
123
  - application_skeleton/scripts/server-lighttpd-fcgi.rb
124
+ - application_skeleton/scripts/server-webrick.rb
123
125
  - application_skeleton/scripts/console.rb
124
- - application_skeleton/scripts/test.rb
125
126
  - application_skeleton/scripts/server-lighttpd.rb
126
- - application_skeleton/scripts/server-webrick.rb
127
+ - application_skeleton/scripts/test.rb
127
128
  - application_skeleton/lib
128
- - application_skeleton/public
129
- - application_skeleton/public/404.html
130
- - application_skeleton/public/dispatch.fcgi
131
- - application_skeleton/public/dispatch.cgi
132
- - application_skeleton/public/css
133
- - application_skeleton/public/css/style.css
134
- - application_skeleton/public/img
135
- - application_skeleton/public/img/cuca-seagull.png
136
- - application_skeleton/public/500.html
137
- - application_skeleton/conf
138
- - application_skeleton/conf/config.rb
139
- - application_skeleton/conf/environment.rb
129
+ - application_skeleton/log
130
+ - application_skeleton/log/access.log
131
+ - application_skeleton/log/error.log
132
+ - application_skeleton/log/messages
133
+ - application_skeleton/README
134
+ - application_skeleton/app
135
+ - application_skeleton/app/_widgets
136
+ - application_skeleton/app/_widgets/sourcecode.rb
137
+ - application_skeleton/app/_widgets/test.rb
138
+ - application_skeleton/app/index.rb
139
+ - application_skeleton/app/_views
140
+ - application_skeleton/app/_layouts
141
+ - application_skeleton/app/_layouts/simple.rb
142
+ - application_skeleton/app/demo.rb
143
+ - application_skeleton/app/_controllers
144
+ - application_skeleton/app/_controllers/application.rb
140
145
  - CHANGELOG
146
+ - README
141
147
  has_rdoc: true
142
148
  homepage: http://cuca.rubyforge.org/
143
149
  post_install_message:
@@ -160,7 +166,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
166
  requirements: []
161
167
 
162
168
  rubyforge_project: cuca
163
- rubygems_version: 1.1.1
169
+ rubygems_version: 1.2.0
164
170
  signing_key:
165
171
  specification_version: 2
166
172
  summary: A widget-based web framework