sinatra_more 0.3.7 → 0.3.8

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -93,6 +93,16 @@ methods should be very familiar to anyone who has used rails view helpers.
93
93
 
94
94
  ==== Output Helpers
95
95
 
96
+ * <tt>content_for(key, &block)</tt>
97
+ * Capture a block of content to be rendered at a later time.
98
+ * content_for(:head) { ...content... }
99
+ * Also supports arguments passed to the content block
100
+ * content_for(:head) { |param1, param2| ...content... }
101
+ * <tt>yield_content(key, *args)</tt>
102
+ * Render the captured content blocks for a given key.
103
+ * yield_content :head
104
+ * Also supports arguments yielded to the content block
105
+ * yield_content :head, param1, param2
96
106
  * <tt>capture_html(*args, &block)</tt>
97
107
  * Captures the html from a block of template code for erb or haml
98
108
  * <tt>capture_html(&block)</tt> => "...html..."
@@ -603,6 +613,7 @@ See the wiki article for additional information: <http://wiki.github.com/nesquen
603
613
  * Thanks to kelredd (http://github.com/kelredd) for the <tt>sinatra_helpers</tt> code that helped me to create erb capture and concat methods.
604
614
  * Thanks to sbfaulkner for the <tt>sinatra-helpers</tt> code that I browsed through many times while starting this library.
605
615
  * Thanks to vestel for the excellent modified <tt>pony</tt> fork which in part powers the mailer_plugin (http://github.com/vestel/pony)
616
+ * Thanks to focat and sinatra-content-for library (http://github.com/focat/sinatra-content-for) for a good content_for starting point
606
617
  * Thanks to wycats and all for the awesome Thor gem which made creating the sinatra generator relatively painless
607
618
 
608
619
  == Contributors
data/TODO CHANGED
@@ -10,6 +10,7 @@
10
10
 
11
11
  = COMPLETED
12
12
 
13
+ * Add content_for / yield tags similar to rails
13
14
  * Created application generator using thor
14
15
  * Add support for a MailerPlugin which will make sending emails a breeze (http://github.com/hiroshi/pony)
15
16
  * Add support for missing formbuilder fields (select, and standard_form_builder methods i.e check_box_group)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.7
1
+ 0.3.8
@@ -4,7 +4,7 @@ module SinatraMore
4
4
  # capture_html(&block) => "...html..."
5
5
  def capture_html(*args, &block)
6
6
  if self.respond_to?(:is_haml?) && is_haml?
7
- block_is_haml?(block) ? capture_haml(*args, &block) : block.call
7
+ block_is_haml?(block) ? capture_haml(*args, &block) : block.call
8
8
  elsif has_erb_buffer?
9
9
  result_text = capture_erb(*args, &block)
10
10
  result_text.present? ? result_text : (block_given? && block.call(*args))
@@ -12,7 +12,7 @@ module SinatraMore
12
12
  block.call(*args)
13
13
  end
14
14
  end
15
-
15
+
16
16
  # Outputs the given text to the templates buffer directly
17
17
  # concat_content("This will be output to the template buffer in erb or haml")
18
18
  def concat_content(text="")
@@ -24,36 +24,61 @@ module SinatraMore
24
24
  text
25
25
  end
26
26
  end
27
-
27
+
28
28
  # Returns true if the block is from an ERB or HAML template; false otherwise.
29
29
  # Used to determine if html should be returned or concatted to view
30
30
  # block_is_template?(block)
31
31
  def block_is_template?(block)
32
- block && (block_is_erb?(block) || (self.respond_to?(:block_is_haml?) && block_is_haml?(block)))
32
+ block && (block_is_erb?(block) || (self.respond_to?(:block_is_haml?) && block_is_haml?(block)))
33
+ end
34
+
35
+ # Capture a block of content to be rendered at a later time.
36
+ # Your blocks can also receive values, which are passed to them by <tt>yield_content</tt>
37
+ # content_for(:name) { ...content... }
38
+ # content_for(:name) { |name| ...content... }
39
+ def content_for(key, &block)
40
+ content_blocks[key.to_sym] << block
33
41
  end
34
-
42
+
43
+ # Render the captured content blocks for a given key.
44
+ # You can also pass values to the content blocks by passing them
45
+ # as arguments after the key.
46
+ # yield_content :include
47
+ # yield_content :head, "param1", "param2"
48
+ def yield_content(key, *args)
49
+ content_blocks[key.to_sym].map { |content|
50
+ capture_html(*args, &content)
51
+ }.join
52
+ end
53
+
35
54
  protected
36
-
55
+
56
+ # Retrieves content_blocks stored by content_for or within yield_content
57
+ # content_blocks[:name] => ['...', '...']
58
+ def content_blocks
59
+ @content_blocks ||= Hash.new {|h,k| h[k] = [] }
60
+ end
61
+
37
62
  # Used to capture the html from a block of erb code
38
63
  # capture_erb(&block) => '...html...'
39
64
  def capture_erb(*args, &block)
40
65
  erb_with_output_buffer { block_given? && block.call(*args) }
41
66
  end
42
-
67
+
43
68
  # Concats directly to an erb template
44
69
  # erb_concat("Direct to buffer")
45
70
  def erb_concat(text)
46
71
  @_out_buf << text if has_erb_buffer?
47
72
  end
48
-
73
+
49
74
  # Returns true if an erb buffer is detected
50
75
  # has_erb_buffer? => true
51
76
  def has_erb_buffer?
52
77
  !@_out_buf.nil?
53
78
  end
54
-
79
+
55
80
  # Used to determine if a block is called from ERB.
56
- # NOTE: This doesn't actually work yet because the variable __in_erb_template
81
+ # NOTE: This doesn't actually work yet because the variable __in_erb_template
57
82
  # hasn't been defined in ERB. We need to find a way to fix this.
58
83
  def block_is_erb?(block)
59
84
  has_erb_buffer? || block && eval('defined? __in_erb_template', block)
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.3.7"
8
+ s.version = "0.3.8"
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"]
@@ -94,6 +94,8 @@ Gem::Specification.new do |s|
94
94
  "test/fixtures/markup_app/app.rb",
95
95
  "test/fixtures/markup_app/views/capture_concat.erb",
96
96
  "test/fixtures/markup_app/views/capture_concat.haml",
97
+ "test/fixtures/markup_app/views/content_for.erb",
98
+ "test/fixtures/markup_app/views/content_for.haml",
97
99
  "test/fixtures/markup_app/views/content_tag.erb",
98
100
  "test/fixtures/markup_app/views/content_tag.haml",
99
101
  "test/fixtures/markup_app/views/form_for.erb",
@@ -0,0 +1,11 @@
1
+ <% content_for :demo do %>
2
+ <h1>This is content yielded from a content_for</h1>
3
+ <% end %>
4
+
5
+ <div class='demo'><%= yield_content :demo %></p>
6
+
7
+ <% content_for :demo2 do |fname, lname| %>
8
+ <h1>This is content yielded with name <%= fname + " " + lname %></h1>
9
+ <% end %>
10
+
11
+ <div class='demo2'><%= yield_content :demo2, "Johnny", "Smith" %></div>
@@ -0,0 +1,9 @@
1
+ - content_for :demo do
2
+ %h1 This is content yielded from a content_for
3
+
4
+ .demo= yield_content :demo
5
+
6
+ - content_for :demo2 do |fname, lname|
7
+ %h1 This is content yielded with name #{fname + " " + lname}
8
+
9
+ .demo2= yield_content :demo2, "Johnny", "Smith"
@@ -146,7 +146,7 @@ class TestSkeletonGenerator < Test::Unit::TestCase
146
146
  buffer = silence_logger { SinatraMore::SkeletonGenerator.start(['sample_app', '/tmp', '--test=riot', '--script=none']) }
147
147
  assert_match /Applying.*?riot.*?test/, buffer
148
148
  assert_match_in_file(/require 'riot'/, '/tmp/sample_app/test/test_config.rb')
149
- assert_match_in_file(/Riot::Context/, '/tmp/sample_app/test/test_config.rb')
149
+ assert_match_in_file(/Riot::Situation/, '/tmp/sample_app/test/test_config.rb')
150
150
  end
151
151
 
152
152
  should "properly generate for rspec" do
@@ -6,6 +6,20 @@ class TestOutputHelpers < Test::Unit::TestCase
6
6
  MarkupDemo.tap { |app| app.set :environment, :test }
7
7
  end
8
8
 
9
+ context 'for #content_for method' do
10
+ should 'work for erb templates' do
11
+ visit '/erb/content_for'
12
+ assert_have_selector '.demo h1', :content => "This is content yielded from a content_for"
13
+ assert_have_selector '.demo2 h1', :content => "This is content yielded with name Johnny Smith"
14
+ end
15
+
16
+ should "work for haml templates" do
17
+ visit '/haml/content_for'
18
+ assert_have_selector '.demo h1', :content => "This is content yielded from a content_for"
19
+ assert_have_selector '.demo2 h1', :content => "This is content yielded with name Johnny Smith"
20
+ end
21
+ end
22
+
9
23
  context 'for #capture_html method' do
10
24
  should "work for erb templates" do
11
25
  visit '/erb/capture_concat'
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.3.7
4
+ version: 0.3.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Esquenazi
@@ -187,6 +187,8 @@ files:
187
187
  - test/fixtures/markup_app/app.rb
188
188
  - test/fixtures/markup_app/views/capture_concat.erb
189
189
  - test/fixtures/markup_app/views/capture_concat.haml
190
+ - test/fixtures/markup_app/views/content_for.erb
191
+ - test/fixtures/markup_app/views/content_for.haml
190
192
  - test/fixtures/markup_app/views/content_tag.erb
191
193
  - test/fixtures/markup_app/views/content_tag.haml
192
194
  - test/fixtures/markup_app/views/form_for.erb