Skip to content
This repository has been archived by the owner on Nov 23, 2024. It is now read-only.

Commit

Permalink
use ERB for templating
Browse files Browse the repository at this point in the history
  • Loading branch information
steved committed Nov 6, 2012
1 parent a07b6de commit 47526d8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
6 changes: 3 additions & 3 deletions lib/kamcaptcha.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ def encrypt(string)

Kamcaptcha.template = <<-END
<div class="kamcaptcha">
<label for="kamcaptcha_input">%{label}</label><input type="text" id="kamcaptcha_input" name="kamcaptcha[input]" />
<input type="hidden" name="kamcaptcha[validation]" value="%{token}" />
<img src="%{image}" />
<label for="kamcaptcha_input"><%= label %></label><input type="text" id="kamcaptcha_input" name="kamcaptcha[input]" />
<input type="hidden" name="kamcaptcha[validation]" value="<%= token %>" />
<img src="<%= image %>" />
</div>
END
7 changes: 6 additions & 1 deletion lib/kamcaptcha/helper.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'erb'

module Kamcaptcha
module Helper
DEFAULT_LABEL = "Please type the characters in the image below"
Expand All @@ -16,9 +18,12 @@ def kamcaptcha(options = {})

instance_exec(token, &Kamcaptcha::Token.store) if Kamcaptcha::Token.store

form = template % { :label => label, :token => token, :image => image }
# Makes sure the ERB template only gets these variables: label, token, image
form = ERB.new(template).result(OpenStruct.new(:label => label, :token => token, :image => image).send(:binding))

form = form.html_safe if form.respond_to?(:html_safe)
form
end

end
end
4 changes: 2 additions & 2 deletions test/unit/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class String
before(:all) { @prefix, Kamcaptcha.prefix = Kamcaptcha.prefix, "omg/123" }
after(:all) { Kamcaptcha.prefix = @prefix }

subject { Kamcaptcha.stub(:random, "hello.png") { kamcaptcha(:template => "%{image}") } }
subject { Kamcaptcha.stub(:random, "hello.png") { kamcaptcha(:template => "<%= image %>") } }

it "should prefix image path" do
assert_equal "/omg/123/hello.png", subject
Expand All @@ -68,7 +68,7 @@ class String
before(:all) { @generator, Kamcaptcha::Token.generator = Kamcaptcha::Token.generator, lambda {|_| "token"}}
after(:all) { Kamcaptcha::Token.generator = @generator }

subject { Kamcaptcha.stub(:random, "hello.png") { kamcaptcha(:template => "%{token}") } }
subject { Kamcaptcha.stub(:random, "hello.png") { kamcaptcha(:template => "<%= token %>") } }

it "should use the custom generator" do
assert_equal "token", subject
Expand Down

0 comments on commit 47526d8

Please sign in to comment.