ruhl 0.11.2 → 0.12.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/ruhl/engine.rb +23 -12
- data/lib/ruhl/rails.rb +1 -0
- data/lib/ruhl/rails/active_record_presenter.rb +57 -0
- data/lib/ruhl/rails/ruhl_presenter.rb +24 -0
- data/ruhl.gemspec +12 -7
- data/spec/html/collection_of_hashes.html +11 -0
- data/spec/ruhl_spec.rb +24 -2
- data/spec/spec_helper.rb +54 -0
- metadata +7 -4
data/Rakefile
CHANGED
@@ -23,7 +23,7 @@ begin
|
|
23
23
|
gemspec.email = "andy@stonean.com"
|
24
24
|
gemspec.homepage = "http://github.com/stonean/ruhl"
|
25
25
|
gemspec.authors = ["Andrew Stone"]
|
26
|
-
gemspec.add_dependency('nokogiri','
|
26
|
+
gemspec.add_dependency('nokogiri','=1.4.0')
|
27
27
|
gemspec.add_development_dependency('rspec')
|
28
28
|
end
|
29
29
|
Jeweler::GemcutterTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.12.0
|
data/lib/ruhl/engine.rb
CHANGED
@@ -15,6 +15,7 @@ module Ruhl
|
|
15
15
|
if @layout || @local_object || @block_object
|
16
16
|
@document = Nokogiri::HTML.fragment(html)
|
17
17
|
else
|
18
|
+
|
18
19
|
@document = Nokogiri::HTML(html)
|
19
20
|
end
|
20
21
|
end
|
@@ -60,14 +61,20 @@ module Ruhl
|
|
60
61
|
html = current_tag.to_html
|
61
62
|
|
62
63
|
new_content = call_result.collect do |item|
|
63
|
-
|
64
|
-
# and there are no other nested data-ruhls
|
64
|
+
|
65
65
|
if actions.length == 0 && current_tag.xpath('.//*[@data-ruhl]').length == 0
|
66
|
-
|
67
|
-
|
66
|
+
if item.is_a?(Hash)
|
67
|
+
t = current_tag.dup
|
68
|
+
apply_hash(t, item)
|
69
|
+
t.to_html
|
70
|
+
else
|
71
|
+
current_tag.inner_html = item.to_s
|
72
|
+
current_tag.to_html
|
73
|
+
end
|
68
74
|
else
|
69
75
|
Ruhl::Engine.new(html, :local_object => item).render(scope)
|
70
76
|
end
|
77
|
+
|
71
78
|
end.to_s
|
72
79
|
|
73
80
|
current_tag.swap(new_content)
|
@@ -170,18 +177,22 @@ module Ruhl
|
|
170
177
|
|
171
178
|
def process_results
|
172
179
|
if call_result.is_a?(Hash)
|
173
|
-
|
174
|
-
|
175
|
-
current_tag.inner_html = value.to_s
|
176
|
-
else
|
177
|
-
current_tag[key.to_s] = value.to_s
|
178
|
-
end
|
179
|
-
end
|
180
|
-
else
|
180
|
+
apply_hash(current_tag, call_result)
|
181
|
+
else
|
181
182
|
current_tag.inner_html = call_result.to_s
|
182
183
|
end
|
183
184
|
end
|
184
185
|
|
186
|
+
def apply_hash(tag, hash)
|
187
|
+
hash.each do |key, value|
|
188
|
+
if key == :inner_html
|
189
|
+
tag.inner_html = value.to_s
|
190
|
+
else
|
191
|
+
tag[key.to_s] = value.to_s
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
185
196
|
def execute_ruby(code)
|
186
197
|
if code == '_render_'
|
187
198
|
_render_
|
data/lib/ruhl/rails.rb
CHANGED
@@ -0,0 +1,57 @@
|
|
1
|
+
module ActiveRecordPresenter
|
2
|
+
def error_messages?
|
3
|
+
!presentee.errors.empty?
|
4
|
+
end
|
5
|
+
|
6
|
+
def error_messages
|
7
|
+
return if presentee.errors.empty?
|
8
|
+
presentee.errors.full_messages
|
9
|
+
end
|
10
|
+
|
11
|
+
# Pass presenter method call to model so you don't have to
|
12
|
+
# redefine every model method in the presenter class.
|
13
|
+
def method_missing(name, *args)
|
14
|
+
if presentee.respond_to?(name)
|
15
|
+
presentee.send(name, *args)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Extend scope of respond_to? to model.
|
20
|
+
def respond_to?(name)
|
21
|
+
if super
|
22
|
+
true
|
23
|
+
else
|
24
|
+
presentee.respond_to?(name)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def define_paths(model)
|
29
|
+
define_action(model, 'show') # show_path(@user)
|
30
|
+
define_action(model, 'update') # update_path(@user)
|
31
|
+
define_action(model, 'delete') # delete_path(@user)
|
32
|
+
define_action("edit_#{model}", 'edit') # edit_path(@user)
|
33
|
+
define_action(model.pluralize, 'index', false) # index_path
|
34
|
+
define_action(model.pluralize, 'create', false) # create_path
|
35
|
+
define_action("new_#{model}", 'new', false) # new_path
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def define_action(model, action, use_presentee = true)
|
41
|
+
if use_presentee
|
42
|
+
self.class.send(:define_method, "#{action}_path") do
|
43
|
+
context.send("#{model}_path", presentee)
|
44
|
+
end
|
45
|
+
self.class.send(:define_method, "#{action}_url") do
|
46
|
+
context.send("#{model}_url", presentee)
|
47
|
+
end
|
48
|
+
else
|
49
|
+
self.class.send(:define_method, "#{action}_path") do
|
50
|
+
context.send("#{model}_path")
|
51
|
+
end
|
52
|
+
self.class.send(:define_method, "#{action}_url") do
|
53
|
+
context.send("#{model}_url")
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'ruhl/rails/active_record_presenter'
|
2
|
+
|
3
|
+
class RuhlPresenter
|
4
|
+
include ActiveRecordPresenter
|
5
|
+
|
6
|
+
attr_reader :presentee, :context
|
7
|
+
|
8
|
+
def initialize(obj, context)
|
9
|
+
@presentee = obj
|
10
|
+
@context = context
|
11
|
+
define_paths(obj.class.name.underscore.downcase)
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
module ActionController
|
17
|
+
class Base
|
18
|
+
def presenter_for(obj)
|
19
|
+
eval("#{obj.class.name}Presenter").new(obj,self)
|
20
|
+
end
|
21
|
+
|
22
|
+
helper_method :presenter_for
|
23
|
+
end
|
24
|
+
end
|
data/ruhl.gemspec
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# Generated by jeweler
|
2
|
-
# DO NOT EDIT THIS FILE
|
3
|
-
# Instead, edit Jeweler::Tasks in Rakefile, and run
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
4
|
# -*- encoding: utf-8 -*-
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ruhl}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.12.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Andrew Stone"]
|
12
|
-
s.date = %q{2009-
|
12
|
+
s.date = %q{2009-11-14}
|
13
13
|
s.description = %q{Make your HTML dynamic with the addition of a data-ruhl attribute.}
|
14
14
|
s.email = %q{andy@stonean.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -24,9 +24,12 @@ Gem::Specification.new do |s|
|
|
24
24
|
"lib/ruhl/engine.rb",
|
25
25
|
"lib/ruhl/errors.rb",
|
26
26
|
"lib/ruhl/rails.rb",
|
27
|
+
"lib/ruhl/rails/active_record_presenter.rb",
|
28
|
+
"lib/ruhl/rails/ruhl_presenter.rb",
|
27
29
|
"lib/ruhl/sinatra.rb",
|
28
30
|
"ruhl.gemspec",
|
29
31
|
"spec/html/basic.html",
|
32
|
+
"spec/html/collection_of_hashes.html",
|
30
33
|
"spec/html/collection_of_strings.html",
|
31
34
|
"spec/html/form.html",
|
32
35
|
"spec/html/fragment.html",
|
@@ -41,6 +44,7 @@ Gem::Specification.new do |s|
|
|
41
44
|
"spec/html/parameters.html",
|
42
45
|
"spec/html/seo.html",
|
43
46
|
"spec/html/sidebar.html",
|
47
|
+
"spec/html/special.html",
|
44
48
|
"spec/html/use.html",
|
45
49
|
"spec/html/use_if.html",
|
46
50
|
"spec/rcov.opts",
|
@@ -63,14 +67,15 @@ Gem::Specification.new do |s|
|
|
63
67
|
s.specification_version = 3
|
64
68
|
|
65
69
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
66
|
-
s.add_runtime_dependency(%q<nokogiri>, ["
|
70
|
+
s.add_runtime_dependency(%q<nokogiri>, ["= 1.4.0"])
|
67
71
|
s.add_development_dependency(%q<rspec>, [">= 0"])
|
68
72
|
else
|
69
|
-
s.add_dependency(%q<nokogiri>, ["
|
73
|
+
s.add_dependency(%q<nokogiri>, ["= 1.4.0"])
|
70
74
|
s.add_dependency(%q<rspec>, [">= 0"])
|
71
75
|
end
|
72
76
|
else
|
73
|
-
s.add_dependency(%q<nokogiri>, ["
|
77
|
+
s.add_dependency(%q<nokogiri>, ["= 1.4.0"])
|
74
78
|
s.add_dependency(%q<rspec>, [">= 0"])
|
75
79
|
end
|
76
80
|
end
|
81
|
+
|
data/spec/ruhl_spec.rb
CHANGED
@@ -327,6 +327,23 @@ describe Ruhl do
|
|
327
327
|
end
|
328
328
|
end
|
329
329
|
|
330
|
+
|
331
|
+
|
332
|
+
describe "collection_of_hashes.html" do
|
333
|
+
describe "user_list is not empty" do
|
334
|
+
before do
|
335
|
+
@html = File.read html(:collection_of_hashes)
|
336
|
+
@doc = create_doc
|
337
|
+
end
|
338
|
+
|
339
|
+
|
340
|
+
it "should have option for each state" do
|
341
|
+
options = @doc.xpath('/html/body/select//option')
|
342
|
+
options.children.length.should == @co.state_options.length
|
343
|
+
end
|
344
|
+
end
|
345
|
+
end
|
346
|
+
|
330
347
|
describe "special.html" do
|
331
348
|
before do
|
332
349
|
@html = File.read html(:special)
|
@@ -334,9 +351,14 @@ describe Ruhl do
|
|
334
351
|
end
|
335
352
|
|
336
353
|
it "will convert entities" do
|
337
|
-
ps = @doc.xpath("/html/body
|
338
|
-
ps[0].inner_html.should
|
354
|
+
ps = @doc.xpath("/html/body/p")
|
355
|
+
ps[0].inner_html.should =~ /^Here is a space and another one.$/
|
339
356
|
ps[1].inner_html.should == "RuHL © 2009"
|
357
|
+
|
358
|
+
# To verify everything is correct, I did it the old fashioned way
|
359
|
+
# File.open('test.html',"w") do |f|
|
360
|
+
# f << @doc.to_s
|
361
|
+
# end
|
340
362
|
end
|
341
363
|
end
|
342
364
|
|
data/spec/spec_helper.rb
CHANGED
@@ -149,6 +149,60 @@ class ContextObject
|
|
149
149
|
state.new('WY', 'Wyoming')]
|
150
150
|
end
|
151
151
|
|
152
|
+
def state_options
|
153
|
+
[ {:value => 'AL', :inner_html => 'Alabama'},
|
154
|
+
{:value => 'AK', :inner_html => 'Alaska'},
|
155
|
+
{:value => 'AZ', :inner_html => 'Arizona'},
|
156
|
+
{:value => 'AR', :inner_html => 'Arkansas'},
|
157
|
+
{:value => 'CA', :inner_html => 'California'},
|
158
|
+
{:value => 'CO', :inner_html => 'Colorado'},
|
159
|
+
{:value => 'CT', :inner_html => 'Connecticut'},
|
160
|
+
{:value => 'DE', :inner_html => 'Delaware'},
|
161
|
+
{:value => 'FL', :inner_html => 'Florida'},
|
162
|
+
{:value => 'GA', :inner_html => 'Georgia'},
|
163
|
+
{:value => 'HI', :inner_html => 'Hawaii'},
|
164
|
+
{:value => 'ID', :inner_html => 'Idaho'},
|
165
|
+
{:value => 'IL', :inner_html => 'Illinois'},
|
166
|
+
{:value => 'IN', :inner_html => 'Indiana'},
|
167
|
+
{:value => 'IA', :inner_html => 'Iowa'},
|
168
|
+
{:value => 'KS', :inner_html => 'Kansas'},
|
169
|
+
{:value => 'KY', :inner_html => 'Kentucky'},
|
170
|
+
{:value => 'LA', :inner_html => 'Louisiana'},
|
171
|
+
{:value => 'ME', :inner_html => 'Maine'},
|
172
|
+
{:value => 'MD', :inner_html => 'Maryland'},
|
173
|
+
{:value => 'MA', :inner_html => 'Massachusetts'},
|
174
|
+
{:value => 'MI', :inner_html => 'Michigan'},
|
175
|
+
{:value => 'MN', :inner_html => 'Minnesota'},
|
176
|
+
{:value => 'MS', :inner_html => 'Mississippi'},
|
177
|
+
{:value => 'MO', :inner_html => 'Missouri'},
|
178
|
+
{:value => 'MT', :inner_html => 'Montana'},
|
179
|
+
{:value => 'NE', :inner_html => 'Nebraska'},
|
180
|
+
{:value => 'NV', :inner_html => 'Nevada'},
|
181
|
+
{:value => 'NH', :inner_html => 'New Hampshire'},
|
182
|
+
{:value => 'NJ', :inner_html => 'New Jersey'},
|
183
|
+
{:value => 'NM', :inner_html => 'New Mexico'},
|
184
|
+
{:value => 'NY', :inner_html => 'New York'},
|
185
|
+
{:value => 'NC', :inner_html => 'North Carolina'},
|
186
|
+
{:value => 'ND', :inner_html => 'North Dakota'},
|
187
|
+
{:value => 'OH', :inner_html => 'Ohio'},
|
188
|
+
{:value => 'OK', :inner_html => 'Oklahoma'},
|
189
|
+
{:value => 'OR', :inner_html => 'Oregon'},
|
190
|
+
{:value => 'PA', :inner_html => 'Pennsylvania'},
|
191
|
+
{:value => 'RI', :inner_html => 'Rhode Island'},
|
192
|
+
{:value => 'SC', :inner_html => 'South Carolina'},
|
193
|
+
{:value => 'SD', :inner_html => 'South Dakota'},
|
194
|
+
{:value => 'TN', :inner_html => 'Tennessee'},
|
195
|
+
{:value => 'TX', :inner_html => 'Texas'},
|
196
|
+
{:value => 'UT', :inner_html => 'Utah'},
|
197
|
+
{:value => 'VT', :inner_html => 'Vermont'},
|
198
|
+
{:value => 'VA', :inner_html => 'Virginia'},
|
199
|
+
{:value => 'WA', :inner_html => 'Washington'},
|
200
|
+
{:value => 'WV', :inner_html => 'West Virginia'},
|
201
|
+
{:value => 'WI', :inner_html => 'Wisconsin'},
|
202
|
+
{:value => 'WY', :inner_html => 'Wyoming'}]
|
203
|
+
end
|
204
|
+
|
205
|
+
|
152
206
|
end
|
153
207
|
|
154
208
|
def points_of_interest
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruhl
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Stone
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-11-
|
12
|
+
date: 2009-11-14 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -18,9 +18,9 @@ dependencies:
|
|
18
18
|
version_requirement:
|
19
19
|
version_requirements: !ruby/object:Gem::Requirement
|
20
20
|
requirements:
|
21
|
-
- - "
|
21
|
+
- - "="
|
22
22
|
- !ruby/object:Gem::Version
|
23
|
-
version: 1.
|
23
|
+
version: 1.4.0
|
24
24
|
version:
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rspec
|
@@ -49,9 +49,12 @@ files:
|
|
49
49
|
- lib/ruhl/engine.rb
|
50
50
|
- lib/ruhl/errors.rb
|
51
51
|
- lib/ruhl/rails.rb
|
52
|
+
- lib/ruhl/rails/active_record_presenter.rb
|
53
|
+
- lib/ruhl/rails/ruhl_presenter.rb
|
52
54
|
- lib/ruhl/sinatra.rb
|
53
55
|
- ruhl.gemspec
|
54
56
|
- spec/html/basic.html
|
57
|
+
- spec/html/collection_of_hashes.html
|
55
58
|
- spec/html/collection_of_strings.html
|
56
59
|
- spec/html/form.html
|
57
60
|
- spec/html/fragment.html
|