sinatra_more 0.0.14 → 0.1.0

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 (36) hide show
  1. data/README.rdoc +5 -1
  2. data/TODO +4 -4
  3. data/VERSION +1 -1
  4. data/lib/sinatra_more/markup_plugin/asset_tag_helpers.rb +34 -13
  5. data/lib/sinatra_more/markup_plugin/output_helpers.rb +4 -1
  6. data/lib/sinatra_more/markup_plugin/tag_helpers.rb +2 -1
  7. data/lib/sinatra_more/render_plugin/render_helpers.rb +4 -2
  8. data/lib/sinatra_more/warden_plugin/warden_helpers.rb +1 -1
  9. data/lib/sinatra_more/warden_plugin.rb +2 -0
  10. data/sinatra_more.gemspec +31 -5
  11. data/test/fixtures/markup_app/app.rb +47 -0
  12. data/test/fixtures/markup_app/views/capture_concat.erb +14 -0
  13. data/test/fixtures/markup_app/views/capture_concat.haml +13 -0
  14. data/test/fixtures/markup_app/views/content_tag.erb +11 -0
  15. data/test/fixtures/markup_app/views/content_tag.haml +9 -0
  16. data/test/fixtures/markup_app/views/link_to.erb +5 -0
  17. data/test/fixtures/markup_app/views/link_to.haml +4 -0
  18. data/test/fixtures/render_app/app.rb +36 -3
  19. data/test/fixtures/render_app/views/{bar → erb}/test.erb +0 -0
  20. data/test/fixtures/render_app/views/{foo → haml}/test.haml +0 -0
  21. data/test/fixtures/render_app/views/template/_user.haml +1 -0
  22. data/test/fixtures/render_app/views/template/haml_template.haml +1 -0
  23. data/test/fixtures/render_app/views/template/some_template.haml +2 -0
  24. data/test/fixtures/warden_app/app.rb +30 -1
  25. data/test/fixtures/warden_app/views/dashboard.haml +6 -0
  26. data/test/helper.rb +16 -2
  27. data/test/markup_plugin/test_asset_tag_helpers.rb +89 -0
  28. data/test/markup_plugin/test_form_builder.rb +5 -0
  29. data/test/markup_plugin/test_form_helpers.rb +5 -0
  30. data/test/markup_plugin/test_format_helpers.rb +41 -0
  31. data/test/markup_plugin/test_output_helpers.rb +44 -0
  32. data/test/markup_plugin/test_tag_helpers.rb +52 -0
  33. data/test/test_markup_plugin.rb +15 -0
  34. data/test/test_render_plugin.rb +39 -3
  35. data/test/test_warden_plugin.rb +87 -0
  36. metadata +30 -4
data/README.rdoc CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == Introduction
4
4
 
5
- (Not quite ready to be used yet!)
5
+ Note: This library is experimental and may not work 100% yet. This will be removed once more testing has been done.
6
6
 
7
7
  This will be a plugin which expand sinatra's capabilities in many ways.
8
8
  Note that certain template specific helpers are known to work with haml, erb, and erubis
@@ -273,6 +273,10 @@ Thanks to keldredd for the sinatra-helpers code that helped me to create erb cap
273
273
  * Nathan Esquenazi - Project creator and code maintainer
274
274
  * Arthur Chiu - Forming the idea and various code contributions
275
275
 
276
+ == Known Issues
277
+
278
+ * tag helper methods accepting blocks work quite unreliably in erb. Work must be done to improve this.
279
+
276
280
  == Note on Patches/Pull Requests
277
281
 
278
282
  * Fork the project.
data/TODO CHANGED
@@ -1,8 +1,5 @@
1
1
  = UNFINISHED
2
2
 
3
- * fix content_block_tag to eliminate need for concat option
4
- * image_tag should start in images_path (or /images)
5
- * I have got to add tests, basically create dummy sinatra applications and use Webrat
6
3
  * Become total warden solution (basically just require warden gem installed, do everything else)
7
4
  * Make warden password strategy support a callback which explains what to do with username, password
8
5
  * WardenPlugin.authenticate_callback { |username, password| User.authenticate(username, password) }
@@ -11,4 +8,7 @@
11
8
  = COMPLETED
12
9
 
13
10
  * Pull from sinatra-helpers and make erb templates work (and credit keldredd)
14
- - http://github.com/kelredd/sinatra-helpers/tree/master/lib/sinatra_helpers/erb/
11
+ - http://github.com/kelredd/sinatra-helpers/tree/master/lib/sinatra_helpers/erb/
12
+ * fix content_block_tag to eliminate need for concat option
13
+ * image_tag should start in images_path (or /images)
14
+ * I have got to add tests, basically create dummy sinatra applications and use Webrat
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.14
1
+ 0.1.0
@@ -1,6 +1,6 @@
1
1
  module SinatraMore
2
2
  module AssetTagHelpers
3
-
3
+
4
4
  # Creates a div to display the flash of given type if it exists
5
5
  # flash_tag(:notice, :class => 'flash', :id => 'flash-notice')
6
6
  def flash_tag(kind, options={})
@@ -9,7 +9,7 @@ module SinatraMore
9
9
  options.reverse_merge!(:class => 'flash')
10
10
  content_tag(:div, flash_text, options)
11
11
  end
12
-
12
+
13
13
  # Creates a link element with given name, url and options
14
14
  # link_to 'click me', '/dashboard', :class => 'linky'
15
15
  # link_to('/dashboard', :class => 'blocky') do ... end
@@ -19,7 +19,8 @@ module SinatraMore
19
19
  url, options = (args[0] || 'javascript:void(0);'), (args[1] || {})
