Skip to content

Commit

Permalink
Removed support for Ruby 1.8.7
Browse files Browse the repository at this point in the history
  • Loading branch information
iGEL committed Jul 14, 2013
1 parent f8c6711 commit a288e96
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -3,6 +3,6 @@ rvm:
- 2.0.0
- 1.9.3
- 1.9.2
- 1.8.7
- rbx-19mode
- jruby-19mode
- ruby-head
6 changes: 3 additions & 3 deletions README.textile
@@ -1,4 +1,4 @@
"!https://codeclimate.com/github/iGEL/it.png!":https://codeclimate.com/github/iGEL/it "!https://travis-ci.org/iGEL/it.png!":https://travis-ci.org/iGEL/it (Specs pass on Ruby 1.8.7, 1.9.2, 1.9.3, 2.0.0, Rubinius and Jruby)
"!https://codeclimate.com/github/iGEL/it.png!":https://codeclimate.com/github/iGEL/it "!https://travis-ci.org/iGEL/it.png!":https://travis-ci.org/iGEL/it (Tests Ruby 1.9.2, 1.9.3, 2.0.0, HEAD, Rubinius and Jruby)

h1. What is *it*?

Expand Down Expand Up @@ -38,7 +38,7 @@ As you see above, unless the interpolation name is @link@ or starts with @_link@

<pre><code><%=it "copy", guide: It.link("http://guides.rubyonrails.org/i18n.html", target: '_blank', class: "important") %></code></pre>

You may pass any kind of object accepted by @link_to@ as the link target, so your loved named routes like @article_path(:id => article.id)@ will all work fine.
You may pass any kind of object accepted by @link_to@ as the link target, so your loved named routes like @article_path(id: article.id)@ will all work fine.

Want to introduce some markup into your sentences? *it* will help you:

Expand All @@ -52,7 +52,7 @@ Even nested interpolations are possible:
<pre><code>en:
copy: "Want to contact %{user}%? %{link:send %{b:%{user} a message}}!"</code></pre>

<pre><code><%=it "copy", link: "mailto:igel@igels.net", user: 'iGEL', :b => It.tag(:b) %></code></pre>
<pre><code><%=it "copy", link: "mailto:igel@igels.net", user: 'iGEL', b: It.tag(:b) %></code></pre>

If you would like to use the same translations in your html and plain text mails, you will like the @It.plain@ method:
<pre><code>en:
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -2,4 +2,4 @@ require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task default: :spec
4 changes: 2 additions & 2 deletions lib/it.rb
Expand Up @@ -14,13 +14,13 @@ module It
# It outside of your views. See documentation at Helper#it
def self.it(identifier, options = {})
options.stringify_keys!
Parser.new(I18n.t(identifier, :locale => (options["locale"] || I18n.locale)), options).process
Parser.new(I18n.t(identifier, locale: (options["locale"] || I18n.locale)), options).process
end

