kelredd-sinatra-helpers 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,37 +6,106 @@ module SinatraHelpers::Erb; end
6
6
 
7
7
  module SinatraHelpers::Erb::Partials
8
8
 
9
- # helper to emulate rails' 'render :partial' helper, using erb
10
- # => taken from the sinatra book, http://sinatra-book.gittr.com/#implemention_of_rails_style_partials
11
- # Render the partial once:
12
- # Usage: partial :foo
13
- #
14
- # foo partial will be rendered once for each element in the array, passing in a local variable named "foo"
15
- # Usage: partial :foo, :collection => @my_foos
16
- def partial(template, options={})
17
- options.merge!(:layout => false)
18
- path = template.to_s.split(File::SEPARATOR)
19
- object = path[-1].to_sym
20
- path[-1] = "_#{path[-1]}"
21
- template = File.join(path).to_sym
22
- raise 'partial collection specified but is nil' if options.has_key?(:collection) && options[:collection].nil?
23
- if collection = options.delete(:collection)
24
- options.delete(:object) # ignore any object passed in when using :collection
25
- options[:locals] ||= {}
9
+ # helper to somewhat emulate rails' 'render :partial' helper, using erb
10
+ # => originally taken from the sinatra book:
11
+ # http://sinatra-book.gittr.com/#implemention_of_rails_style_partials
12
+ # => updated: pass a block to specify the partial subject (ie object or collection)
13
+ # => updated: pass a hash to specify locals directly
14
+ # => if a block is passed, will look for template in a path relative to
15
+ # to the calling template path. Specify template with a leading '/'
16
+ # to force the path to be relative to the views directory.
17
+ #
18
+ # Render the partial once (no subject) with no locals:
19
+ # Usage: partial :item
20
+ # => looks for template at <views>/_item.erb
21
+ # Render a nested template
22
+ # Usage: partial '/items/item'
23
+ # => looks for template at <views>/items/_item.erb
24
+ #
25
+ # Render the partial once (no subject) WITH locals:
26
+ # Usage: partial :item, :user => @me
27
+ #
28
+ # Render the partial once on an object:
29
+ # Implicit usage: partial :item { @item }
30
+ # => knows subject should be treated as an object (and not a collection)
31
+ # b/c @item is not enumerable
32
+ # Explicit usage: partial :item, :item => @item_set
33
+ # -- or --: partial :item, :object => @item_set
34
+ # => use a local if your subject is enumerable and you want
35
+ # it treated as a single object
36
+ #
37
+ # Render the partial over a collection:
38
+ # => knows subject should be treated as a collection (and not a single object)
39
+ # b/c @items is enumerable
40
+ # Implicit usage: partial :item { @items }
41
+ # Explicit usage: partial :item, :collection => @items
42
+ #
43
+ # Generally, passing a block is the preferred method b/c
44
+ # you can specify templates relative to the calling template's
45
+ # path, for example:
46
+ # => if in template <views>/items/_item_list.erb
47
+ # => partial :item { @items }
48
+ # => will look for the template at <views>/items/_item.erb
49
+ # => however
50
+ # => partial :item, :collection => @items
51
+ # => will look for the template at <views>/_item.erb
52
+ # => you can force this behavior in the block version
53
+ # => by prepending a '/' to the template
54
+ # => partial '/item' { @items }
55
+ # => will look for the template at <views>/_item.erb
56
+
57
+ def partial(path, locals={}, &subject_block)
58
+ raise "locals must be specified with a Hash" unless locals.kind_of?(::Hash)
59
+ params = _partial_params(path, locals, subject_block)
60
+ if params[:subject] && params[:subject].respond_to?(:collect)
26
61
  counter = 0
27
- collection.inject([]) do |buffer, member|
28
- counter += 1
29
- erb_opts = options.dup
30
- erb_opts[:locals].merge!({object => member, "#{object}_counter".to_sym => counter})
31
- buffer << erb(template, erb_opts)
62
+ params[:subject].collect do |subject|
63
+ erb(params[:path], params[:options].merge({
64
+ :locals => params[:locals].merge({
65
+ params[:name].to_sym => subject,
66
+ "#{params[:name]}_counter".to_sym => counter += 1
67
+ })
68
+ }))
32
69
  end.join("\n")
