middleman 0.2.7 → 0.2.8

Sign up to get free protection for your applications and to get access to all the features.
data/.gitmodules CHANGED
@@ -10,3 +10,6 @@
10
10
  [submodule "vendor/sinatra-content-for"]
11
11
  path = vendor/sinatra-content-for
12
12
  url = git://github.com/foca/sinatra-content-for.git
13
+ [submodule "vendor/sinatra-helpers"]
14
+ path = vendor/sinatra-helpers
15
+ url = git://github.com/sbfaulkner/sinatra-helpers.git
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.7
1
+ 0.2.8
data/lib/middleman.rb CHANGED
@@ -12,37 +12,22 @@ require File.join(File.dirname(__FILE__), '..', 'vendor', 'sinatra-maruku', 'lib
12
12
  # Include content_for support
13
13
  require File.join(File.dirname(__FILE__), '..', 'vendor', 'sinatra-content-for', 'lib', 'sinatra', 'content_for')
14
14
 
15
+ # Include common haml/html helper support
16
+ require File.join(File.dirname(__FILE__), '..', 'vendor', 'sinatra-helpers', 'lib', 'sinatra-helpers', 'haml')
17
+
15
18
  class Middleman < Sinatra::Base
16
19
  set :app_file, __FILE__
17
20
  set :static, true
18
21
  set :root, Dir.pwd
19
22
  set :environment, defined?(MIDDLEMAN_BUILDER) ? :build : :development
20
-
23
+
21
24
  helpers Sinatra::Markaby
22
25
  helpers Sinatra::Maruku
23
26
  helpers Sinatra::ContentFor
24
-
25
- helpers do
26
- def link_to(title, url="#", params={})
27
- params = params.map { |k,v| %Q{#{k}="#{v}"}}.join(' ')
28
- %Q{<a href="#{url}" #{params}>#{title}</a>}
29
- end
30
-
31
- def page_classes(*additional)
32
- classes = []
33
- parts = @full_request_path.split('.')[0].split('/')
34
- parts.each_with_index { |path, i| classes << parts.first(i+1).join('_') }
35
-
36
- classes << "index" if classes.empty?
37
- classes += additional unless additional.empty?
38
- classes.join(' ')
39
- end
40
-
41
- def sprite(name)
42
- image_tag("spacer.gif", :class => "#{name}-img")
43
- end
44
- end
45
-
27
+ helpers Sinatra::Helpers::Haml::Forms
28
+ helpers Sinatra::Helpers::Haml::Links
29
+ helpers Sinatra::Helpers::Haml::Partials
30
+
46
31
  def self.run!(options={}, &block)
47
32
  set options
48
33
  handler = detect_rack_handler
@@ -106,4 +91,6 @@ class Middleman < Sinatra::Base
106
91
 
107
92
  result || pass
108
93
  end
109
- end
94
+ end
95
+
96
+ require File.join(File.dirname(__FILE__), 'middleman', 'helpers')
@@ -0,0 +1,16 @@
1
+ Middleman.helpers do
2
+ def link_to(title, url="#", params={ :href => url })
3
+ params = params.map { |k,v| %Q{#{k}="#{v}"}}.join(' ')
4
+ %Q{<a #{params}>#{title}</a>}
5
+ end
6
+
7
+ def page_classes(*additional)
8
+ classes = []
9
+ parts = @full_request_path.split('.')[0].split('/')
10
+ parts.each_with_index { |path, i| classes << parts.first(i+1).join('_') }
11
+
12
+ classes << "index" if classes.empty?
13
+ classes += additional unless additional.empty?
14
+ classes.join(' ')
15
+ end
16
+ end
data/middleman.gemspec CHANGED
@@ -2,11 +2,11 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{middleman}
5
- s.version = "0.2.7"
5
+ s.version = "0.2.8"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Thomas Reynolds"]
9
- s.date = %q{2009-07-30}
9
+ s.date = %q{2009-07-31}
10
10
  s.email = %q{tdreyno@gmail.com}
11
11
  s.executables = ["mm-init", "mm-build", "mm-server"]
12
12
  s.extra_rdoc_files = [
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  "bin/mm-init",
26
26
  "bin/mm-server",
27
27
  "lib/middleman.rb",
28
+ "lib/middleman/helpers.rb",
28
29
  "lib/middleman/template/init.rb",
29
30
  "lib/middleman/template/views/index.haml",
30
31
  "lib/middleman/template/views/layout.haml",
@@ -70,6 +71,27 @@ Gem::Specification.new do |s|
70
71
  "vendor/sinatra-content-for/lib/sinatra/content_for.rb",
71
72
  "vendor/sinatra-content-for/sinatra-content-for.gemspec",
72
73
  "vendor/sinatra-content-for/test/content_for_test.rb",
74
+ "vendor/sinatra-helpers/LICENSE",
75
+ "vendor/sinatra-helpers/README.rdoc",
76
+ "vendor/sinatra-helpers/Rakefile",
77
+ "vendor/sinatra-helpers/VERSION.yml",
78
+ "vendor/sinatra-helpers/lib/sinatra-helpers.rb",
79
+ "vendor/sinatra-helpers/lib/sinatra-helpers/haml.rb",
80
+ "vendor/sinatra-helpers/lib/sinatra-helpers/haml/flash.rb",
81
+ "vendor/sinatra-helpers/lib/sinatra-helpers/haml/forms.rb",
82
+ "vendor/sinatra-helpers/lib/sinatra-helpers/haml/links.rb",
83
+ "vendor/sinatra-helpers/lib/sinatra-helpers/haml/partials.rb",
84
+ "vendor/sinatra-helpers/lib/sinatra-helpers/html.rb",
85
+ "vendor/sinatra-helpers/lib/sinatra-helpers/html/escape.rb",
86
+ "vendor/sinatra-helpers/sinatra-helpers.gemspec",
87
+ "vendor/sinatra-helpers/test/haml/flash_test.rb",
88
+ "vendor/sinatra-helpers/test/haml/forms_test.rb",
89
+ "vendor/sinatra-helpers/test/haml/links_test.rb",
90
+ "vendor/sinatra-helpers/test/haml/partials_test.rb",
91
+ "vendor/sinatra-helpers/test/haml/views/_object.haml",
92
+ "vendor/sinatra-helpers/test/haml/views/_thing.haml",
93
+ "vendor/sinatra-helpers/test/html/escape_test.rb",
94
+ "vendor/sinatra-helpers/test/test_helper.rb",
73
95
  "vendor/sinatra-markaby/CHANGES",
74
96
  "vendor/sinatra-markaby/LICENSE",
75
97
  "vendor/sinatra-markaby/README.rdoc",
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 unwwwired.net
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ = sinatra-helpers
2
+
3
+ sinatra-helpers is an extension for sinatra that provides a bunch of useful helpers
4
+ for sinatra applications.
5
+
6
+ == Installation
7
+
8
+ sudo gem install sbfaulkner-sinatra-helpers -s http://gems.github.com
9
+
10
+ == Usage
11
+
12
+ To use the helpers simply:
13
+
14
+ require 'sinatra-helpers'
15
+
16
+ If you don't want to include all of the functionality you can require specific
17
+ functionality in smaller units:
18
+
19
+ require 'sinatra-helpers/haml'
20
+
21
+ or even:
22
+
23
+ require 'sinatra-helpers/haml/partials'
24
+
25
+
26
+ == Legal
27
+
28
+ Author:: S. Brent Faulkner <brentf@unwwwired.net>
29
+ License:: Copyright (c) 2009 unwwwired.net, released under the MIT license
@@ -0,0 +1,44 @@
1
+ require 'rake'
2
+
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |s|
6
+ s.name = "sinatra-helpers"
7
+ s.summary = %Q{a bunch of useful helpers for sinatra applications}
8
+ s.email = "brentf@unwwwired.net"
9
+ s.homepage = "http://github.com/sbfaulkner/sinatra-helpers"
10
+ s.description = "a bunch of useful helpers for sinatra applications"
11
+ s.authors = ["S. Brent Faulkner"]
12
+ end
13
+ rescue LoadError
14
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
15
+ end
16
+
17
+ require 'rake/rdoctask'
18
+ Rake::RDocTask.new do |rdoc|
19
+ rdoc.rdoc_dir = 'rdoc'
20
+ rdoc.title = 'sinatra-helpers'
21
+ rdoc.options << '--line-numbers' << '--inline-source'
22
+ rdoc.rdoc_files.include('README*')
23
+ rdoc.rdoc_files.include('lib/**/*.rb')
24
+ end
25
+
26
+ require 'rake/testtask'
27
+ Rake::TestTask.new(:test) do |t|
28
+ t.libs << 'lib' << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+ begin
34
+ require 'rcov/rcovtask'
35
+ Rcov::RcovTask.new do |t|
36
+ t.libs << 'test'
37
+ t.test_files = FileList['test/**/*_test.rb']
38
+ t.verbose = true
39
+ end
40
+ rescue LoadError
41
+ puts "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
42
+ end
43
+
44
+ task :default => :test
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 9
4
+ :patch: 1
@@ -0,0 +1,2 @@
1
+ require 'sinatra-helpers/haml'
2
+ require 'sinatra-helpers/html'
@@ -0,0 +1,4 @@
1
+ require File.dirname(__FILE__) + '/haml/flash.rb'
2
+ require File.dirname(__FILE__) + '/haml/forms.rb'
3
+ require File.dirname(__FILE__) + '/haml/links.rb'
4
+ require File.dirname(__FILE__) + '/haml/partials.rb'
@@ -0,0 +1,17 @@
1
+ require 'sinatra/base'
2
+
3
+ module Sinatra
4
+ module Helpers
5
+ module Haml
6
+ module Flash
7
+ def flash_messages
8
+ [ :error, :warning, :notice ].each do |f|
9
+ haml_tag :div, flash[f], :class => f if flash.has?(f)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ helpers Helpers::Haml::Flash
17
+ end
@@ -0,0 +1,149 @@
1
+ require 'sinatra/base'
2
+ require 'active_support/inflector'
3
+
4
+ module Sinatra
5
+ module Helpers
6
+ module Haml
7
+ module Forms
8
+ CHECKBOX_DEFAULTS = {:type => :checkbox, :value => 1}
9
+ FILE_FIELD_DEFAULTS = {:type => :file}
10
+ HIDDEN_FIELD_DEFAULTS = {:type => :hidden}
11
+ PASSWORD_FIELD_DEFAULTS = {:type => :password}
12
+ TEXT_FIELD_DEFAULTS = {:type => :text}
13
+ TEXT_AREA_DEFAULTS = {}
14
+ SELECT_DEFAULTS = {}
15
+
16
+ def errors_for(object)
17
+ unless object.errors.empty?
18
+ errors = object.errors.full_messages
19
+ haml_tag :div, :id => 'errors' do
20
+ haml_tag :p, %Q(Unable to save #{object.class.name.humanize} due to the following #{errors.size == 1 ? 'error' : "#{errors.size} errors"}:)
21
+ haml_tag :ul do
22
+ haml_concat list_of(errors) { |error| haml_concat(error) }
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ def parameterize(arg)
29
+ case arg
30
+ when Array
31
+ arg.compact.collect { |a| parameterize(a) }
32
+ when String
33
+ arg
34
+ when Symbol, Numeric
35
+ arg.to_s
36
+ else
37
+ arg.class.name.underscore
38
+ end
39
+ end
40
+
41
+ def object_for(*args)
42
+ args.pop if args.last.is_a?(Hash)
43
+ args.pop
44
+ case object = args.last
45
+ when String, Symbol
46
+ if args.length > 1
47
+ object_for(*args).send(object)
48
+ else
49
+ instance_variable_get "@#{object}"
50
+ end
51
+ else
52
+ object
53
+ end
54
+ end
55
+
56
+ def value_for(*args)
57
+ object = object_for(*args)
58
+ options = args.last.is_a?(Hash) ? args.pop : {}
59
+ object.send args.pop
60
+ end
61
+
62
+ def id_for(*args)
63
+ options = args.last.is_a?(Hash) ? args.pop : {}
64
+ index = options[:index]
65
+ method = args.pop
66
+ args << index << method
67
+ parameterize(args).join('_')
68
+ end
69
+
70
+ def name_for(*args)
71
+ options = args.last.is_a?(Hash) ? args.pop : {}
72
+ index = options[:index]
73
+ method = args.pop
74
+ # pluralize collection name when indexed... explicit parameterization
75
+ args << parameterize(args.pop).pluralize << index if index
76
+ args << method
77
+ parameterize(args).inject { |name,part| "#{name}[#{part}]" }
78
+ end
79
+
80
+ def extract_options(*args)
81
+ args.last.is_a?(Hash) ? args.last.reject { |k,v| k.to_s == 'index' } : {}
82
+ end
83
+
84
+ def label(*args)
85
+ { :for => id_for(*args) }.merge extract_options(*args)
86
+ end
87
+
88
+ def input(*args)
89
+ { :id => id_for(*args), :name => name_for(*args) }.merge extract_options(*args)
90
+ end
91
+
92
+ def checkbox(*args)
93
+ args << {} unless args.last.is_a?(Hash)
94
+ options = args.last
95
+ options[:checked] = value_for(*args) unless options.include?(:checked)
96
+ CHECKBOX_DEFAULTS.merge input(*args)
97
+ end
98
+
99
+ def file_field(*args)
100
+ args << {} unless args.last.is_a?(Hash)
101
+ options = args.last
102
+ options[:value] = value_for(*args) unless options.include?(:value)
103
+ FILE_FIELD_DEFAULTS.merge input(*args)
104
+ end
105
+
106
+ def hidden_field(*args)
107
+ args << {} unless args.last.is_a?(Hash)
108
+ options = args.last
109
+ options[:value] = value_for(*args) unless options.include?(:value)
110
+ HIDDEN_FIELD_DEFAULTS.merge input(*args)
111
+ end
112
+
113
+ def password_field(*args)
114
+ args << {} unless args.last.is_a?(Hash)
115
+ options = args.last
116
+ options[:value] = value_for(*args) unless options.include?(:value)
117
+ PASSWORD_FIELD_DEFAULTS.merge input(*args)
118
+ end
119
+
120
+ def text_field(*args)
121
+ args << {} unless args.last.is_a?(Hash)
122
+ options = args.last
123
+ options[:value] = value_for(*args) unless options.include?(:value)
124
+ TEXT_FIELD_DEFAULTS.merge input(*args)
125
+ end
126
+
127
+ def text_area(*args)
128
+ args << {} unless args.last.is_a?(Hash)
129
+ options = args.last
130
+ TEXT_AREA_DEFAULTS.merge input(*args)
131
+ end
132
+
133
+ def select(*args)
134
+ SELECT_DEFAULTS.merge input(*args)
135
+ end
136
+
137
+ def options_of(collection, options = {}, &block)
138
+ collection.each do |option|
139
+ text, value = option
140
+ selected = block_given? ? block.call(option) : nil
141
+ haml_tag :option, text, options.merge(:selected => selected, :value => value)
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+
148
+ helpers Helpers::Haml::Forms
149
+ end
@@ -0,0 +1,102 @@
1
+ require 'sinatra/base'
2
+
3
+ module Sinatra
4
+ module Helpers
5
+ module Haml
6
+ module Links
7
+ def domain(tld_length = 1)
8
+ request.host.split('.')[-(tld_length+1)..-1].join('.')
9
+ end
10
+
11
+ def subdomain(name, tld_length = 1)
12
+ "#{name}.#{domain(tld_length)}"
13
+ end
14
+
15
+ def subdomain_url(name, tld_length = 1)
16
+ "http://#{subdomain(name, tld_length)}"
17
+ end
18
+
19
+ def asset_url(path, tld_length = 1)
20
+ "#{subdomain_url('assets', tld_length)}#{path}"
21
+ end
22
+
23
+ def css(path, options = {})
24
+ { :href => asset_url(path), :media => "all", :rel => "stylesheet", :type => "text/css" }.merge(options)
25
+ end
26
+
27
+ def icon(path, options = {})
28
+ { :href => asset_url(path), :rel => 'icon', :type => 'image/x-icon' }.merge(options)
29
+ end
30
+
31
+ def javascript(path, options = {})
32
+ { :src => asset_url(path), :type => 'text/javascript' }.merge(options)
33
+ end
34
+
35
+ def image(path, options = {})
36
+ options[:width], options[:height] = options.delete(:size).split('x') if options.include?(:size)
37
+ options[:alt] ||= File.basename(path, '.*')
38
+ { :src => asset_url(path) }.merge(options)
39
+ end
40
+
41
+ def action(path, options = {})
42
+ { :action => path, :method => 'post' }.merge(options)
43
+ end
44
+
45
+ def href(path, options = {})
46
+ script = if options.include?(:success)
47
+ path, original_path = '#', path
48
+ javascript_for_ajax(original_path, options)
49
+ elsif method = options.delete(:method)
50
+ path, original_path = '#', path
51
+ javascript_for_method(original_path, method)
52
+ else
53
+ options.delete(:onclick)
54
+ end
55
+ confirm = options.delete(:confirm)
56
+ script = javascript_with_confirmation(script, confirm) if confirm
57
+ result = { :href => path }
58
+ result[:onclick] = script unless script.nil?
59
+ result.merge(options)
60
+ end
61
+
62
+ protected
63
+ def javascript_options(options)
64
+ options.collect do |key,value|
65
+ value = "'#{value}'" if [:type,:url].include? key
66
+ "#{key}:#{value}"
67
+ end.join(',')
68
+ end
69
+
70
+ def javascript_with_confirmation(javascript, prompt)
71
+ prompt = 'Are you sure?' if prompt == true
72
+ javascript = 'return true;' if javascript.nil?
73
+ "if (confirm('#{prompt}')) { #{javascript} }; return false;"
74
+ end
75
+
76
+ def javascript_for_method(action, method)
77
+ javascript = "$('<form></form>').css('display','none').attr({'method':'post','action':'#{action}'})"
78
+ javascript << ".append($('<input type=\"hidden\">').attr({'name':'_method','value':'#{method}'}))" if method != 'post'
79
+ javascript << ".appendTo($(this).parent()).submit();return false;"
80
+ end
81
+
82
+ def javascript_for_ajax(url, options = {})
83
+ ajax_options = {:url => url}
84
+ success = options.delete(:success)
85
+ if !success.is_a?(Hash)
86
+ function = "$('#{success}').replaceWith(html)"
87
+ elsif success.include?(:update)
88
+ function = success[:position] || 'replaceWith'
89
+ function = "$('#{success[:update]}').#{function}(html);"
90
+ elsif success.include?(:remove)
91
+ function = "$('#{success[:remove]}').remove();"
92
+ end
93
+ ajax_options[:type] = options.delete(:method) if options.include?(:method)
94
+ ajax_options[:success] = "function(html){#{function}}"
95
+ "$.ajax({#{javascript_options(ajax_options)}});return false;"
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ helpers Helpers::Haml::Links
102
+ end
@@ -0,0 +1,26 @@
1
+ require 'sinatra/base'
2
+ require 'enumerator'
3
+
4
+ module Sinatra
5
+ module Helpers
6
+ module Haml
7
+ module Partials
8
+ def haml_partial(name, options = {})
9
+ item_name = name.to_sym
10
+ counter_name = "#{name}_counter".to_sym
11
+ if collection = options.delete(:collection)
12
+ collection.enum_for(:each_with_index).collect do |item,index|
13
+ haml_partial name, options.merge(:locals => {item_name => item, counter_name => index+1})
14
+ end.join
15
+ elsif object = options.delete(:object)
16
+ haml_partial name, options.merge(:locals => {item_name => object, counter_name => nil})
17
+ else
18
+ haml "_#{name}".to_sym, options.merge(:layout => false)
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ helpers Helpers::Haml::Partials
26
+ end
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + '/html/escape'
@@ -0,0 +1,20 @@
1
+ require 'sinatra/base'
2
+
3
+ module Sinatra
4
+ module Helpers
5
+ module Html
6
+ module Escape
7
+ def h(text)
8
+ Rack::Utils.escape_html(text)
9
+ end
10
+
11
+ def h!(text, blank_text = '&nbsp;')
12
+ return blank_text if text.nil? || text.empty?
13
+ h text
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ helpers Helpers::Html::Escape
20
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{sinatra-helpers}
5
+ s.version = "0.9.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["S. Brent Faulkner"]
9
+ s.date = %q{2009-03-27}
10
+ s.description = %q{a bunch of useful helpers for sinatra applications}
11
+ s.email = %q{brentf@unwwwired.net}
12
+ s.files = ["README.rdoc", "VERSION.yml", "lib/sinatra-helpers", "lib/sinatra-helpers/haml", "lib/sinatra-helpers/haml/flash.rb", "lib/sinatra-helpers/haml/forms.rb", "lib/sinatra-helpers/haml/links.rb", "lib/sinatra-helpers/haml/partials.rb", "lib/sinatra-helpers/haml.rb", "lib/sinatra-helpers/html", "lib/sinatra-helpers/html/escape.rb", "lib/sinatra-helpers/html.rb", "lib/sinatra-helpers.rb", "test/haml", "test/haml/flash_test.rb", "test/haml/forms_test.rb", "test/haml/links_test.rb", "test/haml/partials_test.rb", "test/haml/views", "test/haml/views/_object.haml", "test/haml/views/_thing.haml", "test/html", "test/html/escape_test.rb", "test/test_helper.rb"]
13
+ s.has_rdoc = true
14
+ s.homepage = %q{http://github.com/sbfaulkner/sinatra-helpers}
15
+ s.rdoc_options = ["--inline-source", "--charset=UTF-8"]
16
+ s.require_paths = ["lib"]
17
+ s.rubygems_version = %q{1.3.1}
18
+ s.summary = %q{a bunch of useful helpers for sinatra applications}
19
+
20
+ if s.respond_to? :specification_version then
21
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
22
+ s.specification_version = 2
23
+
24
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
25
+ else
26
+ end
27
+ else
28
+ end
29
+ end
@@ -0,0 +1,57 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class MockFlash < Hash
4
+ def initialize(flash = {})
5
+ flash.each { |k,v| self[k] = v }
6
+ end
7
+
8
+ alias_method :has?, :include?
9
+ end
10
+
11
+ class FlashTest < Test::Unit::TestCase
12
+ include Haml::Helpers
13
+ include Sinatra::Helpers::Haml::Flash
14
+
15
+ attr_reader :flash
16
+
17
+ def setup
18
+ init_haml_helpers
19
+ super
20
+ end
21
+
22
+ def test_should_not_show_message_for_empty_flash
23
+ @flash = MockFlash.new
24
+ flash_messages
25
+ assert_html ''
26
+ end
27
+
28
+ def test_should_show_message_for_error_flash
29
+ @flash = MockFlash.new :error => 'error message'
30
+ flash_messages
31
+ assert_html 'div', 1
32
+ assert_html 'div.error', :html => 'error message', :count => 1
33
+ end
34
+
35
+ def test_should_show_message_for_warning_flash
36
+ @flash = MockFlash.new :warning => 'warning message'
37
+ flash_messages
38
+ assert_html 'div', 1
39
+ assert_html 'div.warning', :html => 'warning message', :count => 1
40
+ end
41
+
42
+ def test_should_show_message_for_notice_flash
43
+ @flash = MockFlash.new :notice => 'notice message'
44
+ flash_messages
45
+ assert_html 'div', 1
46
+ assert_html 'div.notice', :html => 'notice message', :count => 1
47
+ end
48
+
49
+ def test_should_show_messages_for_multiple_flash
50
+ @flash = MockFlash.new :error => 'error message', :warning => 'warning message', :notice => 'notice message'
51
+ flash_messages
52
+ assert_html 'div', 3
53
+ assert_html 'div.error', :html => 'error message', :count => 1
54
+ assert_html 'div.warning', :html => 'warning message', :count => 1
55
+ assert_html 'div.notice', :html => 'notice message', :count => 1
56
+ end
57
+ end
@@ -0,0 +1,219 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class FormsTest < Test::Unit::TestCase
4
+ include Haml::Helpers
5
+ include Sinatra::Helpers::Haml::Forms
6
+
7
+ def setup
8
+ @invoice = mock
9
+ @invoice.stubs(:id => Time.now.to_i, :class => stub(:name => 'Invoice'))
10
+ init_haml_helpers
11
+ super
12
+ end
13
+
14
+ def test_object_for_should_return_object
15
+ assert_equal @invoice, object_for(:invoice, :id)
16
+ end
17
+
18
+ def test_object_for_should_support_explicit_object
19
+ assert_equal @invoice, object_for(@invoice, :id)
20
+ end
21
+
22
+ def test_value_for_should_return_value
23
+ @invoice.stubs(:client_name => 'Client name')
24
+ assert_equal @invoice.client_name, value_for(:invoice, :client_name)
25
+ end
26
+
27
+ def test_value_for_should_support_explicit_object
28
+ @invoice.stubs(:client_name => 'Client name')
29
+ assert_equal @invoice.client_name, value_for(@invoice, :client_name)
30
+ end
31
+
32
+ def test_id_for_should_build_simple_id
33
+ assert_equal "invoice_id", id_for(:invoice, :id)
34
+ end
35
+
36
+ def test_name_for_should_build_simple_name
37
+ assert_equal "invoice[id]", name_for(:invoice, :id)
38
+ end
39
+
40
+ def test_id_for_should_support_explicit_object
41
+ assert_equal "invoice_id", id_for(@invoice, :id)
42
+ end
43
+
44
+ def test_name_for_should_support_explicit_object
45
+ assert_equal "invoice[id]", name_for(@invoice, :id)
46
+ end
47
+
48
+ def test_id_for_should_index_collection
49
+ assert_equal "line_1_id", id_for(:line, :id, :index => 1)
50
+ end
51
+
52
+ def test_name_for_should_index_collection
53
+ assert_equal "lines[1][id]", name_for(:line, :id, :index => 1)
54
+ end
55
+
56
+ def test_id_for_should_index_collection_by_id
57
+ line = line_stub
58
+ assert_equal "invoice_line_#{line.id}_id", id_for(:invoice, line, :id, :index => line.id)
59
+ end
60
+
61
+ def test_name_for_should_index_collection_by_id
62
+ line = line_stub
63
+ assert_equal "invoice[lines][#{line.id}][id]", name_for(:invoice, line, :id, :index => line.id)
64
+ end
65
+
66
+ def test_id_for_should_index_collection_by_new_id
67
+ line = line_stub
68
+ assert_equal "invoice_line_#{line.id}_id", id_for(:invoice, line, :id, :index => line.id)
69
+ end
70
+
71
+ def test_name_for_should_index_collection_by_new_id
72
+ line = line_stub
73
+ assert_equal "invoice[lines][#{line.id}][id]", name_for(:invoice, line, :id, :index => line.id)
74
+ end
75
+
76
+ def test_name_and_id_should_index_collection_by_same_new_id
77
+ line = line_stub
78
+ id = line.id
79
+ assert_equal "invoice_line_#{id}_id", id_for(:invoice, line, :id, :index => line.id)
80
+ assert_equal "invoice[lines][#{id}][id]", name_for(:invoice, line, :id, :index => line.id)
81
+ end
82
+
83
+ def test_id_for_should_support_parts
84
+ assert_equal "invoice_invoiced_on_year", id_for(@invoice, :invoiced_on, :year)
85
+ end
86
+
87
+ def test_name_for_should_support_parts
88
+ assert_equal "invoice[invoiced_on][year]", name_for(@invoice, :invoiced_on, :year)
89
+ end
90
+
91
+ def test_value_for_should_support_parts
92
+ @invoice.stubs(:invoiced_on => Time.now)
93
+ assert_equal @invoice.invoiced_on.year, value_for(@invoice, :invoiced_on, :year)
94
+ end
95
+
96
+ def test_id_for_should_support_parent
97
+ assert_equal "invoice_line_1_description", id_for(:invoice, :line, :description, :index => 1)
98
+ end
99
+
100
+ def test_name_for_should_support_parent
101
+ assert_equal "invoice[lines][1][description]", name_for(:invoice, :line, :description, :index => 1)
102
+ end
103
+
104
+ def test_id_for_should_support_explicit_object_with_parent
105
+ line = line_stub
106
+ assert_equal "invoice_line_1_description", id_for(@invoice, line, :description, :index => 1)
107
+ end
108
+
109
+ def test_name_for_should_support_explicit_object_with_parent
110
+ line = line_stub
111
+ assert_equal "invoice[lines][1][description]", name_for(@invoice, line, :description, :index => 1)
112
+ end
113
+
114
+ def test_errors_for_returns_nil_when_no_errors
115
+ @invoice.stubs(:errors => stub(:empty? => true))
116
+ errors_for(@invoice)
117
+ assert_html '', nil
118
+ end
119
+
120
+ def test_errors_for_returns_error_messages
121
+ @invoice.stubs(:class => stub(:name => 'Class name'), :errors => stub(:empty? => false, :full_messages => ['Field name']))
122
+ errors_for(@invoice)
123
+ assert_html 'p', :html => /Class name/, :count => 1
124
+ assert_html 'li', :html => /Field name/, :min => 1
125
+ end
126
+
127
+ def test_label
128
+ result = label(@invoice, :client_name)
129
+ assert_equal id_for(@invoice, :client_name), result[:for]
130
+ end
131
+
132
+ def test_checkbox
133
+ line = line_stub(:taxable => true)
134
+ @invoice.stubs(:lines).returns([line])
135
+ result = checkbox(@invoice, line, :taxable, :index => 1)
136
+ assert_equal :checkbox, result[:type]
137
+ assert_equal name_for(@invoice, line, :taxable, :index => 1), result[:name]
138
+ assert_equal id_for(@invoice, line, :taxable, :index => 1), result[:id]
139
+ assert_equal 1, result[:value]
140
+ assert result[:checked]
141
+ end
142
+
143
+ def test_checkbox_not_checked
144
+ line = line_stub(:taxable => false)
145
+ @invoice.stubs(:lines => [line])
146
+ result = checkbox(@invoice, line, :taxable, :index => 1)
147
+ assert_equal :checkbox, result[:type]
148
+ assert_equal name_for(@invoice, line, :taxable, :index => 1), result[:name]
149
+ assert_equal id_for(@invoice, line, :taxable, :index => 1), result[:id]
150
+ assert_equal 1, result[:value]
151
+ assert !result[:checked]
152
+ end
153
+
154
+ def test_hidden_field
155
+ @invoice.stubs(:client_name => 'Client name')
156
+ result = hidden_field(@invoice, :client_name)
157
+ assert_equal :hidden, result[:type]
158
+ assert_equal name_for(@invoice, :client_name), result[:name]
159
+ assert_equal id_for(@invoice, :client_name), result[:id]
160
+ assert_equal @invoice.client_name, result[:value]
161
+ end
162
+
163
+ def test_text_field
164
+ @invoice.stubs(:client_name => 'Client name')
165
+ result = text_field(@invoice, :client_name)
166
+ assert_equal :text, result[:type]
167
+ assert_equal name_for(@invoice, :client_name), result[:name]
168
+ assert_equal id_for(@invoice, :client_name), result[:id]
169
+ assert_equal @invoice.client_name, result[:value]
170
+ end
171
+
172
+ def test_text_area
173
+ result = text_area(@invoice, :client_name)
174
+ assert_equal name_for(@invoice, :client_name), result[:name]
175
+ assert_equal id_for(@invoice, :client_name), result[:id]
176
+ end
177
+
178
+ def test_select
179
+ result = select(@invoice, :client_name)
180
+ assert_equal name_for(@invoice, :client_name), result[:name]
181
+ assert_equal id_for(@invoice, :client_name), result[:id]
182
+ end
183
+
184
+ def test_options_of
185
+ dwarfs = %w(Bashful Doc Dopey Grumpy Happy Sleepy Sneezy)
186
+ options_of dwarfs
187
+ assert_html 'option', :count => 7 do |options|
188
+ assert_equal dwarfs, options.collect { |o| o.inner_html }
189
+ end
190
+ end
191
+
192
+ def test_options_of_with_selection
193
+ dwarfs = %w(Bashful Doc Dopey Grumpy Happy Sleepy Sneezy)
194
+ options_of dwarfs do |dwarf|
195
+ dwarf == 'Sleepy'
196
+ end
197
+ assert_html 'option', :count => 7 do |options|
198
+ assert_equal dwarfs, options.collect { |o| o.inner_html }
199
+ end
200
+ assert_html 'option[@selected="selected"]', :count => 1, :html => 'Sleepy'
201
+ end
202
+
203
+ def test_options_of_with_values
204
+ months = Date::MONTHNAMES.enum_for(:each_with_index).collect[1..12]
205
+ options_of months
206
+ assert_html 'option', :count => 12 do |options|
207
+ options.each_with_index do |option,i|
208
+ assert_html option, "option[@value='#{months[i].last}']", months[i].first
209
+ end
210
+ end
211
+ end
212
+
213
+ protected
214
+ def line_stub(methods = {})
215
+ line = stub({:class => stub(:name => 'Line'), :id => Time.now.to_i}.merge(methods))
216
+ @invoice.stubs(:lines).returns([line])
217
+ line
218
+ end
219
+ end
@@ -0,0 +1,71 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class LinksTest < Test::Unit::TestCase
4
+ include Haml::Helpers
5
+ include Sinatra::Helpers::Haml::Links
6
+
7
+ attr_reader :request
8
+
9
+ def setup
10
+ @request = mock()
11
+ @request.stubs(:host => 'www.example.com')
12
+ init_haml_helpers
13
+ super
14
+ end
15
+
16
+ def test_domain
17
+ assert_equal 'example.com', domain
18
+ end
19
+
20
+ def test_subdomain
21
+ assert_equal 'test.example.com', subdomain('test')
22
+ end
23
+
24
+ def test_subdomain_url
25
+ assert_equal 'http://another.example.com', subdomain_url('another')
26
+ end
27
+
28
+ def test_asset_url
29
+ assert_equal 'http://assets.example.com/images/logo.gif', asset_url('/images/logo.gif')
30
+ end
31
+
32
+ def test_css
33
+ assert_equal({ :href => 'http://assets.example.com/stylesheets/test.css', :media => 'all', :rel => 'stylesheet', :type => 'text/css' }, css('/stylesheets/test.css'))
34
+ end
35
+
36
+ def test_icon
37
+ assert_equal({ :href => 'http://assets.example.com/icons/save.gif', :rel => 'icon', :type => 'image/x-icon' }, icon('/icons/save.gif'))
38
+ end
39
+
40
+ def test_javascript
41
+ assert_equal({ :src => 'http://assets.example.com/javascripts/jquery.js', :type => 'text/javascript' }, javascript('/javascripts/jquery.js'))
42
+ end
43
+
44
+ def test_image
45
+ assert_equal({ :alt => 'test', :height => '100', :src => 'http://assets.example.com/images/test.jpg', :width => '200' }, image('/images/test.jpg', :size => '200x100'))
46
+ end
47
+
48
+ def test_action
49
+ assert_equal({ :action => '/invoices/123', :method => 'post' }, action('/invoices/123'))
50
+ end
51
+
52
+ def test_action_should_override_method
53
+ assert_equal({ :action => '/invoices/123', :method => 'delete' }, action('/invoices/123', :method => 'delete'))
54
+ end
55
+
56
+ def test_href
57
+ assert_equal({ :href => '/link' }, href('/link'))
58
+ end
59
+
60
+ def test_href_should_support_method_using_hidden_form
61
+ assert_equal({ :href => '#', :onclick => "$('<form></form>').css('display','none').attr({'method':'post','action':'/link'}).append($('<input type=\"hidden\">').attr({'name':'_method','value':'put'})).appendTo($(this).parent()).submit();return false;" }, href('/link', :method => 'put'))
62
+ end
63
+
64
+ def test_href_should_support_confirm
65
+ assert_equal({ :href=>"/link", :onclick => "if (confirm('Really?')) { return true; }; return false;" }, href('/link', :confirm => 'Really?'))
66
+ end
67
+
68
+ def test_href_should_support_ajax
69
+ assert_equal({ :href=> "#", :onclick=> "$.ajax({url:'/link',success:function(html){$('#result').replaceWith(html);}});return false;" }, href('/link', :success => { :update => '#result' }))
70
+ end
71
+ end
@@ -0,0 +1,38 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class PartialsTest < Test::Unit::TestCase
4
+ include Haml::Helpers
5
+ include Sinatra::Helpers::Haml::Partials
6
+ include Sinatra::Templates
7
+
8
+ @templates = {}
9
+
10
+ class << self
11
+ attr_accessor :templates
12
+ end
13
+
14
+ attr_accessor :options
15
+
16
+ def setup
17
+ @options = mock()
18
+ @options.stubs(:views => File.dirname(__FILE__) + '/views')
19
+ init_haml_helpers
20
+ super
21
+ end
22
+
23
+ def test_partial
24
+ assert_html haml_partial('thing'), nil, "<p>Hello world</p>\n"
25
+ end
26
+
27
+ def test_partial_with_object
28
+ assert_html haml_partial('object', :object => mock(:name => 'object')), nil, "<p>Hello object</p>\n"
29
+ end
30
+
31
+ def test_partial_with_locals
32
+ assert_html haml_partial('object', :locals => { :object => mock(:name => 'local')}), nil, "<p>Hello local</p>\n"
33
+ end
34
+
35
+ def test_partial_with_collection
36
+ assert_html haml_partial('object', :collection => [ mock(:name => 'one'), mock(:name => 'two')]), nil, "<p>Hello one</p>\n<p>Hello two</p>\n"
37
+ end
38
+ end
@@ -0,0 +1 @@
1
+ %p== Hello #{object.name}
@@ -0,0 +1 @@
1
+ %p Hello world
@@ -0,0 +1,37 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class EscapeTest < Test::Unit::TestCase
4
+ include Sinatra::Helpers::Html::Escape
5
+
6
+ def test_h_should_escape_text
7
+ assert_equal 'this &amp; that', h('this & that')
8
+ end
9
+
10
+ def test_h_should_return_blank_for_empty_string
11
+ assert_equal '', h('')
12
+ end
13
+
14
+ def test_h_should_return_blank_for_nil
15
+ assert_equal '', h(nil)
16
+ end
17
+
18
+ def test_hbang_should_escape_text
19
+ assert_equal 'this &amp; that', h!('this & that')
20
+ end
21
+
22
+ def test_hbang_should_return_nbsp_for_empty_string
23
+ assert_equal '&nbsp;', h!('')
24
+ end
25
+
26
+ def test_hbang_should_return_nbsp_for_nil
27
+ assert_equal '&nbsp;', h!(nil)
28
+ end
29
+
30
+ def test_hbang_should_return_arbitrary_placeholder_for_empty_string
31
+ assert_equal 'N/A', h!('', 'N/A')
32
+ end
33
+
34
+ def test_hbang_should_return_arbitrary_placeholder_for_nil
35
+ assert_equal 'N/A', h!(nil, 'N/A')
36
+ end
37
+ end
@@ -0,0 +1,39 @@
1
+ require 'rubygems'
2
+
3
+ $:.unshift File.dirname(__FILE__) + '../lib'
4
+
5
+ require 'haml'
6
+
7
+ require 'sinatra-helpers'
8
+
9
+ require 'test/unit'
10
+ require 'mocha'
11
+ require 'hpricot'
12
+
13
+ class Test::Unit::TestCase
14
+ def assert_html(*args) # [doc], selector, match)
15
+ match = args.pop
16
+ selector = args.pop
17
+ html = args.pop || haml_buffer.buffer
18
+ html = html.to_html if html.respond_to? :to_html
19
+ selection = Hpricot(html)/selector unless match.nil?
20
+ case match
21
+ when NilClass
22
+ assert_equal selector, html
23
+ when Numeric
24
+ assert_equal match, selection.size, "Expected selection of exactly <#{match}>"
25
+ when String
26
+ assert_equal match, selection.inner_html, "Expected selection to equal <#{match}>"
27
+ when Regexp
28
+ assert_match match, selection.inner_html, "Expected selection to match <#{match}>"
29
+ when Hash
30
+ assert selection.size >= match[:min], "Expected selection of at least <#{match[:min]}>" if match.include?(:min)
31
+ assert selection.size <= match[:max], "Expected selection of at most <#{match[:max]}>" if match.include?(:max)
32
+ assert_html html, selector, match[:count] if match.include?(:count)
33
+ assert_html html, selector, match[:html] if match.include?(:html)
34
+ end
35
+ if block_given? && !selection.empty?
36
+ yield selection
37
+ end
38
+ end
39
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: middleman
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.7
4
+ version: 0.2.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Reynolds
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-30 00:00:00 -07:00
12
+ date: 2009-07-31 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -95,6 +95,7 @@ files:
95
95
  - bin/mm-init
96
96
  - bin/mm-server
97
97
  - lib/middleman.rb
98
+ - lib/middleman/helpers.rb
98
99
  - lib/middleman/template/init.rb
99
100
  - lib/middleman/template/views/index.haml
100
101
  - lib/middleman/template/views/layout.haml
@@ -140,6 +141,27 @@ files:
140
141
  - vendor/sinatra-content-for/lib/sinatra/content_for.rb
141
142
  - vendor/sinatra-content-for/sinatra-content-for.gemspec
142
143
  - vendor/sinatra-content-for/test/content_for_test.rb
144
+ - vendor/sinatra-helpers/LICENSE
145
+ - vendor/sinatra-helpers/README.rdoc
146
+ - vendor/sinatra-helpers/Rakefile
147
+ - vendor/sinatra-helpers/VERSION.yml
148
+ - vendor/sinatra-helpers/lib/sinatra-helpers.rb
149
+ - vendor/sinatra-helpers/lib/sinatra-helpers/haml.rb
150
+ - vendor/sinatra-helpers/lib/sinatra-helpers/haml/flash.rb
151
+ - vendor/sinatra-helpers/lib/sinatra-helpers/haml/forms.rb
152
+ - vendor/sinatra-helpers/lib/sinatra-helpers/haml/links.rb
153
+ - vendor/sinatra-helpers/lib/sinatra-helpers/haml/partials.rb
154
+ - vendor/sinatra-helpers/lib/sinatra-helpers/html.rb
155
+ - vendor/sinatra-helpers/lib/sinatra-helpers/html/escape.rb
156
+ - vendor/sinatra-helpers/sinatra-helpers.gemspec
157
+ - vendor/sinatra-helpers/test/haml/flash_test.rb
158
+ - vendor/sinatra-helpers/test/haml/forms_test.rb
159
+ - vendor/sinatra-helpers/test/haml/links_test.rb
160
+ - vendor/sinatra-helpers/test/haml/partials_test.rb
161
+ - vendor/sinatra-helpers/test/haml/views/_object.haml
162
+ - vendor/sinatra-helpers/test/haml/views/_thing.haml
163
+ - vendor/sinatra-helpers/test/html/escape_test.rb
164
+ - vendor/sinatra-helpers/test/test_helper.rb
143
165
  - vendor/sinatra-markaby/CHANGES
144
166
  - vendor/sinatra-markaby/LICENSE
145
167
  - vendor/sinatra-markaby/README.rdoc