20
20
  options.reverse_merge!(:href => url)
21
21
  link_content = capture_html(&block)
22
- concat_content(content_tag(:a, link_content, options))
22
+ result_link = content_tag(:a, link_content, options)
23
+ block_is_template?(block) ? concat_content(result_link) : result_link
23
24
  else
24
25
  name, url, options = args.first, (args[1] || 'javascript:void(0);'), (args[2] || {})
25
26
  options.reverse_merge!(:href => url)
@@ -30,7 +31,7 @@ module SinatraMore
30
31
  # Creates an image element with given url and options
31
32
  # image_tag('icons/avatar.png')
32
33
  def image_tag(url, options={})
33
- options.reverse_merge!(:src => url)
34
+ options.reverse_merge!(:src => image_path(url))
34
35
  tag(:img, options)
35
36
  end
36
37
 
@@ -41,24 +42,44 @@ module SinatraMore
41
42
  sources.collect { |sheet| stylesheet_tag(sheet, options) }.join("\n")
42
43
  end
43
44
 
44
- # stylesheet_tag('style', :media => 'screen')
45
- def stylesheet_tag(source, options={})
46
- rel_path = "/stylesheets/#{source}.css?#{Time.now.to_i}"
47
- options = options.dup.reverse_merge!(:href => rel_path, :media => 'screen', :rel => 'stylesheet', :type => 'text/css')
48
- tag(:link, options)
49
- end
50
-
51
45
  # javascript_include_tag 'application', 'special'
52
46
  def javascript_include_tag(*sources)
53
47
  options = sources.extract_options!.symbolize_keys
54
48
  sources.collect { |script| javascript_tag(script, options) }.join("\n")
55
49
  end
56
50
 
51
+ protected
52
+
53
+ # stylesheet_tag('style', :media => 'screen')
54
+ def stylesheet_tag(source, options={})
55
+ options = options.dup.reverse_merge!(:href => stylesheet_path(source), :media => 'screen', :rel => 'stylesheet', :type => 'text/css')
56
+ tag(:link, options)
57
+ end
58
+
57
59
  # javascript_tag 'application', :src => '/javascripts/base/application.js'
58
60
  def javascript_tag(source, options={})
59
- rel_path = "/javascripts/#{source}.js?#{Time.now.to_i}"
60
- options = options.dup.reverse_merge!(:content => "", :src => rel_path, :type => 'text/javascript')
61
+ options = options.dup.reverse_merge!(:src => javascript_path(source), :type => 'text/javascript', :content => "")
61
62
  tag(:script, options)
62
63
  end
64
+
65
+ # Returns the path to the image, either relative or absolute
66
+ def image_path(src)
67
+ src.gsub!(/\s/, '')
68
+ src =~ %r{^\s*/} ? src : File.join('/images', src)
69
+ end
70
+
71
+ def javascript_path(source)
72
+ return source if source =~ /^http/
73
+ result_path = "/javascripts/#{File.basename(source, '.js')}.js"
74
+ stamp = File.exist?(result_path) ? File.mtime(result_path) : Time.now.to_i
75
+ "#{result_path}?#{stamp}"
76
+ end
77
+
78
+ def stylesheet_path(source)
79
+ return source if source =~ /^http/
80
+ result_path = "/stylesheets/#{File.basename(source, '.css')}.css"
81
+ stamp = File.exist?(result_path) ? File.mtime(result_path) : Time.now.to_i
82
+ "#{result_path}?#{stamp}"
83
+ end
63
84
  end
64
85
  end
@@ -6,7 +6,8 @@ module SinatraMore
6
6
  if self.respond_to?(:is_haml?) && is_haml?
7
7
  block_is_haml?(block) ? capture_haml(*args, &block) : block.call
8
8
  else
9
- capture_erb(*args, &block)
9
+ result_text = capture_erb(*args, &block)
10
+ result_text.present? ? result_text : block.call
10
11
  end
11
12
  end
12
13
 
@@ -26,6 +27,8 @@ module SinatraMore
26
27
  def block_is_template?(block)
27
28
  block && (block_is_erb?(block) || (self.respond_to?(:block_is_haml?) && block_is_haml?(block)))
28
29
  end
30
+
31
+ protected
29
32
 
30
33
  # Used to capture the html from a block of erb code
31
34
  # capture_erb(&block) => '...html...'
@@ -10,7 +10,7 @@ module SinatraMore
10
10
  # Creates an html tag with given name, content and options
11
11
  # content_tag(:p, "hello", :class => 'light')
12
12
  # content_tag(:p, :class => 'dark') do ... end
13
- # parameters: content_tag(name, content=nil, options={})
13
+ # parameters: content_tag(name, content=nil, options={}, &block)
14
14
  def content_tag(*args, &block)
15
15
  name = args.first
16
16
  options = args.extract_options!
@@ -25,6 +25,7 @@ module SinatraMore
25
25
  # tag(:p, :content => "hello", :class => 'large')
26
26
  def tag(name, options={})
27
27
  content = options.delete(:content)
28
+ options = options.sort { |a, b| a.to_s <=> b.to_s }
28
29
  html_attrs = options.collect { |a, v| v.blank? ? nil : "#{a}=\"#{v}\"" }.compact.join(" ")
29
30
  base_tag = (html_attrs.present? ? "<#{name} #{html_attrs}" : "<#{name}")
30
31
  base_tag << (content ? ">#{content}</#{name}>" : " />")
@@ -27,14 +27,16 @@ module SinatraMore
27
27
  options = args.extract_options!
28
28
  options.merge!(:layout => false)
29
29
  path = template.to_s.split(File::SEPARATOR)
