alfa 0.0.4.pre → 0.0.5.pre

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/assets/js/jquery/jquery-1.11.0.js +10337 -0
  3. data/assets/js/jquery/jquery-1.11.0.min.js +4 -0
  4. data/assets/js/jquery/jquery-latest.js +4 -2
  5. data/dummy/project/Gemfile.source +6 -0
  6. data/dummy/project/Rakefile +5 -0
  7. data/dummy/project/apps/admin/controllers/default.rb +4 -0
  8. data/dummy/project/apps/admin/layouts/base.tpl +14 -0
  9. data/dummy/project/apps/admin/layouts/index.tpl.source +5 -0
  10. data/dummy/project/apps/admin/routes.rb +3 -0
  11. data/dummy/project/apps/admin/templates/default/index.tpl +1 -0
  12. data/dummy/project/apps/frontend/controllers/default.rb +5 -0
  13. data/dummy/project/apps/frontend/layouts/base.tpl +14 -0
  14. data/dummy/project/apps/frontend/layouts/index.tpl.source +5 -0
  15. data/dummy/project/apps/frontend/routes.rb +11 -0
  16. data/dummy/project/apps/frontend/templates/default/index.tpl +1 -0
  17. data/dummy/project/config/cli_application.rb +14 -0
  18. data/dummy/project/config/config.rb +11 -0
  19. data/dummy/project/config/db.rb +11 -0
  20. data/dummy/project/config/env.rb +6 -0
  21. data/dummy/project/config/groups.rb +17 -0
  22. data/dummy/project/config/passwords/db.sample.yml +8 -0
  23. data/dummy/project/config/routes.rb +4 -0
  24. data/dummy/project/config/setup_load_paths.rb +14 -0
  25. data/dummy/project/config/web_application.rb +15 -0
  26. data/dummy/project/config.ru +3 -0
  27. data/dummy/project/db/main/schema.yml +3 -0
  28. data/dummy/project/public/favicon.ico +0 -0
  29. data/dummy/project/public/robots.txt +1 -0
  30. data/lib/alfa/application.rb +6 -5
  31. data/lib/alfa/commands/new.rb +49 -4
  32. data/lib/alfa/controller.rb +118 -1
  33. data/lib/alfa/database/mysql.rb +1 -1
  34. data/lib/alfa/exceptions.rb +13 -0
  35. data/lib/alfa/logger.rb +3 -0
  36. data/lib/alfa/router.rb +25 -0
  37. data/lib/alfa/ruty.rb +10 -0
  38. data/lib/alfa/support.rb +39 -4
  39. data/lib/alfa/template-inheritance.rb +3 -0
  40. data/lib/alfa/user.rb +63 -0
  41. data/lib/alfa/web_application.rb +113 -39
  42. data/lib/haml/alfa_patch.rb +25 -0
  43. data/lib/rack/file_alfa.rb +11 -0
  44. data/lib/ruty/tags/href.rb +12 -0
  45. data/lib/ruty/upgrade.rb +1 -1
  46. data/lib/template-inheritance/alfa_bugfix.rb +15 -0
  47. data/lib/template-inheritance/alfa_helpers.rb +93 -0
  48. data/lib/tilt/alfa_patch.rb +25 -0
  49. data/test/data/test_web_application/apps/admin/controllers/default.rb +20 -0
  50. data/test/data/test_web_application/apps/admin/routes.rb +3 -0
  51. data/test/data/test_web_application/apps/frontend/controllers/default.rb +35 -0
  52. data/test/data/test_web_application/apps/frontend/routes.rb +2 -2
  53. data/test/data/test_web_application/config/routes.rb +1 -0
  54. data/test/test_controller.rb +30 -2
  55. data/test/test_router.rb +24 -0
  56. data/test/test_support.rb +72 -19
  57. data/test/test_web_application.rb +72 -5
  58. data/version.rb +1 -0
  59. metadata +143 -74
  60. data/test/data/test_web_application/apps/frontend/controllers/kfk.rb +0 -9
@@ -1,22 +1,32 @@
1
- # coding: utf-8
2
1
  require 'alfa/support'
3
2
  require 'alfa/exceptions'
4
3
  require 'alfa/application'
5
4
  require 'alfa/tfile'
6
5
  require 'alfa/controller'
