merb 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +21 -14
- data/Rakefile +157 -108
- data/SVN_REVISION +1 -0
- data/app_generators/merb/templates/Rakefile +20 -4
- data/app_generators/merb/templates/app/views/exceptions/internal_server_error.html.erb +1 -1
- data/app_generators/merb/templates/config/boot.rb +1 -1
- data/app_generators/merb/templates/config/dependencies.rb +3 -3
- data/app_generators/merb/templates/config/merb.yml +5 -0
- data/app_generators/merb/templates/config/merb_init.rb +3 -3
- data/app_generators/merb/templates/script/destroy +3 -0
- data/app_generators/merb/templates/script/generate +1 -1
- data/app_generators/merb/templates/spec/spec_helper.rb +2 -2
- data/app_generators/merb/templates/test/test_helper.rb +1 -1
- data/app_generators/merb_plugin/merb_plugin_generator.rb +4 -0
- data/bin/merb +1 -3
- data/lib/merb.rb +144 -76
- data/lib/merb/abstract_controller.rb +6 -5
- data/lib/merb/assets.rb +119 -0
- data/lib/merb/boot_loader.rb +217 -0
- data/lib/merb/caching.rb +1 -1
- data/lib/merb/caching/action_cache.rb +1 -1
- data/lib/merb/caching/fragment_cache.rb +1 -1
- data/lib/merb/caching/store/file_cache.rb +1 -1
- data/lib/merb/config.rb +290 -0
- data/lib/merb/controller.rb +5 -5
- data/lib/merb/core_ext/get_args.rb +1 -0
- data/lib/merb/core_ext/hash.rb +182 -169
- data/lib/merb/core_ext/kernel.rb +57 -26
- data/lib/merb/dispatcher.rb +6 -6
- data/lib/merb/drb_server.rb +1 -1
- data/lib/merb/generators/merb_generator_helpers.rb +7 -6
- data/lib/merb/logger.rb +1 -1
- data/lib/merb/mail_controller.rb +3 -4
- data/lib/merb/mailer.rb +2 -2
- data/lib/merb/mixins/basic_authentication.rb +2 -2
- data/lib/merb/mixins/controller.rb +1 -1
- data/lib/merb/mixins/general_controller.rb +13 -20
- data/lib/merb/mixins/inline_partial.rb +32 -0
- data/lib/merb/mixins/render.rb +3 -3
- data/lib/merb/mixins/responder.rb +1 -1
- data/lib/merb/mixins/view_context.rb +159 -33
- data/lib/merb/mongrel_handler.rb +9 -9
- data/lib/merb/plugins.rb +1 -1
- data/lib/merb/request.rb +25 -1
- data/lib/merb/router.rb +264 -226
- data/lib/merb/server.rb +66 -560
- data/lib/merb/session/cookie_store.rb +14 -13
- data/lib/merb/session/mem_cache_session.rb +20 -10
- data/lib/merb/session/memory_session.rb +21 -11
- data/lib/merb/template.rb +2 -2
- data/lib/merb/template/erubis.rb +3 -33
- data/lib/merb/template/haml.rb +8 -3
- data/lib/merb/test/fake_request.rb +8 -3
- data/lib/merb/test/helper.rb +66 -22
- data/lib/merb/test/rspec.rb +9 -155
- data/lib/merb/test/rspec_matchers/controller_matchers.rb +117 -0
- data/lib/merb/test/rspec_matchers/markup_matchers.rb +98 -0
- data/lib/merb/upload_handler.rb +2 -1
- data/lib/merb/version.rb +38 -3
- data/lib/merb/view_context.rb +1 -2
- data/lib/tasks/merb.rake +11 -11
- data/merb_generators/part_controller/USAGE +5 -0
- data/merb_generators/part_controller/part_controller_generator.rb +27 -0
- data/merb_generators/part_controller/templates/controller.rb +8 -0
- data/merb_generators/part_controller/templates/helper.rb +5 -0
- data/merb_generators/part_controller/templates/index.html.erb +3 -0
- data/rspec_generators/merb_controller_test/merb_controller_test_generator.rb +1 -1
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/fixtures/controllers/dispatch_spec_controllers.rb +9 -1
- data/spec/fixtures/controllers/render_spec_controllers.rb +5 -5
- data/spec/fixtures/models/router_spec_models.rb +10 -0
- data/spec/merb/abstract_controller_spec.rb +2 -2
- data/spec/merb/assets_spec.rb +207 -0
- data/spec/merb/caching_spec.rb +2 -2
- data/spec/merb/controller_spec.rb +7 -2
- data/spec/merb/cookie_store_spec.rb +1 -1
- data/spec/merb/core_ext/class_spec.rb +97 -0
- data/spec/merb/core_ext/enumerable_spec.rb +27 -0
- data/spec/merb/core_ext/hash_spec.rb +251 -0
- data/spec/merb/core_ext/inflector_spec.rb +34 -0
- data/spec/merb/core_ext/kernel_spec.rb +25 -0
- data/spec/merb/core_ext/numeric_spec.rb +26 -0
- data/spec/merb/core_ext/object_spec.rb +47 -0
- data/spec/merb/core_ext/string_spec.rb +22 -0
- data/spec/merb/core_ext/symbol_spec.rb +7 -0
- data/spec/merb/dependency_spec.rb +22 -0
- data/spec/merb/dispatch_spec.rb +23 -12
- data/spec/merb/fake_request_spec.rb +8 -0
- data/spec/merb/generator_spec.rb +140 -21
- data/spec/merb/handler_spec.rb +5 -5
- data/spec/merb/mail_controller_spec.rb +3 -3
- data/spec/merb/render_spec.rb +1 -1
- data/spec/merb/responder_spec.rb +3 -3
- data/spec/merb/router_spec.rb +260 -191
- data/spec/merb/server_spec.rb +5 -5
- data/spec/merb/upload_handler_spec.rb +7 -0
- data/spec/merb/version_spec.rb +33 -0
- data/spec/merb/view_context_spec.rb +217 -59
- data/spec/spec_generator_helper.rb +15 -0
- data/spec/spec_helper.rb +5 -3
- data/spec/spec_helpers/url_shared_behaviour.rb +5 -7
- metadata +32 -7
- data/lib/merb/caching/store/memcache.rb +0 -20
- data/lib/merb/mixins/form_control.rb +0 -332
- data/lib/patch +0 -69
- data/spec/merb/core_ext_spec.rb +0 -464
- data/spec/merb/form_control_mixin_spec.rb +0 -431
data/spec/spec_helper.rb
CHANGED
@@ -7,18 +7,20 @@ require 'spec'
|
|
7
7
|
require 'mocha'
|
8
8
|
require 'hpricot'
|
9
9
|
$:.push File.join(File.dirname(__FILE__), '..', 'lib')
|
10
|
-
$:.push File.join(File.dirname(__FILE__), '..', 'lib', 'server')
|
10
|
+
$:.push File.join(File.dirname(__FILE__), '..', 'lib', 'merb', 'server')
|
11
|
+
require File.join(File.dirname(__FILE__), '..', 'lib', 'merb', 'config')
|
12
|
+
Merb::Config.setup
|
11
13
|
require 'merb'
|
12
14
|
require 'merb/test/helper'
|
13
15
|
|
14
|
-
FIXTURES = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures'))
|
16
|
+
FIXTURES = File.expand_path(File.join(File.dirname(__FILE__), 'fixtures')) unless defined?(FIXTURES)
|
15
17
|
|
16
18
|
require File.join(File.dirname(__FILE__), "spec_helpers", "url_shared_behaviour")
|
17
19
|
|
18
20
|
Spec::Runner.configure do |config|
|
19
21
|
config.include(Merb::Test::Helper)
|
20
22
|
config.include(Merb::Test::RspecMatchers)
|
21
|
-
config.include(Merb::Test::MerbRspecControllerRedirect)
|
23
|
+
# config.include(Merb::Test::MerbRspecControllerRedirect)
|
22
24
|
end
|
23
25
|
|
24
26
|
# Creates a new controller, e.g.
|
@@ -55,24 +55,22 @@ describe "class with general url generation", :shared => true do
|
|
55
55
|
|
56
56
|
it "should handle an object as the second arg" do
|
57
57
|
c = new_url_controller(@resource_routes, :controller => "blogs", :action => "show")
|
58
|
-
blog =
|
59
|
-
blog.should_receive(:id).once.and_return(7)
|
58
|
+
blog = Blog.new
|
60
59
|
url = c.url(:blog, blog)
|
61
|
-
url.should == "/blogs/
|
60
|
+
url.should == "/blogs/hello42"
|
62
61
|
end
|
63
62
|
|
64
63
|
it "should point to /blogs/:blog_id if @blog is not new_record" do
|
65
64
|
c = new_url_controller(@resource_routes, :controller => "blogs", :action => "index")
|
66
|
-
blog =
|
67
|
-
blog.should_receive(:id).once.and_return(7)
|
65
|
+
blog = Blog.new
|
68
66
|
blog.should_receive(:new_record?).once.and_return(false)
|
69
67
|
url = c.url(:blog, blog)
|
70
|
-
url.should == "/blogs/
|
68
|
+
url.should == "/blogs/hello42"
|
71
69
|
end
|
72
70
|
|
73
71
|
it "should point to /blogs/ if @blog is new_record" do
|
74
72
|
c = new_url_controller(@resource_routes, :controller => "blogs", :action => "index")
|
75
|
-
blog =
|
73
|
+
blog = Blog.new
|
76
74
|
blog.should_receive(:new_record?).once.and_return(true)
|
77
75
|
url = c.url(:blog, blog)
|
78
76
|
url.should == "/blogs/"
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
|
|
3
3
|
specification_version: 1
|
4
4
|
name: merb
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.
|
7
|
-
date:
|
6
|
+
version: 0.5.0
|
7
|
+
date: 2008-01-09 00:00:00 -08:00
|
8
8
|
summary: Merb == Mongrel + Erb. Pocket rocket web framework.
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -33,6 +33,7 @@ files:
|
|
33
33
|
- README
|
34
34
|
- Rakefile
|
35
35
|
- TODO
|
36
|
+
- SVN_REVISION
|
36
37
|
- bin/merb
|
37
38
|
- spec/fixtures
|
38
39
|
- spec/fixtures/config
|
@@ -148,15 +149,25 @@ files:
|
|
148
149
|
- spec/fixtures/views/test.dir/the_template.html.erb
|
149
150
|
- spec/merb
|
150
151
|
- spec/merb/abstract_controller_spec.rb
|
152
|
+
- spec/merb/assets_spec.rb
|
151
153
|
- spec/merb/caching_spec.rb
|
152
154
|
- spec/merb/config_spec.rb
|
153
155
|
- spec/merb/controller_filters_spec.rb
|
154
156
|
- spec/merb/controller_spec.rb
|
155
157
|
- spec/merb/cookie_store_spec.rb
|
156
|
-
- spec/merb/
|
158
|
+
- spec/merb/core_ext
|
159
|
+
- spec/merb/core_ext/class_spec.rb
|
160
|
+
- spec/merb/core_ext/enumerable_spec.rb
|
161
|
+
- spec/merb/core_ext/hash_spec.rb
|
162
|
+
- spec/merb/core_ext/inflector_spec.rb
|
163
|
+
- spec/merb/core_ext/kernel_spec.rb
|
164
|
+
- spec/merb/core_ext/numeric_spec.rb
|
165
|
+
- spec/merb/core_ext/object_spec.rb
|
166
|
+
- spec/merb/core_ext/string_spec.rb
|
167
|
+
- spec/merb/core_ext/symbol_spec.rb
|
168
|
+
- spec/merb/dependency_spec.rb
|
157
169
|
- spec/merb/dispatch_spec.rb
|
158
170
|
- spec/merb/fake_request_spec.rb
|
159
|
-
- spec/merb/form_control_mixin_spec.rb
|
160
171
|
- spec/merb/generator_spec.rb
|
161
172
|
- spec/merb/handler_spec.rb
|
162
173
|
- spec/merb/mail_controller_spec.rb
|
@@ -171,6 +182,7 @@ files:
|
|
171
182
|
- spec/merb/server_spec.rb
|
172
183
|
- spec/merb/template_spec.rb
|
173
184
|
- spec/merb/upload_handler_spec.rb
|
185
|
+
- spec/merb/version_spec.rb
|
174
186
|
- spec/merb/view_context_spec.rb
|
175
187
|
- spec/spec_generator_helper.rb
|
176
188
|
- spec/spec_helper.rb
|
@@ -181,14 +193,16 @@ files:
|
|
181
193
|
- lib/autotest/merb_rspec.rb
|
182
194
|
- lib/merb
|
183
195
|
- lib/merb/abstract_controller.rb
|
196
|
+
- lib/merb/assets.rb
|
197
|
+
- lib/merb/boot_loader.rb
|
184
198
|
- lib/merb/caching
|
185
199
|
- lib/merb/caching/action_cache.rb
|
186
200
|
- lib/merb/caching/fragment_cache.rb
|
187
201
|
- lib/merb/caching/store
|
188
202
|
- lib/merb/caching/store/file_cache.rb
|
189
|
-
- lib/merb/caching/store/memcache.rb
|
190
203
|
- lib/merb/caching/store/memory_cache.rb
|
191
204
|
- lib/merb/caching.rb
|
205
|
+
- lib/merb/config.rb
|
192
206
|
- lib/merb/constants.rb
|
193
207
|
- lib/merb/controller.rb
|
194
208
|
- lib/merb/core_ext
|
@@ -223,8 +237,8 @@ files:
|
|
223
237
|
- lib/merb/mixins/basic_authentication.rb
|
224
238
|
- lib/merb/mixins/controller.rb
|
225
239
|
- lib/merb/mixins/erubis_capture.rb
|
226
|
-
- lib/merb/mixins/form_control.rb
|
227
240
|
- lib/merb/mixins/general_controller.rb
|
241
|
+
- lib/merb/mixins/inline_partial.rb
|
228
242
|
- lib/merb/mixins/render.rb
|
229
243
|
- lib/merb/mixins/responder.rb
|
230
244
|
- lib/merb/mixins/view_context.rb
|
@@ -253,12 +267,14 @@ files:
|
|
253
267
|
- lib/merb/test/hpricot.rb
|
254
268
|
- lib/merb/test/multipart.rb
|
255
269
|
- lib/merb/test/rspec.rb
|
270
|
+
- lib/merb/test/rspec_matchers
|
271
|
+
- lib/merb/test/rspec_matchers/controller_matchers.rb
|
272
|
+
- lib/merb/test/rspec_matchers/markup_matchers.rb
|
256
273
|
- lib/merb/upload_handler.rb
|
257
274
|
- lib/merb/upload_progress.rb
|
258
275
|
- lib/merb/version.rb
|
259
276
|
- lib/merb/view_context.rb
|
260
277
|
- lib/merb.rb
|
261
|
-
- lib/patch
|
262
278
|
- lib/tasks
|
263
279
|
- lib/tasks/merb.rake
|
264
280
|
- lib/tasks.rb
|
@@ -336,6 +352,13 @@ files:
|
|
336
352
|
- merb_generators/controller/templates/helper.rb
|
337
353
|
- merb_generators/controller/templates/index.html.erb
|
338
354
|
- merb_generators/controller/USAGE
|
355
|
+
- merb_generators/part_controller
|
356
|
+
- merb_generators/part_controller/part_controller_generator.rb
|
357
|
+
- merb_generators/part_controller/templates
|
358
|
+
- merb_generators/part_controller/templates/controller.rb
|
359
|
+
- merb_generators/part_controller/templates/helper.rb
|
360
|
+
- merb_generators/part_controller/templates/index.html.erb
|
361
|
+
- merb_generators/part_controller/USAGE
|
339
362
|
- merb_generators/resource
|
340
363
|
- merb_generators/resource/resource_generator.rb
|
341
364
|
- merb_generators/resource/USAGE
|
@@ -376,6 +399,8 @@ files:
|
|
376
399
|
- test_unit_generators/merb_model_test/merb_model_test_generator.rb
|
377
400
|
- test_unit_generators/merb_model_test/templates
|
378
401
|
- test_unit_generators/merb_model_test/templates/model_test_unit_template.erb
|
402
|
+
- script/destroy
|
403
|
+
- script/generate
|
379
404
|
test_files: []
|
380
405
|
|
381
406
|
rdoc_options: []
|
@@ -1,20 +0,0 @@
|
|
1
|
-
module Merb
|
2
|
-
module Caching
|
3
|
-
module MemcachedStore
|
4
|
-
|
5
|
-
|
6
|
-
def get(name)
|
7
|
-
::Cache.get("fragment:#{name}")
|
8
|
-
end
|
9
|
-
|
10
|
-
def put(name, content = nil)
|
11
|
-
::Cache.put("fragment:#{name}", content)
|
12
|
-
content
|
13
|
-
end
|
14
|
-
|
15
|
-
def expire_fragment(name)
|
16
|
-
::Cache.delete(name)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
@@ -1,332 +0,0 @@
|
|
1
|
-
require 'date'
|
2
|
-
require 'ostruct'
|
3
|
-
|
4
|
-
class OpenStruct
|
5
|
-
def temp(hash)
|
6
|
-
OpenStruct.new(@table.merge(hash))
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
module Merb
|
11
|
-
module FormControls
|
12
|
-
#
|
13
|
-
# This is the main merb form control helper.
|
14
|
-
# The types that can be rendered are
|
15
|
-
#
|
16
|
-
# text => A standard textfield
|
17
|
-
# textarea => A textarea
|
18
|
-
# password => A password field
|
19
|
-
# date => A select menu with day, month and year
|
20
|
-
# time => As date + hour, minute and second
|
21
|
-
# select => A select menu for a custom collection in Array or Hash
|
22
|
-
#
|
23
|
-
#
|
24
|
-
# HTML-formatting and some control-options can be given in +options+
|
25
|
-
#
|
26
|
-
# === Defaults
|
27
|
-
# id => obj.class (class_name_in_camel_case)
|
28
|
-
# name => obj.class[meth] (class_name_in_camel_case[control_method])
|
29
|
-
# value => The value of obj.meth
|
30
|
-
#
|
31
|
-
# ==== Options
|
32
|
-
# +HTML+, +DOM+ and +CSS+
|
33
|
-
# :class, :size, :rows, :cols, :name, ...
|
34
|
-
#
|
35
|
-
# +label+
|
36
|
-
# Setting this label will include a label tag pointing at the field that
|
37
|
-
# displays the value of :label in the options hash
|
38
|
-
#
|
39
|
-
#
|
40
|
-
# ==== Examples
|
41
|
-
# The bare minimum
|
42
|
-
# <%= control_for @post, :title, :text%>
|
43
|
-
# renders a textfield for @post.title
|
44
|
-
#
|
45
|
-
# <%= control_for @post, :content, :textarea %>
|
46
|
-
# renders a textarea for @post.content
|
47
|
-
#
|
48
|
-
# <%= control_for @post, :secret, :password %>
|
49
|
-
# renders a password-field for @post.secret
|
50
|
-
#
|
51
|
-
# Some HTML and CSS options
|
52
|
-
# <%= control_for @post, :title, :text,
|
53
|
-
# :id => 'foo', :size => 53 %>
|
54
|
-
#
|
55
|
-
# <%= control_for @post, :content, :textarea,
|
56
|
-
# :class => 'post_intro', :rows => 10, :cols => 50 %>
|
57
|
-
#
|
58
|
-
#
|
59
|
-
# === Time and date
|
60
|
-
#
|
61
|
-
# +monthnames+
|
62
|
-
# for time and date selects, shows monthnames as text.
|
63
|
-
# An array of monthnames can be supplied
|
64
|
-
# (defaults to Date::MONTHNAMES)
|
65
|
-
#
|
66
|
-
# +min_year+ and +max_year+
|
67
|
-
# for time and date selects, sets the first and last year
|
68
|
-
# (defaults to 1950 and 2050)
|
69
|
-
#
|
70
|
-
# ==== Examples
|
71
|
-
#
|
72
|
-
# Simple time and date
|
73
|
-
# <%= control_for @post, :created_at, :time %>
|
74
|
-
# <%= control_for @post, :published_at, :date %>
|
75
|
-
#
|
76
|
-
# In time and date controls, monthnames can be added
|
77
|
-
# <%= control_for @post, :created_at, :time, :monthnames => true %>
|
78
|
-
#
|
79
|
-
# You can also specify other month-names
|
80
|
-
# <%= control_for @post, :published_at, :date,
|
81
|
-
# :monthnames => Date::ABBR_MONTHNAMES %>
|
82
|
-
# or
|
83
|
-
# french_months = [nil] + %w(janvier février mars avril mai juin
|
84
|
-
# juillet août septembre octobre novembre décembre)
|
85
|
-
#
|
86
|
-
# <%= control_for @post, :published_at, :date,
|
87
|
-
# {:monthnames => french_months} %>
|
88
|
-
#
|
89
|
-
# In time and date controls, :min_year and :max_year can be specified.
|
90
|
-
# Defaults to 1950..2050
|
91
|
-
# <%= control_for @post, :created_at, :time,
|
92
|
-
# :min_year => 2000, :max_year => 2010 %>
|
93
|
-
#
|
94
|
-
# <%= control_for @post, :published_at, :date,
|
95
|
-
# :min_year => Date.today.year, :max_year => Date.today.year+2 %>
|
96
|
-
#
|
97
|
-
#
|
98
|
-
# === Select Tags
|
99
|
-
# The control_for( object, :method, :select, {:collection => collection} )
|
100
|
-
# creates a <select> tag that automatically selects the given object
|
101
|
-
#
|
102
|
-
# === Special Options
|
103
|
-
# +collection+
|
104
|
-
# The collection can be an array of objects, or a hash of arrays of objects.
|
105
|
-
# This is required if you want any options to be displayed
|
106
|
-
#
|
107
|
-
# +text_method+
|
108
|
-
# The method that will provide the text for the option to display to the user.
|
109
|
-
# By default it will use the control method
|
110
|
-
#
|
111
|
-
# +include_blank+
|
112
|
-
# Includes a blank option at the top of the list if set to true
|
113
|
-
#
|
114
|
-
# All options provided to the call to control_for are rendered as xml/html tag attributes
|
115
|
-
#
|
116
|
-
# ==== Examples
|
117
|
-
#
|
118
|
-
# Imagine cars, with a brand (BMW, Toyota, ...) a model
|
119
|
-
# (Z3, Carina, Prius) and some internal code.
|
120
|
-
#
|
121
|
-
# class Car
|
122
|
-
# ...
|
123
|
-
# attr_reader :brand, :model, :code
|
124
|
-
# end
|
125
|
-
#
|
126
|
-
# An array of objects
|
127
|
-
# @all_cars = [ car1, car2, car3 ]
|
128
|
-
#
|
129
|
-
# <%= control_for @car2, :code, :select,
|
130
|
-
# {:text_method => :model, :collection => @all_cars } %>
|
131
|
-
#
|
132
|
-
# <select name="car[code]" id="car_code">
|
133
|
-
# <option value="code_for_car_1">Z3</option>
|
134
|
-
# <option selected="selected" value="code_for_car_2">Carina</option>
|
135
|
-
# <option value="code_for_car_3">Prius</option>
|
136
|
-
# </select>
|
137
|
-
#
|
138
|
-
# The same array of cars but run through a group_by on :brand to give a hash
|
139
|
-
#
|
140
|
-
# @all_cars = @all_cars.group_by{|car| car.brand }
|
141
|
-
#
|
142
|
-
# { :bmw => [car1],
|
143
|
-
# :toyota => [car2, car3]
|
144
|
-
# }
|
145
|
-
#
|
146
|
-
# <%= control_for @car2, :code, :select,
|
147
|
-
# {:text_method => :model, :collection => @all_cars } %>
|
148
|
-
#
|
149
|
-
# <select name="car[code]" id="car_code">
|
150
|
-
# <optgroup label="BMW">
|
151
|
-
# <option value="code_for_car_1">Z3</option>
|
152
|
-
# </optgroup>
|
153
|
-
# <optgroup label="Toyota">
|
154
|
-
# <option selected="selected" value="code_for_car_2">Carina</option>
|
155
|
-
# <option value="code_for_car_3">Prius</option>
|
156
|
-
# </optgroup>
|
157
|
-
# </select>
|
158
|
-
#
|
159
|
-
|
160
|
-
def control_for( obj, meth, type, opts = {} )
|
161
|
-
instance = obj
|
162
|
-
obj = obj.class
|
163
|
-
# FooBar => foo_bar
|
164
|
-
# Admin::FooBar => admin_foo_bar
|
165
|
-
obj_dom_id = Inflector.underscore(obj.to_s).gsub('/', '_')
|
166
|
-
default_opts = {
|
167
|
-
# These are in here to make sure that they are set. They can be overridden if the user wants to.
|
168
|
-
:id => "#{obj_dom_id}_#{meth}",
|
169
|
-
:name => "#{obj_dom_id}[#{meth}]",
|
170
|
-
:value => (instance.send(meth) rescue nil) || ""
|
171
|
-
}
|
172
|
-
o = OpenStruct.new(
|
173
|
-
:value => default_opts[:value], # Just for convenience
|
174
|
-
:label => ( opts.has_key?( :label ) ? opts.delete( :label ) : nil ),
|
175
|
-
:monthnames => (if opts.has_key?(:monthnames)
|
176
|
-
opts[:monthnames]==true ? Date::MONTHNAMES : opts.delete(:monthnames)
|
177
|
-
else
|
178
|
-
nil
|
179
|
-
end),
|
180
|
-
:meth => meth,
|
181
|
-
:obj => instance,
|
182
|
-
:min_year => (opts.has_key?(:min_year) ? opts.delete(:min_year) : 1950),
|
183
|
-
:max_year => (opts.has_key?(:max_year) ? opts.delete(:max_year) : 2050),
|
184
|
-
:html => default_opts.merge(opts))
|
185
|
-
Control.send(type, o)
|
186
|
-
end
|
187
|
-
|
188
|
-
module Control
|
189
|
-
|
190
|
-
# This was ripped wholesale from Ramaze and has had some fairly major modifications to work
|
191
|
-
# with AR objects instead of Og. Thanks again to Michael Fellinger.
|
192
|
-
class << self
|
193
|
-
|
194
|
-
def number(o)
|
195
|
-
o.value = o.html[:value] = 0 if o.value == ""
|
196
|
-
text(o)
|
197
|
-
end
|
198
|
-
|
199
|
-
def hidden(o)
|
200
|
-
input_tag( o.html.merge( :type => 'hidden' ) )
|
201
|
-
end
|
202
|
-
|
203
|
-
def text(o)
|
204
|
-
tag = ''
|
205
|
-
tag << label_for_object( o )
|
206
|
-
tag << input_tag( o.html.merge( :type => 'text' ) )
|
207
|
-
end
|
208
|
-
|
209
|
-
def password(o)
|
210
|
-
o.html.delete( :value ) if o.html.has_key?( :value ) # The password field should not be filled in
|
211
|
-
tag = ''
|
212
|
-
tag << label_for_object( o )
|
213
|
-
tag << input_tag( o.html.merge( :type => 'password' ) )
|
214
|
-
end
|
215
|
-
|
216
|
-
def textarea(o)
|
217
|
-
tag = ''
|
218
|
-
tag << label_for_object( o )
|
219
|
-
tag << %{<textarea #{o.html.to_xml_attributes }>#{o.value}</textarea>}
|
220
|
-
end
|
221
|
-
|
222
|
-
def date(o)
|
223
|
-
o.value = Date.today unless (o.value.is_a? Time or o.value.is_a? Date or o.value.is_a? DateTime)
|
224
|
-
selects = []
|
225
|
-
selects << label_for_object( o )
|
226
|
-
selects << date_day(o.temp(:value => o.value.day))
|
227
|
-
selects << date_month(o.temp(:value => o.value.month))
|
228
|
-
selects << date_year(o.temp(:value => o.value.year))
|
229
|
-
selects.join("\n")
|
230
|
-
end
|
231
|
-
|
232
|
-
def time(o)
|
233
|
-
o.value = Time.now unless (o.value.is_a? Time or o.value.is_a? DateTime)
|
234
|
-
selects = []
|
235
|
-
selects << label_for_object( o )
|
236
|
-
selects << date_day(o.temp(:value => o.value.day))
|
237
|
-
selects << date_month(o.temp(:value => o.value.month))
|
238
|
-
selects << date_year(o.temp(:value => o.value.year))
|
239
|
-
selects << time_hour(o.temp(:value => o.value.hour))
|
240
|
-
selects << time_minute(o.temp(:value => o.value.min))
|
241
|
-
selects << time_second(o.temp(:value => o.value.sec))
|
242
|
-
selects.join("\n")
|
243
|
-
end
|
244
|
-
|
245
|
-
def time_second(o) select_tag(o.html[:name] +'[second]', (0...60),o.value) end
|
246
|
-
def time_minute(o) select_tag(o.html[:name] +'[minute]', (0...60),o.value) end
|
247
|
-
def time_hour(o) select_tag(o.html[:name] +'[hour]', (0...24),o.value) end
|
248
|
-
def date_day(o) select_tag(o.html[:name] +'[day]', (1..31),o.value) end
|
249
|
-
def date_month(o) select_tag(o.html[:name] +'[month]', (1..12),o.value, ( o.monthnames.compact unless o.monthnames.nil? )) end
|
250
|
-
def date_year(o) select_tag(o.html[:name] +'[year]', (o.min_year..o.max_year),o.value) end
|
251
|
-
|
252
|
-
def select( o )
|
253
|
-
options = {}
|
254
|
-
[:collection, :text_method, :include_blank].each do |value|
|
255
|
-
options[value] = o.html.has_key?( value ) ? o.html.delete( value ) : nil
|
256
|
-
end
|
257
|
-
out = ""
|
258
|
-
out << label_for_object( o )
|
259
|
-
out << %{<select #{o.html.to_xml_attributes }>}
|
260
|
-
out << %{#{options_for_select( o.obj, o.meth, options )}}
|
261
|
-
out << %{</select>}
|
262
|
-
end
|
263
|
-
|
264
|
-
# Creates an input tag with the given +option+ hash as xml/html attributes
|
265
|
-
def input_tag( options )
|
266
|
-
%{<input #{options.to_xml_attributes }/>}
|
267
|
-
end
|
268
|
-
|
269
|
-
# Creates an select tag that is not nesiscarily bound to an objects value
|
270
|
-
# === Options
|
271
|
-
# +name+ The name of the select tag
|
272
|
-
# +range+ A range or array that specifies the values of the options
|
273
|
-
# +default+ The default value. This will be selected if present
|
274
|
-
# +txt+ A parrallel array of text values
|
275
|
-
def select_tag(name, range, default, txt = nil)
|
276
|
-
out = %{<select name="#{name}">\n}
|
277
|
-
range.each_with_index do |value, index |
|
278
|
-
out << option_for_select( value, (txt ? txt[index] : value ), (default == value ) )
|
279
|
-
end
|
280
|
-
out << "</select>\n"
|
281
|
-
end
|
282
|
-
|
283
|
-
protected
|
284
|
-
|
285
|
-
# Creates a label from the openstruct created in control_for
|
286
|
-
def label_for_object( o )
|
287
|
-
o.label.nil? ? "" : %{<label for="#{o.html[:id]}">#{o.label}</label>}
|
288
|
-
end
|
289
|
-
|
290
|
-
# The gateway to creating options for a select box
|
291
|
-
def options_for_select( obj, value_method, options )
|
292
|
-
text_method = options[:text_method] || value_method
|
293
|
-
collection = options[:collection] || []
|
294
|
-
|
295
|
-
out = ""
|
296
|
-
out = "<option></option>" if options[:include_blank]
|
297
|
-
out << case collection
|
298
|
-
when Array
|
299
|
-
options_for_select_from_array( obj, collection, value_method, text_method )
|
300
|
-
when Hash
|
301
|
-
options_for_select_from_hash( obj, collection, value_method, text_method )
|
302
|
-
end
|
303
|
-
end
|
304
|
-
|
305
|
-
def options_for_select_from_array( selected_object, collection, value_method, text_method )
|
306
|
-
out = ""
|
307
|
-
collection.each do | element|
|
308
|
-
out << option_for_select( element.send( value_method ), element.send( text_method ), (selected_object == element) )
|
309
|
-
end
|
310
|
-
out
|
311
|
-
end
|
312
|
-
|
313
|
-
def options_for_select_from_hash( selected_object, collection, value_method, text_method )
|
314
|
-
out = ""
|
315
|
-
collection.keys.sort.each do |key|
|
316
|
-
out << %{<optgroup label="#{key.to_s.humanize.titleize}">}
|
317
|
-
out << options_for_select_from_array( selected_object, collection[key], value_method, text_method )
|
318
|
-
out << %{</optgroup>}
|
319
|
-
end
|
320
|
-
out
|
321
|
-
end
|
322
|
-
|
323
|
-
# Creates that actual option tag for any given value, text and selected (true/false) combination
|
324
|
-
def option_for_select( value, text, selected )
|
325
|
-
out = %{<option#{ selected ? " selected=\"selected\"" : nil } }
|
326
|
-
out << %{value="#{value}">#{text}</option>}
|
327
|
-
end
|
328
|
-
end
|
329
|
-
|
330
|
-
end # Control
|
331
|
-
end # FormHelper
|
332
|
-
end # Merb
|