merb 0.3.7 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (173) hide show
  1. data/README +25 -26
  2. data/Rakefile +48 -36
  3. data/app_generators/merb/USAGE +5 -0
  4. data/app_generators/merb/merb_generator.rb +107 -0
  5. data/app_generators/merb/templates/Rakefile +99 -0
  6. data/{examples/skeleton/dist → app_generators/merb/templates}/app/controllers/application.rb +1 -1
  7. data/app_generators/merb/templates/app/controllers/exceptions.rb +13 -0
  8. data/{examples/skeleton/dist → app_generators/merb/templates}/app/helpers/global_helper.rb +0 -0
  9. data/{examples/skeleton/dist/app/mailers → app_generators/merb/templates/app/mailers/views}/layout/application.erb +0 -0
  10. data/app_generators/merb/templates/app/views/exceptions/internal_server_error.html.erb +207 -0
  11. data/app_generators/merb/templates/app/views/exceptions/not_acceptable.html.erb +38 -0
  12. data/app_generators/merb/templates/app/views/exceptions/not_found.html.erb +40 -0
  13. data/app_generators/merb/templates/app/views/layout/application.html.erb +11 -0
  14. data/app_generators/merb/templates/config/boot.rb +11 -0
  15. data/app_generators/merb/templates/config/dependencies.rb +41 -0
  16. data/{examples/skeleton/dist/conf → app_generators/merb/templates/config}/environments/development.rb +0 -0
  17. data/{examples/skeleton/dist/conf → app_generators/merb/templates/config}/environments/production.rb +0 -0
  18. data/{examples/skeleton/dist/conf → app_generators/merb/templates/config}/environments/test.rb +0 -0
  19. data/app_generators/merb/templates/config/merb.yml +64 -0
  20. data/app_generators/merb/templates/config/merb_init.rb +16 -0
  21. data/app_generators/merb/templates/config/plugins.yml +1 -0
  22. data/app_generators/merb/templates/config/router.rb +32 -0
  23. data/{lib/merb/core_ext/merb_array.rb → app_generators/merb/templates/config/upload.conf} +0 -0
  24. data/app_generators/merb/templates/public/images/merb.jpg +0 -0
  25. data/app_generators/merb/templates/public/merb.fcgi +6 -0
  26. data/app_generators/merb/templates/public/stylesheets/master.css +119 -0
  27. data/app_generators/merb/templates/script/destroy +28 -0
  28. data/app_generators/merb/templates/script/generate +28 -0
  29. data/{examples/skeleton → app_generators/merb/templates}/script/stop_merb +0 -0
  30. data/app_generators/merb/templates/script/win_script.cmd +1 -0
  31. data/app_generators/merb/templates/spec/spec.opts +6 -0
  32. data/app_generators/merb/templates/spec/spec_helper.rb +10 -0
  33. data/app_generators/merb/templates/test/test_helper.rb +13 -0
  34. data/app_generators/merb_plugin/USAGE +5 -0
  35. data/app_generators/merb_plugin/merb_plugin_generator.rb +64 -0
  36. data/app_generators/merb_plugin/templates/LICENSE +20 -0
  37. data/app_generators/merb_plugin/templates/README +4 -0
  38. data/app_generators/merb_plugin/templates/Rakefile +35 -0
  39. data/app_generators/merb_plugin/templates/TODO +5 -0
  40. data/app_generators/merb_plugin/templates/merbtasks.rb +6 -0
  41. data/app_generators/merb_plugin/templates/sampleplugin.rb +10 -0
  42. data/app_generators/merb_plugin/templates/sampleplugin_spec.rb +7 -0
  43. data/app_generators/merb_plugin/templates/spec_helper.rb +2 -0
  44. data/bin/merb +1 -1
  45. data/lib/autotest/discover.rb +3 -0
  46. data/lib/autotest/merb_rspec.rb +79 -0
  47. data/lib/merb.rb +72 -93
  48. data/lib/merb/{merb_abstract_controller.rb → abstract_controller.rb} +28 -5
  49. data/lib/merb/caching/action_cache.rb +65 -29
  50. data/lib/merb/caching/fragment_cache.rb +9 -4
  51. data/lib/merb/caching/store/file_cache.rb +22 -14
  52. data/lib/merb/caching/store/memory_cache.rb +26 -8
  53. data/lib/merb/{merb_constants.rb → constants.rb} +9 -7
  54. data/lib/merb/controller.rb +178 -0
  55. data/lib/merb/core_ext.rb +13 -11
  56. data/lib/merb/core_ext/array.rb +0 -0
  57. data/lib/merb/core_ext/{merb_class.rb → class.rb} +0 -0
  58. data/lib/merb/core_ext/{merb_enumerable.rb → enumerable.rb} +0 -0
  59. data/lib/merb/core_ext/get_args.rb +52 -0
  60. data/lib/merb/core_ext/{merb_hash.rb → hash.rb} +40 -11
  61. data/lib/merb/core_ext/{merb_inflections.rb → inflections.rb} +0 -0
  62. data/lib/merb/core_ext/{merb_inflector.rb → inflector.rb} +1 -1
  63. data/lib/merb/core_ext/{merb_kernel.rb → kernel.rb} +56 -3
  64. data/lib/merb/core_ext/mash.rb +88 -0
  65. data/lib/merb/core_ext/{merb_module.rb → module.rb} +0 -0
  66. data/lib/merb/core_ext/{merb_numeric.rb → numeric.rb} +0 -0
  67. data/lib/merb/core_ext/{merb_object.rb → object.rb} +10 -47
  68. data/lib/merb/core_ext/string.rb +56 -0
  69. data/lib/merb/core_ext/{merb_symbol.rb → symbol.rb} +0 -0
  70. data/lib/merb/dispatcher.rb +109 -0
  71. data/lib/merb/{merb_drb_server.rb → drb_server.rb} +0 -0
  72. data/lib/merb/erubis_ext.rb +10 -0
  73. data/lib/merb/exceptions.rb +173 -0
  74. data/lib/merb/generators/merb_app/merb_app.rb +5 -25
  75. data/lib/merb/generators/merb_generator_helpers.rb +317 -0
  76. data/lib/merb/generators/merb_plugin.rb +19 -0
  77. data/lib/merb/logger.rb +65 -0
  78. data/lib/merb/{merb_mail_controller.rb → mail_controller.rb} +102 -49
  79. data/lib/merb/{merb_mailer.rb → mailer.rb} +31 -27
  80. data/lib/merb/mixins/{basic_authentication_mixin.rb → basic_authentication.rb} +3 -3
  81. data/lib/merb/mixins/{controller_mixin.rb → controller.rb} +131 -112
  82. data/lib/merb/mixins/{erubis_capture_mixin.rb → erubis_capture.rb} +12 -21
  83. data/lib/merb/mixins/{form_control_mixin.rb → form_control.rb} +6 -12
  84. data/lib/merb/mixins/render.rb +401 -0
  85. data/lib/merb/mixins/responder.rb +378 -0
  86. data/lib/merb/mixins/{view_context_mixin.rb → view_context.rb} +65 -10
  87. data/lib/merb/mixins/web_controller.rb +29 -0
  88. data/lib/merb/{merb_handler.rb → mongrel_handler.rb} +59 -38
  89. data/lib/merb/part_controller.rb +19 -0
  90. data/lib/merb/plugins.rb +16 -0
  91. data/lib/merb/rack_adapter.rb +37 -0
  92. data/lib/merb/request.rb +421 -0
  93. data/lib/merb/router.rb +576 -0
  94. data/lib/merb/{merb_server.rb → server.rb} +275 -71
  95. data/lib/merb/session.rb +10 -10
  96. data/lib/merb/session/cookie_store.rb +125 -0
  97. data/lib/merb/session/{merb_mem_cache_session.rb → mem_cache_session.rb} +22 -9
  98. data/lib/merb/session/{merb_memory_session.rb → memory_session.rb} +15 -11
  99. data/lib/merb/template.rb +35 -8
  100. data/lib/merb/template/erubis.rb +16 -10
  101. data/lib/merb/template/haml.rb +33 -20
  102. data/lib/merb/template/markaby.rb +16 -14
  103. data/lib/merb/template/xml_builder.rb +8 -4
  104. data/lib/merb/test/{merb_fake_request.rb → fake_request.rb} +11 -5
  105. data/lib/merb/test/helper.rb +31 -0
  106. data/lib/merb/test/hpricot.rb +136 -0
  107. data/lib/merb/test/{merb_multipart.rb → multipart.rb} +1 -1
  108. data/lib/merb/test/rspec.rb +93 -0
  109. data/lib/merb/{merb_upload_handler.rb → upload_handler.rb} +5 -6
  110. data/lib/merb/{merb_upload_progress.rb → upload_progress.rb} +1 -1
  111. data/lib/merb/{merb_view_context.rb → view_context.rb} +27 -42
  112. data/lib/{merb_tasks.rb → tasks.rb} +0 -0
  113. data/lib/tasks/merb.rake +21 -11
  114. data/merb_default_generators/model/USAGE +0 -0
  115. data/merb_default_generators/model/model_generator.rb +16 -0
  116. data/merb_default_generators/model/templates/new_model_template.erb +5 -0
  117. data/merb_default_generators/resource_controller/USAGE +0 -0
  118. data/merb_default_generators/resource_controller/resource_controller_generator.rb +26 -0
  119. data/merb_default_generators/resource_controller/templates/controller.rb +30 -0
  120. data/merb_default_generators/resource_controller/templates/edit.html.erb +1 -0
  121. data/merb_default_generators/resource_controller/templates/helper.rb +5 -0
  122. data/merb_default_generators/resource_controller/templates/index.html.erb +1 -0
  123. data/merb_default_generators/resource_controller/templates/new.html.erb +1 -0
  124. data/merb_default_generators/resource_controller/templates/show.html.erb +1 -0
  125. data/merb_generators/controller/USAGE +5 -0
  126. data/merb_generators/controller/controller_generator.rb +16 -0
  127. data/merb_generators/controller/templates/controller.rb +8 -0
  128. data/merb_generators/controller/templates/helper.rb +5 -0
  129. data/merb_generators/controller/templates/index.html.erb +3 -0
  130. data/merb_generators/resource/USAGE +0 -0
  131. data/merb_generators/resource/resource_generator.rb +60 -0
  132. data/rspec_generators/merb_controller_test/merb_controller_test_generator.rb +67 -0
  133. data/rspec_generators/merb_controller_test/templates/controller_spec.rb +8 -0
  134. data/rspec_generators/merb_controller_test/templates/edit_spec.rb +12 -0
  135. data/rspec_generators/merb_controller_test/templates/helper_spec.rb +5 -0
  136. data/rspec_generators/merb_controller_test/templates/index_spec.rb +12 -0
  137. data/rspec_generators/merb_controller_test/templates/new_spec.rb +12 -0
  138. data/rspec_generators/merb_controller_test/templates/show_spec.rb +5 -0
  139. data/rspec_generators/merb_model_test/merb_model_test_generator.rb +26 -0
  140. data/rspec_generators/merb_model_test/templates/model_spec_template.erb +7 -0
  141. data/script/destroy +14 -0
  142. data/script/generate +14 -0
  143. data/test_unit_generators/merb_controller_test/merb_controller_test_generator.rb +53 -0
  144. data/test_unit_generators/merb_controller_test/templates/functional_test.rb +17 -0
  145. data/test_unit_generators/merb_controller_test/templates/helper_test.rb +9 -0
  146. data/test_unit_generators/merb_model_test/merb_model_test_generator.rb +29 -0
  147. data/test_unit_generators/merb_model_test/templates/model_test_unit_template.erb +9 -0
  148. metadata +172 -94
  149. data/examples/README_EXAMPLES +0 -10
  150. data/examples/skeleton/Rakefile +0 -68
  151. data/examples/skeleton/dist/app/views/layout/application.herb +0 -12
  152. data/examples/skeleton/dist/conf/database.yml +0 -23
  153. data/examples/skeleton/dist/conf/merb.yml +0 -57
  154. data/examples/skeleton/dist/conf/merb_init.rb +0 -24
  155. data/examples/skeleton/dist/conf/router.rb +0 -22
  156. data/examples/skeleton/dist/conf/upload.conf +0 -5
  157. data/examples/skeleton/dist/schema/migrations/001_add_sessions_table.rb +0 -14
  158. data/examples/skeleton/script/new_migration +0 -21
  159. data/lib/merb/core_ext/merb_string.rb +0 -18
  160. data/lib/merb/merb_controller.rb +0 -206
  161. data/lib/merb/merb_dispatcher.rb +0 -87
  162. data/lib/merb/merb_exceptions.rb +0 -319
  163. data/lib/merb/merb_part_controller.rb +0 -42
  164. data/lib/merb/merb_plugins.rb +0 -293
  165. data/lib/merb/merb_request.rb +0 -165
  166. data/lib/merb/merb_router.rb +0 -309
  167. data/lib/merb/merb_yaml_store.rb +0 -31
  168. data/lib/merb/mixins/render_mixin.rb +0 -283
  169. data/lib/merb/mixins/responder_mixin.rb +0 -159
  170. data/lib/merb/session/merb_ar_session.rb +0 -131
  171. data/lib/merb/vendor/paginator/README.txt +0 -84
  172. data/lib/merb/vendor/paginator/paginator.rb +0 -124
  173. data/lib/tasks/db.rake +0 -55