# Creates a new link to be used in +it+.
#
# * +href+: The url for the link. You may specify it as a String or as a named route like +article_path+. It's not possible to specify
# a Hash like <code>{:controller => "articles", :action => "index"}</code> directly. Use the +url_for+ helper, if you would like to specify your
# a Hash like <code>{controller: "articles", action: "index"}</code> directly. Use the +url_for+ helper, if you would like to specify your
# links like that.
# * +options+: The options as an Hash. Use them like you would with +link_to+. <em>(optional)</em>
def self.link(href, options = {})
Expand Down
8 changes: 4 additions & 4 deletions lib/it/helper.rb
Expand Up @@ -13,21 +13,21 @@ module Helper
#
# # translation: "Already signed up? %{login_link:Sign in}!"
#
# <%=it("translation", :login_link => It.link(login_path))
# <%=it("translation", login_link: It.link(login_path))
#
# If your link doesn't require additional attributes and the name is +link+, starts with +link_+ or ends with +_link+,
# you may specify the argument as a String or your helper.
#
# # translation: "Already signed up? %{login_link:Sign in}!"
#
# <%=it("translation", :login_link => login_path)
# <%=it("translation", login_link: login_path)
#
# You may have as many tags inside of one translation as you like, and you even may nest them into each other. Also you
# may specify arguments to links and other tags.
#
# # translation: "The top contributor of %{wiki_link:our wiki} is currently %{user_link:%{b:%{name}}}. Thanks a lot, %{name}!"
#
# <%= it("translation", :wiki_link => wiki_path, :name => user.name, :b => It.tag(:b, :class => "user"), :user_link => It.link(user_path(user), :target => "_blank"))
# <%= it("translation", wiki_link: wiki_path, name: user.name, b: It.tag(:b, class: "user"), user_link: It.link(user_path(user), target: "_blank"))
#
# I recommend to limit the use of +it+ as much as possible. You could use it for <code><div></code> or <code><br /></code>, but I think,
# things seperated over multiple lines should go into different translations. Use it for inline tags like links, <code><span></code>,
Expand All @@ -40,7 +40,7 @@ module Helper
#
def it(identifier, options = {})
options.stringify_keys!
It::Parser.new(t(identifier, :locale => (options["locale"] || I18n.locale)), options).process
It::Parser.new(t(identifier, locale: (options["locale"] || I18n.locale)), options).process
end
end
end
80 changes: 40 additions & 40 deletions spec/it/helper_spec.rb
Expand Up @@ -5,8 +5,8 @@

describe It::Helper, "#it" do
before do
I18n.backend.store_translations(:en, :test1 => "I have a %{link:link to Rails} in the middle.")
I18n.backend.store_translations(:de, :test1 => "Ich habe einen %{link:Link zu Rails} in der Mitte.")
I18n.backend.store_translations(:en, test1: "I have a %{link:link to Rails} in the middle.")
I18n.backend.store_translations(:de, test1: "Ich habe einen %{link:Link zu Rails} in der Mitte.")

@view = ActionView::Base.new
@controller = ActionController::Base.new
Expand All @@ -18,120 +18,120 @@
end

it "should insert the link into the string" do
expect(@view.it("test1", :link => It.link("http://www.rubyonrails.org"))).to eq('I have a <a href="http://www.rubyonrails.org">link to Rails</a> in the middle.')
expect(@view.it("test1", link: It.link("http://www.rubyonrails.org"))).to eq('I have a <a href="http://www.rubyonrails.org">link to Rails</a> in the middle.')
end

it "should insert the link into the German translation" do
I18n.locale = :de
expect(@view.it("test1", :link => It.link("http://www.rubyonrails.org"))).to eq('Ich habe einen <a href="http://www.rubyonrails.org">Link zu Rails</a> in der Mitte.')
expect(@view.it("test1", link: It.link("http://www.rubyonrails.org"))).to eq('Ich habe einen <a href="http://www.rubyonrails.org">Link zu Rails</a> in der Mitte.')
end

it "should allow link options to be set" do
expect(@view.it("test1", :link => It.link("http://www.rubyonrails.org", :target => "_blank"))).to eq('I have a <a href="http://www.rubyonrails.org" target="_blank">link to Rails</a> in the middle.')
expect(@view.it("test1", link: It.link("http://www.rubyonrails.org", target: "_blank"))).to eq('I have a <a href="http://www.rubyonrails.org" target="_blank">link to Rails</a> in the middle.')
end

it "should support the plain thing" do
expect(@view.it("test1", :link => It.plain("%s[http://www.rubyonrails.org]"))).to eq('I have a link to Rails[http://www.rubyonrails.org] in the middle.')
expect(@view.it("test1", link: It.plain("%s[http://www.rubyonrails.org]"))).to eq('I have a link to Rails[http://www.rubyonrails.org] in the middle.')
end

it "should parse other tags as well" do
expect(@view.it("test1", :link => It.tag(:b, :class => "classy"))).to eq('I have a <b class="classy">link to Rails</b> in the middle.')
expect(@view.it("test1", link: It.tag(:b, class: "classy"))).to eq('I have a <b class="classy">link to Rails</b> in the middle.')
end

it "should mark the result as html safe" do
expect(@view.it("test1", :link => It.link("http://www.rubyonrails.org")).html_safe?).to be_true
expect(@view.it("test1", link: It.link("http://www.rubyonrails.org")).html_safe?).to be_true
end