7
6
  require 'alfa/router'
8
- require 'ruty'
9
- require 'ruty/bugfix'
10
- require 'ruty/upgrade'
11
- require 'ruty/tags/resources'
7
+ require 'alfa/ruty'
8
+ require 'alfa/user'
9
+ require 'rack/utils'
10
+ require 'rack/request'
11
+ require 'rack/file_alfa'
12
+ require 'digest/md5'
13
+ require 'securerandom'
14
+ require 'haml'
15
+ require 'alfa/template-inheritance'
16
+ require 'tilt/alfa_patch'
17
+ require 'haml/alfa_patch'
12
18
 
13
19
  module Alfa
14
20
  class WebApplication < Alfa::Application
15
21
 
16
- @namespaces_stack = []
17
22
  @bputs = []
23
+ @haml_templates = {}
18
24
 
19
- def self.inherited subclass
25
+ class << self
26
+ attr_reader :request
27
+ end
28
+
29
+ def self.inherited(subclass)
20
30
  instance_variables.each do |var|
21
31
  subclass.instance_variable_set(var, instance_variable_get(var))
22
32
  end
@@ -27,56 +37,74 @@ module Alfa
27
37
  Alfa::Router.reset
28
38
  Alfa::Router.apps_dir = File.join(@config[:project_root], 'apps')
29
39
  load File.join(@config[:project_root], 'config/routes.rb')
40
+ TemplateInheritance.logger = @logger
41
+ Alfa.GROUPS = @config[:groups]
30
42
  end
31
43
 
32
- # main rack routine
33
- def self.call env
44
+ # main Rack routine
45
+ def self.call(env, &block)
34
46
  start_time = Time.now
35
47
  response_code = nil # required for store context inside @logger.portion
36
48
  headers = {} # required for store context inside @logger.portion
37
49
  body = nil # required for store context inside @logger.portion
38
50
  @logger.portion(:sync=>true) do |l|
39
51
  @config[:db].each_value { |db| db[:instance].loggers = [l] }
40
- @env = env
52
+ TemplateInheritance.logger = l
41
53
  @bputs = []
42
54
  headers = {"Content-Type" => 'text/html; charset=utf-8'}
43
55
  t_sym = :default
44
56
  begin
45
57
  l.info "#{env['REQUEST_METHOD']} #{env['REQUEST_URI']} #{env['SERVER_PROTOCOL']} from #{env['REMOTE_ADDR']} at #{DateTime.now}"
46
- #l.info " HTTP_HOST: #{env['HTTP_HOST']}"
47
- #@logger.info " HTTP_ACCEPT: #{env['HTTP_ACCEPT']}"
48
- #@logger.info " HTTP_ACCEPT_LANGUAGE: #{env['HTTP_ACCEPT_LANGUAGE']}"
49
- #@logger.info " PATH_INFO: #{env['PATH_INFO']}"
58
+ # l.info " HTTP_HOST: #{env['HTTP_HOST']}"
59
+ # l.info " HTTP_ACCEPT: #{env['HTTP_ACCEPT']}"
60
+ # l.info " HTTP_ACCEPT_LANGUAGE: #{env['HTTP_ACCEPT_LANGUAGE']}"
61
+ # l.info " PATH_INFO: #{env['PATH_INFO']}"
50
62
  response_code = 200
51
- route, params = self.routes.find_route(@env['PATH_INFO'])
63
+ route, params = self.routes.find_route(Rack::Utils.unescape(env['PATH_INFO']))
52
64
  t_sym = route[:options].has_key?(:type) ? route[:options][:type] : :default
53
65
  if t_sym == :asset
54
- body = File.read(File.expand_path('../../../assets/' + params[:path], __FILE__))
66
+ realpath = File.expand_path('../../../assets/' + params[:path], __FILE__)
67
+ body = File.read(realpath)
55
68
  case File.extname(params[:path]).downcase
56
69
  when '.js'
57
- headers = {'Content-Type' => 'application/javascript; charset=utf-8'}
70
+ headers['Content-Type'] = 'application/javascript; charset=utf-8'
58
71
  when '.css'
59
- headers = {'Content-Type' => 'text/css; charset=utf-8'}
72
+ headers['Content-Type'] = 'text/css; charset=utf-8'
60
73
  else