33
70
  else
34
- if member = options.delete(:object)
35
- options[:locals] ||= {}
36
- options[:locals].merge!({object => member})
37
- end
38
- erb(template, options)
71
+ erb(params[:path], params[:options].merge({
72
+ :locals => params[:locals].merge({
73
+ params[:name].to_sym => params[:subject]
74
+ })
75
+ }))
76
+ end
77
+ end
78
+
79
+ private
80
+
81
+ def _partial_params(path, locals, subject_block)
82
+ {
83
+ :name => name = File.basename(path.to_s).split(".").first.to_sym,
84
+ :subject => begin
85
+ subject_block.call
86
+ rescue
87
+ nil
88
+ end ||
89
+ locals.delete(:collection) ||
90
+ locals.delete(:object) ||
91
+ locals.delete(name),
92
+ :locals => locals.delete(:locals) || locals,
93
+ :options => { :layout => false },
94
+ :path => _partial_path(path, subject_block).to_sym
95
+ }
96
+ end
97
+
98
+ def _partial_path(path, subject_block)
99
+ partial_path = if path.to_s =~ /\A\// || subject_block.nil?
100
+ path.to_s
101
+ else
102
+ File.join(*[
103
+ eval("File.expand_path(File.dirname(__FILE__))", subject_block).
104
+ gsub(File.expand_path(Sinatra::Application.views), ''),
105
+ path.to_s
106
+ ])
39
107
  end
108
+ File.join(File.dirname(partial_path), "_#{File.basename(partial_path)}")
40
109
  end
41
110
 
42
111
  end