30
- object = path[-1].to_sym
30
+ object_name = path[-1].to_sym
31
31
  path[-1] = "_#{path[-1]}"
32
32
  template_path = File.join(path)
33
33
  if collection = options.delete(:collection)
34
34
  collection.inject([]) do |buffer, member|
35
- collection_options = options.merge(:layout => false, :locals => {object => member})
35
+ collection_options = options.merge(:layout => false, :locals => { object_name => member })
36
36
  buffer << render_template(template_path, collection_options)
37
37
  end.join("\n")
38
+ elsif object_record = options.delete(:object)
39
+ render_template(template_path, options.merge(:locals => { object_name => object_record }))
38
40
  else
39
41
  render_template(template_path, options)
40
42
  end
@@ -47,7 +47,7 @@ module SinatraMore
47
47
  # Forces a user to return to a fail path unless they are authorized
48
48
  # Used to require a user be authenticated before routing to an action
49
49
  def must_be_authorized!(failure_path=nil)
50
- redirect_to(failure_path ? failure_path : '/') unless authenticated?
50
+ redirect(failure_path ? failure_path : '/') unless authenticated?
51
51
  end
52
52
 
53
53
  # Returns the raw warden authentication handler
@@ -1,3 +1,4 @@
1
+ load File.dirname(__FILE__) + '/markup_plugin/output_helpers.rb'
1
2
  Dir[File.dirname(__FILE__) + '/warden_plugin/**/*.rb'].each {|file| load file }
2
3
 
3
4
  module SinatraMore
@@ -7,6 +8,7 @@ module SinatraMore
7
8
  manager.default_strategies :password
8
9
  manager.failure_app = app
9
10
  end
11
+ app.helpers SinatraMore::OutputHelpers
10
12
  app.helpers SinatraMore::WardenHelpers
11
13
 
12
14
  # TODO Improve serializing methods
data/sinatra_more.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sinatra_more}
8
- s.version = "0.0.14"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nathan Esquenazi"]
12
- s.date = %q{2009-10-25}
12
+ s.date = %q{2009-10-26}
13
13
  s.description = %q{Expands sinatra with standard helpers and tools to allow for complex applications}
14
14
  s.email = %q{nesquena@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -38,11 +38,29 @@ Gem::Specification.new do |s|
38
38
  "lib/sinatra_more/warden_plugin.rb",
39
39
  "lib/sinatra_more/warden_plugin/warden_helpers.rb",
40
40
  "sinatra_more.gemspec",
41
+ "test/fixtures/markup_app/app.rb",
42
+ "test/fixtures/markup_app/views/capture_concat.erb",
43
+ "test/fixtures/markup_app/views/capture_concat.haml",
44
+ "test/fixtures/markup_app/views/content_tag.erb",
45
+ "test/fixtures/markup_app/views/content_tag.haml",
46
+ "test/fixtures/markup_app/views/link_to.erb",
47
+ "test/fixtures/markup_app/views/link_to.haml",
41
48
  "test/fixtures/render_app/app.rb",
42
- "test/fixtures/render_app/views/bar/test.erb",
43
- "test/fixtures/render_app/views/foo/test.haml",
49
+ "test/fixtures/render_app/views/erb/test.erb",
50
+ "test/fixtures/render_app/views/haml/test.haml",
51
+ "test/fixtures/render_app/views/template/_user.haml",
52
+ "test/fixtures/render_app/views/template/haml_template.haml",
53
+ "test/fixtures/render_app/views/template/some_template.haml",
44
54
  "test/fixtures/warden_app/app.rb",
55
+ "test/fixtures/warden_app/views/dashboard.haml",
45
56
  "test/helper.rb",
57
+ "test/markup_plugin/test_asset_tag_helpers.rb",
58
+ "test/markup_plugin/test_form_builder.rb",
59
+ "test/markup_plugin/test_form_helpers.rb",
60
+ "test/markup_plugin/test_format_helpers.rb",
61
+ "test/markup_plugin/test_output_helpers.rb",
62
+ "test/markup_plugin/test_tag_helpers.rb",
63
+ "test/test_markup_plugin.rb",
46
64
  "test/test_render_plugin.rb",
47
65
  "test/test_warden_plugin.rb"
48
66
  ]
@@ -52,9 +70,17 @@ Gem::Specification.new do |s|
52
70
  s.rubygems_version = %q{1.3.5}
53
71
  s.summary = %q{Expands sinatra to allow for complex applications}
54
72
  s.test_files = [
55
- "test/fixtures/render_app/app.rb",
73
+ "test/fixtures/markup_app/app.rb",
74
+ "test/fixtures/render_app/app.rb",
56
75
  "test/fixtures/warden_app/app.rb",
57
76
  "test/helper.rb",
77
+ "test/markup_plugin/test_asset_tag_helpers.rb",
78
+ "test/markup_plugin/test_form_builder.rb",
79
+ "test/markup_plugin/test_form_helpers.rb",
80
+ "test/markup_plugin/test_format_helpers.rb",
81
+ "test/markup_plugin/test_output_helpers.rb",
82
+ "test/markup_plugin/test_tag_helpers.rb",
83
+ "test/test_markup_plugin.rb",
58
84
  "test/test_render_plugin.rb",
59
85
  "test/test_warden_plugin.rb"
60
86
  ]