61
74
  end
75
+ headers['Last-Modified'] = File.mtime(realpath).httpdate
76
+ headers['Cache-Control'] = 'max-age=2592000'
77
+ headers['Expires'] = (Time.now + 2592000).httpdate
62
78
  else
79
+ request = Rack::Request.new(env) # weakref?
63
80
  app_sym = route[:options].has_key?(:app) ? route[:options][:app] : params[:app]
64
81
  c_sym = route[:options].has_key?(:controller) ? route[:options][:controller] : params[:controller]
65
82
  a_sym = route[:options].has_key?(:action) ? route[:options][:action] : params[:action]
66
83
  l_sym = route[:options].has_key?(:layout) ? route[:options][:layout] : :default
67
- controller = self.invoke_controller_check_action(app_sym, c_sym, a_sym)
84
+ controller = self.invoke_controller(app_sym, c_sym)
85
+ raise Exceptions::Route404 unless controller.class.instance_methods(false).include?(a_sym)
86
+ controller._clear_instance_variables # cleanup
87
+ controller.application = self
88
+ controller.request = request
89
+ controller.app_sym = app_sym
90
+ controller.c_sym = c_sym
68
91
  controller.__send__(a_sym)
69
92
  data = controller._instance_variables_hash
70
- Ruty::Tags::RequireStyle.clean_cache
71
- content = self.render_template(app_sym.to_s, File.join(c_sym.to_s, a_sym.to_s + '.tpl'), data)
72
- body = self.render_layout(app_sym.to_s, l_sym.to_s + '.tpl', {body: content})
73
- headers = {"Content-Type" => 'text/html; charset=utf-8'}
93
+ Ruty::Tags::RequireStyle.clean_cache # cleanup
94
+ Ruty::Tags::RequireScript.clean_cache # cleanup
95
+ content = self.render_template(app_sym, c_sym, a_sym, controller, data, &block)
96
+ body = self.render_layout(app_sym.to_s, l_sym.to_s, data.merge({:@body => content}))
97
+ headers["Content-Type"] = 'text/html; charset=utf-8'
74
98
  end
75
99
  rescue Alfa::Exceptions::Route404 => e
76
100
  response_code = 404
77
101
  body = 'Url not found<br>urls map:<br>'
78
102
  body += self.routes.instance_variable_get(:@routes).inspect
79
103
  l.info "404: Url not found (#{e.message})"
104
+ rescue Exceptions::HttpRedirect => e
105
+ response_code = e.code
106
+ headers['Location'] = e.url.to_s
107
+ body = ''
80
108
  rescue Exception => e
81
109
  response_code = 500
82
110
  body = "Error occured: #{e.message} at #{e.backtrace.first}<br>Full backtrace:<br>\n#{e.backtrace.join("<br>\n")}"
@@ -102,39 +130,85 @@ module Alfa
102
130
  end
103
131
 
104
132
 
133
+ def self.rackup(builder)
134
+ builder.use Rack::Session::Cookie
135
+ if @config[:serve_static]
136
+ builder.run Rack::Cascade.new([
137
+ Rack::FileAlfa.new(@config[:document_root]),
138
+ self,
139
+ ])
140
+ else
141
+ builder.run self
142
+ end
143
+ end
144
+
145
+
105
146
  def self.bputs arg
106
147
  @bputs << "#{arg}\n"
107
148
  end
108
149
 
109
150
 
151
+ def self.redirect(url, code=302)
152
+ raise Exceptions::HttpRedirect.new(url, code)
153
+ end
154
+
110
155
  # private section
111
156
 
112
157
  def self.verify_config
113
158
  super
114
- raise Exceptions::E002.new unless @config[:document_root]
159
+ raise Exceptions::E002.new('config[:document_root] should be defined') unless @config[:document_root]
160
+ raise Exceptions::E002.new('config[:templates_priority] should be defined') unless @config[:templates_priority]
161
+ raise Exceptions::E001.new('config[:groups] should be a hash') unless @config[:groups].is_a?(::Hash)
162
+ @config[:groups][:public] = [] unless @config[:groups][:public]
115
163
  end
116
164
 
