Changeset 616
- Timestamp:
- 02/15/05 01:24:55 (4 years ago)
- Files:
-
- trunk/actionpack/lib/action_controller (modified) (1 prop)
- trunk/actionpack/lib/action_controller/assertions/action_pack_assertions.rb (modified) (2 diffs)
- trunk/actionpack/lib/action_controller/base.rb (modified) (5 diffs)
- trunk/actionpack/lib/action_controller/cgi_process.rb (modified) (1 diff)
- trunk/actionpack/lib/action_controller/helpers.rb (modified) (2 diffs)
- trunk/actionpack/lib/action_controller/request.rb (modified) (4 diffs)
- trunk/actionpack/lib/action_controller/rescue.rb (modified) (2 diffs)
- trunk/actionpack/lib/action_controller/routing.rb (deleted)
- trunk/actionpack/lib/action_controller/scaffolding.rb (modified) (1 diff)
- trunk/actionpack/lib/action_controller/templates/rescues/routing_error.rhtml (deleted)
- trunk/actionpack/lib/action_controller/test_process.rb (modified) (3 diffs)
- trunk/actionpack/lib/action_controller/url_rewriter.rb (modified) (3 diffs)
- trunk/actionpack/lib/action_view/partials.rb (modified) (1 diff)
- trunk/actionpack/test/controller/cookie_test.rb (modified) (2 diffs)
- trunk/actionpack/test/controller/helper_test.rb (modified) (2 diffs)
- trunk/actionpack/test/controller/render_test.rb (modified) (2 diffs)
- trunk/actionpack/test/controller/request_test.rb (modified) (1 diff)
- trunk/actionpack/test/controller/routing_tests.rb (deleted)
- trunk/actionpack/test/controller/url_obsolete.rb (deleted)
- trunk/actionpack/test/fixtures/fun (deleted)
- trunk/actionpack/test/fixtures/helpers/fun (deleted)
- trunk/activerecord/lib/active_record (modified) (1 prop)
- trunk/activesupport/CHANGELOG (modified) (1 diff)
- trunk/activesupport/lib/core_ext/hash/indifferent_access.rb (modified) (1 diff)
- trunk/activesupport/lib/core_ext/string/inflections.rb (modified) (1 diff)
- trunk/activesupport/lib/dependencies.rb (modified) (3 diffs)
- trunk/activesupport/lib/inflector.rb (modified) (2 diffs)
- trunk/activesupport/test/inflector_test.rb (modified) (4 diffs)
- trunk/activesupport/test/loading_module (deleted)
- trunk/activesupport/test/loading_module_tests.rb (deleted)
- trunk/railties/bin/destroy (modified) (1 diff)
- trunk/railties/bin/generate (modified) (1 diff)
- trunk/railties/bin/rails (modified) (1 diff)
- trunk/railties/bin/update (modified) (1 diff)
- trunk/railties/configs/apache.conf (modified) (1 diff)
- trunk/railties/environments/shared_for_gem.rb (modified) (1 diff)
- trunk/railties/environments/shared.rb (modified) (1 diff)
- trunk/railties/generators (copied) (copied from trunk/railties/generators)
- trunk/railties/generators/controller (copied) (copied from trunk/railties/generators/controller)
- trunk/railties/generators/controller/templates (copied) (copied from trunk/railties/generators/controller/templates)
- trunk/railties/generators/mailer (copied) (copied from trunk/railties/generators/mailer)
- trunk/railties/generators/mailer/templates (copied) (copied from trunk/railties/generators/mailer/templates)
- trunk/railties/generators/model (copied) (copied from trunk/railties/generators/model)
- trunk/railties/generators/model/templates (copied) (copied from trunk/railties/generators/model/templates)
- trunk/railties/generators/model/USAGE (copied) (copied from trunk/railties/generators/model/USAGE)
- trunk/railties/generators/scaffold (copied) (copied from trunk/railties/generators/scaffold)
- trunk/railties/generators/scaffold/templates (copied) (copied from trunk/railties/generators/scaffold/templates)
- trunk/railties/lib/dispatcher.rb (modified) (1 diff)
- trunk/railties/lib/rails_generator.rb (modified) (2 diffs)
- trunk/railties/lib/rails_generator/base.rb (modified) (3 diffs)
- trunk/railties/lib/rails_generator/generators/applications/app/app_generator.rb (modified) (4 diffs)
- trunk/railties/lib/rails_generator/generators/components/controller/controller_generator.rb (modified) (1 diff)
- trunk/railties/lib/rails_generator/generators/components/controller/templates/functional_test.rb (modified) (1 diff)
- trunk/railties/lib/rails_generator/generators/components/mailer/mailer_generator.rb (modified) (1 diff)
- trunk/railties/lib/rails_generator/generators/components/model/model_generator.rb (modified) (1 diff)
- trunk/railties/lib/rails_generator/generators/components/model/templates/fixtures.yml (modified) (1 diff)
- trunk/railties/lib/rails_generator/generators/components/model/templates/unit_test.rb (modified) (1 diff)
- trunk/railties/lib/rails_generator/generators/components/scaffold/scaffold_generator.rb (modified) (5 diffs)
- trunk/railties/lib/rails_generator/generators/components/scaffold/templates/functional_test.rb (modified) (4 diffs)
- trunk/railties/lib/rails_generator/generators/components/scaffold/templates/view_list.rhtml (modified) (1 diff)
- trunk/railties/lib/rails_generator/lookup.rb (modified) (2 diffs)
- trunk/railties/lib/rails_generator/options.rb (modified) (3 diffs)
- trunk/railties/lib/rails_generator/scripts.rb (modified) (1 diff)
- trunk/railties/lib/rails_generator/scripts/destroy.rb (modified) (1 diff)
- trunk/railties/lib/rails_generator/scripts/generate.rb (modified) (1 diff)
- trunk/railties/lib/rails_generator/scripts/update.rb (modified) (1 diff)
- trunk/railties/test/rails_generator_test.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/lib/action_controller
- Property svn:externals changed from
support http://dev.rubyonrails.org/svn/rails/branches/routing/activesupport/lib
to
support http://dev.rubyonrails.org/svn/rails/trunk/activesupport/lib
- Property svn:externals changed from
trunk/actionpack/lib/action_controller/assertions/action_pack_assertions.rb
r614 r616 142 142 end 143 143 end 144 144 145 145 # ensure our redirection url is an exact match 146 146 def assert_redirect_url(url=nil, message=nil) … … 157 157 msg = build_message(message, "<?> was not found in the location: <?>", pattern, response.redirect_url) 158 158 assert_block(msg) { response.redirect_url_match?(pattern) } 159 end160 161 # -- routing assertions --------------------------------------------------162 163 # Asserts that the routing of the given path is handled correctly and that the parsed options match.164 # Also verifies that the provided options can be used to generate the provided path.165 def assert_routing(path, options, defaults={}, extras={}, message=nil)166 defaults[:controller] ||= options[:controller] # Assume given controller,167 request = ActionController::TestRequest.new({}, {}, nil)168 request.path_parameters = defaults169 170 ActionController::Routing::Routes.reload if ActionController::Routing::Routes.empty? # Load routes.rb if it hasn't been loaded.171 172 generated_path, found_extras = ActionController::Routing::Routes.generate(defaults.merge(options), request)173 generated_path = generated_path.join('/')174 msg = build_message(message, "found extras <?>, not <?>", found_extras, extras)175 assert_block(msg) { found_extras == extras }176 177 msg = build_message(message, "The generated path <?> did not match <?>", generated_path, path)178 assert_block(msg) { path == generated_path }179 180 request = ActionController::TestRequest.new({}, {}, nil)181 request.path = path182 ActionController::Routing::Routes.recognize!(request)183 184 expected_options = options.clone185 extras.each {|k,v| expected_options.delete k}186 187 msg = build_message(message, "The recognized options <?> did not match <?>", request.path_parameters, expected_options)188 assert_block(msg) { request.path_parameters == expected_options }189 159 end 190 160 trunk/actionpack/lib/action_controller/base.rb
r614 r616 13 13 end 14 14 class MissingTemplate < ActionControllerError #:nodoc: 15 end16 class RoutingError < ActionControllerError17 attr_reader :failures18 def initialize(message, failures=[])19 super(message)20 @failures = failures21 end22 15 end 23 16 class UnknownAction < ActionControllerError #:nodoc: … … 213 206 @@consider_all_requests_local = true 214 207 cattr_accessor :consider_all_requests_local 215 216 # Enable or disable the collection of failure information for RoutingErrors.217 # This information can be extremely useful when tweaking custom routes, but is218 # pointless once routes have been tested and verified.219 @@debug_routes = true220 cattr_accessor :debug_routes221 208 222 209 # Template root determines the base from which template references will be made. So a call to render("test/template") … … 274 261 def controller_name 275 262 Inflector.underscore(controller_class_name.sub(/Controller/, "")) 276 end277 278 # Convert the class name from something like "OneModule::TwoModule::NeatController" to "one_module/two_module/neat".279 def controller_path280 components = self.name.to_s.split('::').collect { |name| name.underscore }281 components[-1] = $1 if /^(.*)_controller$/ =~ components[-1]282 components.shift if components.first == 'controllers' # Transitional conditional to accomodate root Controllers module283 components.join('/')284 263 end 285 264 end … … 357 336 when Hash then @url.rewrite(rewrite_options(options)) 358 337 end 338 end 339 340 def module_name 341 @params["module"] 359 342 end 360 343 … … 709 692 710 693 def default_template_name(default_action_name = action_name) 711 "#{self.class.controller_path}/#{default_action_name}"694 module_name ? "#{module_name}/#{controller_name}/#{default_action_name}" : "#{controller_name}/#{default_action_name}" 712 695 end 713 696 end trunk/actionpack/lib/action_controller/cgi_process.rb
r614 r616 47 47 end 48 48 49 def query_string50 return @cgi.query_string unless @cgi.query_string.nil? || @cgi.query_string.empty?51 parts = env['REQUEST_URI'].split('?')52 parts.shift53 return parts.join('?')54 end55 56 49 def query_parameters 57 qs = self.query_string 58 qs.empty? ? {} : CGIMethods.parse_query_parameters(query_string) 50 @cgi.query_string ? CGIMethods.parse_query_parameters(@cgi.query_string) : {} 59 51 end 60 52 trunk/actionpack/lib/action_controller/helpers.rb
r614 r616 49 49 args.flatten.each do |arg| 50 50 case arg 51 when Module 52 add_template_helper(arg) 53 when String, Symbol 54 file_name = arg.to_s.underscore + '_helper' 55 class_name = file_name.camelize 56 57 begin 58 require_dependency(file_name) 59 rescue LoadError => load_error 60 requiree = / -- (.*?)(\.rb)?$/.match(load_error).to_a[1] 61 raise LoadError, requiree == file_name ? "Missing helper file helpers/#{file_name}.rb" : "Can't load file: #{requiree}" 51 when Module 52 add_template_helper(arg) 53 when String, Symbol 54 file_name = Inflector.underscore(arg.to_s.downcase) + '_helper' 55 class_name = Inflector.camelize(file_name) 56 begin 57 require_dependency(file_name) 58 rescue LoadError => load_error 59 requiree = / -- (.*?)(\.rb)?$/.match(load_error).to_a[1] 60 if requiree == file_name 61 raise LoadError, "Missing helper file helpers/#{file_name}.rb" 62 else 63 raise LoadError, "Can't load file: #{requiree}" 62 64 end 63 64 add_template_helper(class_name.constantize) 65 else 66 raise ArgumentError, 'helper expects String, Symbol, or Module argument' 65 end 66 raise ArgumentError, "Missing #{class_name} module in helpers/#{file_name}.rb" unless Object.const_defined?(class_name) 67 add_template_helper(Object.const_get(class_name)) 68 else 69 raise ArgumentError, 'helper expects String, Symbol, or Module argument' 67 70 end 68 71 end … … 93 96 inherited_without_helper(child) 94 97 begin 95 child.helper(child.controller_ path)98 child.helper(child.controller_name) 96 99 rescue ArgumentError, LoadError 97 100 # No default helper available for this controller trunk/actionpack/lib/action_controller/request.rb
r614 r616 4 4 # Returns both GET and POST parameters in a single hash. 5 5 def parameters 6 @parameters ||= request_parameters. merge(query_parameters).merge(path_parameters).with_indifferent_access6 @parameters ||= request_parameters.update(query_parameters) 7 7 end 8 8 … … 74 74 75 75 def request_uri 76 (%r{^\w+\://[^/]+(/.*|$)$} =~ env['REQUEST_URI']) ? $1 : env['REQUEST_URI'] # Remove domain, which webrick puts into the request_uri.76 env['REQUEST_URI'] 77 77 end 78 78 … … 86 86 87 87 def path 88 path =request_uri ? request_uri.split('?').first : ''88 request_uri ? request_uri.split('?').first : '' 89 89 end 90 90 … … 101 101 env['HTTP_HOST'] || host + port_string 102 102 end 103 104 def path_parameters=(parameters)105 @path_parameters = parameters106 @parameters = nil107 end108 103 109 def path_parameters110 @path_parameters ||= {}111 end112 113 104 #-- 114 105 # Must be implemented in the concrete request trunk/actionpack/lib/action_controller/rescue.rb
r614 r616 49 49 # Overwrite to implement public exception handling (for requests answering false to <tt>local_request?</tt>). 50 50 def rescue_action_in_public(exception) #:doc: 51 case exception 52 when RoutingError, UnknownAction then 53 render_text(IO.read(File.join(RAILS_ROOT, 'public', '404.html')), "404 Not Found") 54 else render_text "<html><body><h1>Application error (Rails)</h1></body></html>" 55 end 51 render_text "<html><body><h1>Application error (Rails)</h1></body></html>" 56 52 end 57 53 … … 115 111 case exception 116 112 when MissingTemplate then "missing_template" 117 when RoutingError then "routing_error"118 113 when UnknownAction then "unknown_action" 119 114 when ActionView::TemplateError then "template_error" 120 else raise ;"diagnostics"115 else "diagnostics" 121 116 end 122 117 ) 123 end124 125 def response_code_for_rescue(exception)126 case exception127 when UnknownAction, RoutingError then "404 Page Not Found"128 else "500 Internal Error"129 end130 118 end 131 119 trunk/actionpack/lib/action_controller/scaffolding.rb
r614 r616 150 150 private 151 151 def render#{suffix}_scaffold(action = caller_method_name(caller)) 152 if template_exists?("\#{ self.class.controller_path}/\#{action}")152 if template_exists?("\#{controller_name}/\#{action}") 153 153 render_action(action) 154 154 else trunk/actionpack/lib/action_controller/test_process.rb
r614 r616 32 32 class TestRequest < AbstractRequest #:nodoc: 33 33 attr_accessor :cookies 34 attr_accessor :query_parameters, :request_parameters, : path, :session, :env35 attr_accessor :host, : remote_addr34 attr_accessor :query_parameters, :request_parameters, :session, :env 35 attr_accessor :host, :path, :request_uri, :remote_addr 36 36 37 37 def initialize(query_parameters = nil, request_parameters = nil, session = nil) … … 59 59 end 60 60 61 # Used to check AbstractRequest's request_uri functionality.62 # Disables the use of @path and @request_uri so superclass can handle those.63 def set_REQUEST_URI(value)64 @env["REQUEST_URI"] = value65 @request_uri = nil66 @path = nil67 end68 69 61 def request_uri=(uri) 70 62 @request_uri = uri 71 63 @path = uri.split("?").first 72 64 end 73 74 def request_uri75 @request_uri || super()76 end77 78 def path79 @path || super()80 end81 82 65 83 66 private … … 255 238 @request.env['REQUEST_METHOD'] ||= "GET" 256 239 @request.action = action.to_s 257 @request.path_parameters = { :controller => @controller.class.controller_path }258 240 @request.parameters.update(parameters) unless parameters.nil? 259 241 @request.session = ActionController::TestSession.new(session) unless session.nil? trunk/actionpack/lib/action_controller/url_rewriter.rb
r614 r616 2 2 # Rewrites URLs for Base.redirect_to and Base.url_for in the controller. 3 3 class UrlRewriter #:nodoc: 4 RESERVED_OPTIONS = [:anchor, :params, :path_params, :only_path, :host, :protocol] 5 def initialize(request, parameters) 6 @request, @parameters = request, parameters 4 VALID_OPTIONS = [:action, :action_prefix, :action_suffix, :application_prefix, :module, :controller, :controller_prefix, :anchor, :params, :path_params, :id, :only_path, :overwrite_params, :host, :protocol ] 5 6 def initialize(request, controller, action) 7 @request, @controller, @action = request, controller, action 7 8 @rewritten_path = @request.path ? @request.path.dup : "" 8 9 end … … 22 23 23 24 def to_str 24 "#{@request.protocol}, #{@request.host_with_port}, #{@request.path}, #{@parameters[:controller]}, #{@parameters[:action]}, #{@request.parameters.inspect}"25 "#{@request.protocol}, #{@request.host_with_port}, #{@request.path}, #{@controller}, #{@action}, #{@request.parameters.inspect}" 25 26 end 26 27 … … 48 49 end 49 50 50 def rewrite_path(options) 51 options = options.symbolize_keys 52 RESERVED_OPTIONS.each {|k| options.delete k} 53 54 path, extras = Routing::Routes.generate(options, @request) 55 path = "/#{path.join('/')}" 56 path += build_query_string(extras) 57 51 def rewrite_path(path, options) 52 include_id_in_path_params(options) 53 54 path = rewrite_action(path, options) if options[:action] || options[:action_prefix] 55 path = rewrite_path_params(path, options) if options[:path_params] 56 path = rewrite_controller(path, options) if options[:controller] || options[:controller_prefix] 58 57 return path 59 58 end trunk/actionpack/lib/action_view/partials.rb
r614 r616 61 61 return File.dirname(partial_path), File.basename(partial_path) 62 62 else 63 return controller. class.controller_path, partial_path63 return controller.send(:controller_name), partial_path 64 64 end 65 65 end trunk/actionpack/test/controller/cookie_test.rb
r614 r616 27 27 cookies["login"] = "XJ-122" 28 28 render_text "hello world" 29 end 30 31 def access_frozen_cookies 32 @cookies["wont"] = "work" 29 33 end 30 34 … … 64 68 end 65 69 70 def test_setting_cookie_on_frozen_instance_variable 71 @request.action = "access_frozen_cookies" 72 assert_raises(TypeError) { process_request } 73 end 74 66 75 private 67 76 def process_request trunk/actionpack/test/controller/helper_test.rb
r614 r616 1 1 require File.dirname(__FILE__) + '/../abstract_unit' 2 $:.unshift(File.dirname(__FILE__) + '/../fixtures/helpers')3 4 class TestController < ActionController::Base5 attr_accessor :delegate_attr6 def delegate_method() end7 def rescue_action(e) raise end8 end9 10 module Fun11 class GamesController < ActionController::Base12 def render_hello_world13 render_template "hello: <%= stratego %>"14 end15 16 def rescue_action(e) raise end17 end18 end19 20 module LocalAbcHelper21 def a() end22 def b() end23 def c() end24 end25 2 26 3 class HelperTest < Test::Unit::TestCase 27 4 HELPER_PATHS = %w(/../fixtures/helpers) 5 6 class TestController < ActionController::Base 7 attr_accessor :delegate_attr 8 def delegate_method() end 9 def rescue_action(e) raise end 10 end 11 12 module LocalAbcHelper 13 def a() end 14 def b() end 15 def c() end 16 end 17 28 18 29 19 def setup … … 113 103 end 114 104 115 def test_helper_for_nested_controller116 @request = ActionController::TestRequest.new117 @response = ActionController::TestResponse.new118 @request.action = "render_hello_world"119 120 assert_equal "hello: Iz guuut!", Fun::GamesController.process(@request, @response).body121 end122 105 123 106 private trunk/actionpack/test/controller/render_test.rb
r614 r616 3 3 Customer = Struct.new("Customer", :name) 4 4 5 module Fun 6 class GamesController < ActionController::Base 5 class RenderTest < Test::Unit::TestCase 6 class TestController < ActionController::Base 7 layout :determine_layout 8 7 9 def hello_world 8 10 end 9 end10 end11 11 12 def render_hello_world 13 render "test/hello_world" 14 end 12 15 13 class TestController < ActionController::Base 14 layout :determine_layout 16 def render_hello_world_from_variable 17 @person = "david" 18 render_text "hello #{@person}" 19 end 15 20 16 def hello_world 21 def render_action_hello_world 22 render_action "hello_world" 23 end 24 25 def render_text_hello_world 26 render_text "hello world" 27 end 28 29 def render_custom_code 30 render_text "hello world", "404 Moved" 31 end 32 33 def render_xml_hello 34 @name = "David" 35 render "test/hello" 36 end 37 38 def greeting 39 # let's just rely on the template 40 end 41 42 def layout_test 43 render_action "hello_world" 44 end 45 46 def builder_layout_test 47 render_action "hello" 48 end 49 50 def partials_list 51 @customers = [ Customer.new("david"), Customer.new("mary") ] 52 render_action "list" 53 end 54 55 def modgreet 56 end 57 58 def rescue_action(e) raise end 59 60 private 61 def determine_layout 62 case action_name 63 when "layout_test": "layouts/standard" 64 when "builder_layout_test": "layouts/builder" 65 end 66 end 17 67 end 18 68 19 def render_hello_world 20 render "test/hello_world" 69 TestController.template_root = File.dirname(__FILE__) + "/../fixtures/" 70 71 class TestLayoutController < ActionController::Base 72 layout "layouts/standard" 73 74 def hello_world 75 end 76 77 def hello_world_outside_layout 78 end 79 80 def rescue_action(e) 81 raise unless ActionController::MissingTemplate === e 82 end 21 83 end 22 84 23 def render_hello_world_from_variable24 @person = "david"25 render_text "hello #{@person}"26 end27 28 def render_action_hello_world29 render_action "hello_world"30 end31 32 def render_text_hello_world33 render_text "hello world"34 end35 36 def render_custom_code37 render_text "hello world", "404 Moved"38 end39 40 def render_xml_hello41 @name = "David"42 render "test/hello"43 end44 45 def greeting46 # let's just rely on the template47 end48 49 def layout_test50 render_action "hello_world"51 end52 53 def builder_layout_test54 render_action "hello"55 end56 57 def partials_list58 @customers = [ Customer.new("david"), Customer.new("mary") ]59 render_action "list"60 end61 62 def rescue_action(e) raise end63 64 private65 def determine_layout66 case action_name67 when "layout_test": "layouts/standard"68 when "builder_layout_test": "layouts/builder"69 end70 end71 end72 73 TestController.template_root = File.dirname(__FILE__) + "/../fixtures/"74 75 class TestLayoutController < ActionController::Base76 layout "layouts/standard"77 78 def hello_world79 end80 81 def hello_world_outside_layout82 end83 84 def rescue_action(e)85 raise unless ActionController::MissingTemplate === e86 end87 end88 89 class RenderTest < Test::Unit::TestCase90 85 def setup 91 86 @request = ActionController::TestRequest.new … … 176 171 end 177 172 178 def test_nested_rendering 179 @request.action = "hello_world" 180 assert_equal "Living in a nested world", Fun::GamesController.process(@request, @response).body 173 def test_module_rendering 174 @request.action = "modgreet" 175 @request.parameters["module"] = "scope" 176 assert_equal "<p>Beautiful modules!</p>", process_request.body 181 177 end 182 178 trunk/actionpack/test/controller/request_test.rb
r614 r616 32 32 assert_equal ":8080", @request.port_string 33 33 end 34 35 def test_request_uri36 @request.set_REQUEST_URI "http://www.rubyonrails.org/path/of/some/uri?mapped=1"37 assert_equal "/path/of/some/uri?mapped=1", @request.request_uri38 assert_equal "/path/of/some/uri", @request.path39 40 @request.set_REQUEST_URI "http://www.rubyonrails.org/path/of/some/uri"41 assert_equal "/path/of/some/uri", @request.request_uri42 assert_equal "/path/of/some/uri", @request.path43 44 @request.set_REQUEST_URI "/path/of/some/uri"45 assert_equal "/path/of/some/uri", @request.request_uri46 assert_equal "/path/of/some/uri", @request.path47 48 @request.set_REQUEST_URI "/"49 assert_equal "/", @request.request_uri50 assert_equal "/", @request.path51 52 @request.set_REQUEST_URI "/?m=b"53 assert_equal "/?m=b", @request.request_uri54 assert_equal "/", @request.path55 end56 34 57 35 def test_host_with_port trunk/activerecord/lib/active_record
- Property svn:externals changed from
support http://dev.rubyonrails.org/svn/rails/branches/routing/activesupport/lib
to
support http://dev.rubyonrails.org/svn/rails/trunk/activesupport/lib
- Property svn:externals changed from
trunk/activesupport/CHANGELOG
r614 r616 2 2 3 3 * Added IndifferentAccess as a way to wrap a hash by a symbol-based store that also can be accessed by string keys 4 5 * Added Inflector.constantize to turn "Admin::User" into a reference for the constant Admin::User6 7 * Added that Inflector.camelize and Inflector.underscore can deal with modules like turning "Admin::User" into "admin/user" and back8 4 9 5 * Added Inflector.humanize to turn attribute names like employee_salary into "Employee salary". Used by automated error reporting in AR. trunk/activesupport/lib/core_ext/hash/indifferent_access.rb
r614 r616 9 9 end 10 10 11 alias_method :regular_read er, :[] unless method_defined?(:regular_reader)11 alias_method :regular_read, :[] 12 12 13 13 def [](key) 14 14 case key 15 when Symbol: regular_read er(key) || regular_reader(key.to_s)16 when String: regular_read er(key) || regular_reader(key.to_sym)17 else regular_read er(key)15 when Symbol: regular_read(key) || regular_read(key.to_s) 16 when String: regular_read(key) || regular_read(key.to_sym) 17 else regular_read(key) 18 18 end 19 19 end 20 20 21 alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)21 alias_method :regular_writer, :[]= 22 22 23 23 def []=(key, value) trunk/activesupport/lib/core_ext/string/inflections.rb
r614 r616 40 40 Inflector.foreign_key(self, separate_class_name_and_id_with_underscore) 41 41 end 42 43 def constantize44 Inflector.constantize(self)45 end46 42 end 47 43 end trunk/activesupport/lib/dependencies.rb
r614 r616 3 3 module Dependencies 4 4 extend self 5 5 6 6 @@loaded = [ ] 7 7 mattr_accessor :loaded … … 42 42 classes.each { |klass| klass.remove_subclasses } 43 43 end 44 45 # LoadingModules implement namespace-safe dynamic loading.46 # They support automatic loading via const_missing, allowing contained items to be automatically47 # loaded when required. No extra syntax is required, as expressions such as Controller::Admin::UserController48 # load the relavent files automatically.49 #50 # Ruby-style modules are supported, as a folder named 'submodule' will load 'submodule.rb' when available.51 class LoadingModule < Module52 attr_reader :path53 54 def initialize(filesystem_root, path=[])55 @path = path56 @filesystem_root = filesystem_root57 end58 59 # The path to this module in the filesystem.60 # Any subpath provided is taken to be composed of filesystem names.61 def filesystem_path(subpath=[])62 File.join(@filesystem_root, self.path, subpath)63 end64 65 # Load missing constants if possible.66 def const_missing(name)67 return const_get(name) if const_defined?(name) == false && const_load!(name)68 super(name)69 end70 71 # Load the controller class or a parent module.72 def const_load!(name)73 name = name.to_s if name.kind_of? Symbol74 75 if File.directory? filesystem_path(name.underscore)76 # Is it a submodule? If so, create a new LoadingModule *before* loading it.77 # This ensures that subitems will be loadable78 new_module = LoadingModule.new(@filesystem_root, self.path + [name.underscore])79 const_set(name, new_module)80 Object.const_set(name, new_module) if @path.empty?81 end82 83 source_file = filesystem_path("#{(name == 'ApplicationController' ? 'Application' : name).underscore}.rb")84 self.load_file(source_file) if File.file?(source_file)85 self.const_defined?(name.camelize)86 end87 88 # Is this name present or loadable?89 # This method is used by Routes to find valid controllers.90 def const_available?(name)91 name = name.to_s unless name.kind_of? String92 File.directory?(filesystem_path(name.underscore)) || File.file?(filesystem_path("#{name.underscore}.rb"))93 end94 95 def clear96 constants.each do |name|97 Object.send(:remove_const, name) if Object.const_defined?(name) && @path.empty?98 self.send(:remove_const, name)99 end100 end101 102 def load_file(file_path)103 Controllers.module_eval(IO.read(file_path), file_path, 1) # Hard coded Controller line here!!!104 end105 end106 44 end 107 45 … … 115 53 # require_association when using single-table inheritance. 116 54 def const_missing(class_id) 117 if Object.const_defined?(:Controllers) and Object::Controllers.const_available?(class_id)118 return Object::Controllers.const_get(class_id)119 end120 55 begin 121 56 require_or_load(class_id.to_s.demodulize.underscore) trunk/activesupport/lib/inflector.rb
r614 r616 21 21 22 22 def camelize(lower_case_and_underscored_word) 23 lower_case_and_underscored_word.to_s.gsub(/ \/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase}23 lower_case_and_underscored_word.to_s.gsub(/(^|_)(.)/){$2.upcase} 24 24 end 25 25 26 26 def underscore(camel_cased_word) 27 camel_cased_word.to_s.gsub(/ ::/, '/').gsub(/([A-Z]+)([A-Z])/,'\1_\2').gsub(/([a-z])([A-Z])/,'\1_\2').downcase27 camel_cased_word.to_s.gsub(/([A-Z]+)([A-Z])/,'\1_\2').gsub(/([a-z])([A-Z])/,'\1_\2').downcase 28 28 end 29 29 … … 48 48 (separate_class_name_and_id_with_underscore ? "_id" : "id") 49 49 end 50 51 def constantize(camel_cased_word) 52 camel_cased_word.split("::").inject(Object) do |final_type, part| 53 final_type = final_type.const_get(part) 54 end 55 end 56 50 57 51 private 58 52 def plural_rules #:doc: trunk/activesupport/test/inflector_test.rb
r614 r616 1 1 require 'test/unit' 2 2 require File.dirname(__FILE__) + '/../lib/inflector' 3 4 module A5 module B6 class C7 end8 end9 end10 3 11 4 class InflectorTest < Test::Unit::TestCase … … 57 50 "SpecialGuest" => "special_guest", 58 51 "ApplicationController" => "application_controller"