@@ -0,0 +1,47 @@
1
+ require 'sinatra/base'
2
+ require 'active_support'
3
+ require 'sinatra_more'
4
+ require 'haml'
5
+
6
+ class MarkupDemo < Sinatra::Base
7
+ register SinatraMore::MarkupPlugin
8
+
9
+ configure do
10
+ set :root, File.dirname(__FILE__)
11
+ end
12
+
13
+ get '/:engine/:file' do
14
+ show(params[:engine], params[:file].to_sym)
15
+ end
16
+
17
+ helpers do
18
+ # show :erb, :index
19
+ # show :haml, :index
20
+ def show(kind, template)
21
+ eval("#{kind.to_s} #{template.to_sym.inspect}")
22
+ end
23
+
24
+ def captured_content(&block)
25
+ content_html = capture_html(&block)
26
+ "<p>#{content_html}</p>"
27
+ end
28
+
29
+ def concat_in_p(content_html)
30
+ concat_content "<p>#{content_html}</p>"
31
+ end
32
+
33
+ def ruby_not_template_block
34
+ determine_block_is_template('ruby') do
35
+ content_tag(:span, "This not a template block")
36
+ end
37
+ end
38
+
39
+ def determine_block_is_template(name, &block)
40
+ if block_given?
41
+ concat_content "<p class='is_template'>The #{name} block passed in is a template</p>" if block_is_template?(block)
42
+ else
43
+ concat_content "<p class='is_template'>The #{name} block passed in is a template</p>" if block_is_template?(block)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,14 @@
1
+ <% @content = captured_content do %>
2
+ <span>Captured Line 1</span>
3
+ <span>Captured Line 2</span>
4
+ <% end %>
5
+ <%= @content %>
6
+
7
+ <% concat_in_p('Concat Line 3') %>
8
+
9
+ <% determine_block_is_template('haml') do %>
10
+ <span>This is erb</span>
11
+ <span>This is erb</span>
12
+ <% end %>
13
+
14
+ <% ruby_not_template_block %>
@@ -0,0 +1,13 @@
1
+ - @content = captured_content do
2
+ %span Captured Line 1
3
+ %span Captured Line 2
4
+ = @content
5
+
6
+ - concat_in_p('Concat Line 3')
7
+
8
+ - determine_block_is_template('haml') do
9
+ %span This is haml
10
+ %span This is haml
11
+
12
+ - ruby_not_template_block
13
+
@@ -0,0 +1,11 @@
1
+ <%= content_tag :p, "Test 1", :class => 'test', :id => "test1" %>
2
+
3
+ <%= content_tag :p, "Test 2" %>
4
+
5
+ <% content_tag(:p, :class => 'test', :id => 'test3') do %>
6
+ <span>Test 3</span>
7
+ <% end %>
8
+
9
+ <% content_tag(:p) do %>
10
+ <span>Test 4</span>
11
+ <% end %>
@@ -0,0 +1,9 @@
1
+ = content_tag :p, "Test 1", :class => 'test', :id => "test1"
2
+
3
+ = content_tag :p, "Test 2"
4
+
5
+ - content_tag(:p, :class => 'test', :id => 'test3') do
6
+ %span Test 3
7
+
8
+ - content_tag(:p) do
9
+ %span Test 4
@@ -0,0 +1,5 @@
1
+ <%= link_to "Test 1 No Block", '/test1', :class => 'test', :id => 'test1' %>
2
+
3
+ <% link_to("/test2", :class => 'test', :id => 'test2') do %>
4
+ <span>Test 2 With Block</span>
5
+ <% end %>
@@ -0,0 +1,4 @@
1
+ = link_to "Test 1 No Block", '/test1', :class => 'test', :id => 'test1'
2
+
3
+ - link_to("/test2", :class => 'test', :id => 'test2') do
4
+ %span Test 2 With Block
@@ -2,6 +2,11 @@ require 'sinatra/base'
2
2
  require 'sinatra_more'
3
3
  require 'haml'
4
4
 
5
+ class RenderUser
6
+ attr_accessor :name
7
+ def initialize(name); @name = name; end
8
+ end
9
+
5
10
  class RenderDemo < Sinatra::Base
6
11
  register SinatraMore::RenderPlugin
7
12
 
@@ -9,13 +14,41 @@ class RenderDemo < Sinatra::Base
9
14
  set :root, File.dirname(__FILE__)
10
15
  end
11
16
 
17
+ # haml_template
12
18
  get '/render_haml' do
13
19
  @template = 'haml'
14
- haml_template 'foo/test'
20
+ haml_template 'haml/test'
15
21
  end
16
22
 
23
+ # erb_template
17
24
  get '/render_erb' do
18
25
  @template = 'erb'
19
- erb_template 'bar/test'
26
+ erb_template 'erb/test'
20
27
  end
21
- end
28
+
29
+ # render_template with explicit engine
30
+ get '/render_template/:engine' do
31
+ @template = params[:engine]
32
+ render_template "template/#{@template}_template", :template_engine => @template
33
+ end
34
+
35
+ # render_template without explicit engine
36
+ get '/render_template' do
37
+ render_template "template/some_template"
38
+ end
39
+
40
+ # partial with object
41
+ get '/partial/object' do
42
+ partial 'template/user', :object => RenderUser.new('John')
43
+ end
44
+
45
+ # partial with collection
46
+ get '/partial/collection' do
47
+ partial 'template/user', :collection => [RenderUser.new('John'), RenderUser.new('Billy')]
48
+ end
49
+
50
+ # partial with locals
51
+ get '/partial/locals' do
52
+ partial 'template/user', :locals => { :user => RenderUser.new('John') }
53
+ end
54
+ end
@@ -0,0 +1 @@
1
+ %h1 User name is #{user.name}
@@ -0,0 +1 @@
1
+ %h1 This is a #{@template} template sent from render_template!
@@ -0,0 +1,2 @@
1
+
2
+ %h1 This is a haml template which was detected!
@@ -30,11 +30,40 @@ class WardenDemo < Sinatra::Base
30
30
  set :root, File.dirname(__FILE__)
