block_helpers 0.2.11

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Mark Evans
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.
data/README.markdown ADDED
@@ -0,0 +1,190 @@
1
+ Block Helpers
2
+ =============
3
+
4
+ When we write ERB views in Rails, etc., we generally DRY up the markup using helpers or partials.
5
+
6
+ However, it's quite common to overdo the 'DRYing up'.
7
+
8
+ When you find yourself passing in optional arguments to a helper/partial such as `:extra_text => 'eggs', :to_s_method => 'cheese'`, you know that there must be a better way.
9
+
10
+ Rails already has a great solution for forms with form-builders, using helpers which yield an object which can be used for further rendering.
11
+
12
+ This small gem generates helpers similar to the form-builders, but for the general case.
13
+
14
+ Example usage
15
+ =============
16
+ Please note that these examples are very contrived just for brevity! These block helpers are much more useful than just printing 'Hi there Marmaduke!'
17
+
18
+ Simple case
19
+ -----------
20
+
21
+ In the helper file:
22
+
23
+ module MyHelper
24
+
25
+ class MyBlockHelper < BlockHelpers::Base
26
+
27
+ def hello(name)
28
+ "<p>Hi there #{name}!</p>"
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+
35
+ This has generated a helper called `my_block_helper`.
36
+ So in the view:
37
+
38
+ <% my_block_helper do |h| %>
39
+ Here goes...
40
+ <%= h.hello('Marmaduke') %>
41
+ ...hooray!
42
+ <% end %>
43
+
44
+ This will generate the following:
45
+
46
+ Here goes...
47
+ <p>Hi there Marmaduke!</p>
48
+ ...hooray!
49
+
50
+
51
+ Accessing other helper methods
52
+ ------------------------------
53
+
54
+ Methods available in the parent helper are available to the block helper class.
55
+ In case of name clashes, you can also access those methods via the protected object `helper`.
56
+ In the helper:
57
+
58
+ module MyHelper
59
+
60
+ def angry
61
+ "I'm very angry"
62
+ end
63
+
64
+ class MyBlockHelper < BlockHelpers::Base
65
+
66
+ def angry
67
+ content_tag :div, helper.angry
68
+ end
69
+
70
+ end
71
+
72
+ end
73
+
74
+ In the view:
75
+
76
+ <% my_block_helper do |h| %>
77
+ <%= h.angry %>
78
+ <% end %>
79
+
80
+ This generates:
81
+
82
+ <div>I'm very angry</div>
83
+
84
+ Using arguments
85
+ ---------------
86
+
87
+ You can pass in arguments to the helper, and these will be passed through to the class's `initialize` method.
88
+ In the helper:
89
+
90
+ module MyHelper
91
+
92
+ class MyBlockHelper < BlockHelpers::Base
93
+
94
+ def initialize(tag_type)
95
+ @tag_type = tag_type
96
+ end
97
+
98
+ def hello(name)
99
+ content_tag @tag_type, "Hi there #{name}!"
100
+ end
101
+
102
+ end
103
+
104
+ end
105
+
106
+ In the view:
107
+
108
+ <% my_block_helper(:span) do |h| %>
109
+ <%= h.hello('Marmaduke') %>
110
+ <% end %>
111
+
112
+ This generates:
113
+
114
+ <span>Hi there Marmaduke!</span>
115
+
116
+ Surrounding markup
117
+ ------------------
118
+
119
+ Use the `display` method to surround the block with markup, e.g.
120
+ In the helper:
121
+
122
+ module MyHelper
123
+
124
+ class RoundedBox < BlockHelpers::Base
125
+
126
+ def display(body)
127
+ %(
128
+ <div class="tl">
129
+ <div class="tr">
130
+ <div class="bl">
131
+ <div class="br">
132
+ #{body}
133
+ </div>
134
+ </div>
135
+ </div>
136
+ </div>
137
+ )
138
+ end
139
+
140
+ end
141
+
142
+ end
143
+
144
+ In the view:
145
+
146
+ <% rounded_box do %>
147
+ Oi oi!!!
148
+ <% end %>
149
+
150
+ This generates:
151
+
152
+ <div class="tl">
153
+ <div class="tr">
154
+ <div class="bl">
155
+ <div class="br">
156
+ Oi oi!!!
157
+ </div>
158
+ </div>
159
+ </div>
160
+ </div>
161
+
162
+ Of course, you could use `display` for more than just surrounding markup.
163
+
164
+ Installation
165
+ ============
166
+
167
+ To use in Rails, add to your `environment.rb`:
168
+
169
+ config.gem "markevans-block_helpers", :lib => "block_helpers", :source => "http://gems.github.com"
170
+
171
+ for Merb, in `init.rb`:
172
+
173
+ dependency "markevans-block_helpers", :require_as => "block_helpers"
174
+
175
+ Compatibility
176
+ =============
177
+ Currently it depends on activesupport, and requires that the helper already has the methods `concat` and `capture` available (which is the case in Rails and Merb).
178
+
179
+ It works with both the one and two argument versions of `concat`, so should work with all recent versions of Rails and Merb.
180
+
181
+ Credits
182
+ =======
183
+ - <a href="http://github.com/markevans">Mark Evans</a> (author)
184
+ - <a href="http://github.com/nesquena">Nathan Esquenazi</a> and <a href="http://github.com/2collegebums">2collegebums</a> (contributor)
185
+
186
+
187
+ Copyright
188
+ ========
189
+
190
+ Copyright (c) 2009 Mark Evans. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "block_helpers"
8
+ gem.summary = %Q{An extension to ActionView for adding block helpers to views}
9
+ gem.email = "mark@new-bamboo.co.uk"
10
+ gem.homepage = "http://github.com/markevans/block_helpers"
11
+ gem.authors = ["Mark Evans"]
12
+ gem.add_dependency('activesupport', '>= 2.0')
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+
32
+
33
+ task :default => :spec
34
+
35
+ require 'rake/rdoctask'
36
+ Rake::RDocTask.new do |rdoc|
37
+ if File.exist?('VERSION.yml')
38
+ config = YAML.load(File.read('VERSION.yml'))
39
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
40
+ else
41
+ version = ""
42
+ end
43
+
44
+ rdoc.rdoc_dir = 'rdoc'
45
+ rdoc.title = "block_helpers #{version}"
46
+ rdoc.rdoc_files.include('README*')
47
+ rdoc.rdoc_files.include('lib/**/*.rb')
48
+ end
49
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.2.11
@@ -0,0 +1,52 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{block_helpers}
5
+ s.version = "0.2.11"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Mark Evans"]
9
+ s.date = %q{2009-08-28}
10
+ s.email = %q{mark@new-bamboo.co.uk}
11
+ s.extra_rdoc_files = [
12
+ "LICENSE",
13
+ "README.markdown"
14
+ ]
15
+ s.files = [
16
+ ".document",
17
+ ".gitignore",
18
+ "LICENSE",
19
+ "README.markdown",
20
+ "Rakefile",
21
+ "VERSION",
22
+ "block_helpers.gemspec",
23
+ "lib/block_helpers.rb",
24
+ "spec/for_spec_rails/application.rb",
25
+ "spec/helpers/block_helpers_spec.rb",
26
+ "spec/spec_helper.rb"
27
+ ]
28
+ s.has_rdoc = true
29
+ s.homepage = %q{http://github.com/markevans/block_helpers}
30
+ s.rdoc_options = ["--charset=UTF-8"]
31
+ s.require_paths = ["lib"]
32
+ s.rubygems_version = %q{1.3.1}
33
+ s.summary = %q{An extension to ActionView for adding block helpers to views}
34
+ s.test_files = [
35
+ "spec/for_spec_rails/application.rb",
36
+ "spec/helpers/block_helpers_spec.rb",
37
+ "spec/spec_helper.rb"
38
+ ]
39
+
40
+ if s.respond_to? :specification_version then
41
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
42
+ s.specification_version = 2
43
+
44
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
45
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.0"])
46
+ else
47
+ s.add_dependency(%q<activesupport>, [">= 2.0"])
48
+ end
49
+ else
50
+ s.add_dependency(%q<activesupport>, [">= 2.0"])
51
+ end
52
+ end
@@ -0,0 +1,55 @@
1
+ require 'activesupport'
2
+
3
+ module BlockHelpers
4
+
5
+ class Base
6
+
7
+ def self.inherited(klass)
8
+ # Define the helper method
9
+ # e.g. for a class:
10
+ # class HelloHelper < BlockHelpers::Base
11
+ # #.....
12
+ # end
13
+ #
14
+ # then we define a helper method 'hello_helper'
15
+ #
16
+ method_name = klass.name.split('::').last.underscore
17
+ klass.parent.class_eval %(
18
+ def #{method_name}(*args, &block)
19
+ renderer = #{klass.name}.new(*args)
20
+ renderer.send(:helper=, self)
21
+ if renderer.public_methods(false).include? 'display'
22
+ body = block ? capture(renderer, &block) : nil
23
+ if method(:concat).arity == 2
24
+ concat renderer.display(body), binding
25
+ else
26
+ concat renderer.display(body)
27
+ end
28
+ else
29
+ block.call(renderer) if block
30
+ end
31
+ renderer
32
+ end
33
+ )
34
+ end
35
+
36
+ def respond_to?(method)
37
+ super or helper.respond_to?(method)
38
+ end
39
+
40
+ protected
41
+
42
+ attr_accessor :helper
43
+
44
+ def method_missing(method, *args, &block)
45
+ if helper.respond_to?(method)
46
+ self.class_eval "def #{method}(*args, &block); helper.send('#{method}', *args, &block); end"
47
+ self.send(method, *args, &block)
48
+ else
49
+ super
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,10 @@
1
+ # Hacks to get spec/rails to work
2
+
3
+ class ApplicationController < ActionController::Base
4
+ end
5
+
6
+ module Rails
7
+ module VERSION
8
+ STRING = '2.3.0'
9
+ end
10
+ end
@@ -0,0 +1,228 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ module TestHelperModule
4
+ end
5
+
6
+ describe TestHelperModule do
7
+
8
+ describe "simple block_helper" do
9
+
10
+ before(:each) do
11
+ module TestHelperModule
12
+ remove_const(:TestHelper) if defined?(TestHelper)
13
+ class TestHelper < BlockHelpers::Base
14
+ def hello
15
+ 'Hi there'
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ it "should make the named helper available" do
22
+ helper.should respond_to(:test_helper)
23
+ end
24
+
25
+ it "should work for a simple yielded object" do
26
+ eval_erb(%(
27
+ <% test_helper do |h| %>
28
+ <p>Before</p>
29
+ <%= h.hello %>
30
+ <p>After</p>
31
+ <% end %>
32
+ )).should match_html("<p>Before</p> Hi there <p>After</p>")
33
+ end
34
+
35
+ it "should do nothing if no block given" do
36
+ eval_erb(%(
37
+ <% test_helper %>
38
+ )).should match_html("")
39
+ end
40
+
41
+ it "should return itself (the renderer object)" do
42
+ eval_erb(%(
43
+ <% e = test_helper %>
44
+ <%= e.hello %>
45
+ )).should match_html('Hi there')
46
+ end
47
+
48
+ end
49
+
50
+ describe "access to other methods" do
51
+ before(:each) do
52
+ module TestHelperModule
53
+
54
+ def yoghurt
55
+ 'Yoghurt'
56
+ end
57
+
58
+ remove_const(:TestHelper) if defined?(TestHelper)
59
+ class TestHelper < BlockHelpers::Base
60
+ def yog
61
+ yoghurt[0..2]
62
+ end
63
+ def jelly_in_div
64
+ content_tag :div, 'jelly'
65
+ end
66
+ def cheese
67
+ helper.cheese[0..3]
68
+ end
69
+ def label_tag(text)
70
+ helper.label_tag(text[0..1])
71
+ end
72
+ def check_capture(&block)
73
+ string = capture(&block)
74
+ 2.times{ concat(string) }
75
+ end
76
+ end
77
+
78
+ def cheese
79
+ 'Cheese'
80
+ end
81
+
82
+ end
83
+ end
84
+ it "should give the yielded renderer access to other methods" do
85
+ eval_erb(%(
86
+ <% test_helper do |r| %>
87
+ <%= r.yog %>
88
+ <% end %>
89
+ )).should match_html("Yog")
90
+ end
91
+ it "should give the yielded renderer access to normal actionview helper methods" do
92
+ eval_erb(%(
93
+ <% test_helper do |r| %>
94
+ <%= r.jelly_in_div %>
95
+ <% end %>
96
+ )).should match_html("<div>jelly</div>")
97
+ end
98
+ it "should give the yielded renderer access to other methods via 'helper'" do
99
+ eval_erb(%(
100
+ <% test_helper do |r| %>
101
+ <%= r.cheese %>
102
+ <% end %>
103
+ )).should match_html("Chee")
104
+ end
105
+ it "should give the yielded renderer access to normal actionview helper methods via 'helper'" do
106
+ eval_erb(%(
107
+ <% test_helper do |r| %>
108
+ <%= r.label_tag 'hide' %>
109
+ <% end %>
110
+ )).should match_html('<label for="hi">Hi</label>')
111
+ end
112
+ it "should work with methods like 'capture'" do
113
+ eval_erb(%(
114
+ <% test_helper do |r| %>
115
+ <% r.check_capture do %>
116
+ HELLO
117
+ <% end %>
118
+ <% end %>
119
+ )).should match_html('HELLO HELLO')
120
+ end
121
+ end
122
+
123
+ describe "accesibility" do
124
+ def run_erb
125
+ eval_erb(%(
126
+ <% compat_helper do |r| %>
127
+ HELLO
128
+ <% end %>
129
+ ))
130
+ end
131
+
132
+ it "should work when concat has one arg" do
133
+ module TestHelperModule
134
+ def concat(html); super(html); end
135
+ remove_const(:CompatHelper) if defined?(CompatHelper)
136
+ class CompatHelper < BlockHelpers::Base
137
+ def display(body)
138
+ "Before...#{body}...after"
139
+ end
140
+ end
141
+ end
142
+ run_erb.should match_html("Before... HELLO ...after")
143
+ end
144
+ it "should work when concat has two args" do
145
+ module TestHelperModule
146
+ def concat(html, binding); super(html); end
147
+ remove_const(:CompatHelper) if defined?(CompatHelper)
148
+ class CompatHelper < BlockHelpers::Base
149
+ def display(body)
150
+ "Before...#{body}...after"
151
+ end
152
+ end
153
+ end
154
+ run_erb.should match_html("Before... HELLO ...after")
155
+ end
156
+ it "should work when concat has one optional arg" do
157
+ module TestHelperModule
158
+ def concat(html, binding=nil); super(html); end
159
+ remove_const(:CompatHelper) if defined?(CompatHelper)
160
+ class CompatHelper < BlockHelpers::Base
161
+ def display(body)
162
+ "Before...#{body}...after"
163
+ end
164
+ end
165
+ end
166
+ run_erb.should match_html("Before... HELLO ...after")
167
+ end
168
+ end
169
+
170
+ describe "surrounding the block" do
171
+
172
+ before(:each) do
173
+ module TestHelperModule
174
+ remove_const(:TestHelperSurround) if defined?(TestHelperSurround)
175
+ class TestHelperSurround < BlockHelpers::Base
176
+ def display(body)
177
+ if body.nil?
178
+ "This is nil!"
179
+ else
180
+ %(
181
+ <p>Before</p>
182
+ #{body}
183
+ <p>After</p>
184
+ )
185
+ end
186
+ end
187
+ end
188
+ end
189
+ end
190
+
191
+ it "should surround a simple block" do
192
+ eval_erb(%(
193
+ <% test_helper_surround do %>
194
+ Body here!!!
195
+ <% end %>
196
+ )).should match_html("<p>Before</p> Body here!!! <p>After</p>")
197
+ end
198
+ it "should pass in the body as nil if no block given" do
199
+ eval_erb(%(
200
+ <% test_helper_surround %>
201
+ )).should match_html("This is nil!")
202
+ end
203
+ end
204
+
205
+ describe "block helpers with arguments" do
206
+ before(:each) do
207
+ module TestHelperModule
208
+ remove_const(:TestHelperWithArgs) if defined?(TestHelperWithArgs)
209
+ class TestHelperWithArgs < BlockHelpers::Base
210
+ def initialize(id, klass)
211
+ @id, @klass = id, klass
212
+ end
213
+ def hello
214
+ %(<p class="#{@klass}" id="#{@id}">Hello</p>)
215
+ end
216
+ end
217
+ end
218
+ end
219
+ it "should use the args passed in" do
220
+ eval_erb(%(
221
+ <% test_helper_with_args('hello', 'there') do |r| %>
222
+ <%= r.hello %>
223
+ <% end %>
224
+ )).should match_html(%(<p class="there" id="hello">Hello</p>))
225
+ end
226
+ end
227
+
228
+ end
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ gem 'activesupport'
3
+ gem 'actionpack'
4
+ require File.dirname(__FILE__)+'/../lib/block_helpers'
5
+
6
+ # Hacks to get spec/rails to work
7
+ require 'action_controller'
8
+ $:.unshift File.dirname(__FILE__)+'/for_spec_rails'
9
+ RAILS_ENV = 'test'
10
+
11
+ require 'spec'
12
+ require 'spec/rails'
13
+
14
+ Spec::Runner.configure do |config|
15
+ end
16
+
17
+ def match_html(html)
18
+ # Match two strings, but don't care about whitespace
19
+ simple_matcher("should match #{html}"){|given| given.strip.gsub(/\s+/,' ').gsub('> <','><') == html.strip.gsub(/\s+/,' ').gsub('> <','><') }
20
+ end
21
+
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: block_helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.11
5
+ platform: ruby
6
+ authors:
7
+ - Mark Evans
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-08-28 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "2.0"
24
+ version:
25
+ description:
26
+ email: mark@new-bamboo.co.uk
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.markdown
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.markdown
39
+ - Rakefile
40
+ - VERSION
41
+ - block_helpers.gemspec
42
+ - lib/block_helpers.rb
43
+ - spec/for_spec_rails/application.rb
44
+ - spec/helpers/block_helpers_spec.rb
45
+ - spec/spec_helper.rb
46
+ has_rdoc: true
47
+ homepage: http://github.com/markevans/block_helpers
48
+ licenses: []
49
+
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --charset=UTF-8
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: "0"
66
+ version:
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 1.3.5
71
+ signing_key:
72
+ specification_version: 2
73
+ summary: An extension to ActionView for adding block helpers to views
74
+ test_files:
75
+ - spec/for_spec_rails/application.rb
76
+ - spec/helpers/block_helpers_spec.rb
77
+ - spec/spec_helper.rb