it "should escape all html in the translation" do
I18n.backend.store_translations(:en, :test2 => "<a href=\"hax0r\"> & %{link:link -> Rails} in <b>the middle</b>.")
expect(@view.it("test2", :link => It.link("http://www.rubyonrails.org"))).to eq('&lt;a href=&quot;hax0r&quot;&gt; &amp; <a href="http://www.rubyonrails.org">link -&gt; Rails</a> in &lt;b&gt;the middle&lt;/b&gt;.')
I18n.backend.store_translations(:en, test2: "<a href=\"hax0r\"> & %{link:link -> Rails} in <b>the middle</b>.")
expect(@view.it("test2", link: It.link("http://www.rubyonrails.org"))).to eq('&lt;a href=&quot;hax0r&quot;&gt; &amp; <a href="http://www.rubyonrails.org">link -&gt; Rails</a> in &lt;b&gt;the middle&lt;/b&gt;.')
end

it "should also work with 2 links" do
I18n.backend.store_translations(:en, :test3 => "I like %{link1:rails} and %{link2:github}.")
expect(@view.it("test3", :link1 => It.link("http://www.rubyonrails.org"), :link2 => It.link("http://www.github.com"))).to eq('I like <a href="http://www.rubyonrails.org">rails</a> and <a href="http://www.github.com">github</a>.')
I18n.backend.store_translations(:en, test3: "I like %{link1:rails} and %{link2:github}.")
expect(@view.it("test3", link1: It.link("http://www.rubyonrails.org"), link2: It.link("http://www.github.com"))).to eq('I like <a href="http://www.rubyonrails.org">rails</a> and <a href="http://www.github.com">github</a>.')
end

it "should allow normal I18n interpolations" do
I18n.backend.store_translations(:en, :test4 => "I have a %{link:link to Rails} in the %{position}.")
expect(@view.it("test4", :link => It.link("http://www.rubyonrails.org"), :position => "middle")).to eq('I have a <a href="http://www.rubyonrails.org">link to Rails</a> in the middle.')
I18n.backend.store_translations(:en, test4: "I have a %{link:link to Rails} in the %{position}.")
expect(@view.it("test4", link: It.link("http://www.rubyonrails.org"), position: "middle")).to eq('I have a <a href="http://www.rubyonrails.org">link to Rails</a> in the middle.')
end

it "should allow Integers as normal interpolation" do
I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
expect(@view.it("test5", :name => 2)).to eq('Hello 2.')
I18n.backend.store_translations(:en, test5: "Hello %{name}.")
expect(@view.it("test5", name: 2)).to eq('Hello 2.')
end

it "should escape the HTML in normal interpolations" do
I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
expect(@view.it("test5", :name => '<a href="http://evil.haxor.com">victim</a>')).to eq('Hello &lt;a href=&quot;http://evil.haxor.com&quot;&gt;victim&lt;/a&gt;.')
I18n.backend.store_translations(:en, test5: "Hello %{name}.")
expect(@view.it("test5", name: '<a href="http://evil.haxor.com">victim</a>')).to eq('Hello &lt;a href=&quot;http://evil.haxor.com&quot;&gt;victim&lt;/a&gt;.')
end

it "should not escape html_safe interpolations" do
I18n.backend.store_translations(:en, :test5 => "Hello %{name}.")
expect(@view.it("test5", :name => '<a href="http://www.rubyonrails.org">Rails</a>'.html_safe)).to eq('Hello <a href="http://www.rubyonrails.org">Rails</a>.')
I18n.backend.store_translations(:en, test5: "Hello %{name}.")
expect(@view.it("test5", name: '<a href="http://www.rubyonrails.org">Rails</a>'.html_safe)).to eq('Hello <a href="http://www.rubyonrails.org">Rails</a>.')
end

it "should allow interpolations inside of links" do
I18n.backend.store_translations(:en, :test6 => "Did you read our %{link:nice %{article}}?")
expect(@view.it("test6", :link => It.link("/article/2"), :article => "article")).to eq('Did you read our <a href="/article/2">nice article</a>?')
I18n.backend.store_translations(:en, test6: "Did you read our %{link:nice %{article}}?")
expect(@view.it("test6", link: It.link("/article/2"), article: "article")).to eq('Did you read our <a href="/article/2">nice article</a>?')
end