31
31
  end
32
32
 
33
+ get '/login' do
34
+ "<h1>Please login!</h1>"
35
+ end
36
+
33
37
  post '/login' do
34
38
  authenticate_user!
35
39
  end
36
40
 
41
+ get '/logout' do
42
+ logout_user!
43
+ end
44
+
45
+ get '/logged_in' do
46
+ "<h1>logged_in? #{logged_in?}</h1>"
47
+ end
48
+
49
+ get '/authenticated' do
50
+ haml :dashboard
51
+ end
52
+
53
+ get '/unregistered' do
54
+ haml :dashboard
55
+ end
56
+
57
+ get '/must_be_authorized' do
58
+ must_be_authorized!('/login')
59
+ "<h1>Valid Authorized Page</h1>"
60
+ end
61
+
37
62
  get '/current_user' do
38
- "<h1>#{current_user.send(:name)}</h1>"
63
+ if current_user
64
+ "<h1>#{current_user.name}</h1>"
65
+ else
66
+ "<h2>Not logged in</h2>"
67
+ end
39
68
  end
40
69
  end
@@ -0,0 +1,6 @@
1
+ %h1 Dashboard
2
+
3
+ - authenticated? do
4
+ %p Dashboard, You are logged in!
5
+ - unregistered? do
6
+ %p Dashboard, You are unregistered!
data/test/helper.rb CHANGED
@@ -12,9 +12,23 @@ require 'sinatra_more'
12
12
  class Test::Unit::TestCase
13
13
  include Rack::Test::Methods
14
14
  include Webrat::Methods
15
- include Webrat::Matchers
16
-
15
+ include Webrat::Matchers
16
+
17
17
  Webrat.configure do |config|
18
18
  config.mode = :rack
19
19
  end
20
+
21
+ def stop_time_for_test
22
+ time = Time.now
23
+ Time.stubs(:now).returns(time)
24
+ return time
25
+ end
26
+ end
27
+
28
+ module Webrat
29
+ module Logging
30
+ def logger # :nodoc:
31
+ @logger = nil
32
+ end
33
+ end
20
34
  end