117
- def self.invoke_controller_check_action application, controller, action
118
- @controllers ||= {}
119
- require File.join(@config[:project_root], 'apps', application.to_s, 'controllers', controller.to_s)
120
- # @todo put klass to controllers cache
121
- klass = Kernel.const_get(Alfa::Support.capitalize_name(controller)+'Controller')
122
- raise Exceptions::Route404 unless klass.instance_methods(false).include?(action)
123
- @controllers[[application, controller]] ||= klass.new
165
+ def self.invoke_controller(a_sym, c_sym)
166
+ load File.join(@config[:project_root], 'apps', a_sym.to_s, 'controllers', c_sym.to_s + '.rb')
167
+ klass_name = Alfa::Support.camelcase_name(c_sym)+'Controller'
168
+ klass = Kernel.const_get(klass_name) # weakref?
169
+ instance = klass.new
170
+ Object.module_eval{remove_const(klass_name)}
171
+ return instance
124
172
  end
125
173
 
126
- def self.render_template app, template, data = {}
127
- t = self.loader.get_template File.join(app, 'templates', template)
128
- t.render data
174
+ def self.render_template(app_sym, c_sym, a_sym, controller, data = {}, &block)
175
+ render(file: File.join(@config[:project_root], 'apps', app_sym.to_s, 'templates', c_sym.to_s, a_sym.to_s), controller: controller, data: data, &block)
176
+ end
177
+
178
+ def self.render_layout(app, layout, data = {})
179
+ render(file: File.join(@config[:project_root], 'apps', app, 'layouts', layout.to_s), data: data)
180
+ end
181
+
182
+ def self.render(file: nil, controller: nil, data: {})
183
+ @config[:templates_priority].each do |ext|
184
+ f = "#{file}.#{ext}"
185
+ if File.exist?(f)
186
+ case ext
187
+ when :haml
188
+ template = self.haml_template(f, controller)
189
+ yield(controller, template) if block_given? # required only for thread isolation test
190
+ return template.render data
191
+ when :tpl
192
+ Ruty::AUX_VARS[:controller] = controller
193
+ template = self.ruty_loader.get_template(f)
194
+ return template.render data
195
+ else
196
+ raise StandardError.new("Unknown template type: #{ext}")
197
+ end
198
+ break
199
+ end
200
+ end
201
+ raise StandardError.new("Can't find template #{file}.[#{@config[:templates_priority].join('|')}]")
129
202
  end
130
203
 
131
- def self.render_layout app, layout, data = {}
132
- t = self.loader.get_template File.join(app, 'layouts', layout)
133
- t.render data
204
+ def self.ruty_loader
205
+ @ruty_loader ||= Ruty::Loaders::Filesystem.new(:dirname => File.join(@config[:project_root], 'apps'))
134
206
  end
135
207
 
136
- def self.loader
137
- @loader ||= Ruty::Loaders::Filesystem.new(:dirname => File.join(@config[:project_root], 'apps'))
208
+ def self.haml_template(file, controller)
209
+ # @haml_templates[file.to_sym] ||= TemplateInheritance::Template.new(file)
210
+ scope = TemplateInheritance::RenderScope.new(controller)
211
+ TemplateInheritance::Template.new(file, scope)
138
212
  end
139
213
 
140
214
  end
@@ -0,0 +1,25 @@
1
+ module Haml
2
+ class Options
3
+ @defaults = @defaults.merge({:raw_interpolated_tags => []})
4
+ attr_accessor :raw_interpolated_tags
5
+ end
6
+
7
+ module Util
8
+ def unescape_interpolation(str, escape_html = nil)
9
+ res = ''
10
+ rest = Haml::Util.handle_interpolation str.dump do |scan|
11
+ escapes = (scan[2].size - 1) / 2
12
+ res << scan.matched[0...-3 - escapes]
13
+ if escapes % 2 == 1
14
+ res << '#{'
15
+ else
16
+ content = eval('"' + balance(scan, ?{, ?}, 1)[0][0...-1] + '"')
17
+ tag = content[/(\S+\s?){1}/].strip.to_sym
18
+ content = "Haml::Helpers.html_escape((#{content}))" if escape_html && !@options[:raw_interpolated_tags].include?(tag)
19
+ res << '#{' + content + "}"# Use eval to get rid of string escapes
20
+ end
21
+ end
22
+ res + rest
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ require 'rack/file'
2
+
3
+ module Rack
4
+ class FileAlfa < File
5
+ #Deny .htaccess files
6
+ def call(env)
7
+ return fail(404, 'Not found') if env['PATH_INFO'].split(SEPS).last == '.htaccess'
8
+ super
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ # HREF
2
+
3
+ class Ruty::Tags::Href < Ruty::Tag
4
+ def initialize(parser, argstring)
5
+ @argstring = argstring
6
+ end
7
+ def render_node(context, stream)
8
+ stream << Ruty::AUX_VARS[:controller].href(@argstring)
9
+ end
10
+ Ruty::Tags.register(self, :href)
11
+ Ruty::Tags.register(self, :href_to)
12
+ end
data/lib/ruty/upgrade.rb CHANGED
@@ -1,4 +1,4 @@
1
- # escape variables by default
1
+ # Escaping variables by default
2
2
  # {{ variable }} transforms to {{ variable|escape }}