it "should raise a KeyError, if the key was not given" do
expect { @view.it("test1", :blubb => true) }.to raise_error(KeyError, "key{link} not found")
expect { @view.it("test1", blubb: true) }.to raise_error(KeyError, "key{link} not found")
end

it "should raise an ArgumentError, if a String was given for an interpolation with argument" do
I18n.backend.store_translations(:en, :test7 => "Sign up %{asdf:here}!")
expect { @view.it("test7", :asdf => "Heinz") }.to raise_error(ArgumentError, "key{asdf} has an argument, so it cannot resolved with a String")
I18n.backend.store_translations(:en, test7: "Sign up %{asdf:here}!")
expect { @view.it("test7", asdf: "Heinz") }.to raise_error(ArgumentError, "key{asdf} has an argument, so it cannot resolved with a String")
end

it "should allow Strings, if the interpolation name is link" do
I18n.backend.store_translations(:en, :test8 => "Sign up %{link:here}!")
expect(@view.it("test8", :link => "/register")).to eq('Sign up <a href="/register">here</a>!')
I18n.backend.store_translations(:en, test8: "Sign up %{link:here}!")
expect(@view.it("test8", link: "/register")).to eq('Sign up <a href="/register">here</a>!')
end

it "should allow Strings, if the interpolation name ends with _link" do
I18n.backend.store_translations(:en, :test8 => "Sign up %{register_link:here}!")
expect(@view.it("test8", :register_link => "/register")).to eq('Sign up <a href="/register">here</a>!')
I18n.backend.store_translations(:en, test8: "Sign up %{register_link:here}!")
expect(@view.it("test8", register_link: "/register")).to eq('Sign up <a href="/register">here</a>!')
end

it "should allow Strings, if the interpolation name starts with link_" do
I18n.backend.store_translations(:en, :test8 => "Sign up %{link_to_register:here}!")
expect(@view.it("test8", :link_to_register => "/register")).to eq('Sign up <a href="/register">here</a>!')
I18n.backend.store_translations(:en, test8: "Sign up %{link_to_register:here}!")
expect(@view.it("test8", link_to_register: "/register")).to eq('Sign up <a href="/register">here</a>!')
end

it "should work with tags without arguments" do
I18n.backend.store_translations(:en, :test9 => "We can %{br} do line breaks")
expect(@view.it("test9", :br => It.tag(:br))).to eq('We can <br /> do line breaks')
I18n.backend.store_translations(:en, test9: "We can %{br} do line breaks")
expect(@view.it("test9", br: It.tag(:br))).to eq('We can <br /> do line breaks')
end

it 'should support dot-prefixed keys' do
I18n.backend.store_translations(:en, :widgets => { :show => { :all_widgets => "See %{widgets_link:all widgets}" } })
I18n.backend.store_translations(:en, widgets: { show: { all_widgets: "See %{widgets_link:all widgets}" } })
@view.instance_variable_set(:"@_virtual_path", "widgets/show") # For Rails 3.0
@view.instance_variable_set(:"@virtual_path", "widgets/show") # For Rails 3.1, 3.2 and probably 4.0
expect(@view.it('.all_widgets', :widgets_link => '/widgets')).to eq('See <a href="/widgets">all widgets</a>')
expect(@view.it('.all_widgets', widgets_link: '/widgets')).to eq('See <a href="/widgets">all widgets</a>')
end

it 'should support the locale option' do
expect(@view.it('test1', :locale => "de", :link => It.link("http://www.rubyonrails.org"))).to eq('Ich habe einen <a href="http://www.rubyonrails.org">Link zu Rails</a> in der Mitte.')
expect(@view.it('test1', locale: "de", link: It.link("http://www.rubyonrails.org"))).to eq('Ich habe einen <a href="http://www.rubyonrails.org">Link zu Rails</a> in der Mitte.')
end

