sinatra_more 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -19,3 +19,4 @@ rdoc
19
19
  pkg
20
20
 
21
21
  ## PROJECT::SPECIFIC
22
+ webrat.log
data/TODO CHANGED
@@ -2,4 +2,4 @@
2
2
  * Pull from sinatra-helpers and make erb templates work (and credit keldredd)
3
3
  - http://github.com/kelredd/sinatra-helpers/tree/master/lib/sinatra_helpers/erb/
4
4
  * Become total warden solution (basically just require warden gem installed, do everything else)
5
- * Add support for plugin
5
+ * Remove dependency on activesupport! and enumerate dependencies in rake
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.6
1
+ 0.0.7
@@ -1,8 +1,25 @@
1
1
  module SinatraMore
2
2
  module AssetTagHelpers
3
- def link_to(name, url='javascript:void(0)', options={})
4
- options.reverse_merge!(:href => url)
5
- content_tag(:a, name, options)
3
+ # flash_tag(:notice)
4
+ def flash_tag(kind, options={})
5
+ flash_text = flash[kind]
6
+ return '' if flash_text.blank?
7
+ options.reverse_merge!(:class => 'flash')
8
+ content_tag(:div, flash_text, options)
9
+ end
10
+
11
+ # name, url='javascript:void(0)', options={}, &block
12
+ def link_to(*args, &block)
13
+ if block_given?
14
+ url, options = (args[0] || 'javascript:void(0);'), (args[1] || {})
15
+ options.reverse_merge!(:href => url)
16
+ link_content = capture_haml(&block)
17
+ haml_concat(content_tag(:a, link_content, options))
18
+ else
19
+ name, url, options = args.first, (args[1] || 'javascript:void(0);'), (args[2] || {})
20
+ options.reverse_merge!(:href => url)
21
+ content_tag(:a, name, options)
22
+ end
6
23
  end
7
24
 
8
25
  def image_tag(url, options={})
@@ -18,21 +18,22 @@ class AbstractFormBuilder
18
18
  end
19
19
 
20
20
  def text_field(field, options={})
21
- options.reverse_merge!(:value => field_value(field))
21
+ options.reverse_merge!(:value => field_value(field), :id => field_id(field))
22
22
  @template.text_field_tag field_name(field), options
23
23
  end
24
24
 
25
25
  def text_area(field, options={})
26
- options.reverse_merge!(:value => field_value(field))
26
+ options.reverse_merge!(:value => field_value(field), :id => field_id(field))
27
27
  @template.text_area_tag field_name(field), options
28
28
  end
29
29
 
30
30
  def password_field(field, options={})
31
- options.reverse_merge!(:value => field_value(field))
31
+ options.reverse_merge!(:value => field_value(field), :id => field_id(field))
32
32
  @template.password_field_tag field_name(field), options
33
33
  end
34
34
 
35
35
  def file_field(field, options={})
36
+ options.reverse_merge!(:id => field_id(field))
36
37
  @template.file_field_tag field_name(field), options
37
38
  end
38
39
 
@@ -40,6 +41,12 @@ class AbstractFormBuilder
40
41
  @template.submit_tag caption, options
41
42
  end
42
43
 
44
+ protected
45
+
46
+ def self.field_types
47
+ [:text_field, :text_area, :password_field, :file_field]
48
+ end
49
+
43
50
  private
44
51
 
45
52
  def object_name
@@ -1,35 +1,19 @@
1
1
  class StandardFormBuilder < AbstractFormBuilder
2
- def text_field_block(field, options={})
3
- @template.content_block_tag(:p) do
4
- html = label(field)
5
- html << text_field(field, options)
6
- end
7
- end
8
-
9
- def text_area_block(field, options={})
10
- @template.content_block_tag(:p) do
11
- html = label(field)
12
- html << text_area(field, options)
13
- end
14
- end
15
-
16
- def password_field_block(field, options={})
17
- @template.content_block_tag(:p) do
18
- html = label(field)
19
- html << password_field(field, options)
20
- end
21
- end
22
-
23
- def file_field_block(field, options={})
24
- @template.content_block_tag(:p) do
25
- html = label(field)
26
- html << file_field(field, options)
2
+ self.field_types.each do |field_type|
3
+ class_eval <<-EOF
4
+ def #{field_type}_block(field, options={}, label_options={})
5
+ label_options.reverse_merge!(:caption => options.delete(:caption)) if options[:caption]
6
+ @template.content_block_tag(:p) do
7
+ html = label(field, label_options)
8
+ html << #{field_type}(field, options)
9
+ end
27
10
  end
11
+ EOF
28
12
  end
29
-
13
+
30
14
  def submit_block(caption)
31
15
  @template.content_block_tag(:p) do
32
16
  @template.submit_tag(caption)
33
17
  end
34
18
  end
35
- end
19
+ end
@@ -16,6 +16,14 @@ module SinatraMore
16
16
  # TODO make this work with erb!!