@@ -1,10 +0,0 @@
1
- I have removed the useless example app from here for now.
2
- PLease see mrblog for a better sample app:
3
-
4
- svn co http://svn.devjavu.com/merb/mrblog/trunk
5
-
6
- To generate your own new merb app first install the gem and then:
7
-
8
- $ merb -g appname
9
- or
10
- $ merb --generate-app appname
@@ -1,68 +0,0 @@
1
- require 'rake'
2
- require 'rake/rdoctask'
3
- require 'rake/testtask'
4
- require 'fileutils'
5
- require 'rubygems'
6
- if File.directory?( File.join(File.dirname(__FILE__), "dist/framework"))
7
- require File.join(File.dirname(__FILE__), "dist/framework/merb")
8
- else
9
- require 'merb'
10
- end
11
- require MERB_FRAMEWORK_ROOT+'/merb_tasks'
12
- MERB_ROOT = File.dirname(__FILE__)
13
- include FileUtils
14
-
15
-
16
- #desc "Packages up Merb."
17
- #task :default => [:package]
18
-
19
- desc "load merb_init.rb"
20
- task :merb_init do
21
- require 'merb'
22
- require File.dirname(__FILE__)+'/dist/conf/merb_init.rb'
23
- end
24
-
25
- desc "Load db schema"
26
- task :load_schema => [:merb_init] do
27
- require File.dirname(__FILE__)+'/dist/schema/schema.rb'
28
- end
29
-
30
- task :uninstall => [:clean] do
31
- sh %{sudo gem uninstall #{NAME}}
32
- end
33
-
34
- desc 'Run unit tests'
35
- Rake::TestTask.new('test_unit') do |t|
36
- t.libs << 'test'
37
- t.pattern = 'test/unit/*_test.rb'
38
- t.verbose = true
39
- end
40
-
41
- desc 'Run functional tests'
42
- Rake::TestTask.new('test_functional') do |t|
43
- t.libs << 'test'
44
- t.pattern = 'test/functional/*_test.rb'
45
- t.verbose = true
46
- end
47
-
48
- desc 'Run all tests'
49
- Rake::TestTask.new('test') do |t|
50
- t.libs << 'test'
51
- t.pattern = 'test/**/*_test.rb'
52
- t.verbose = true
53
- end
54
-
55
- desc 'Run all tests, specs and finish with rcov'
56
- task :aok do
57
- sh %{rake rcov}
58
- sh %{rake spec}
59
- end
60
-
61
- ##############################################################################
62
- # SVN
63
- ##############################################################################
64
-
65
- desc "Add new files to subversion"
66
- task :svn_add do
67
- system "svn status | grep '^\?' | sed -e 's/? *//' | sed -e 's/ /\ /g' | xargs svn add"
68
- end
@@ -1,12 +0,0 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
- <html xmlns="http://www.w3.org/1999/xhtml">
3
- <head>
4
- <title>Fresh Merb App</title>
5
- <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
6
- </head>
7
- <body>
8
- <div id='content'>
9
- <%= catch_content :layout %>
10
- </div>
11
- </body>
12
- </html>
@@ -1,23 +0,0 @@
1
- development:
2
- adapter: mysql
3
- database: sample_development
4
- username: teh_user
5
- password: secrets
6
- host: localhost
7
- socket: /tmp/mysql.sock
8
-
9
- test:
10
- adapter: mysql
11
- database: sample_test
12
- username: teh_user
13
- password: secrets
14
- host: localhost
15
- socket: /tmp/mysql.sock
16
-
17
- production:
18
- adapter: mysql
19
- database: sample_production
20
- username: teh_user
21
- password: secrets
22
- host: /tmp/mysql.sock
23
-
@@ -1,57 +0,0 @@
1
- ---
2
- # Hostname or IP address to bind to.
3
- :host: 127.0.0.1
4
-
5
- # Port merb runs on or starting port for merb cluster.
6
- :port: "4000"
7
-
8
- # In development mode your controller classes get reloaded on every request,
9
- # and templates are parsed each time and not cached. In production mode
10
- # templates are cached, as well as all your classes
11
- :environment: development
12
-
13
- # Uncomment for memory sessions. This only works when you are running 1 merb
14
- # at a time. And sessions do not persist between restarts.
15
- # :memory_session: true
16
-
17
- # Turn on memcached sessions.
18
- # Requires these lines in merb_init.rb (and a running memcached server):
19
- # require 'memcache_util'
20
- # CACHE = MemCache.new('127.0.0.1:11211', { :namespace => 'my_app' })
21
- # :mem_cache_session: true
22
-
23
- # This turns on the ActiveRecord sessions with rails parasite mode if
24
- # active_support gem is installed. Skeleton app comes with a migration to
25
- # create the sessions table. Or you can point merb to the same sessions
26
- # table that your rails app uses to share sessions between merb and rails.
27
- :sql_session: true
28
- :log_level: debug
29
-
30
- # Uncomment to use the merb upload progress
31
- #:config: dist/conf/upload.conf
32
-
33
- # Uncomment to cache templates in dev mode. Templates are cached
34
- # automatically in production mode.
35
- #:cache_templates: true
36
-
37
- # Uncomment and set this if you want to run a drb server for upload progress
38
- # or other drb services.
39
- #:drb_server_port: 32323
40
-
41
- # If you want to protect some or all of your app with HTTP basic auth then
42
- # uncomment the following and fill in your credentials you want it to use.
43
- # You will then need to set a 'before' filter in a controller. For example:
44
- # before :basic_authentication
45
- #:basic_auth:
46
- # :username: ezra
47
- # :password: test
48
- # :domain: localhost
49
-
50
- # Uncomment this if you want merb to daemonize when you start it. You can also
51
- # just use merb -d for the same effect. Don't uncomment this if you use the
52
- # cluster option.
53
- #:daemonize: true
54
-
55
- # Uncomment this to set the number of members in your merb cluster. Don't set
56
- # this and :daemonize: at the same time.
57
- #:cluster: 3
@@ -1,24 +0,0 @@
1
- puts "merb init called"
2
- require 'active_record'
3
- ActiveRecord::Base.verification_timeout = 14400
4
- ActiveRecord::Base.logger = MERB_LOGGER
5
-
6
- require DIST_ROOT+"/app/controllers/application.rb"
7
- Dir[DIST_ROOT+"/app/controllers/*.rb"].each{ |m| require m }
8
- Dir[DIST_ROOT+"/app/helpers/*.rb"].each { |m| require m }
9
- Dir[DIST_ROOT+"/app/models/*.rb"].each { |m| require m }
10
- Dir[DIST_ROOT+"/app/mailers/*.rb"].each { |m| require m }
11
- Dir[DIST_ROOT+"/lib/*/lib/*.rb"].each { |m| require m }
12
- Dir[DIST_ROOT+"/lib/*/bin/*.rb"].each { |m| require m }
13
- Dir[DIST_ROOT+"/plugins/*/init.rb"].each { |m| require m }
14
-
15
- #Get Database Config
16
- puts "Connecting to database..."
17
- conn_options = YAML::load(Erubis::Eruby.new(IO.read("#{DIST_ROOT}/conf/database.yml")).result)
18
- ActiveRecord::Base.establish_connection conn_options["#{MERB_ENV}"]
19
-
20
- #Get Environment File
21
- require "#{DIST_ROOT}/conf/environments/#{MERB_ENV}"
22
-
23
- # add your own ruby code here for app specific stuff. This file gets loaded
24
- # after the framework is loaded.
@@ -1,22 +0,0 @@
1
- # Merb::RouteMatcher is the request routing mapper for the merb framework.
2
- # You can define placeholder parts of the url with the :symbol notation. For
3
- # example:
4
- #
5
- # r.add '/admin/:email/users/:id', :controller => 'admin_users', :action => 'foo'
6
- #
7
- # will match against a request to /admin/me@gmail.com/users/456. It will then
8
- # use the class AdminUsers as your merb controller and call the 'foo' method
9
- # on it. The 'foo' method will be able to access the :email and :id values via
10
- # the 'params' hash, e.g. 'params[:email]' will return 'me@gmail.com'.
11
-
12
- puts "Compiling routes.."
13
- Merb::Router.prepare do |r|
14
- # restfull routes
15
- # r.resources :posts
16
-
17
- # default route, usually you don't want to change this
18
- r.default_routes
19
-
20
- # change this for your home page to be avaiable at /
21
- #r.add '/', :controller => 'whatever', :action =>'index'
22
- end
@@ -1,5 +0,0 @@
1
- # upload progress conf
2
- ---
3
- #:drb: true # uncomment for drb UploadProgress if you run a merb cluster
4
- :path_info: /files/upload
5
- :frequency: 2
@@ -1,14 +0,0 @@
1
- class AddSessionsTable < ActiveRecord::Migration
2
- def self.up
3
- create_table :sessions, :force => true do |t|
4
- t.column :session_id, :string, :limit => 32
5
- t.column :created_at, :datetime
6
- t.column :data, :text
7
- end
8
- add_index "sessions", ["session_id"], :name => "session_id_index"
9
- end
10
-
11
- def self.down
12
- drop_table :sessions
13
- end
14
- end
@@ -1,21 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'rubygems'
3
- require 'merb'
4
-
5
- TMPL = <<EOF
6
- class <%= class_name.snake_case.camel_case %> < ActiveRecord::Migration
7
- def self.up
8
- end
9
-
10
- def self.down
11
- end
12
- end
13
- EOF
14
-
15
- class_name = ARGV[0]
16
- highest_migration = Dir[Dir.pwd+'/dist/schema/migrations/*'].map{|f| File.basename(f) =~ /^(\d+)/; $1}.max
17
- filename = format("%03d_%s", (highest_migration.to_i+1), class_name.snake_case)
18
-
19
- File.open(Dir.pwd+"/dist/schema/migrations/#{filename}.rb", 'w+') do |file|
20
- file.write Erubis::Eruby.new(TMPL).result(binding)
21
- end
@@ -1,18 +0,0 @@
1
- class String
2
-
3
- # "FooBar".snake_case #=> "foo_bar"
4
- def snake_case
5
- gsub(/\B[A-Z]/, '_\&').downcase
6
- end
7
-
8
- # "foo_bar".camel_case #=> "FooBar"
9
- def camel_case
10
- split('_').map{|e| e.capitalize}.join
11
- end
12
-
13
- # Concatenates a path
14
- def /(o)
15
- File.join(self, o.to_s)
16
- end
17
-
18
- end
@@ -1,206 +0,0 @@
1
- require File.dirname(__FILE__)+'/mixins/controller_mixin'
2
- require File.dirname(__FILE__)+'/mixins/responder_mixin'
3
- require File.dirname(__FILE__)+'/merb_request'
4
- require File.dirname(__FILE__)+'/merb_exceptions'
5
- require 'set'
6
- module Merb
7
-
8
- # All of your web controllers will inherit from Merb::Controller. This
9
- # superclass takes care of parsing the incoming headers and body into
10
- # params and cookies and headers. If the request is a file upload it will
11
- # stream it into a tempfile and pass in the filename and tempfile object
12
- # to your controller via params. It also parses the ?query=string and
13
- # puts that into params as well.
14
- class Controller < AbstractController
15
-
16
- class_inheritable_accessor :_session_id_key, :_session_expiry
17
- self._session_id_key = :_session_id
18
- self._session_expiry = Time.now + Merb::Const::WEEK * 2
19
-
20
- include Merb::ControllerMixin
21
- include Merb::ResponderMixin
22
- include Merb::ControllerExceptions::HTTPErrors
23
-
24
- class << self
25
- def callable_actions
26
- @callable_actions ||= Set.new(public_instance_methods - hidden_actions)
27
- end
28
-
29
- def hidden_actions
30
- write_inheritable_attribute(:hidden_actions, Merb::Controller.public_instance_methods) unless read_inheritable_attribute(:hidden_actions)
31
- read_inheritable_attribute(:hidden_actions)
32
- end
33
-
34
- # Hide each of the given methods from being callable as actions.
35
- def hide_action(*names)
36
- write_inheritable_attribute(:hidden_actions, hidden_actions | names.collect { |n| n.to_s })
37
- end
38
-
39
- def build(req, env, args, resp)
40
- cont = new
41
- cont.parse_request(req, env, args, resp)
42
- cont
43
- end
44
- end
45
-
46
- # Parses the http request into params, headers and cookies that you can use
47
- # in your controller classes. Also handles file uploads by writing a
48
- # tempfile and passing a reference in params.
49
- def parse_request(req, env, args, resp)
50
- env = env.to_hash
51
- @_status, method, @_response, @_headers = 200, (env[Merb::Const::REQUEST_METHOD]||Merb::Const::GET).downcase.to_sym, resp,
52
- {'Content-Type' =>'text/html'}
53
- cookies = query_parse(env[Merb::Const::HTTP_COOKIE], ';,')
54
- querystring = query_parse(env[Merb::Const::QUERY_STRING])
55
-
56
- if Merb::Const::MULTIPART_REGEXP =~ env[Merb::Const::UPCASE_CONTENT_TYPE] && [:put,:post].include?(method)
57
- querystring.update(parse_multipart(req, $1, env))
58
- elsif [:post, :put].include?(method)
59
- if [Merb::Const::APPLICATION_JSON, Merb::Const::TEXT_JSON].include?(env[Merb::Const::UPCASE_CONTENT_TYPE])
60
- MERB_LOGGER.info("JSON Request")
61
- json = JSON.parse(req.read || "") || {}
62
- querystring.update(json)
63
- elsif [Merb::Const::APPLICATION_XML, Merb::Const::TEXT_XML].include?(env[Merb::Const::UPCASE_CONTENT_TYPE])
64
- querystring.update(Hash.from_xml(req.read).with_indifferent_access)
65
- else
66
- querystring.update(query_parse(req.read))
67
- end
68
- end
69
-
70
- @_cookies, @_params = cookies.symbolize_keys!, querystring.update(args).symbolize_keys!
71
-
72
- if @_params.key?(_session_id_key) && !Merb::Server.config[:session_id_cookie_only]
73
- @_cookies[_session_id_key] = @_params[_session_id_key]
74
- elsif @_params.key?(_session_id_key) && Merb::Server.config[:session_id_cookie_only]
75
- # This condition allows for certain controller/action paths to allow a
76
- # session ID to be passed in a query string. This is needed for Flash
77
- # Uploads to work since flash will not pass a Session Cookie Recommend
78
- # running session.regenerate after any controller taking advantage of
79
- # this in case someone is attempting a session fixation attack
80
- @_cookies[_session_id_key] = @_params[_session_id_key] if Merb::Server.config[:query_string_whitelist].include?("#{params[:controller]}/#{params[:action]}")
81
- end
82
-
83
- # Handle alternate HTTP method passed as _method parameter. Doesn't allow
84
- # method to be overridden for :get unless Merb is in development mode.
85
- #
86
- # i.e. You can pass _method=put on the querystring if you are in
87
- # development mode.
88
- allow = [:post, :put, :delete]
89
- allow << :get if MERB_ENV == 'development'
90
- if @_params.key?(:_method) && allow.include?(method)
91
- method = @_params.delete(:_method).downcase.intern
92
- end
93
- @_request = Request.new(env, method, req)
94
- MERB_LOGGER.info("Params: #{params.inspect}\nCookies: #{cookies.inspect}")
95
- end
96
-
97
-
98
-
99
- def dispatch(action=:index)
100
- start = Time.now
101
- begin
102
- if !self.class.callable_actions.include?(action.to_s)
103
- raise NotFound
104
- MERB_LOGGER.info "Action: #{action} not in callable_actions: #{self.class.callable_actions}"
105
- else
106
- setup_session
107
- super(action)
108
- finalize_session
109
- end
110
- rescue ControllerExceptions::Base => e
111
- e.set_controller(self) # for access to session, params, etc
112
- @_body = e.call_action
113
- set_status(e.status)
114
- end
115
-
116
- @_benchmarks[:action_time] = Time.now - start
117
- MERB_LOGGER.info("Time spent in #{self.class}##{action} action: #{@_benchmarks[:action_time]} seconds")
118
- end
119
-
120
- # Accessor for @_body. Please use status and never @status directly.
121
- def body
122
- @_body
123
- end
124
-
125
- # Accessor for @_status. Please use status and never @_status directly.
126
- def status
127
- @_status
128
- end
129
-
130
-
131
- # Accessor for @_request. Please use request and never @_request directly.
132
- def request
133
- @_request
134
- end
135
-
136
- # Accessor for @_params. Please use params and never @_params directly.
137
- def params
138
- @_params
139
- end
140
-
141
- # Accessor for @_cookies. Please use cookies and never @_cookies directly.
142
- def cookies
143
- @_cookies
144
- end
145
-
146
- # Accessor for @_headers. Please use headers and never @_headers directly.
147
- def headers
148
- @_headers
149
- end
150
-
151
- # Accessor for @_session. Please use session and never @_session directly.
152
- def session
153
- @_session
154
- end
155
-
156
- # Accessor for @_response. Please use response and never @_response directly.
157
- def response
158
- @_response
159
- end
160
-
161
- # Sends a mail from a MailController
162
- #
163
- # send_mail FooMailer, :bar, :from => "foo@bar.com", :to => "baz@bat.com"
164
- #
165
- # would send an email via the FooMailer's bar method.
166
- #
167
- # The mail_params hash would be sent to the mailer, and includes items
168
- # like from, to subject, and cc. See
169
- # Merb::MailController#dispatch_and_deliver for more details.
170
- #
171
- # The send_params hash would be sent to the MailController, and is
172
- # available to methods in the MailController as <tt>params</tt>. If you do
173
- # not send any send_params, this controller's params will be available to
174
- # the MailController as <tt>params</tt>
175
- def send_mail(klass, method, mail_params, send_params = nil)
176
- klass.new(send_params || params, self).dispatch_and_deliver(method, mail_params)
177
- end
178
-
179
- # Dispatches a PartController. Use like:
180
- #
181
- # <%= part TodoPart => :list %>
182
- #
183
- # will instantiate a new TodoPart controller and call the :list action
184
- # invoking the Part's before and after filters as part of the call.
185
- #
186
- # returns a string containing the results of the Part controllers dispatch
187
- #
188
- # You can compose parts easily as well, these two parts will stil be wrapped
189
- # in the layout of the Foo controller:
190
- #
191
- # class Foo < Application
192
- # def some_action
193
- # wrap_layout(part(TodoPart => :new) + part(TodoPart => :list))
194
- # end
195
- #end
196
- #
197
- def part(opts={})
198
- res = opts.inject([]) do |memo,(klass,action)|
199
- memo << klass.new(self).dispatch(action)
200
- end
201
- res.size == 1 ? res[0] : res
202
- end
203
-
204
- end
205
-
206
- end