3
3
  # {{ variable|raw }} transforms to {{ variable }}
4
4
  module Ruty
@@ -0,0 +1,15 @@
1
+ # Monkeypatch bugfix for gem TemplateInheritance v0.3.1
2
+ # Without this patch the line 10 in template-inheritance/exts/tilt.rb
3
+ # klass.send(:remove_method, :initialize_engine)
4
+ # raises error during initialization because Tilt 2.0.0 have no method Tilt::HamlTemplate.initialize_engine
5
+ # Correct syntax in tilt.rb should be:
6
+ # klass.send(:remove_method, :initialize_engine) if klass.respond_to?(:initialize_engine)
7
+ module Tilt
8
+ class HamlTemplate
9
+ unless self.respond_to?(:initialize_engine)
10
+ def initialize_engine
11
+ end
12
+ end
13
+ end
14
+ end
15
+ # End of patch
@@ -0,0 +1,93 @@
1
+ # Monkeypatch functionalfix for gem TemplateInheritance v0.3.1
2
+ module TemplateInheritance
3
+ class Template
4
+ attr_writer :resources
5
+ def resources
6
+ @resources ||= {styles: [], scripts:[], added_scripts: []}
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
+
17
+
18
+ class RenderScope
19
+ attr_reader :controller
20
+
21
+ def initialize(controller = nil)
22
+ @controller = controller
23
+ end
24
+ end
25
+
26
+
27
+ module TemplateHelpers
28
+ def require_style(src, *modes)
29
+ case src
30
+ when :'960gs', '960gs'
31
+ require_style '/~assets/css/960gs/reset.css' if modes.include?(:reset)
32
+ require_style '/~assets/css/960gs/text.css' if modes.include?(:text)
33
+ require_style '/~assets/css/960gs/960.css'
34
+ when :'960gs24', '960gs24'
35
+ require_style '/~assets/css/960gs/reset.css' if modes.include?(:reset)
36
+ require_style '/~assets/css/960gs/text.css' if modes.include?(:text)
37
+ require_style '/~assets/css/960gs/960_24_col.css'
38
+ when :alfa_classic, 'alfa_classic'
39
+
40
+ else
41
+ self.template.resources[:styles] << src
42
+ end
43
+ end
44
+
45
+ def styles
46
+ self.template.resources[:styles].uniq.map{|s|
47
+ if s.match(/^\/~assets\/(.*)/)
48
+ f = File.join(File.expand_path('../../../assets/', __FILE__), $1)
49
+ else
50
+ f = File.join(Alfa::WebApplication.config[:document_root], s)
51
+ end
52
+ mtime = File.exist?(f) ? File.mtime(f).to_i : nil
53
+ "<link rel=\"stylesheet\" type=\"text/css\" href=\"#{s}?#{mtime}\">\n"
54
+ }.join('')
55
+ end
56
+
57
+ def top_scripts
58
+
59
+ end
60
+
61
+ def require_script(src, type: 'text/javascript')
62
+ raise ArgumentError, 'src required' if src.nil?
63
+ self.template.resources[:scripts] << src
64
+ end
65
+
66
+ def add_script(type: 'text/javascript', &block)
67
+ self.template.resources[:scripts] << self.template.scope.capture(&block)
68
+ end
69
+
70
+ def scripts
71
+ self.template.resources[:scripts].reverse.uniq.map{|s| "<script type='text/javascript' src='#{s}'></script>\n" }.join('')
72
+ end
73
+
74
+ def href(*o)
75
+ @controller.href(*o)
76
+ end
77
+
78
+ def a(text, url)
79
+ "<a href='#{url}'>#{Haml::Helpers.html_escape(text)}</a>"
80
+ end
81
+
82
+ alias :link_to :a
83
+
84
+ def controller
85
+ @controller
86
+ end
87
+
88
+ def user
89
+ @controller.user
90
+ end
91
+ end
92
+ end
93
+ # End of patch
@@ -0,0 +1,25 @@
1
+ module Tilt
2
+ class Template
3
+ # This patch allows to use of @keys as well as keys
4
+ # Example:
5
+ # template.render({:@name => 'Piter'})
6
+ def local_extraction(local_keys)
7
+ local_keys.map do |k|
8
+ if k.to_s =~ /\A@?[a-z_][a-zA-Z_0-9]*\z/ # this line patched
9
+ "#{k} = locals[#{k.inspect}]"
10
+ else
11
+ raise "invalid locals key: #{k.inspect} (keys must be variable names)"
12
+ end
13
+ end.join("\n")
14
+ end
15
+ end
16
+ end
17
+
18
+ module TemplateInheritance
19
+ class Template
20
+ def template(options = {})
21
+ options = {:escape_html => true, :raw_interpolated_tags => [:a, :link_to]}.merge(options)
22
+ @template ||= Tilt.new(self.fullpath, nil, options)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,20 @@
1
+ class DefaultController < Alfa::Controller
2
+ def test_04
3
+ @str = 'Admin'
4
+ end
5
+
6
+ def test_06
7
+ end
8
+
9
+ def test_08
10
+ @request = request
11
+ @env = @request.env
12
+ @path_info = @env['PATH_INFO']
13
+ session[:foo] = :baz
14
+ @link = href('test_08')
15
+ end
16
+
17
+ def test_08a
18
+ session[:foo] = :faz
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ Alfa::WebApplication.routes.draw do
2
+ route '/:action', :controller => :default
3
+ end
@@ -0,0 +1,35 @@
1
+ class DefaultController < Alfa::Controller
2
+ def index
3
+ end
4
+
5
+ def bar
6
+ end
7
+
8
+ def test_04
9
+ @str = 'Frontend'
10
+ end
11
+
12
+ def frontend_only
13
+ end
14
+
15
+ def test_06
16
+ @some_var = :some_value
17
+ end
18
+
19
+ def test_07
20
+ @other_var = :other_value
21
+ end
22
+
23
+ def test_08
24
+ @request = request
25
+ @env = @request.env
26
+ @path_info = @env['PATH_INFO']
27
+ session[:foo] = :bar
28
+ @link = href('test_08')
29
+ end
30
+
31
+ def test_08a
32
+ @controller = self
33
+ session[:foo] = :far
34
+ end
35
+ end
@@ -1,4 +1,4 @@
1
1
  Alfa::WebApplication.routes.draw do