17
17
  haml_concat content_tag('form', capture_haml(&block), options)
18
18
  end
19
+
20
+ def field_set_tag(legend=nil, options={}, &block)
21
+ # TODO make this work with erb!!
22
+ field_set_content = ''
23
+ field_set_content << content_tag(:legend, legend) if legend.present?
24
+ field_set_content << capture_haml(&block)
25
+ haml_concat content_tag('fieldset', field_set_content, options)
26
+ end
19
27
 
20
28
  # error_messages_for @user
21
29
  def error_messages_for(record, options={})
@@ -31,9 +39,16 @@ module SinatraMore
31
39
  end
32
40
 
33
41
  # label_tag :username
34
- def label_tag(name, options={})
35
- options.reverse_merge!(:caption => name.to_s.titleize)
36
- content_tag(:label, options.delete(:caption) + ": ", :for => name)
42
+ def label_tag(name, options={}, &block)
43
+ options.reverse_merge!(:caption => name.to_s.titleize, :for => name)
44
+ caption_text = options.delete(:caption) + ": "
45
+ # TODO make this work with erb!!
46
+ if block_given? # label with inner content
47
+ label_content = caption_text + capture_haml(&block)
48
+ haml_concat(content_tag(:label, label_content, options))
49
+ else # regular label
50
+ content_tag(:label, caption_text, options)
51
+ end
37
52
  end
38
53
 
39
54
  # text_field_tag :username
data/sinatra_more.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sinatra_more}
8
- s.version = "0.0.6"
8
+ s.version = "0.0.7"
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"]
@@ -31,14 +31,19 @@ Gem::Specification.new do |s|
31
31
  "lib/sinatra_more/markup_plugin/form_builder/standard_form_builder.rb",
32
32
  "lib/sinatra_more/markup_plugin/form_helpers.rb",
33
33
  "lib/sinatra_more/markup_plugin/format_helpers.rb",
34
- "lib/sinatra_more/markup_plugin/render_helpers.rb",
35
34
  "lib/sinatra_more/markup_plugin/tag_helpers.rb",
36
35
  "lib/sinatra_more/render_plugin.rb",
36
+ "lib/sinatra_more/render_plugin/render_helpers.rb",
37
37
  "lib/sinatra_more/warden_plugin.rb",
38
38
  "lib/sinatra_more/warden_plugin/warden_helpers.rb",
39
39
  "sinatra_more.gemspec",
40
+ "test/fixtures/render_app/app.rb",
41
+ "test/fixtures/render_app/views/bar/test.erb",
42
+ "test/fixtures/render_app/views/foo/test.haml",
43
+ "test/fixtures/warden_app/app.rb",
40
44
  "test/helper.rb",
41
- "test/test_sinatra_more.rb"
45
+ "test/test_render_plugin.rb",
46
+ "test/test_warden_plugin.rb"
42
47
  ]
