alfa 0.0.5.pre → 0.0.6.pre
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/dummy/project/.gitignore +16 -0
- data/dummy/project/.ruby-version +1 -0
- data/dummy/project/CHANGELOG +0 -0
- data/dummy/project/Gemfile.source +7 -2
- data/dummy/project/README +0 -0
- data/dummy/project/apps/admin/layouts/base.haml +10 -0
- data/dummy/project/apps/admin/layouts/index.haml.source +7 -0
- data/dummy/project/apps/admin/templates/default/index.haml +1 -0
- data/dummy/project/apps/frontend/layouts/base.haml +10 -0
- data/dummy/project/apps/frontend/layouts/index.haml.source +7 -0
- data/dummy/project/apps/frontend/templates/default/{index.tpl → index.haml} +0 -0
- data/dummy/project/config/db.rb +15 -0
- data/dummy/project/config/passwords/.gitignore.source +3 -0
- data/dummy/project/config/passwords/db-main.yml.source +8 -0
- data/dummy/project/config/passwords/secrets.sample.yml +2 -0
- data/dummy/project/config/passwords/secrets.yml.source +2 -0
- data/dummy/project/config/web_application.rb +1 -0
- data/dummy/project/db/main/migrations/.keep +0 -0
- data/dummy/project/db/main/models/.keep +0 -0
- data/dummy/project/db/main/schema/.keep +0 -0
- data/dummy/project/db/main/seed.rb +0 -0
- data/dummy/project/log/.keep +0 -0
- data/dummy/project/public/.htaccess +13 -0
- data/dummy/project/tasks/.keep +0 -0
- data/dummy/project/test/.keep +0 -0
- data/lib/alfa/application.rb +21 -1
- data/lib/alfa/commands/new.rb +14 -9
- data/lib/alfa/config.rb +3 -2
- data/lib/alfa/controller.rb +19 -104
- data/lib/alfa/database.rb +2 -1
- data/lib/alfa/exceptions.rb +5 -0
- data/lib/alfa/logger.rb +4 -3
- data/lib/alfa/resourcer.rb +29 -0
- data/lib/alfa/router.rb +34 -14
- data/lib/alfa/snippeter.rb +42 -0
- data/lib/alfa/support/common.rb +195 -0
- data/lib/alfa/support/nil_operations.rb +207 -0
- data/lib/alfa/support/time.rb +5 -0
- data/lib/alfa/support.rb +3 -135
- data/lib/alfa/tasks/assets.rake +16 -0
- data/lib/alfa/tasks/db.rake +169 -0
- data/lib/alfa/tasks/generators.rake +80 -0
- data/lib/alfa/tasks.rb +4 -0
- data/lib/alfa/tfile.rb +1 -1
- data/lib/alfa/user.rb +41 -6
- data/lib/alfa/web_application.rb +96 -31
- data/lib/alfa/wrapper.rb +155 -0
- data/lib/alfa.rb +0 -2
- data/lib/rack/file_alfa.rb +2 -1
- data/lib/sequel/extensions/igrep.rb +19 -0
- data/lib/sequel/extensions/with_pks.rb +15 -0
- data/lib/template-inheritance/alfa_helpers.rb +106 -25
- data/lib/tilt/alfa_patch.rb +1 -1
- data/test/data/test_router/1/apps/frontend/routes.rb +1 -0
- data/test/data/test_router/2/apps/frontend/routes.rb +1 -0
- data/test/data/test_web_application/apps/admin/controllers/second.rb +5 -0
- data/test/data/test_web_application/apps/admin/layouts/default.haml +1 -0
- data/test/data/test_web_application/apps/admin/layouts/default.tpl +1 -0
- data/test/data/test_web_application/apps/admin/routes.rb +1 -0
- data/test/data/test_web_application/apps/admin/templates/default/test_04.haml +1 -0
- data/test/data/test_web_application/apps/admin/templates/default/test_04.tpl +1 -0
- data/test/data/test_web_application/apps/admin/templates/default/test_08.haml +2 -0
- data/test/data/test_web_application/apps/admin/templates/default/test_08a.haml +1 -0
- data/test/data/test_web_application/apps/admin/templates/second/index.haml +1 -0
- data/test/data/test_web_application/apps/frontend/controllers/second.rb +5 -0
- data/test/data/test_web_application/apps/frontend/layouts/default.haml +1 -0
- data/test/data/test_web_application/apps/frontend/layouts/default.tpl +1 -0
- data/test/data/test_web_application/apps/frontend/routes.rb +1 -0
- data/test/data/test_web_application/apps/frontend/templates/default/bar.haml +0 -0
- data/test/data/test_web_application/apps/frontend/templates/default/bar.tpl +0 -0
- data/test/data/test_web_application/apps/frontend/templates/default/frontend_only.haml +0 -0
- data/test/data/test_web_application/apps/frontend/templates/default/frontend_only.tpl +0 -0
- data/test/data/test_web_application/apps/frontend/templates/default/index.haml +0 -0
- data/test/data/test_web_application/apps/frontend/templates/default/index.tpl +0 -0
- data/test/data/test_web_application/apps/frontend/templates/default/test_04.haml +1 -0
- data/test/data/test_web_application/apps/frontend/templates/default/test_04.tpl +1 -0
- data/test/data/test_web_application/apps/frontend/templates/default/test_08.haml +2 -0
- data/test/data/test_web_application/apps/frontend/templates/default/test_08a.haml +1 -0
- data/test/data/test_web_application/apps/frontend/templates/second/index.haml +1 -0
- data/test/test_config.rb +2 -2
- data/test/test_controller.rb +13 -3
- data/test/test_router.rb +13 -6
- data/test/test_ruby_core.rb +32 -0
- data/test/test_support.rb +152 -11
- data/test/test_web_application.rb +7 -0
- data/version.rb +1 -1
- metadata +100 -16
- data/lib/alfa/database/mysql.rb +0 -107
- data/lib/alfa/models/base.rb +0 -7
- data/lib/alfa/models/base_sql.rb +0 -9
- data/lib/alfa/models/dummy.rb +0 -7
- data/lib/alfa/models/mysql.rb +0 -62
- data/lib/alfa/models.rb +0 -2
data/lib/alfa/wrapper.rb
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
require 'alfa/support'
|
2
|
+
require 'alfa/exceptions'
|
3
|
+
|
4
|
+
module Alfa
|
5
|
+
module WrapperMethods
|
6
|
+
def _instance_variables_hash
|
7
|
+
Hash[instance_variables.map { |name| [name.to_sym, instance_variable_get(name)] } ]
|
8
|
+
end
|
9
|
+
|
10
|
+
def _clear_instance_variables
|
11
|
+
instance_variables.each {|name| remove_instance_variable(name)}
|
12
|
+
end
|
13
|
+
|
14
|
+
def href(*o)
|
15
|
+
kwargs = _extract_href_params(*o)
|
16
|
+
@application.routes.href(kwargs)
|
17
|
+
end
|
18
|
+
|
19
|
+
alias :href_to :href
|
20
|
+
|
21
|
+
def _extract_href_params(*o)
|
22
|
+
args, kwargs = Support.args_kwargs(*o)
|
23
|
+
kwargs.merge!(_string_to_aca(args.first.to_s)) if args.any?
|
24
|
+
kwargs = {:app=>@app_sym}.merge kwargs
|
25
|
+
kwargs = {:controller=>@c_sym}.merge kwargs if kwargs[:action]
|
26
|
+
kwargs[:action] = :index if kwargs[:controller] && !kwargs[:action]
|
27
|
+
kwargs
|
28
|
+
end
|
29
|
+
|
30
|
+
# Convert string to App-Controller-Action hash
|
31
|
+
# 'app*controller#action'
|
32
|
+
def _string_to_aca(str)
|
33
|
+
res = {}
|
34
|
+
s1 = str.split('@')
|
35
|
+
raise Exceptions::E004.new("E004: Bad href argument #{str}: it should contain at most one @ symbol") if s1.length > 2
|
36
|
+
res[:app] = s1.last.to_sym if s1.length > 1
|
37
|
+
s2 = s1.first.split('#', -1)
|
38
|
+
raise Exceptions::E004.new("E004: Bad href argument #{str}: it should contain at most one # symbol") if s2.length > 2
|
39
|
+
if s2.length > 0
|
40
|
+
res[:action] = s2.last.to_sym unless s2.last.empty?
|
41
|
+
if s2.length > 1
|
42
|
+
res[:controller] = s2.first.to_sym unless s2.first.empty?
|
43
|
+
end
|
44
|
+
end
|
45
|
+
res
|
46
|
+
end
|
47
|
+
|
48
|
+
|
49
|
+
def session
|
50
|
+
@request.session
|
51
|
+
end
|
52
|
+
|
53
|
+
# Return current user
|
54
|
+
def user
|
55
|
+
@user ||= (
|
56
|
+
if @request.session[:user_id] && (u = ::User.first(id: @request.session[:user_id]))
|
57
|
+
Alfa::User.new(u)
|
58
|
+
else
|
59
|
+
GuestUser
|
60
|
+
end
|
61
|
+
)
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
def grant?(grant)
|
66
|
+
user.grant?(grant)
|
67
|
+
end
|
68
|
+
|
69
|
+
|
70
|
+
[300, 301, 302, 303].each do |code|
|
71
|
+
define_method ("redirect_#{code}".to_sym) do |url|
|
72
|
+
@application.redirect(url, code)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
alias :redirect :redirect_302
|
77
|
+
|
78
|
+
|
79
|
+
def try_login(login, password)
|
80
|
+
u = @application.config[:db][:main][:instance][:users].first(login: login)
|
81
|
+
raise "No such login: #{login}" unless u
|
82
|
+
if u[:passhash] == Digest::MD5.hexdigest("#{u[:salt]}#{password}")
|
83
|
+
# success
|
84
|
+
session[:user_id] = u[:id]
|
85
|
+
return true
|
86
|
+
else
|
87
|
+
# fail
|
88
|
+
session[:user_id] = nil
|
89
|
+
raise 'login fail'
|
90
|
+
return false
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
def try_register(login, password)
|
96
|
+
@application.try_register(login, password)
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
def try_logout
|
101
|
+
session[:user_id] = nil
|
102
|
+
@user = GuestUser
|
103
|
+
end
|
104
|
+
|
105
|
+
# Store flash message to session
|
106
|
+
def flash(message)
|
107
|
+
|
108
|
+
end
|
109
|
+
|
110
|
+
|
111
|
+
def breadcrumb_match?(controller: nil, action: nil)
|
112
|
+
(controller ? (@route[:options][:controller] ? @route[:options][:controller] == controller : @params[:controller] == controller) : true) &&
|
113
|
+
(action ? (@route[:options][:action] ? @route[:options][:action] == action : @params[:action] == action) : true)
|
114
|
+
end
|
115
|
+
|
116
|
+
|
117
|
+
def csrf_token
|
118
|
+
session[:csrf_token] ||= SecureRandom.base64(12)
|
119
|
+
end
|
120
|
+
|
121
|
+
def check_csrf_token
|
122
|
+
obtained_token = @request.post? ? @request.POST['csrf_token'] : @request.GET['csrf_token']
|
123
|
+
raise Alfa::Exceptions::Route403, 'CSRF token fail' unless obtained_token == csrf_token
|
124
|
+
true
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
|
129
|
+
# Wrapper class for snippets and actions
|
130
|
+
class Wrapper
|
131
|
+
include Alfa::WrapperMethods
|
132
|
+
|
133
|
+
attr_reader :application, :request, :app_sym, :c_sym, :resourcer, :params
|
134
|
+
|
135
|
+
def initialize(application: nil, request: nil, app_sym: nil, c_sym: nil, resourcer: nil, params: nil, route: nil, caller: nil)
|
136
|
+
if caller
|
137
|
+
@application = caller.application
|
138
|
+
@request = caller.request
|
139
|
+
@app_sym = caller.app_sym
|
140
|
+
@c_sym = caller.c_sym
|
141
|
+
@resourcer = caller.resourcer
|
142
|
+
@params = caller.params
|
143
|
+
@route = caller.route
|
144
|
+
else
|
145
|
+
@application = application
|
146
|
+
@request = request
|
147
|
+
@app_sym = app_sym
|
148
|
+
@c_sym = c_sym
|
149
|
+
@resourcer = resourcer
|
150
|
+
@params = params
|
151
|
+
@route = route
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
data/lib/alfa.rb
CHANGED
data/lib/rack/file_alfa.rb
CHANGED
@@ -3,8 +3,9 @@ require 'rack/file'
|
|
3
3
|
module Rack
|
4
4
|
class FileAlfa < File
|
5
5
|
#Deny .htaccess files
|
6
|
-
def
|
6
|
+
def _call(env)
|
7
7
|
return fail(404, 'Not found') if env['PATH_INFO'].split(SEPS).last == '.htaccess'
|
8
|
+
@headers['Expires'] = (Time.now + 2592000).httpdate
|
8
9
|
super
|
9
10
|
end
|
10
11
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Sequel
|
2
|
+
class Dataset
|
3
|
+
QUERY_METHODS << :igrep
|
4
|
+
|
5
|
+
def igrep(columns, patterns, opts=OPTS)
|
6
|
+
self.grep(columns, patterns, opts.merge({:case_insensitive=>true}))
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
Dataset.register_extension :igrep
|
11
|
+
|
12
|
+
class Model
|
13
|
+
DATASET_METHODS << :igrep
|
14
|
+
|
15
|
+
module ClassMethods
|
16
|
+
Plugins.def_dataset_methods(self, [:igrep])
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -1,25 +1,27 @@
|
|
1
1
|
# Monkeypatch functionalfix for gem TemplateInheritance v0.3.1
|
2
2
|
module TemplateInheritance
|
3
|
-
class Template
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
3
|
+
# class Template
|
4
|
+
# attr_writer :resources
|
5
|
+
# def resources
|
6
|
+
#
|
7
|
+
# end
|
8
|
+
#
|
9
|
+
# def instantiate_supertemplate
|
10
|
+
# supertemplate = self.class.new(self.supertemplate, self.scope)
|
11
|
+
# supertemplate.blocks = self.blocks
|
12
|
+
# supertemplate.resources = self.resources
|
13
|
+
# supertemplate
|
14
|
+
# end
|
15
|
+
# end
|
16
16
|
|
17
17
|
|
18
18
|
class RenderScope
|
19
|
-
attr_reader :controller
|
19
|
+
attr_reader :controller, :wrapper
|
20
20
|
|
21
|
-
def initialize(controller = nil)
|
21
|
+
def initialize(controller = nil, wrapper = nil, resourcer = nil)
|
22
22
|
@controller = controller
|
23
|
+
@wrapper = wrapper
|
24
|
+
@resourcer = resourcer
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
@@ -38,13 +40,13 @@ module TemplateInheritance
|
|
38
40
|
when :alfa_classic, 'alfa_classic'
|
39
41
|
|
40
42
|
else
|
41
|
-
|
43
|
+
@resourcer[:styles] << src
|
42
44
|
end
|
43
45
|
end
|
44
46
|
|
45
47
|
def styles
|
46
|
-
|
47
|
-
if s.match(
|
48
|
+
@resourcer.styles.uniq.map{|s|
|
49
|
+
if s.match(/\A\/~assets\/(.*)/)
|
48
50
|
f = File.join(File.expand_path('../../../assets/', __FILE__), $1)
|
49
51
|
else
|
50
52
|
f = File.join(Alfa::WebApplication.config[:document_root], s)
|
@@ -60,33 +62,112 @@ module TemplateInheritance
|
|
60
62
|
|
61
63
|
def require_script(src, type: 'text/javascript')
|
62
64
|
raise ArgumentError, 'src required' if src.nil?
|
63
|
-
|
65
|
+
case src
|
66
|
+
when :jquery, 'jquery'
|
67
|
+
@resourcer[:scripts] << {src: '/~assets/js/jquery/jquery-1.11.0.min.js', type: 'text/javascript'}
|
68
|
+
else
|
69
|
+
@resourcer[:scripts] << {src: src, type: type}
|
70
|
+
end
|
64
71
|
end
|
65
72
|
|
66
73
|
def add_script(type: 'text/javascript', &block)
|
67
|
-
|
74
|
+
@resourcer[:scripts] << {code: self.template.scope.capture(&block), type: type}
|
68
75
|
end
|
69
76
|
|
70
77
|
def scripts
|
71
|
-
|
78
|
+
@resourcer.scripts.uniq.map{|s|
|
79
|
+
if (s[:src])
|
80
|
+
if s[:src].match(/\A\/~assets\/(.*)/)
|
81
|
+
f = File.join(File.expand_path('../../../assets/', __FILE__), $1)
|
82
|
+
else
|
83
|
+
f = File.join(Alfa::WebApplication.config[:document_root], s[:src])
|
84
|
+
end
|
85
|
+
mtime = File.exist?(f) ? File.mtime(f).to_i : nil
|
86
|
+
"<script type='#{s[:type]}' src='#{s[:src]}?#{mtime}'></script>\n"
|
87
|
+
else
|
88
|
+
"<script type='#{s[:type]}'>\n#{s[:code].rstrip}\n</script>\n"
|
89
|
+
end
|
90
|
+
}.join('')
|
72
91
|
end
|
73
92
|
|
74
93
|
def href(*o)
|
75
|
-
@
|
94
|
+
@wrapper.href(*o)
|
95
|
+
end
|
96
|
+
|
97
|
+
# TODO: Fix casual white-spaces around returned tag string
|
98
|
+
def a(text, url, attributes = {})
|
99
|
+
active_class = 'active'
|
100
|
+
if url.is_a?(Array)
|
101
|
+
args, params = ::Alfa::Support.args_kwargs(*url)
|
102
|
+
url = args.first
|
103
|
+
else
|
104
|
+
params = {}
|
105
|
+
end
|
106
|
+
if url.is_a?(Symbol)
|
107
|
+
active_class = attributes[:active_class] if attributes.has_key?(:active_class)
|
108
|
+
zp = @wrapper._string_to_aca(url.to_s)
|
109
|
+
attributes[:class] = "#{attributes[:class]} #{active_class}".strip if breadcrumb_match?(controller: zp[:controller], action: zp[:action])
|
110
|
+
url = href(url.to_s, params)
|
111
|
+
end
|
112
|
+
attributes.delete(:active_class)
|
113
|
+
attributes[:href] = url
|
114
|
+
capture_haml do
|
115
|
+
haml_tag(:a, text, attributes)
|
116
|
+
end
|
76
117
|
end
|
77
118
|
|
78
|
-
def
|
79
|
-
|
119
|
+
def a_post(text, url, attributes = {})
|
120
|
+
if url.is_a?(Array)
|
121
|
+
args, params = ::Alfa::Support.args_kwargs(*url)
|
122
|
+
urlf = args.first
|
123
|
+
else
|
124
|
+
urlf = url
|
125
|
+
params = {}
|
126
|
+
end
|
127
|
+
url_str = urlf.is_a?(Symbol) ? href(urlf.to_s, params) : urlf.to_s
|
128
|
+
attributes[:onclick] = "{var form=document.createElement(\"form\"); form.setAttribute(\"method\", \"post\"); form.setAttribute(\"action\", \"#{url_str}\"); document.body.appendChild(form); form.submit(); return false;}"
|
129
|
+
a(text, url, attributes)
|
80
130
|
end
|
81
131
|
|
82
132
|
alias :link_to :a
|
83
133
|
|
134
|
+
# TODO: Fix casual white-spaces around returned tag string
|
135
|
+
def img(attributes = {})
|
136
|
+
capture_haml do
|
137
|
+
haml_tag(:img, attributes)
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def application
|
142
|
+
@wrapper.application
|
143
|
+
end
|
144
|
+
|
84
145
|
def controller
|
85
146
|
@controller
|
86
147
|
end
|
87
148
|
|
88
149
|
def user
|
89
|
-
@
|
150
|
+
@wrapper.user
|
151
|
+
end
|
152
|
+
|
153
|
+
def grant?(*o)
|
154
|
+
user.grant?(*o)
|
155
|
+
end
|
156
|
+
|
157
|
+
def snippet(name)
|
158
|
+
@wrapper.application.snippet(name, @wrapper)
|
159
|
+
end
|
160
|
+
|
161
|
+
def breadcrumb_match?(controller: nil, action: nil)
|
162
|
+
@wrapper.breadcrumb_match?(controller: controller, action: action)
|
163
|
+
end
|
164
|
+
|
165
|
+
def csrf_token_tag
|
166
|
+
"<input type='hidden' name='csrf_token' value='#{@wrapper.csrf_token}'/>"
|
167
|
+
end
|
168
|
+
|
169
|
+
def csrf_token
|
170
|
+
@wrapper.csrf_token
|
90
171
|
end
|
91
172
|
end
|
92
173
|
end
|
data/lib/tilt/alfa_patch.rb
CHANGED
@@ -18,7 +18,7 @@ end
|
|
18
18
|
module TemplateInheritance
|
19
19
|
class Template
|
20
20
|
def template(options = {})
|
21
|
-
options = {:escape_html => true, :raw_interpolated_tags => [:a, :link_to]}.merge(options)
|
21
|
+
options = {:escape_html => true, :raw_interpolated_tags => [:a, :link_to, :a_post, :img, :csrf_token, :csrf_token_tag]}.merge(options)
|
22
22
|
@template ||= Tilt.new(self.fullpath, nil, options)
|
23
23
|
end
|
24
24
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
Alfa::Router.draw do
|
2
2
|
route '/', :controller => :main, :action => :index, :layout => :index
|
3
3
|
route '/:action', :controller => :main, :layout => :internal
|
4
|
+
route '/:controller', action: :index, layout: :internal
|
4
5
|
route '/:controller/:action', :layout => :internal
|
5
6
|
route '/:controller/:action/:id', :layout => :internal
|
6
7
|
end
|
@@ -1,6 +1,7 @@
|
|
1
1
|
Alfa::Router.draw do
|
2
2
|
route '/' => 'main#index', :layout => :index
|
3
3
|
route '/:action', :controller => :main, :layout => :internal
|
4
|
+
route '/:controller', action: :index, layout: :internal
|
4
5
|
route '/:controller/:action', :layout => :internal
|
5
6
|
route '/:controller/:action/:id', :layout => :internal
|
6
7
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
!= @body
|
@@ -0,0 +1 @@
|
|
1
|
+
{{@body|raw}}
|
@@ -0,0 +1 @@
|
|
1
|
+
#{@str}
|
@@ -0,0 +1 @@
|
|
1
|
+
{{@str}}
|
@@ -0,0 +1 @@
|
|
1
|
+
!= controller.hash
|
@@ -0,0 +1 @@
|
|
1
|
+
second#index@admin
|
@@ -0,0 +1 @@
|
|
1
|
+
!= @body
|
@@ -0,0 +1 @@
|
|
1
|
+
{{@body|raw}}
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1 @@
|
|
1
|
+
#{@str}
|
@@ -0,0 +1 @@
|
|
1
|
+
{{@str}}
|
@@ -0,0 +1 @@
|
|
1
|
+
!= controller.hash
|
@@ -0,0 +1 @@
|
|
1
|
+
second#index@frontend
|
data/test/test_config.rb
CHANGED
@@ -19,8 +19,8 @@ class AlfaConfigTest < Test::Unit::TestCase
|
|
19
19
|
assert_raise RuntimeError do
|
20
20
|
config.store(:log, nil)
|
21
21
|
end
|
22
|
-
assert_equal({:db=>{}, :log=>{}}, config)
|
22
|
+
assert_equal({:db=>{}, :log=>{}, :session=>{:key=>'session', :secret=>nil}}, config)
|
23
23
|
config[:foo] = 1
|
24
|
-
assert_equal({:db=>{}, :log=>{}, :foo=>1}, config)
|
24
|
+
assert_equal({:db=>{}, :log=>{}, :session=>{:key=>'session', :secret=>nil}, :foo=>1}, config)
|
25
25
|
end
|
26
26
|
end
|
data/test/test_controller.rb
CHANGED
@@ -16,9 +16,9 @@ end
|
|
16
16
|
EOL
|
17
17
|
z = Z.new
|
18
18
|
z.some_action
|
19
|
-
assert_equal({:@foo=>:bar}, z._instance_variables_hash)
|
19
|
+
assert_equal({:@foo=>:bar}, z._instance_variables_hash.except(:@route))
|
20
20
|
z.other_action
|
21
|
-
assert_equal({:@foo=>:bar, :@fuu=>:baz}, z._instance_variables_hash)
|
21
|
+
assert_equal({:@foo=>:bar, :@fuu=>:baz}, z._instance_variables_hash.except(:@route))
|
22
22
|
end
|
23
23
|
|
24
24
|
# _string_to_aca
|
@@ -26,8 +26,11 @@ EOL
|
|
26
26
|
c = Alfa::Controller.new
|
27
27
|
assert_equal({:action=>:foo}, c._string_to_aca('foo'))
|
28
28
|
assert_equal({:action=>:foo, :controller=>:default}, c._string_to_aca('default#foo'))
|
29
|
+
assert_equal({:controller=>:default}, c._string_to_aca('default#'))
|
30
|
+
assert_equal({:action=>:foo}, c._string_to_aca('#foo'))
|
29
31
|
assert_equal({:app=>:admin, :controller=>:default, :action=>:foo}, c._string_to_aca('default#foo@admin'))
|
30
32
|
assert_equal({:app=>:admin}, c._string_to_aca('@admin'))
|
33
|
+
assert_equal({:app=>:admin, :controller=>:default}, c._string_to_aca('default#@admin'))
|
31
34
|
assert_raise Alfa::Exceptions::E004 do c._string_to_aca('default#foo@admi@n') end
|
32
35
|
assert_raise Alfa::Exceptions::E004 do c._string_to_aca('de#fault#foo@admin') end
|
33
36
|
assert_raise Alfa::Exceptions::E004 do c._string_to_aca('#default#f#oo@admin') end
|
@@ -40,11 +43,18 @@ EOL
|
|
40
43
|
c.c_sym = :default
|
41
44
|
assert_equal({:app=>:frontend, :controller=>:default, :action=>:foo}, c._extract_href_params(:action=>:foo))
|
42
45
|
assert_equal({:app=>:frontend, :controller=>:default, :action=>:foo}, c._extract_href_params(:action=>:foo, :controller=>:default))
|
43
|
-
assert_equal({:app=>:frontend, :controller=>:default, :action=>:foo}, c._extract_href_params(:foo))
|
44
46
|
assert_equal({:app=>:frontend, :controller=>:default, :action=>:foo}, c._extract_href_params('foo'))
|
47
|
+
assert_equal({:app=>:frontend, :controller=>:default, :action=>:foo}, c._extract_href_params(:foo))
|
45
48
|
assert_equal({:app=>:frontend, :controller=>:admin, :action=>:foo}, c._extract_href_params('admin#foo'))
|
49
|
+
assert_equal({:app=>:frontend, :controller=>:admin, :action=>:foo}, c._extract_href_params(:'admin#foo'))
|
50
|
+
assert_equal({:app=>:frontend, :controller=>:admin, :action=>:index}, c._extract_href_params('admin#'))
|
51
|
+
assert_equal({:app=>:frontend, :controller=>:admin, :action=>:index}, c._extract_href_params(:'admin#'))
|
46
52
|
assert_equal({:app=>:zoo, :controller=>:admin, :action=>:foo}, c._extract_href_params('admin#foo', :app=>:zoo))
|
53
|
+
assert_equal({:app=>:zoo, :controller=>:admin, :action=>:foo}, c._extract_href_params(:'admin#foo', :app=>:zoo))
|
47
54
|
assert_equal({:app=>:zoo, :controller=>:admin, :action=>:foo}, c._extract_href_params('admin#foo@zoo'))
|
55
|
+
assert_equal({:app=>:zoo, :controller=>:admin, :action=>:foo}, c._extract_href_params(:'admin#foo@zoo'))
|
56
|
+
assert_equal({:app=>:zoo, :controller=>:admin, :action=>:index}, c._extract_href_params('admin#@zoo'))
|
57
|
+
assert_equal({:app=>:zoo, :controller=>:admin, :action=>:index}, c._extract_href_params(:'admin#@zoo'))
|
48
58
|
assert_equal({:app=>:admin}, c._extract_href_params(:app=>:admin))
|
49
59
|
end
|
50
60
|
end
|
data/test/test_router.rb
CHANGED
@@ -15,6 +15,9 @@ class AlfaRouterTest < Test::Unit::TestCase
|
|
15
15
|
assert_equal([true, {controller: :foo}], Alfa::Router.route_match?('/:controller/**', '/foo/bar/baz'))
|
16
16
|
assert_equal([true, {path: 'js/jquery/jquery-latest.js', type: :asset}], Alfa::Router.route_match?('/~assets/:path**', '/~assets/js/jquery/jquery-latest.js'))
|
17
17
|
assert_equal([true, {}], Alfa::Router.route_match?('/hello.html', '/hello.html'))
|
18
|
+
assert_equal([true, {action: :index}], Alfa::Router.route_match?('/foo/:action?', '/foo'))
|
19
|
+
assert_equal([true, {action: :index}], Alfa::Router.route_match?('/foo/:action?/', '/foo/'))
|
20
|
+
assert_equal([true, {action: :bar}], Alfa::Router.route_match?('/foo/:action?', '/foo/bar'))
|
18
21
|
|
19
22
|
# string rules, negative cases
|
20
23
|
assert_equal([false, {action: :foo}], Alfa::Router.route_match?('/:action', '/foo/'))
|
@@ -31,11 +34,11 @@ class AlfaRouterTest < Test::Unit::TestCase
|
|
31
34
|
assert_equal([false, {}], Alfa::Router.route_match?('/hello.html', '/~assets/js/jquery/jquery-latest.js'))
|
32
35
|
|
33
36
|
# regexp rules, positive cases
|
34
|
-
rule = Regexp.new('
|
37
|
+
rule = Regexp.new('\A/(?<controller>[^/]+)/(?<action>[^/]+)?\z')
|
35
38
|
assert_equal([true, {controller: 'default', action: 'index'}], Alfa::Router.route_match?(rule, '/default/index'))
|
36
39
|
|
37
40
|
# regexp rules, negative cases
|
38
|
-
rule = Regexp.new('
|
41
|
+
rule = Regexp.new('\A/(?<controller>[^/]+)/(?<action>[^/]+)?\z')
|
39
42
|
assert_equal([false, {}], Alfa::Router.route_match?(rule, '/'))
|
40
43
|
end
|
41
44
|
|
@@ -70,6 +73,7 @@ class AlfaRouterTest < Test::Unit::TestCase
|
|
70
73
|
Alfa::Router.draw do
|
71
74
|
route '/', :controller => :main, :action => :index, :layout => :index
|
72
75
|
route '/:action', :controller => :main, :layout => :internal
|
76
|
+
route '/:controller', :action => :index, :layout => :internal
|
73
77
|
route '/:controller/:action', :layout => :internal
|
74
78
|
route '/:controller/:action/:id', :layout => :internal
|
75
79
|
end
|
@@ -94,6 +98,7 @@ class AlfaRouterTest < Test::Unit::TestCase
|
|
94
98
|
:routes=>[
|
95
99
|
{:rule=>"/", :options=>{:controller=>:main, :action=>:index, :layout=>:index}},
|
96
100
|
{:rule=>"/:action", :options=>{:controller=>:main, :layout=>:internal}},
|
101
|
+
{:rule=>"/:controller", :options=>{:action=>:index, :layout=>:internal}},
|
97
102
|
{:rule=>"/:controller/:action", :options=>{:layout=>:internal}},
|
98
103
|
{:rule=>"/:controller/:action/:id", :options=>{:layout=>:internal}},
|
99
104
|
]},
|
@@ -111,7 +116,7 @@ class AlfaRouterTest < Test::Unit::TestCase
|
|
111
116
|
assert_equal([{rule: '/', options: {app: :frontend, controller: :main, action: :index, layout: :index}}, {}], Alfa::Router.find_route('/'))
|
112
117
|
assert_equal([{rule: '/:action', options: {app: :frontend, controller: :main, layout: :internal}}, {action: :foo}], Alfa::Router.find_route('/foo'))
|
113
118
|
assert_equal([{rule: '/:controller/:action', options: {app: :frontend, layout: :internal}}, {controller: :foo, action: :bar}], Alfa::Router.find_route('/foo/bar'))
|
114
|
-
assert_equal([{rule: '/:controller/:action/:id', options: {app: :frontend, layout: :internal}}, {controller: :foo, action: :bar, id:
|
119
|
+
assert_equal([{rule: '/:controller/:action/:id', options: {app: :frontend, layout: :internal}}, {controller: :foo, action: :bar, id: '8'}], Alfa::Router.find_route('/foo/bar/8'))
|
115
120
|
assert_equal([{rule: '/', options: {app: :admin, controller: :main, action: :index, layout: :admin}}, {}], Alfa::Router.find_route('/admin/'))
|
116
121
|
assert_equal([{rule: '/:controller', options: {app: :admin, action: :index}}, {controller: :foo}], Alfa::Router.find_route('/admin/foo'))
|
117
122
|
assert_raise Alfa::Exceptions::Route404 do
|
@@ -138,6 +143,7 @@ class AlfaRouterTest < Test::Unit::TestCase
|
|
138
143
|
:routes=>[
|
139
144
|
{:rule=>"/", :options=>{:controller=>:main, :action=>:index, :layout=>:index}},
|
140
145
|
{:rule=>"/:action", :options=>{:controller=>:main, :layout=>:internal}},
|
146
|
+
{:rule=>"/:controller", :options=>{:action=>:index, :layout=>:internal}},
|
141
147
|
{:rule=>"/:controller/:action", :options=>{:layout=>:internal}},
|
142
148
|
{:rule=>"/:controller/:action/:id", :options=>{:layout=>:internal}},
|
143
149
|
]},
|
@@ -155,7 +161,7 @@ class AlfaRouterTest < Test::Unit::TestCase
|
|
155
161
|
assert_equal([{rule: '/', options: {app: :frontend, controller: :main, action: :index, layout: :index}}, {}], Alfa::Router.find_route('/'))
|
156
162
|
assert_equal([{rule: '/:action', options: {app: :frontend, controller: :main, layout: :internal}}, {action: :foo}], Alfa::Router.find_route('/foo'))
|
157
163
|
assert_equal([{rule: '/:controller/:action', options: {app: :frontend, layout: :internal}}, {controller: :foo, action: :bar}], Alfa::Router.find_route('/foo/bar'))
|
158
|
-
assert_equal([{rule: '/:controller/:action/:id', options: {app: :frontend, layout: :internal}}, {controller: :foo, action: :bar, id:
|
164
|
+
assert_equal([{rule: '/:controller/:action/:id', options: {app: :frontend, layout: :internal}}, {controller: :foo, action: :bar, id: '8'}], Alfa::Router.find_route('/foo/bar/8'))
|
159
165
|
assert_equal([{rule: '/', options: {app: :backend, controller: :main, action: :index, layout: :index}}, {}], Alfa::Router.find_route('/admin/'))
|
160
166
|
assert_equal([{rule: '/:controller', options: {app: :backend, action: :index}}, {controller: :foo}], Alfa::Router.find_route('/admin/foo'))
|
161
167
|
end
|
@@ -176,10 +182,10 @@ class AlfaRouterTest < Test::Unit::TestCase
|
|
176
182
|
[
|
177
183
|
{:rule=>"/~assets/:path**", :options=>{:type=>:asset}},
|
178
184
|
{:rule=>'/', :options=>{:controller=>:default, :action=>:index}},
|
179
|
-
{:rule=>'/zoo', :options=>{:controller=>:default, :action=>:zoo
|
185
|
+
{:rule=>'/zoo', :options=>{:layout => :default, :controller=>:default, :action=>:zoo}},
|
180
186
|
{:context=>{:app=>{:path=>'/admin/', :app=>:backend, :options=>{}}},
|
181
187
|
:routes=>[
|
182
|
-
{:rule=>'/', :options=>{:controller=>:kfk, :action=>:index
|
188
|
+
{:rule=>'/', :options=>{:layout=>:fantastic, :controller=>:kfk, :action=>:index}}
|
183
189
|
]},
|
184
190
|
],
|
185
191
|
Alfa::Router.instance_variable_get(:@routes)
|
@@ -203,6 +209,7 @@ class AlfaRouterTest < Test::Unit::TestCase
|
|
203
209
|
:routes=>[
|
204
210
|
{:rule=>"/", :options=>{:controller=>:main, :action=>:index, :layout=>:index}},
|
205
211
|
{:rule=>"/:action", :options=>{:controller=>:main, :layout=>:internal}},
|
212
|
+
{:rule=>"/:controller", :options=>{:action=>:index, :layout=>:internal}},
|
206
213
|
{:rule=>"/:controller/:action", :options=>{:layout=>:internal}},
|
207
214
|
{:rule=>"/:controller/:action/:id", :options=>{:layout=>:internal}},
|
208
215
|
]},
|