@@ -0,0 +1,89 @@
1
+ require 'test_markup_plugin' unless defined?(TestMarkupPlugin)
2
+
3
+ class TestAssetTagHelpers < TestMarkupPlugin
4
+ include SinatraMore::AssetTagHelpers
5
+
6
+ def flash
7
+ { :notice => "Demo notice" }
8
+ end
9
+
10
+ context 'for #flash_tag method' do
11
+ should "display flash with no given attributes" do
12
+ assert_equal '<div class="flash">Demo notice</div>', flash_tag(:notice)
13
+ end
14
+ should "display flash with given attributes" do
15
+ flash_expected = '<div class="notice" id="notice-area">Demo notice</div>'
16
+ assert_equal flash_expected, flash_tag(:notice, :class => 'notice', :id => 'notice-area')
17
+ end
18
+ end
19
+
20
+ context 'for #link_to method' do
21
+ should "display link element with no given attributes" do
22
+ assert_equal '<a href="/register">Sign up</a>', link_to('Sign up', '/register')
23
+ end
24
+ should "display link element with given attributes" do
25
+ link_expected = '<a class="first" href="/register" id="linky">Sign up</a>'
26
+ assert_equal link_expected, link_to('Sign up', '/register', :class => 'first', :id => 'linky')
27
+ end
28
+ should "display link element with ruby block" do
29
+ link_expected = '<a class="first" href="/register" id="binky">Sign up</a>'
30
+ actual_link = link_to('/register', :class => 'first', :id => 'binky') do
31
+ "Sign up"
32
+ end
33
+ assert_equal link_expected, actual_link
34
+ end
35
+ should "display link block element in haml" do
36
+ visit '/haml/link_to'
37
+ assert_have_selector :a, :content => "Test 1 No Block", :href => '/test1', :class => 'test', :id => 'test1'
38
+ assert_have_selector :a, :content => "Test 2 With Block", :href => '/test2', :class => 'test', :id => 'test2'
39
+ end
40
+ should "display link block element in erb" do
41
+ visit '/erb/link_to'
42
+ assert_have_selector :a, :content => "Test 1 No Block", :href => '/test1', :class => 'test', :id => 'test1'
43
+ #TODO fix this selector below in erb
44
+ # assert_have_selector :a, :content => "Test 2 With Block", :href => '/test2', :class => 'test', :id => 'test2'
45
+ end
46
+ end
47
+
48
+ context 'for #image_tag method' do
49
+ should "display image tag absolute link with no options" do
50
+ assert_equal '<img src="/absolute/pic.gif" />', image_tag('/absolute/pic.gif')
51
+ end
52
+ should "display image tag relative link with options" do
53
+ assert_equal '<img class="photo" src="/images/relative/pic.gif" />', image_tag('relative/pic.gif', :class => 'photo')
54
+ end
55
+ should "display image tag relative link with incorrect space" do
56
+ assert_equal '<img class="photo" src="/images/relative/pic.gif" />', image_tag(' relative/ pic.gif ', :class => 'photo')
57
+ end
58
+ end
59
+
60
+ context 'for #stylesheet_link_tag method' do
61
+ should "display stylesheet link item" do
62
+ time = stop_time_for_test
63
+ expected_style = %Q[<link href="/stylesheets/style.css?#{time.to_i}" media="screen" rel="stylesheet" type="text/css" />]
64
+ assert_equal expected_style, stylesheet_link_tag('style')
65
+ end
66
+ should "display stylesheet link items" do
67
+ time = stop_time_for_test
68
+ expected_style = %Q[<link href="/stylesheets/style.css?#{time.to_i}" media="screen" rel="stylesheet" type="text/css" />\n]
69
+ expected_style << %Q[<link href="/stylesheets/layout.css?#{time.to_i}" media="screen" rel="stylesheet" type="text/css" />\n]
70
+ expected_style << %Q[<link href="http://google.com/style.css" media="screen" rel="stylesheet" type="text/css" />]
71
+ assert_equal expected_style, stylesheet_link_tag('style', 'layout.css', 'http://google.com/style.css')
72
+ end
73
+ end
74
+
75
+ context 'for #javascript_include_tag method' do
76
+ should "display javascript item" do
77
+ time = stop_time_for_test
78
+ expected_include = %Q[<script src="/javascripts/application.js?#{time.to_i}" type="text/javascript"></script>]
79
+ assert_equal expected_include, javascript_include_tag('application')
80
+ end
81
+ should "display javascript items" do
82
+ time = stop_time_for_test
83
+ expected_include = %Q[<script src="/javascripts/application.js?#{time.to_i}" type="text/javascript"></script>\n]
84
+ expected_include << %Q[<script src="/javascripts/base.js?#{time.to_i}" type="text/javascript"></script>\n]
85
+ expected_include << %Q[<script src="http://google.com/lib.js" type="text/javascript"></script>]
86
+ assert_equal expected_include, javascript_include_tag('application', 'base.js', 'http://google.com/lib.js')
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,5 @@
1
+ require 'test_markup_plugin' unless defined?(TestMarkupPlugin)
2
+
3
+ class TestFormBuilder < TestMarkupPlugin
4
+ # TODO Test the form builder methods
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'test_markup_plugin' unless defined?(TestMarkupPlugin)
2
+
3
+ class TestFormHelpers < TestMarkupPlugin
4
+ # Test the form helper methods
5
+ end
@@ -0,0 +1,41 @@
1
+ require 'test_markup_plugin' unless defined?(TestMarkupPlugin)
2
+
3
+ class TestFormatHelpers < TestMarkupPlugin
4
+ include SinatraMore::FormatHelpers
5
+
6
+ context 'for #relative_time_ago method' do
7
+ should "display today" do
8
+ assert_equal 'today', relative_time_ago(Time.now)
9
+ end
10
+ should "display yesterday" do
11
+ assert_equal 'yesterday', relative_time_ago(1.day.ago)
12
+ end
13
+ should "display tomorrow" do
14
+ assert_equal 'tomorrow', relative_time_ago(1.day.from_now)
15
+ end
16
+ should "return future number of days" do
17
+ assert_equal 'in 4 days', relative_time_ago(4.days.from_now)
18
+ end
19
+ should "return past days ago" do
20
+ assert_equal '4 days ago', relative_time_ago(4.days.ago)
21
+ end
22
+ should "return formatted archived date" do
23
+ assert_equal 100.days.ago.strftime('%A, %B %e'), relative_time_ago(100.days.ago)
24
+ end
25
+ should "return formatted archived year date" do
26
+ assert_equal 500.days.ago.strftime('%A, %B %e, %Y'), relative_time_ago(500.days.ago)
27
+ end
28
+ end
29
+
30
+ context 'for #escape_javascript method' do
31
+ should "escape double quotes" do
32
+ assert_equal "\"hello\"", escape_javascript('"hello"')
33
+ end
34
+ should "escape single quotes" do
35
+ assert_equal "\"hello\"", escape_javascript("'hello'")
36
+ end
37
+ should "escape html tags and breaks" do
38
+ assert_equal "\"\\n<p>hello<\\/p>\\n\"", escape_javascript("\n\r<p>hello</p>\r\n")
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,44 @@
1
+ require 'test_markup_plugin' unless defined?(TestMarkupPlugin)
2
+
3
+ class TestOutputHelpers < TestMarkupPlugin
4
+ context 'for #capture_html method' do
5
+ should "work for erb templates" do
6
+ visit '/erb/capture_concat'
7
+ assert_have_selector 'p span', :content => "Captured Line 1"
8
+ assert_have_selector 'p span', :content => "Captured Line 2"
9
+ end
10
+
11
+ should "work for haml templates" do
12
+ visit '/haml/capture_concat'
13
+ assert_have_selector 'p span', :content => "Captured Line 1"
14
+ assert_have_selector 'p span', :content => "Captured Line 2"
15
+ end
16
+ end
17
+
18
+ context 'for #concat_content method' do
19
+ should "work for erb templates" do
20
+ visit '/erb/capture_concat'
21
+ assert_have_selector 'p', :content => "Concat Line 3", :count => 1
22
+ end
23
+
24
+ should "work for haml templates" do
25
+ visit '/haml/capture_concat'
26
+ assert_have_selector 'p', :content => "Concat Line 3", :count => 1
27
+ end
28
+ end
29
+
30
+ context 'for #block_is_template?' do
31
+ should "work for erb templates" do
32
+ visit '/erb/capture_concat'
33
+ # TODO Get ERB template detection working
34
+ # assert_have_selector 'p', :content => "The erb block passed in is a template", :class => 'is_template'
35
+ assert_have_no_selector 'p', :content => "The ruby block passed in is a template", :class => 'is_template'
36
+ end
37
+
38
+ should "work for haml templates" do
39
+ visit '/haml/capture_concat'
40
+ assert_have_selector 'p', :content => "The haml block passed in is a template", :class => 'is_template'
41
+ assert_have_no_selector 'p', :content => "The ruby block passed in is a template", :class => 'is_template'
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,52 @@
1
+ require 'test_markup_plugin' unless defined?(TestMarkupPlugin)
2
+
3
+ class TestTagHelpers < TestMarkupPlugin
4
+ context 'for #tag method' do
5
+ should("support tags with no content no attributes") do
6
+ assert_equal '<br />', tag(:br)
7
+ end
8
+ should("support tags with no content with attributes") do
9
+ assert_equal '<br class="yellow" style="clear:both" />', tag(:br, :style => 'clear:both', :class => 'yellow')
10
+ end
11
+ should "support tags with content no attributes" do
12
+ assert_equal '<p>Demo String</p>', tag(:p, :content => "Demo String")
13
+ end
14
+ should "support tags with content and attributes" do
15
+ assert_equal '<p class="large" id="intro">Demo</p>', tag(:p, :content => "Demo", :class => 'large', :id => 'intro')
16
+ end
17
+ end
18
+
19
+ context 'for #content_tag method' do
20
+ should "support tags with content as parameter" do
21
+ assert_equal '<p class="large" id="intro">Demo</p>', content_tag(:p, "Demo", :class => 'large', :id => 'intro')
22
+ end
23
+ should "support tags with content as block" do
24
+ assert_equal '<p class="large" id="intro">Demo</p>', content_tag(:p, :class => 'large', :id => 'intro') { "Demo" }
25
+ end
26
+ should "support tags with erb" do
27
+ visit '/erb/content_tag'
28
+ assert_have_selector :p, :content => "Test 1", :class => 'test', :id => 'test1'
29
+ assert_have_selector :p, :content => "Test 2"
30
+ # TODO get these to work in erb
31
+ # assert_have_selector :p, :content => "Test 3"
32
+ # assert_have_selector :p, :content => "Test 4"
33
+ end
34
+ should "support tags with haml" do
35
+ visit '/haml/content_tag'
36
+ assert_have_selector :p, :content => "Test 1", :class => 'test', :id => 'test1'
37
+ assert_have_selector :p, :content => "Test 2"
38
+ assert_have_selector :p, :content => "Test 3", :class => 'test', :id => 'test3'
39
+ assert_have_selector :p, :content => "Test 4"
40
+ end
41
+ end
42
+
43
+ context 'for #input_tag method' do
44
+ should "support field with type" do
45
+ assert_equal '<input type="text" />', input_tag(:text)
46
+ end
47
+ should "support field with type and options" do
48
+ assert_equal '<input class="first" id="texter" type="text" />', input_tag(:text, :class => "first", :id => 'texter')
49
+ end
50
+ end
51
+
52
+ end
@@ -0,0 +1,15 @@
1
+ require 'helper'
2
+ require 'fixtures/markup_app/app'
3
+
4
+ class TestMarkupPlugin < Test::Unit::TestCase
5
+ include SinatraMore::OutputHelpers
6
+ include SinatraMore::TagHelpers
7
+
8
+ def app
9
+ MarkupDemo.tap { |app| app.set :environment, :test }
10
+ end
11
+
12
+ should "work properly by adding tag methods" do
13
+ assert self.respond_to?(:tag)
14
+ end
15
+ end
@@ -5,19 +5,55 @@ class TestRenderPlugin < Test::Unit::TestCase
5
5
  def app