context "With a pluralized translation" do
before do
I18n.backend.store_translations(:en, :test10 => {:zero => "You have zero messages.", :one => "You have %{link:one message}.", :other => "You have %{link:%{count} messages}."})
I18n.backend.store_translations(:en, test10: {zero: "You have zero messages.", one: "You have %{link:one message}.", other: "You have %{link:%{count} messages}."})
end

it 'should work with count = 0' do
expect(@view.it("test10", :count => 0, :link => "/messages")).to eq('You have zero messages.')
expect(@view.it("test10", count: 0, link: "/messages")).to eq('You have zero messages.')
end

it 'should work with count = 1' do
expect(@view.it("test10", :count => 1, :link => "/messages")).to eq('You have <a href="/messages">one message</a>.')
expect(@view.it("test10", count: 1, link: "/messages")).to eq('You have <a href="/messages">one message</a>.')
end

it 'should work with count > 1' do
expect(@view.it("test10", :count => 2, :link => "/messages")).to eq('You have <a href="/messages">2 messages</a>.')
expect(@view.it("test10", count: 2, link: "/messages")).to eq('You have <a href="/messages">2 messages</a>.')
end
end
end
6 changes: 3 additions & 3 deletions spec/it/link_spec.rb
Expand Up @@ -9,15 +9,15 @@
end

it "should accept a Hash as first param" do
expect { It::Link.new({:controller => "articles", :action => "index"}) }.not_to raise_error
expect { It::Link.new({controller: "articles", action: "index"}) }.not_to raise_error
end

it "should raise a TypeError if the first param is an Integer" do
expect { It::Link.new(1) }.to raise_error(TypeError)
end

it "should accept options as a Hash" do
expect { It::Link.new("http://www.rubyonrails.org/", {:id => "identity", :class => "classy"}) }.not_to raise_error
expect { It::Link.new("http://www.rubyonrails.org/", {id: "identity", class: "classy"}) }.not_to raise_error
end

it "should raise a TypeError, if the options are a String" do
Expand All @@ -37,6 +37,6 @@

describe It::Link, '#process' do
it "should return a link with the options set and the content as label" do
expect(It::Link.new("http://www.rubyonrails.org", :target => "_blank").process("Rails")).to eq('<a href="http://www.rubyonrails.org" target="_blank">Rails</a>')
expect(It::Link.new("http://www.rubyonrails.org", target: "_blank").process("Rails")).to eq('<a href="http://www.rubyonrails.org" target="_blank">Rails</a>')
end
end
8 changes: 4 additions & 4 deletions spec/it/parser_spec.rb
Expand Up @@ -7,10 +7,10 @@
values = {'b' => It.tag(:b), 'link' => '/messages'}
parser = It::Parser.new('You have %{b:%{link:new messages}}!', values)

return1 = double('It::Interpolation', :process => '<a href="/messages">new messages</a>')
return1 = double('It::Interpolation', process: '<a href="/messages">new messages</a>')
It::Interpolation.should_receive(:new).with('%{link:new messages}', values).and_return(return1)

return2 = double('It::Interpolation', :process => '<b><a href="/messages">new messages</a></b>')
return2 = double('It::Interpolation', process: '<b><a href="/messages">new messages</a></b>')
It::Interpolation.should_receive(:new).with('%{b:<a href="/messages">new messages</a>}', values).and_return(return2)

expect(parser.process).to eq('You have <b><a href="/messages">new messages</a></b>!')
Expand All @@ -29,8 +29,8 @@
end

it 'delegates pluralization to I18n' do
I18n.backend.stub(:pluralize).with('en', {:other => 'You have %{count} messages'}, 2) { 'This is the pluralized string' }
parser = It::Parser.new({:other => 'You have %{count} messages'}, 'locale' => 'en', 'count' => 2)
I18n.backend.stub(:pluralize).with('en', {other: 'You have %{count} messages'}, 2) { 'This is the pluralized string' }
parser = It::Parser.new({other: 'You have %{count} messages'}, 'locale' => 'en', 'count' => 2)

expect(parser.process).to eq('This is the pluralized string')
end
Expand Down

0 comments on commit a288e96

Please sign in to comment.