crystal_ext 0.0.7 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +70 -7
- data/lib/crystal/controller.rb +28 -0
- data/lib/crystal/controller/abstract_controller.rb +60 -0
- data/lib/crystal/controller/abstract_controller/micelaneous.rb +31 -0
- data/lib/crystal/controller/abstract_controller/render.rb +133 -0
- data/lib/crystal/controller/abstract_controller/responder.rb +17 -0
- data/lib/crystal/controller/controller_context.rb +7 -0
- data/lib/crystal/controller/http_controller.rb +43 -0
- data/lib/crystal/controller/processors/controller_caller.rb +40 -0
- data/lib/crystal/controller/processors/controller_error_handling.rb +53 -0
- data/lib/crystal/controller/processors/controller_logger.rb +13 -0
- data/lib/crystal/conveyor.rb +29 -0
- data/lib/crystal/conveyor/conveyor.rb +74 -0
- data/lib/crystal/conveyor/conveyors.rb +20 -0
- data/lib/crystal/conveyor/params.rb +22 -0
- data/lib/crystal/conveyor/processor.rb +68 -0
- data/lib/crystal/conveyor/processors/conveyor_logger.rb +24 -0
- data/lib/crystal/conveyor/workspace.rb +36 -0
- data/lib/crystal/environment.rb +52 -0
- data/lib/crystal/environment/config.rb +118 -0
- data/lib/crystal/environment/dependency_resolver.rb +107 -0
- data/lib/crystal/environment/environment.rb +5 -0
- data/lib/crystal/environment/files_helper.rb +42 -0
- data/lib/crystal/environment/logger.rb +21 -0
- data/lib/crystal/html.rb +28 -0
- data/lib/crystal/html/controller_helpers/controller_url_helper.rb +52 -0
- data/lib/crystal/html/controller_helpers/flash_helper.rb +5 -0
- data/lib/crystal/html/flash.rb +40 -0
- data/lib/crystal/html/include_into_controller.rb +24 -0
- data/lib/crystal/html/processors/prepare_flash.rb +26 -0
- data/lib/crystal/html/processors/scoped_params.rb +30 -0
- data/lib/crystal/html/view_helpers/basic_html_helper.rb +83 -0
- data/lib/crystal/html/view_helpers/form_helper.rb +104 -0
- data/lib/crystal/html/view_helpers/javascript_helper.rb +18 -0
- data/lib/crystal/html/view_helpers/model_helper.rb +119 -0
- data/lib/crystal/html/view_helpers/view_url_helper.rb +66 -0
- data/lib/crystal/http.rb +47 -0
- data/lib/crystal/http/http.rb +18 -0
- data/lib/crystal/http/http_adapter.rb +30 -0
- data/lib/crystal/http/middleware/static_files.rb +20 -0
- data/lib/crystal/http/processors/evaluate_format.rb +22 -0
- data/lib/crystal/http/processors/http_logger.rb +15 -0
- data/lib/crystal/http/processors/http_writer.rb +48 -0
- data/lib/crystal/http/processors/prepare_params.rb +24 -0
- data/lib/crystal/http/rack_config.rb +15 -0
- data/lib/crystal/http/support/rack/rack_adapter.rb +65 -0
- data/lib/crystal/http/support/rack/request.rb +12 -0
- data/lib/crystal/http/support/rack/response.rb +39 -0
- data/lib/crystal/mailer.rb +13 -0
- data/lib/crystal/mailer/mail.rb +7 -0
- data/lib/{crystal_ext/profiles/web_ext.rb → crystal/profiles/web.rb} +9 -15
- data/lib/crystal/profiles/web_require.rb +5 -0
- data/lib/crystal/remote.rb +9 -0
- data/lib/crystal/remote/processors/remote_caller.rb +54 -0
- data/lib/crystal/remote/processors/remote_logger.rb +13 -0
- data/lib/crystal/remote/remote.rb +15 -0
- data/lib/crystal/router.rb +19 -0
- data/lib/crystal/router/configurator.rb +23 -0
- data/lib/crystal/router/default_format_processor.rb +12 -0
- data/lib/crystal/router/default_router.rb +23 -0
- data/lib/crystal/router/named_router.rb +63 -0
- data/lib/crystal/router/processors/router.rb +22 -0
- data/lib/crystal/router/router.rb +218 -0
- data/lib/crystal/router/routing_helper.rb +17 -0
- data/lib/crystal/spec.rb +8 -0
- data/lib/crystal/spec/controller.rb +15 -0
- data/lib/crystal/spec/environment.rb +41 -0
- data/lib/crystal/spec/http.rb +67 -0
- data/lib/crystal/spec/mail.rb +20 -0
- data/lib/crystal/spec/remote.rb +9 -0
- data/lib/crystal/spec/view.rb +10 -0
- data/lib/crystal/spec/xhtml.rb +33 -0
- data/lib/crystal/support.rb +46 -0
- data/lib/crystal/support/active_support.rb +39 -0
- data/lib/crystal/support/active_support/micelaneous.rb +2 -0
- data/lib/crystal/support/addressable.rb +45 -0
- data/lib/crystal/support/buffered_logger.rb +38 -0
- data/lib/crystal/support/callbacks.rb +155 -0
- data/lib/crystal/support/exception.rb +20 -0
- data/lib/crystal/support/filters.rb +34 -0
- data/lib/crystal/support/format.rb +9 -0
- data/lib/crystal/support/gems.rb +8 -0
- data/lib/crystal/support/micon.rb +13 -0
- data/lib/crystal/support/mime.rb +11 -0
- data/lib/crystal/support/module.rb +44 -0
- data/lib/crystal/support/rson.rb +53 -0
- data/lib/crystal/support/ruby_ext_with_active_support.rb +4 -0
- data/lib/crystal/support/safe_hash.rb +135 -0
- data/lib/crystal/support/string.rb +26 -0
- data/lib/crystal/template.rb +21 -0
- data/lib/crystal/template/support/tilt.rb +68 -0
- data/lib/crystal/template/template.rb +244 -0
- data/lib/crystal/template/template_context.rb +57 -0
- data/lib/views/crystal_default_templates/development/error.html.erb +11 -0
- data/lib/views/crystal_default_templates/development/error.js.erb +1 -0
- data/readme.md +8 -11
- data/spec/_mailer/basic_spec.rb +110 -0
- data/spec/_mailer/helper.rb +7 -0
- data/spec/controller/abstract_controller_spec.rb +137 -0
- data/spec/controller/abstract_controller_spec/views/OperationsOrderSpec/action.erb +1 -0
- data/spec/controller/abstract_controller_spec/views/ViewVariablesSpec/action.erb +10 -0
- data/spec/controller/controller_context_spec.rb +122 -0
- data/spec/controller/controller_context_spec/views/ItemSpec/actions.erb +4 -0
- data/spec/controller/controller_context_spec/views/ItemSpec/show.erb +0 -0
- data/spec/controller/controller_context_spec/views/ItemSpec/update.erb +0 -0
- data/spec/controller/controller_context_spec/views/NamespaceSpec/ClassSpec/show.erb +0 -0
- data/spec/controller/controller_context_spec/views/PageSpec/actions.erb +3 -0
- data/spec/controller/controller_context_spec/views/PageSpec/show.erb +0 -0
- data/spec/controller/controller_context_spec/views/namespace_spec/class_spec/update.erb +0 -0
- data/spec/controller/controller_helper_spec.rb +57 -0
- data/spec/controller/controller_helper_spec/views/HelperMethodSpec/action.erb +1 -0
- data/spec/controller/controller_helper_spec/views/HelperSpec/action.erb +1 -0
- data/spec/controller/controller_render_spec.rb +191 -0
- data/spec/controller/controller_render_spec/views/AlreadyRenderedSpec/action.erb +1 -0
- data/spec/controller/controller_render_spec/views/AlreadyRenderedSpec/custom_template.erb +1 -0
- data/spec/controller/controller_render_spec/views/AnotherActionSpec/another_action.erb +1 -0
- data/spec/controller/controller_render_spec/views/AnotherLayout/action.erb +1 -0
- data/spec/controller/controller_render_spec/views/ExplicitRenderSpec/action.erb +1 -0
- data/spec/controller/controller_render_spec/views/ExplicitRenderSpec/another_action.erb +1 -0
- data/spec/controller/controller_render_spec/views/ForbidPartialAsActionSpec/_action.erb +1 -0
- data/spec/controller/controller_render_spec/views/FormatSpec/action.html.erb +1 -0
- data/spec/controller/controller_render_spec/views/FormatSpec/action.js.erb +1 -0
- data/spec/controller/controller_render_spec/views/LayoutFiltersSpec/action_with_layout.erb +1 -0
- data/spec/controller/controller_render_spec/views/LayoutFiltersSpec/action_without_layout.erb +1 -0
- data/spec/controller/controller_render_spec/views/LayoutSpec/action.erb +1 -0
- data/spec/controller/controller_render_spec/views/layouts/admin.erb +1 -0
- data/spec/controller/controller_render_spec/views/layouts/app.html.erb +1 -0
- data/spec/controller/controller_render_spec/views/layouts/app.js.erb +1 -0
- data/spec/controller/controller_render_spec/views/some_template.erb +1 -0
- data/spec/controller/controller_render_spec/views/standalone.html.erb +1 -0
- data/spec/controller/error_handling_spec.rb +96 -0
- data/spec/controller/error_handling_spec/views/diferrent_error_handling_spec/a.erb +1 -0
- data/spec/controller/error_handling_spec/views/diferrent_error_handling_spec/b.erb +1 -0
- data/spec/controller/error_handling_spec/views/diferrent_error_handling_spec/c.erb +1 -0
- data/spec/controller/helper.rb +21 -0
- data/spec/controller/http_controller_spec.rb +89 -0
- data/spec/conveyor/conveyor_spec.rb +124 -0
- data/spec/conveyor/helper.rb +8 -0
- data/spec/environment/_dependency_resolver_spec.rb +50 -0
- data/spec/environment/environment_spec.rb +85 -0
- data/spec/environment/environment_spec/app/app_plugin/config/ec_layout +1 -0
- data/spec/environment/environment_spec/app/app_plugin/ec_some_file +1 -0
- data/spec/environment/environment_spec/app/config/config.test.yml +1 -0
- data/spec/environment/environment_spec/app/config/config.yml +2 -0
- data/spec/environment/environment_spec/gem_plugin/config/ec_layout +1 -0
- data/spec/environment/helper.rb +6 -0
- data/spec/environment/logger_spec.rb +18 -0
- data/spec/environment/minimal_app_spec.rb +27 -0
- data/spec/environment/minimal_app_spec/app.rb +3 -0
- data/spec/environment/standard_app_spec.rb +63 -0
- data/spec/environment/standard_app_spec_data/config/init.rb +18 -0
- data/spec/environment/standard_app_spec_data/plugin_a/plugin_a.rb +7 -0
- data/spec/html/basic_html_helper_spec.rb +28 -0
- data/spec/html/flash_spec.rb +184 -0
- data/spec/html/form_helper_spec.rb +56 -0
- data/spec/html/helper.rb +31 -0
- data/spec/html/javascript_helper_spec.rb +15 -0
- data/spec/html/model_helper_spec.rb +65 -0
- data/spec/html/scoped_params_spec.rb +17 -0
- data/spec/html/url_helper_spec.rb +151 -0
- data/spec/http/helper.rb +9 -0
- data/spec/http/http_spec.rb +56 -0
- data/spec/http/http_spec_data/config/init.rb +7 -0
- data/spec/http/http_spec_data/plugin_b/plugin_b.rb +6 -0
- data/spec/integration/basic_spec.rb +82 -0
- data/spec/integration/basic_spec/views/smoke_test_spec/action.erb +1 -0
- data/spec/integration/helper.rb +10 -0
- data/spec/remote/helper.rb +20 -0
- data/spec/remote/remote_spec.rb +75 -0
- data/spec/router/basic_spec.rb +130 -0
- data/spec/router/configurator_spec.rb +29 -0
- data/spec/router/helper.rb +24 -0
- data/spec/router/integration_spec.rb +42 -0
- data/spec/spec.opts +4 -0
- data/spec/support/callbacks_spec.rb +153 -0
- data/spec/support/filters_spec.rb +59 -0
- data/spec/support/helper.rb +7 -0
- data/spec/support/safe_hash_spec.rb +82 -0
- data/spec/template/helper.rb +8 -0
- data/spec/template/template_spec.rb +189 -0
- data/spec/template/template_spec/file.erb +1 -0
- data/spec/template/template_spec/views/basic/custom_context.erb +1 -0
- data/spec/template/template_spec/views/basic/extension.erb +1 -0
- data/spec/template/template_spec/views/basic/general.html.erb +5 -0
- data/spec/template/template_spec/views/basic/non_existing_format.html.erb +0 -0
- data/spec/template/template_spec/views/format/format.erb +1 -0
- data/spec/template/template_spec/views/format/format.html.erb +1 -0
- data/spec/template/template_spec/views/format/format.js.erb +1 -0
- data/spec/template/template_spec/views/layout/basic/content.erb +1 -0
- data/spec/template/template_spec/views/layout/basic/layout.erb +1 -0
- data/spec/template/template_spec/views/layout/content_for/content.erb +3 -0
- data/spec/template/template_spec/views/layout/content_for/layout.erb +3 -0
- data/spec/template/template_spec/views/layout/format/content.html.erb +1 -0
- data/spec/template/template_spec/views/layout/format/content.js.erb +1 -0
- data/spec/template/template_spec/views/layout/format/layout.html.erb +1 -0
- data/spec/template/template_spec/views/layout/format/layout.js.erb +1 -0
- data/spec/template/template_spec/views/layout/nested_yield/a.erb +1 -0
- data/spec/template/template_spec/views/layout/nested_yield/layout.erb +1 -0
- data/spec/template/template_spec/views/layout/nested_yield/layout_b.erb +1 -0
- data/spec/template/template_spec/views/layout/same_context/a.erb +1 -0
- data/spec/template/template_spec/views/layout/same_context/b.erb +1 -0
- data/spec/template/template_spec/views/layout/same_context/layout.erb +1 -0
- data/spec/template/template_spec/views/nested/format/a.erb +1 -0
- data/spec/template/template_spec/views/nested/format/b.erb +1 -0
- data/spec/template/template_spec/views/nested/relative/a.erb +1 -0
- data/spec/template/template_spec/views/nested/relative/b.erb +1 -0
- data/spec/template/template_spec/views/nested/relative/c.erb +1 -0
- data/spec/template/template_spec/views/nested/shared/c.erb +1 -0
- data/spec/template/template_spec/views/nesting_format/_dialog.html.erb +1 -0
- data/spec/template/template_spec/views/nesting_format/_form.html.erb +1 -0
- data/spec/template/template_spec/views/nesting_format/dialog.js.erb +1 -0
- data/spec/template/template_spec/views/other/template.erb +1 -0
- data/spec/template/template_spec/views/prefixes/_underscored.erb +1 -0
- data/spec/template/template_spec/views/prefixes/without_prefix.erb +1 -0
- data/spec/template/tilt_spec.rb +96 -0
- data/spec/template/tilt_spec/views/concat_and_capture.erb +1 -0
- data/spec/template/tilt_spec/views/concat_and_capture.haml +3 -0
- data/spec/template/tilt_spec/views/errors.erb +3 -0
- data/spec/template/tilt_spec/views/errors.haml +3 -0
- data/spec/template/tilt_spec/views/mixed_templates/broken_erb_concat_erb.erb +4 -0
- data/spec/template/tilt_spec/views/mixed_templates/broken_erb_concat_haml.haml +1 -0
- data/spec/template/tilt_spec/views/mixed_templates/broken_haml_concat_erb.erb +1 -0
- data/spec/template/tilt_spec/views/mixed_templates/broken_haml_concat_haml.haml +2 -0
- data/spec/template/tilt_spec/views/nested_capture_and_concat.haml +3 -0
- data/spec/template/tilt_spec/views/ugly.haml +3 -0
- data/spec/template/tilt_spec/views/yield.erb +1 -0
- metadata +303 -33
- data/lib/crystal_ext/defer_js.rb +0 -75
- data/lib/crystal_ext/ensure_no_www.rb +0 -39
- data/lib/crystal_ext/gems.rb +0 -2
- data/lib/crystal_ext/i18n.rb +0 -25
- data/lib/crystal_ext/i18n/locales/ru/pluralization.rb +0 -62
- data/lib/crystal_ext/plugin.rb +0 -23
- data/lib/crystal_ext/plugin/app.rb +0 -19
- data/lib/crystal_ext/plugin/web.rb +0 -44
- data/lib/crystal_ext/prepare_model.rb +0 -20
- data/lib/crystal_ext/profiles/web_ext_require.rb +0 -13
- data/lib/crystal_ext/protect_from_forgery.rb +0 -66
- data/lib/crystal_ext/user_error.rb +0 -14
- data/spec/controller_spec_helper.rb +0 -19
- data/spec/defer_js_spec.rb +0 -33
- data/spec/i18n_spec.rb +0 -31
- data/spec/i18n_spec/locales/en/general.yml +0 -5
- data/spec/i18n_spec/locales/ru/general.yml +0 -7
- data/spec/prepare_model_spec.rb +0 -43
- data/spec/protect_from_forgery_spec.rb +0 -169
- data/spec/spec_helper.rb +0 -4
- data/spec/user_error_spec.rb +0 -41
@@ -0,0 +1,23 @@
|
|
1
|
+
module Crystal
|
2
|
+
module Routes
|
3
|
+
class Configurator
|
4
|
+
def initialize router
|
5
|
+
@router = router
|
6
|
+
end
|
7
|
+
|
8
|
+
def named_route *args, &block
|
9
|
+
named_router = @router.routes[:named_router]
|
10
|
+
raise "There's no NamedRouter (use config to add it)!" unless named_router
|
11
|
+
named_router.add *args, &block
|
12
|
+
end
|
13
|
+
|
14
|
+
class << self
|
15
|
+
inject :router => :router
|
16
|
+
|
17
|
+
def routes &block
|
18
|
+
block.call Configurator.new(router)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Crystal
|
2
|
+
class DefaultRouter
|
3
|
+
inject :logger => :logger
|
4
|
+
|
5
|
+
def encode klass, method, params
|
6
|
+
path = "/#{klass.name.underscore}/#{method}"
|
7
|
+
return path, params
|
8
|
+
end
|
9
|
+
|
10
|
+
def decode path, params
|
11
|
+
parts = path[1..-1].split '/'
|
12
|
+
if parts.size >= 2
|
13
|
+
method = parts.pop
|
14
|
+
|
15
|
+
class_path = parts.join('/')
|
16
|
+
class_name = class_path.camelize
|
17
|
+
klass = class_name.constantize
|
18
|
+
|
19
|
+
return klass, method, params
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
module Crystal
|
2
|
+
class NamedRouter
|
3
|
+
attr_accessor :names, :classes
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@names, @classes = {}, {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def add name, klass
|
10
|
+
name = name.to_s
|
11
|
+
names[name] = klass
|
12
|
+
classes[klass] = name
|
13
|
+
end
|
14
|
+
|
15
|
+
def encode klass, method, params
|
16
|
+
if name = classes[klass]
|
17
|
+
path = "/#{name}/#{method}"
|
18
|
+
return path, params
|
19
|
+
else
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def decode path, params
|
25
|
+
parts = path[1..-1].split '/'
|
26
|
+
if parts.size >= 2
|
27
|
+
method = parts.pop
|
28
|
+
name = parts.join('/')
|
29
|
+
|
30
|
+
if klass = names[name]
|
31
|
+
return klass, method, params
|
32
|
+
else
|
33
|
+
nil
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def encode_route_method method, params = {}
|
39
|
+
method = method.sub(/_path$/, '')
|
40
|
+
names.each do |name, klass|
|
41
|
+
if i = method.rindex(name)
|
42
|
+
if method.size == i + name.size and method[(i-1)..(i-1)] == '_'
|
43
|
+
return encode(klass, method[0..(i-2)], params)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
raise "Unknown route #{method}!"
|
48
|
+
end
|
49
|
+
cache_method_with_params :encode_route_method
|
50
|
+
|
51
|
+
module Helper
|
52
|
+
protected
|
53
|
+
def method_missing m, *args, &block
|
54
|
+
m = m.to_s
|
55
|
+
if m =~ /_path$/
|
56
|
+
crystal[:router].routes[:named_router].encode_route_method m, *args
|
57
|
+
else
|
58
|
+
super m.to_sym, *args, &block
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Crystal
|
2
|
+
module Processors
|
3
|
+
class Router < Processor
|
4
|
+
inject :router => :router
|
5
|
+
|
6
|
+
def initialize next_processor, class_variable = 'class', method_variable = 'method'
|
7
|
+
super(next_processor)
|
8
|
+
@class_variable, @method_variable = class_variable, method_variable
|
9
|
+
end
|
10
|
+
|
11
|
+
def call
|
12
|
+
klass, method, params = router.decode workspace.path, workspace.params
|
13
|
+
|
14
|
+
# params.class_name, params.method_name = klass.name, method
|
15
|
+
|
16
|
+
workspace[@class_variable], workspace[@method_variable], workspace.params = klass, method, params
|
17
|
+
|
18
|
+
next_processor.call
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,218 @@
|
|
1
|
+
module Crystal
|
2
|
+
class Router
|
3
|
+
|
4
|
+
inject(
|
5
|
+
:config => :config,
|
6
|
+
:workspace => :workspace
|
7
|
+
)
|
8
|
+
|
9
|
+
attr_reader :routes, :format_processor
|
10
|
+
|
11
|
+
def initialize class_variable, routes = [[:default_router, DefaultRouter.new]], format_processor = DefaultFormatProcessor.new
|
12
|
+
@class_variable, @format_processor = class_variable, format_processor
|
13
|
+
@routes = Dictionary.new
|
14
|
+
routes.each do |k, v|
|
15
|
+
k.must_be.present
|
16
|
+
v.must_be.present
|
17
|
+
v.must.respond_to(:decode)
|
18
|
+
v.must.respond_to(:encode)
|
19
|
+
|
20
|
+
@routes[k] = v
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def url_for *args
|
25
|
+
if args.first.is_a?(Class)
|
26
|
+
args.size.must_be.in 2..3
|
27
|
+
klass, method, options = args
|
28
|
+
url_for_class(klass, method, (options || {}))
|
29
|
+
else
|
30
|
+
first = args.first.to_s
|
31
|
+
if first !~ /^\//
|
32
|
+
args.size.must_be.in 1..2
|
33
|
+
method, options = args
|
34
|
+
url_for_class(nil, method, (options || {}))
|
35
|
+
else
|
36
|
+
args.size.must_be.in 1..2
|
37
|
+
path, options = args
|
38
|
+
url_for_path(path, (options || {}))
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def decode path, params
|
44
|
+
path.first.must == '/'
|
45
|
+
params = params.to_openobject
|
46
|
+
|
47
|
+
path, format = format_processor.remove_format path if format_processor
|
48
|
+
|
49
|
+
result = nil
|
50
|
+
routes.each do |name, route|
|
51
|
+
result = route.decode path, params
|
52
|
+
break if result
|
53
|
+
end
|
54
|
+
|
55
|
+
raise "No route for '#{workspace.path}' request!" unless result
|
56
|
+
|
57
|
+
klass, method, params = result
|
58
|
+
method ||= config.default_method
|
59
|
+
params.must_be.defined
|
60
|
+
|
61
|
+
raise "Invalid route! No method '#{method}' for #{klass}!" unless klass.instance_methods.include? method
|
62
|
+
raise "Invalid route! You try to call protected method '#{method}' for '#{klass}'!" unless klass.public_instance_methods.include? method
|
63
|
+
|
64
|
+
return klass, method, params
|
65
|
+
end
|
66
|
+
|
67
|
+
def encode klass, method, params = {}
|
68
|
+
klass.must_be.defined
|
69
|
+
method, params = method.to_s, params.to_openobject
|
70
|
+
|
71
|
+
format = params.delete :format
|
72
|
+
|
73
|
+
raise "Invalid route! No method '#{method}' for #{klass}!" unless klass.instance_methods.include? method
|
74
|
+
raise "Invalid route! You try to call protected method '#{method}' for '#{klass}'!" unless klass.public_instance_methods.include? method
|
75
|
+
|
76
|
+
result = nil
|
77
|
+
routes.each do |name, route|
|
78
|
+
result = route.encode klass, method, params
|
79
|
+
break if result
|
80
|
+
end
|
81
|
+
|
82
|
+
raise "No route for '#{klass}.#{method}'!" unless result
|
83
|
+
|
84
|
+
path, params = result
|
85
|
+
path.must_be.defined
|
86
|
+
params.must_be.defined
|
87
|
+
|
88
|
+
path = format_processor.add_format path, format if format_processor and format
|
89
|
+
|
90
|
+
return path, params
|
91
|
+
end
|
92
|
+
|
93
|
+
def persist_params &block
|
94
|
+
before = Thread.current[:persist_params]
|
95
|
+
begin
|
96
|
+
Thread.current[:persist_params] = true
|
97
|
+
block.call
|
98
|
+
ensure
|
99
|
+
Thread.current[:persist_params] = before
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def dont_persist_params &block
|
104
|
+
before = Thread.current[:persist_params]
|
105
|
+
begin
|
106
|
+
Thread.current[:persist_params] = false
|
107
|
+
block.call
|
108
|
+
ensure
|
109
|
+
Thread.current[:persist_params] = before
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def persist_params?
|
114
|
+
!!Thread.current[:persist_params]
|
115
|
+
end
|
116
|
+
|
117
|
+
def global_persistent_params; @global_persistent_params ||= [] end
|
118
|
+
|
119
|
+
protected
|
120
|
+
def current_class; workspace[@class_variable] end
|
121
|
+
|
122
|
+
def url_for_class klass, method, params
|
123
|
+
klass ||= current_class
|
124
|
+
params = params.to_openobject
|
125
|
+
|
126
|
+
# special params
|
127
|
+
format = params.format
|
128
|
+
|
129
|
+
path, params = encode klass, method.to_s, params
|
130
|
+
url = url_for_path path, params
|
131
|
+
|
132
|
+
url.marks.format = format.to_s if format
|
133
|
+
|
134
|
+
url
|
135
|
+
end
|
136
|
+
|
137
|
+
def url_for_path path, params
|
138
|
+
params = params.to_openobject
|
139
|
+
|
140
|
+
# special params
|
141
|
+
url = if params.include? :url_root
|
142
|
+
(params.delete(:url_root) || "") + path
|
143
|
+
else
|
144
|
+
config.url_root! + path
|
145
|
+
end
|
146
|
+
host, port = params.delete(:host), params.delete(:port)
|
147
|
+
format = params.delete(:format)
|
148
|
+
raise ":no_prefix is deprecated!" if params.no_prefix? # TODO3 now it uses :url_root instead, remove this after updating spaces
|
149
|
+
|
150
|
+
# inject persistent params
|
151
|
+
inject_persistent_params! params, params.delete(:persist)
|
152
|
+
|
153
|
+
# json
|
154
|
+
params = {:json => params.to_json}.to_openobject if params.delete :as_json #and !params.empty?
|
155
|
+
|
156
|
+
# format
|
157
|
+
if format
|
158
|
+
params.format = format
|
159
|
+
url.marks.format = format.to_s
|
160
|
+
end
|
161
|
+
|
162
|
+
# Delete 'nil' parameters
|
163
|
+
to_delete = []
|
164
|
+
params.each{|k, v| to_delete << k if v.nil?}
|
165
|
+
to_delete.each{|k| params.delete k}
|
166
|
+
|
167
|
+
# build url
|
168
|
+
delimiter = path.include?('?') ? '&' : '?'
|
169
|
+
url << "#{delimiter}#{params.to_query}" unless params.empty?
|
170
|
+
|
171
|
+
if host.blank?
|
172
|
+
url
|
173
|
+
else
|
174
|
+
%{http://#{host}#{":#{port}" unless port.blank?}#{url}}
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
DONOT_PERSIST = ['_', '_method']
|
179
|
+
|
180
|
+
def inject_persistent_params! params, persist
|
181
|
+
# global persistent params
|
182
|
+
unless cached_global_persistend_params = workspace.cached_global_persistend_params
|
183
|
+
cached_global_persistend_params, workspace_params = {}, workspace.params
|
184
|
+
global_persistent_params.collect!{|k| k.to_s}
|
185
|
+
|
186
|
+
workspace_params.each do |k, v|
|
187
|
+
k = k.to_s
|
188
|
+
cached_global_persistend_params[k] = v if global_persistent_params.include?(k)
|
189
|
+
end
|
190
|
+
|
191
|
+
workspace.cached_global_persistend_params = cached_global_persistend_params
|
192
|
+
end
|
193
|
+
|
194
|
+
cached_global_persistend_params.each do |k, v|
|
195
|
+
params[k] = v unless params.include? k
|
196
|
+
end
|
197
|
+
|
198
|
+
# underscored persistent params
|
199
|
+
if persist_params? or persist
|
200
|
+
unless cached_persistend_params = workspace.cached_persistend_params
|
201
|
+
cached_persistend_params, workspace_params = {}, workspace.params
|
202
|
+
|
203
|
+
workspace_params.each do |k, v|
|
204
|
+
k = k.to_s
|
205
|
+
cached_persistend_params[k] = v if (k =~ /^_/) and !DONOT_PERSIST.include?(k)
|
206
|
+
end
|
207
|
+
|
208
|
+
workspace.cached_persistend_params = cached_persistend_params
|
209
|
+
end
|
210
|
+
|
211
|
+
cached_persistend_params.each do |k, v|
|
212
|
+
params[k] = v unless params.include? k
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
|
217
|
+
end
|
218
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Crystal
|
2
|
+
module RoutingHelper
|
3
|
+
inject :router => :router
|
4
|
+
|
5
|
+
delegate :url_for, :persist_params, :dont_persist_params, :to => :router
|
6
|
+
|
7
|
+
def default_path options = {}
|
8
|
+
url_for_path config.default_path!, options.merge(:url_root => false)
|
9
|
+
end
|
10
|
+
|
11
|
+
def return_to_path options = {}
|
12
|
+
do_not_persist_params do
|
13
|
+
url_for_path params[:_return_to] || session[:return_to] || config.default_path!, options.merge(:url_root => false)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/crystal/spec.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'crystal/spec/environment'
|
2
|
+
|
3
|
+
Spec::Example::ExampleGroup.class_eval do
|
4
|
+
def ccall klass, method, params = {}
|
5
|
+
crystal.conveyors.web.call(
|
6
|
+
:class => klass, :method_name => method.to_s, :params => Crystal::Params.new(params)
|
7
|
+
)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
Crystal::AbstractController.class_eval do
|
12
|
+
def render_ok
|
13
|
+
render :inline => 'ok'
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_ext'
|
2
|
+
|
3
|
+
Spec::Example::ExampleGroup.class_eval do
|
4
|
+
def self.swap_metadata
|
5
|
+
old_metadata = nil
|
6
|
+
|
7
|
+
before :all do
|
8
|
+
old_metadata = Micon.metadata
|
9
|
+
Micon.swap_metadata old_metadata.deep_clone
|
10
|
+
end
|
11
|
+
|
12
|
+
after :all do
|
13
|
+
Micon.swap_metadata old_metadata
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.with_environment environment = :test
|
18
|
+
swap_metadata
|
19
|
+
|
20
|
+
before :each do
|
21
|
+
crystal.clear
|
22
|
+
crystal[:config].environment = environment
|
23
|
+
end
|
24
|
+
|
25
|
+
after :each do
|
26
|
+
crystal.clear
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
inject(
|
31
|
+
:config => :config,
|
32
|
+
:logger => :logger
|
33
|
+
)
|
34
|
+
|
35
|
+
def silence_logger!
|
36
|
+
self.logger = Logger.new nil
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# Spec::Runner.configure do |config|
|
41
|
+
# end
|