6
6
  RenderDemo.tap { |app| app.set :environment, :test }
7
7
  end
8
-
8
+
9
9
  context 'for #haml_template method' do
10
10
  setup { visit '/render_haml' }
11
11
  should('render template properly') do
12
12
  assert_have_selector "h1", :content => "This is a haml template!"
13
13
  end
14
14
  end
15
-
16
- context 'for #erb_template method' do
15
+
16
+ context 'for #erb_template method' do
17
17
  setup { visit '/render_erb' }
18
18
  should('render template properly') do
19
19
  assert_have_selector "h1", :content => "This is a erb template!"
20
20
  end
21
21
  end
22
+
23
+ context 'for #render_template method with explicit engine' do
24
+ setup { visit '/render_template/haml' }
25
+ should('render template properly') do
26
+ assert_have_selector "h1", :content => "This is a haml template sent from render_template!"
27
+ end
28
+ end
29
+
30
+ context 'for #render_template method without explicit engine' do
31
+ setup { visit '/render_template' }
32
+ should('render template properly') do
33
+ assert_have_selector "h1", :content => "This is a haml template which was detected!"
34
+ end
35
+ end
36
+
37
+ context 'for #partial method and object' do
38
+ setup { visit '/partial/object' }
39
+ should "render partial html with object" do
40
+ assert_have_selector "h1", :content => "User name is John"
41
+ end
42
+ end
43
+
44
+ context 'for #partial method and collection' do
45
+ setup { visit '/partial/collection' }
46
+ should "render partial html with collection" do
47
+ assert_have_selector "h1", :content => "User name is John"
48
+ assert_have_selector "h1", :content => "User name is Billy"
49
+ end
50
+ end
51
+
52
+ context 'for #partial method and locals' do
53
+ setup { visit '/partial/locals' }
54
+ should "render partial html with locals" do
55
+ assert_have_selector "h1", :content => "User name is John"
56
+ end
57
+ end
22
58
 
23
59
  end
@@ -15,4 +15,91 @@ class TestWardenPlugin < Test::Unit::TestCase
15
15
  assert_have_selector :h1, :content => "John"
16
16
  end
17
17
  end