2
- route '/' => 'kfk#index'
3
- route '/:action', :controller => :kfk
2
+ route '/' => 'default#index'
3
+ route '/:action', :controller => :default
4
4
  end
@@ -1,3 +1,4 @@
1
1
  Alfa::WebApplication.routes.draw do
2
+ mount '/admin' => :admin
2
3
  mount '/' => :frontend
3
4
  end
@@ -1,5 +1,6 @@
1
1
  require 'test/unit'
2
2
  require 'alfa/controller'
3
+ require 'alfa/exceptions'
3
4
 
4
5
  class TestAlfaController < Test::Unit::TestCase
5
6
  def test_01
@@ -15,8 +16,35 @@ end
15
16
  EOL
16
17
  z = Z.new
17
18
  z.some_action
18
- assert_equal({:foo=>:bar}, z._instance_variables_hash)
19
+ assert_equal({:@foo=>:bar}, z._instance_variables_hash)
19
20
  z.other_action
20
- assert_equal({:foo=>:bar, :fuu=>:baz}, z._instance_variables_hash)
21
+ assert_equal({:@foo=>:bar, :@fuu=>:baz}, z._instance_variables_hash)
22
+ end
23
+
24
+ # _string_to_aca
25
+ def test_02
26
+ c = Alfa::Controller.new
27
+ assert_equal({:action=>:foo}, c._string_to_aca('foo'))
28
+ assert_equal({:action=>:foo, :controller=>:default}, c._string_to_aca('default#foo'))
29
+ assert_equal({:app=>:admin, :controller=>:default, :action=>:foo}, c._string_to_aca('default#foo@admin'))
30
+ assert_equal({:app=>:admin}, c._string_to_aca('@admin'))
31
+ assert_raise Alfa::Exceptions::E004 do c._string_to_aca('default#foo@admi@n') end
32
+ assert_raise Alfa::Exceptions::E004 do c._string_to_aca('de#fault#foo@admin') end
33
+ assert_raise Alfa::Exceptions::E004 do c._string_to_aca('#default#f#oo@admin') end
34
+ end
35
+
36
+ # _extract_href_params
37
+ def test_03
38
+ c = Alfa::Controller.new
39
+ c.app_sym = :frontend
40
+ c.c_sym = :default
41
+ assert_equal({:app=>:frontend, :controller=>:default, :action=>:foo}, c._extract_href_params(:action=>:foo))
42
+ 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
+ assert_equal({:app=>:frontend, :controller=>:default, :action=>:foo}, c._extract_href_params('foo'))
45
+ assert_equal({:app=>:frontend, :controller=>:admin, :action=>:foo}, c._extract_href_params('admin#foo'))
46
+ assert_equal({:app=>:zoo, :controller=>:admin, :action=>:foo}, c._extract_href_params('admin#foo', :app=>:zoo))
47
+ assert_equal({:app=>:zoo, :controller=>:admin, :action=>:foo}, c._extract_href_params('admin#foo@zoo'))
48
+ assert_equal({:app=>:admin}, c._extract_href_params(:app=>:admin))
21
49
  end