@@ -0,0 +1,8 @@
1
+ /*
2
+ Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3
+ Code licensed under the BSD License:
4
+ http://developer.yahoo.net/yui/license.txt
5
+ version: 2.7.0
6
+ */
7
+ html{color:#000;background:#FFF;}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,textarea,p,blockquote,th,td{margin:0;padding:0;}table{border-collapse:collapse;border-spacing:0;}fieldset,img{border:0;}address,caption,cite,code,dfn,em,strong,th,var,optgroup{font-style:inherit;font-weight:inherit;}del,ins{text-decoration:none;}li{list-style:none;}caption,th{text-align:left;}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;}q:before,q:after{content:'';}abbr,acronym{border:0;font-variant:normal;}sup{vertical-align:baseline;}sub{vertical-align:baseline;}legend{color:#000;}input,button,textarea,select,optgroup,option{font-family:inherit;font-size:inherit;font-style:inherit;font-weight:inherit;}input,button,textarea,select{*font-size:100%;}body{font:13px/1.231 arial,helvetica,clean,sans-serif;*font-size:small;*font:x-small;}select,input,button,textarea,button{font:99% arial,helvetica,clean,sans-serif;}table{font-size:inherit;font:100%;}pre,code,kbd,samp,tt{font-family:monospace;*font-size:108%;line-height:100%;}
8
+
@@ -1,24 +1,13 @@
1
- /*
2
- Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3
- Code licensed under the BSD License:
4
- http://developer.yahoo.net/yui/license.txt
5
- version: 2.7.0
6
- */
7
- html{color:#000;background:#FFF;}body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,code,form,fieldset,legend,input,button,textarea,p,blockquote,th,td{margin:0;padding:0;}table{border-collapse:collapse;border-spacing:0;}fieldset,img{border:0;}address,caption,cite,code,dfn,em,strong,th,var,optgroup{font-style:inherit;font-weight:inherit;}del,ins{text-decoration:none;}li{list-style:none;}caption,th{text-align:left;}h1,h2,h3,h4,h5,h6{font-size:100%;font-weight:normal;}q:before,q:after{content:'';}abbr,acronym{border:0;font-variant:normal;}sup{vertical-align:baseline;}sub{vertical-align:baseline;}legend{color:#000;}input,button,textarea,select,optgroup,option{font-family:inherit;font-size:inherit;font-style:inherit;font-weight:inherit;}input,button,textarea,select{*font-size:100%;}body{font:13px/1.231 arial,helvetica,clean,sans-serif;*font-size:small;*font:x-small;}select,input,button,textarea,button{font:99% arial,helvetica,clean,sans-serif;}table{font-size:inherit;font:100%;}pre,code,kbd,samp,tt{font-family:monospace;*font-size:108%;line-height:100%;}
8
-
9
1
  /*
10
2
  Copyright (c) 2009, Kelly Redding. All rights reserved.
11
3
  See http://github.com/kelredd/sinatra-helpers for license details
12
4
  */
13
- UL {
14
- margin: 0;
15
- padding: 0;
16
- }
17
- UL LI {
18
- list-style-image: none;
19
- list-style-position: outside;
20
- list-style-type: none;
21
- }
5
+ @import "reset-fonts.less";
6
+
7
+ @base_font_family: 'Lucida Grande',arial,helvetica,clean,sans-serif;
8
+ @base_font_size: 10pt;
9
+ @base_font_size_adjust: 1em;
10
+
22
11
  UL.bulleted LI {
23
12
  list-style-position: inside !important;
24
13
  list-style-type: disc !important;
@@ -29,12 +18,12 @@ OL LI {
29
18
  }
30
19
 
31
20
  HTML {
32
- font-size:10pt;
21
+ font-size: @base_font_size;
33
22
  }
34
23
  BODY {
35
24
  width: 100%;
36
- font-family: "Lucida Grande", Verdana, sans-serif;
37
- font-size: 1em;
25
+ font-family: @base_font_family;
26
+ font-size: @base_font_size_adjust;
38
27
  font-size-adjust: none;
39
28
  font-variant: normal;
40
29
  }
@@ -46,8 +35,8 @@ A, IMG, FORM, FIELDSET {
46
35
  INPUT, SELECT, TEXTAREA, BUTTON {
47
36
  margin: 0;
48
37
  padding: .2em .3em;
49
- font-family : 'Lucida Grande', sans-serif;
50
- font-size: 9px;
38
+ font-family : @base_font_family;
39
+ font-size: @base_font_size_adjust;
51
40
  font-size-adjust: none;
52
41
  font-variant: normal;
53
42
  }
@@ -27,6 +27,7 @@ module SinatraHelpers::Generator
27
27
  },
28
28
  :stylesheets => {
29
29
  'reset.less.erb' => 'reset.less',
30
+ 'reset-fonts.less.erb' => 'reset-fonts.less',
30
31
  'app.less.erb' => 'app.less'
31
32
  }
32
33
  },
@@ -3,7 +3,7 @@ module SinatraHelpers
3
3
 
4
4
  MAJOR = 0
5
5
  MINOR = 2
6
- TINY = 2
6
+ TINY = 3
7
7
 
8
8
  def self.to_s # :nodoc:
9
9
  [MAJOR, MINOR, TINY].join('.')
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 2
9
- version: 0.2.2
8
+ - 3
9
+ version: 0.2.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kelly Redding
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-10 00:00:00 -06:00
17
+ date: 2010-04-05 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -131,6 +131,7 @@ files:
131
131
  - lib/sinatra_helpers/generator/file_templates/model_test.rb.erb
132
132
  - lib/sinatra_helpers/generator/file_templates/production.ru.erb
133
133
  - lib/sinatra_helpers/generator/file_templates/Rakefile.erb
134
+ - lib/sinatra_helpers/generator/file_templates/reset-fonts.less.erb
134
135
  - lib/sinatra_helpers/generator/file_templates/reset.less.erb
135
136
  - lib/sinatra_helpers/generator/file_templates/staging.ru.erb
136
137
  - lib/sinatra_helpers/generator/file_templates/test_helper.rb.erb