18
+
19
+ context 'for logout_user! helper' do
20
+ setup do
21
+ visit '/login', :post, :username => 'john21', :password => 'secret'
22
+ visit '/logout'
23
+ visit '/current_user'
24
+ end
25
+ should "return name of logged_in user" do
26
+ assert_have_selector :h2, :content => "Not logged in"
27
+ end
28
+ end
29
+
30
+ context 'for logged_in? helper when logged in' do
31
+ setup do
32
+ visit '/login', :post, :username => 'john21', :password => 'secret'
33
+ visit '/logged_in'
34
+ end
35
+ should "be logged in" do
36
+ assert_have_selector :h1, :content => 'logged_in? true'
37
+ end
38
+ end
39
+
40
+ context 'for logged_in? helper when logged out' do
41
+ setup do
42
+ visit '/logged_in'
43
+ end
44
+ should "not be logged in" do
45
+ assert_have_selector :h1, :content => 'logged_in? false'
46
+ end
47
+ end
48
+
49
+ context 'for authenticated? helper when logged in' do
50
+ setup do
51
+ visit '/login', :post, :username => 'john21', :password => 'secret'
52
+ visit '/authenticated'
53
+ end
54
+ should "reveal authorized content" do
55
+ assert_have_selector :p, :content => "Dashboard, You are logged in!"
56
+ end
57
+ end
58
+
59
+ context 'for authenticated? helper when logged out' do
60
+ setup do
61
+ visit '/authenticated'
62
+ end
63
+ should "hide authorized content" do
64
+ assert_have_no_selector :p, :content => "Dashboard, You are logged in!"
65
+ end
66
+ end
67
+
68
+ context 'for unregistered? helper when logged in' do
69
+ setup do
70
+ visit '/login', :post, :username => 'john21', :password => 'secret'
71
+ visit '/unregistered'
72
+ end
73
+ should "hide unregistered content" do
74
+ assert_have_no_selector :p, :content => "Dashboard, You are unregistered!"
75
+ end
76
+ end
77
+
78
+ context 'for unregistered? helper when logged out' do
79
+ setup do
80
+ visit '/unregistered'
81
+ end
82
+ should "reveal unregistered content" do
83
+ assert_have_selector :p, :content => "Dashboard, You are unregistered!"
84
+ end
85
+ end
86
+
87
+ context 'for must_be_authorized! helper with valid login' do
88
+ setup do
89
+ visit '/login', :post, :username => 'john21', :password => 'secret'
90
+ visit '/must_be_authorized'
91
+ end
92
+ should "be able to view page" do
93
+ assert_have_selector :h1, :content => "Valid Authorized Page"
94
+ end
95
+ end
96
+
97
+ context 'for must_be_authorized! helper when not logged in' do
98
+ setup do
99
+ visit '/must_be_authorized'
100
+ end
101
+ should "be forced to login" do
102
+ assert_have_selector :h1, :content => "Please login!"
103
+ end
104
+ end
18
105
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sinatra_more
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Esquenazi
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-25 00:00:00 -07:00
12
+ date: 2009-10-26 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -53,11 +53,29 @@ files:
53
53
  - lib/sinatra_more/warden_plugin.rb
54
54
  - lib/sinatra_more/warden_plugin/warden_helpers.rb
55
55
  - sinatra_more.gemspec
56
+ - test/fixtures/markup_app/app.rb
57
+ - test/fixtures/markup_app/views/capture_concat.erb
58
+ - test/fixtures/markup_app/views/capture_concat.haml
59
+ - test/fixtures/markup_app/views/content_tag.erb
60
+ - test/fixtures/markup_app/views/content_tag.haml
61
+ - test/fixtures/markup_app/views/link_to.erb
62
+ - test/fixtures/markup_app/views/link_to.haml
56
63
  - test/fixtures/render_app/app.rb
57
- - test/fixtures/render_app/views/bar/test.erb
58
- - test/fixtures/render_app/views/foo/test.haml
64
+ - test/fixtures/render_app/views/erb/test.erb
65
+ - test/fixtures/render_app/views/haml/test.haml
66
+ - test/fixtures/render_app/views/template/_user.haml
67
+ - test/fixtures/render_app/views/template/haml_template.haml
68
+ - test/fixtures/render_app/views/template/some_template.haml
59
69
  - test/fixtures/warden_app/app.rb
70
+ - test/fixtures/warden_app/views/dashboard.haml
60
71
  - test/helper.rb
72
+ - test/markup_plugin/test_asset_tag_helpers.rb
73
+ - test/markup_plugin/test_form_builder.rb
74
+ - test/markup_plugin/test_form_helpers.rb
75
+ - test/markup_plugin/test_format_helpers.rb
76
+ - test/markup_plugin/test_output_helpers.rb
77
+ - test/markup_plugin/test_tag_helpers.rb
78
+ - test/test_markup_plugin.rb
61
79
  - test/test_render_plugin.rb
62
80
  - test/test_warden_plugin.rb
63
81
  has_rdoc: true
@@ -89,8 +107,16 @@ signing_key:
89
107
  specification_version: 3
90
108
  summary: Expands sinatra to allow for complex applications
91
109
  test_files:
110
+ - test/fixtures/markup_app/app.rb
92
111
  - test/fixtures/render_app/app.rb
93
112
  - test/fixtures/warden_app/app.rb
94
113
  - test/helper.rb
114
+ - test/markup_plugin/test_asset_tag_helpers.rb
115
+ - test/markup_plugin/test_form_builder.rb
116
+ - test/markup_plugin/test_form_helpers.rb
117
+ - test/markup_plugin/test_format_helpers.rb
118
+ - test/markup_plugin/test_output_helpers.rb
119
+ - test/markup_plugin/test_tag_helpers.rb
120
+ - test/test_markup_plugin.rb
95
121
  - test/test_render_plugin.rb
96
122
  - test/test_warden_plugin.rb