22
50
  end
data/test/test_router.rb CHANGED
@@ -211,4 +211,28 @@ class AlfaRouterTest < Test::Unit::TestCase
211
211
  )
212
212
  #puts Alfa::Router.instance_variable_get(:@routes).inspect
213
213
  end
214
+
215
+ # Building urls
216
+ def test_09
217
+ Alfa::Router.reset
218
+ Alfa::Router.apps_dir = File.expand_path('../data/test_router/2/apps', __FILE__)
219
+ load File.expand_path('../data/test_router/2/config/routes.rb', __FILE__)
220
+ assert_equal('/', Alfa::Router.href(:app=>:frontend, :controller=>:main, :action=>:index))
221
+ assert_equal('/', Alfa::Router.href(:app=>:frontend))
222
+ assert_equal('/hello', Alfa::Router.href(:app=>:frontend, :controller=>:main, :action=>:hello))
223
+ assert_equal('/hello', Alfa::Router.href(:app=>:frontend, :action=>:hello))
224
+ assert_equal('/hello/babuin', Alfa::Router.href(:app=>:frontend, :controller=>:hello, :action=>:babuin))
225
+ assert_equal('/hello/babuin/11', Alfa::Router.href(:app=>:frontend, :controller=>:hello, :action=>:babuin, :id=>11))
226
+ assert_equal('/admin/', Alfa::Router.href(:app=>:backend, :controller=>:main, :action=>:index))
227
+ assert_equal('/admin/', Alfa::Router.href(:app=>:backend))
228
+ assert_equal('/admin/', Alfa::Router.href(:app=>:backend, :controller=>:main))
229
+ assert_equal('/admin/babuin', Alfa::Router.href(:app=>:backend, :controller=>:babuin, :action=>:index))
230
+ assert_equal('/admin/babuin', Alfa::Router.href(:app=>:backend, :controller=>:babuin))
231
+
232
+ # Build urls with GET params
233
+ assert_equal('/?foo=bar', Alfa::Router.href(:app=>:frontend, :controller=>:main, :action=>:index, :params=>{:foo=>:bar}))
234
+ assert_equal('/?staki=gKyE', Alfa::Router.href(:app=>:frontend, :params=>{:staki=>'gKyE'}))
235
+ assert_equal('/?company=AT%26T', Alfa::Router.href(:app=>:frontend, :params=>{:company=>'AT&T'}))
236
+ end
237
+
214
238
  end