43
48
  s.homepage = %q{http://github.com/nesquena/sinatra_more}
44
49
  s.rdoc_options = ["--charset=UTF-8"]
@@ -46,8 +51,11 @@ Gem::Specification.new do |s|
46
51
  s.rubygems_version = %q{1.3.5}
47
52
  s.summary = %q{Expands sinatra to allow for complex applications}
48
53
  s.test_files = [
49
- "test/helper.rb",
50
- "test/test_sinatra_more.rb"
54
+ "test/fixtures/render_app/app.rb",
55
+ "test/fixtures/warden_app/app.rb",
56
+ "test/helper.rb",
57
+ "test/test_render_plugin.rb",
58
+ "test/test_warden_plugin.rb"
51
59
  ]
52
60
 
53
61
  if s.respond_to? :specification_version then
@@ -0,0 +1,21 @@
1
+ require 'sinatra/base'
2
+ require 'sinatra_more'
3
+ require 'haml'
4
+
5
+ class RenderDemo < Sinatra::Base
6
+ register SinatraMore::RenderPlugin
7
+
8
+ configure do
9
+ set :root, File.dirname(__FILE__)
10
+ end
11
+
12
+ get '/render_haml' do
13
+ @template = 'haml'
14
+ haml_template 'foo/test'
15
+ end
16
+
17
+ get '/render_erb' do
18
+ @template = 'erb'
19
+ erb_template 'bar/test'
20
+ end
21
+ end
@@ -0,0 +1 @@
1
+ <h1>This is a <%= @template %> template!</h1>
@@ -0,0 +1 @@
1
+ %h1 This is a #{@template} template!
@@ -0,0 +1,40 @@
1
+ require 'sinatra/base'
2
+ require 'sinatra_more'
3
+ require 'warden'
4
+
5
+ class User
6
+ attr_accessor :id, :name, :username, :password
7
+
8
+ def initialize(id, name, username, password)
9
+ self.id, self.name, self.username, self.password = id, name, username, password
10
+ end
11
+
12
+ def self.find(id)
13
+ return self.john_user if id == self.john_user.id
14
+ end
15
+
16
+ def self.authenticate(username, password)
17
+ return self.john_user if username == self.john_user.username && password == self.john_user.password
18
+ end
19
+
20
+ def self.john_user
21
+ @john ||= User.new(21, "John", 'john21', 'secret')
22
+ end
23
+ end
24
+
25
+ class WardenDemo < Sinatra::Base
26
+ use Rack::Session::Cookie
27
+ register SinatraMore::WardenPlugin
28
+
29
+ configure do
30
+ set :root, File.dirname(__FILE__)
31
+ end
32
+
33
+ post '/login' do
34
+ authenticate_user!
35
+ end
36
+
37
+ get '/current_user' do
38
+ "<h1>#{current_user.send(:name)}</h1>"
39
+ end
40
+ end
data/test/helper.rb CHANGED
@@ -1,10 +1,20 @@
1
1
  require 'rubygems'
2
2
  require 'test/unit'
3
3
  require 'shoulda'
4
+ require 'mocha'
5
+ require 'rack/test'
6
+ require 'webrat'
4
7
 
5
8
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
9
  $LOAD_PATH.unshift(File.dirname(__FILE__))
7
10
  require 'sinatra_more'
8
11
 
9
12
  class Test::Unit::TestCase
13
+ include Rack::Test::Methods
14
+ include Webrat::Methods
15
+ include Webrat::Matchers
16
+
17
+ Webrat.configure do |config|
18
+ config.mode = :rack
19
+ end
10
20
  end
@@ -0,0 +1,23 @@
1
+ require 'helper'
2
+ require 'fixtures/render_app/app'
3
+
4
+ class TestRenderPlugin < Test::Unit::TestCase
5
+ def app
6
+ RenderDemo.tap { |app| app.set :environment, :test }
7
+ end
8
+
9
+ context 'for #haml_template method' do
10
+ setup { visit '/render_haml' }
11
+ should('render template properly') do
12
+ assert_have_selector "h1", :content => "This is a haml template!"
13
+ end
14
+ end
15
+
16
+ context 'for #erb_template method' do
17
+ setup { visit '/render_erb' }
18
+ should('render template properly') do
19
+ assert_have_selector "h1", :content => "This is a erb template!"
20
+ end
21
+ end
22
+
23
+ end
@@ -0,0 +1,18 @@
1
+ require 'helper'
2
+ require 'fixtures/warden_app/app'
3
+
4
+ class TestWardenPlugin < Test::Unit::TestCase
5
+ def app
6
+ WardenDemo.tap { |app| app.set :environment, :test }
7
+ end
8
+
9
+ context 'for authenticate_user! helper' do
10
+ setup do
11
+ visit '/login', :post, :username => 'john21', :password => 'secret'
12
+ visit '/current_user'
13
+ end
14
+ should "return name of logged_in user" do
15
+ assert_have_selector :h1, :content => "John"
16
+ end
17
+ end
18
+ 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.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Esquenazi
@@ -46,14 +46,19 @@ files:
46
46
  - lib/sinatra_more/markup_plugin/form_builder/standard_form_builder.rb
47
47
  - lib/sinatra_more/markup_plugin/form_helpers.rb
48
48
  - lib/sinatra_more/markup_plugin/format_helpers.rb
49
- - lib/sinatra_more/markup_plugin/render_helpers.rb
50
49
  - lib/sinatra_more/markup_plugin/tag_helpers.rb
51
50
  - lib/sinatra_more/render_plugin.rb
51
+ - lib/sinatra_more/render_plugin/render_helpers.rb
52
52
  - lib/sinatra_more/warden_plugin.rb
53
53
  - lib/sinatra_more/warden_plugin/warden_helpers.rb
54
54
  - sinatra_more.gemspec
55
+ - test/fixtures/render_app/app.rb
56
+ - test/fixtures/render_app/views/bar/test.erb
57
+ - test/fixtures/render_app/views/foo/test.haml
58
+ - test/fixtures/warden_app/app.rb
55
59
  - test/helper.rb
56
- - test/test_sinatra_more.rb
60
+ - test/test_render_plugin.rb
61
+ - test/test_warden_plugin.rb
57
62
  has_rdoc: true
58
63
  homepage: http://github.com/nesquena/sinatra_more
59
64
  licenses: []
@@ -83,5 +88,8 @@ signing_key:
83
88
  specification_version: 3
84
89
  summary: Expands sinatra to allow for complex applications
85
90
  test_files:
91
+ - test/fixtures/render_app/app.rb
92
+ - test/fixtures/warden_app/app.rb
86
93
  - test/helper.rb
87
- - test/test_sinatra_more.rb
94
+ - test/test_render_plugin.rb
95
+ - test/test_warden_plugin.rb
@@ -1,7 +0,0 @@
1
- require 'helper'
2
-
3
- class TestSinatraMore < Test::Unit::TestCase
4
- should "probably rename this file and start testing for real" do
5
- flunk "hey buddy, you should probably rename this file and start testing for real"
6
- end
7
- end