it 0.2.5 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c01da0126bc0960aa7ab5e43f96517aa82eb289d
4
+ data.tar.gz: 1b98dfc90fa506312640cdbb7f900b3534c3d780
5
+ SHA512:
6
+ metadata.gz: 6146d71238cef1aa408fd743d68e371bcf3fb91dde5e32e94249d236640229af060a8c83987df01f8dd14f76e7ae2a628744921689405499d3960a960e157925
7
+ data.tar.gz: 34b7df378be392ecd5fa29ee174be79be7031687c25cdbbebe4bab81e6063a05edaf194b8b9dc32e66ad0ab217733e5dc0353e7eeb97fc9b18e2a5bd8230f70c
@@ -0,0 +1,6 @@
1
+ # Since 0.2.5
2
+
3
+ * Make `It.plain` work with empty content [#14](https://github.com/iGEL/it/pull/14)
4
+ *Emil Sågfors*
5
+ * Pass though `:scope` & `:default` [#13](https://github.com/iGEL/it/pull/13)
6
+ *Emil Sågfors*
@@ -0,0 +1,124 @@
1
+ [![Code Climate](http://img.shields.io/codeclimate/github/iGEL/it.svg?style=flat)](https://codeclimate.com/github/iGEL/it)
2
+ [![Build Status](http://img.shields.io/travis/iGEL/it/master.svg?style=flat)](https://travis-ci.org/iGEL/it)
3
+ [![Coverage](http://img.shields.io/coveralls/iGEL/it/master.svg?style=flat)](https://coveralls.io/r/iGEL/it)
4
+ [![Rubygems](http://img.shields.io/gem/v/it.svg?style=flat)](http://rubygems.org/gems/it)
5
+ [![Github Issues](http://img.shields.io/github/issues/iGEL/it.svg?style=flat)](https://travis-ci.org/iGEL/it)
6
+ [![Dependency Status](http://img.shields.io/gemnasium/iGEL/it.svg?style=flat)](https://gemnasium.com/iGEL/it)
7
+
8
+ Tested against Ruby 2.2, 2.1, 2.0, head, rbx, and jruby and Rails 4.2, 4.1, and 3.2
9
+
10
+ What is **it**?
11
+ =============
12
+
13
+ I18n is baked right into Rails, and it's great. But if you want to place markup or links inside your translated copies, things get a little messy. You need to specify the label of your links separately from the rest of the copy. Writing HTML in your translations is even worse.
14
+
15
+ ```yaml
16
+ en:
17
+ copy: "If you are already registered, %{login_link}!"
18
+ copy_login_link: "please sign in"
19
+ ```
20
+
21
+ ```erb
22
+ <%=raw t("copy", login: link_to(t("copy_login_link"), login_path)) %>
23
+ ```
24
+
25
+ Wouldn't it be much nicer and easier to understand for your translator to have the whole copy in single label? **it** lets you do that:
26
+
27
+ ```yaml
28
+ en:
29
+ copy: "If you are already registered, %{login_link:please sign in}!"
30
+ ```
31
+
32
+ ```erb
33
+ <%=it "copy", login_link: login_path %>
34
+ ```
35
+
36
+ You may have noticed in the example above, that **it** doesn't require `raw` anymore. Of course, all HTML in the translation gets properly escaped, so you don't have to worry about XSS.
37
+
38
+ Installation
39
+ ------------
40
+
41
+ Just add the following line to your Gemfile & run `bundle install`:
42
+
43
+ ```ruby
44
+ gem 'it'
45
+ ```
46
+
47
+ Usage
48
+ -----
49
+
50
+ You may have as many links inside your translations as you like, and normal interpolations are possible as well:
51
+
52
+ ```yaml
53
+ en:
54
+ copy: "Read the %{guide:Rails I18n guide} for more than %{advises} advises. Fork it at {repo:github}."
55
+ ```
56
+
57
+ ```erb
58
+ <%=it "copy",
59
+ guide: It.link("http://guides.rubyonrails.org/i18n.html"),
60
+ advices: 100,
61
+ repo: It.link("https://github.com/lifo/docrails/tree/master/railties/guides") %>
62
+ ```
63
+
64
+ As you see above, unless the interpolation name is `link` or starts with `link_` or ends with `_link`, you need to call `It.link` to create a link. The advantage of `It.link`: You may specify options like you would with `link_to`:
65
+
66
+ ```erb
67
+ <%=it "copy",
68
+ link: It.link("http://rubyonrails.org", target: '_blank', class: "important") %>
69
+ ```
70
+
71
+ 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.
72
+
73
+ Want to introduce some markup into your sentences? **it** will help you:
74
+
75
+ ```yaml
76
+ en:
77
+ advantages: There are %{b:many advantages} for registered users!
78
+ ```
79
+
80
+ ```erb
81
+ <%=it "advantages", b: It.tag(:b, class: "red") %>
82
+ ```
83
+
84
+ Even nested interpolations are possible:
85
+
86
+ ```yaml
87
+ en:
88
+ copy: "Want to contact %{user}%? %{link:send %{b:%{user} a message}}!"
89
+ ```
90
+
91
+ ```erb
92
+ <%=it "copy", link: "mailto:igel@igels.net", user: 'iGEL', b: It.tag(:b) %>
93
+ ```
94
+
95
+ To use **it** outside of the view layer, just use `It.it`:
96
+
97
+ ```ruby
98
+ flash[:notice] = It.it('flash.invitation_accepted_already', link: root_path)
99
+ ```
100
+
101
+ If you would like to use the same translations in your html and plain text mails, you will like the `It.plain` method:
102
+ ```yaml
103
+ en:
104
+ mail_copy: "Do you like %{link:Rails}?"
105
+ ```
106
+
107
+ ```erb
108
+ https://github.com/lifo/docrails/tree/master/railties/guides
109
+ <%= it "mail_copy", link: It.link("http://www.rubyonrails.org/") %>
110
+
111
+ Plain mail:
112
+ <%= it "mail_copy", link: It.plain("%s[http://www.rubyonrails.org/]") %>
113
+ ```
114
+
115
+ The `%s` will be replaced with the label, in the example with Rails. You could provide any other string containing `%s`. The default is just `%s`, so it will return only the label itself.
116
+
117
+ Contribute
118
+ ----------
119
+
120
+ * Fork it
121
+ * Create a feature branch (`git checkout -b my-new-feature`)
122
+ * Create one or more failing specs. If you change code (feature or bug), I won't accept a pull request unless you changed specs.
123
+ * Fix the specs by implementing the feature/bug fix
124
+ * Push and open a pull request
data/Rakefile CHANGED
@@ -1,5 +1,10 @@
1
+ require 'bundler/gem_tasks'
1
2
  require 'rspec/core/rake_task'
3
+ require 'rubocop/rake_task'
4
+ require 'reek/rake/task'
2
5
 
3
6
  RSpec::Core::RakeTask.new(:spec)
7
+ RuboCop::RakeTask.new
8
+ Reek::Rake::Task.new
4
9
 
5
10
  task default: :spec
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'it/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'it'
7
+ spec.version = It::VERSION
8
+ spec.authors = ['Johannes Barre']
9
+ spec.email = ['igel@igels.net']
10
+
11
+ spec.summary = 'A helper for links and other html tags in your translations'
12
+ spec.homepage = 'https://github.com/igel/it'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = %w(MIT-LICENSE README.md Rakefile Gemfile CHANGELOG.md it.gemspec) + Dir['lib/**/*.rb']
16
+ spec.require_paths = ['lib']
17
+
18
+ spec.add_dependency 'actionpack', '>= 3.0.0'
19
+
20
+ spec.add_development_dependency 'bundler', '~> 1.7'
21
+ spec.add_development_dependency 'rake', '~> 10.0'
22
+ spec.add_development_dependency 'rspec', '~> 3.2'
23
+ spec.add_development_dependency 'coveralls'
24
+ spec.add_development_dependency 'nokogiri'
25
+ spec.add_development_dependency 'rubocop'
26
+ spec.add_development_dependency 'reek'
27
+ end
data/lib/it.rb CHANGED
@@ -4,6 +4,7 @@ require 'it/tag'
4
4
  require 'it/link'
5
5
  require 'it/plain'
6
6
  require 'it/helper'
7
+ require 'it/version'
7
8
 
8
9
  ActiveSupport.on_load(:action_view) do
9
10
  include It::Helper
@@ -13,15 +14,17 @@ end
13
14
  module It
14
15
  # It outside of your views. See documentation at Helper#it
15
16
  def self.it(identifier, options = {})
16
- options.stringify_keys!
17
- Parser.new(I18n.t(identifier, locale: (options["locale"] || I18n.locale), default: options['default']), options).process
17
+ Parser.new(
18
+ I18n.t(identifier, Parser.backend_options(options)),
19
+ options.stringify_keys
20
+ ).process
18
21
  end
19
22
 
20
23
  # Creates a new link to be used in +it+.
21
24
  #
22
- # * +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
23
- # a Hash like <code>{controller: "articles", action: "index"}</code> directly. Use the +url_for+ helper, if you would like to specify your
24
- # links like that.
25
+ # * +href+: The url for the link. You may specify it as a String or as a named route like +article_path+. It's not
26
+ # possible to specify a Hash like <code>{controller: "articles", action: "index"}</code> directly. Use the
27
+ # +url_for+ helper, if you would like to specify your links like that.
25
28
  # * +options+: The options as an Hash. Use them like you would with +link_to+. <em>(optional)</em>
26
29
  def self.link(href, options = {})
27
30
  It::Link.new(href, options)
@@ -29,9 +32,9 @@ module It
29
32
 
30
33
  # Creates a new plain replacement to be used in +it+.
31
34
  #
32
- # * +template+: A string to be used as the template. An example would be <code>"%s[http://www.rubyonrails.org]"</code>. Defaults to
33
- # <code>"%s"</code>. <em>(optional)</em>
34
- def self.plain(template = "%s")
35
+ # * +template+: A string to be used as the template. An example would be
36
+ # <code>"%s[http://www.rubyonrails.org]"</code>. Defaults to <code>"%s"</code>. <em>(optional)</em>
37
+ def self.plain(template = '%s')
35
38
  It::Plain.new(template)
36
39
  end
37
40
 
@@ -1,5 +1,3 @@
1
- # encoding: UTF-8
2
-
3
1
  module It
4
2
  # The helper will be available in the views.
5
3
  module Helper
@@ -15,32 +13,36 @@ module It
15
13
  #
16
14
  # <%=it("translation", login_link: It.link(login_path))
17
15
  #
18
- # If your link doesn't require additional attributes and the name is +link+, starts with +link_+ or ends with +_link+,
19
- # you may specify the argument as a String or your helper.
16
+ # If your link doesn't require additional attributes and the name is +link+, starts with +link_+ or ends with
17
+ # +_link+, you may specify the argument as a String or your helper.
20
18
  #
21
19
  # # translation: "Already signed up? %{login_link:Sign in}!"
22
20
  #
23
21
  # <%=it("translation", login_link: login_path)
24
22
  #
25
- # You may have as many tags inside of one translation as you like, and you even may nest them into each other. Also you
26
- # may specify arguments to links and other tags.
23
+ # You may have as many tags inside of one translation as you like, and you even may nest them into each other.
24
+ # Also you may specify arguments to links and other tags.
27
25
  #
28
- # # translation: "The top contributor of %{wiki_link:our wiki} is currently %{user_link:%{b:%{name}}}. Thanks a lot, %{name}!"
26
+ # # translation: "The top %{wiki_link:our wiki} contributor is %{user_link:%{b:%{name}}}. Thanks %{name}!"
29
27
  #
30
- # <%= it("translation", wiki_link: wiki_path, name: user.name, b: It.tag(:b, class: "user"), user_link: It.link(user_path(user), target: "_blank"))
28
+ # <%= it("translation", wiki_link: wiki_path, name: user.name, b: It.tag(:b, class: "user"),
29
+ # user_link: It.link(user_path(user), target: "_blank"))
31
30
  #
32
- # 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,
33
- # things seperated over multiple lines should go into different translations. Use it for inline tags like links, <code><span></code>,
34
- # <code><b></code>, <code><i></code>, <code><em></code> or <code><strong></code>.
31
+ # I recommend to limit the use of +it+ as much as possible. You could use it for <code><div></code> or
32
+ # <code><br /></code>, but I think, things seperated over multiple lines should go into different translations. Use
33
+ # it for inline tags like links, <code><span></code>, <code><b></code>, <code><i></code>, <code><em></code> or
34
+ # <code><strong></code>.
35
35
  #
36
- # It's currently not possible to specify your links as an Hash. Use the +url_for+ helper, if you want to specify your
37
- # link target as an Hash.
36
+ # It's currently not possible to specify your links as an Hash. Use the +url_for+ helper, if you want to specify
37
+ # your link target as an Hash.
38
38
  #
39
39
  # If you need to use it outside of your views, use +It.it+.
40
40
  #
41
41
  def it(identifier, options = {})
42
- options.stringify_keys!
43
- It::Parser.new(t(identifier, locale: (options["locale"] || I18n.locale)), options).process
42
+ It::Parser.new(
43
+ t(identifier, It::Parser.backend_options(options)),
44
+ options.stringify_keys
45
+ ).process
44
46
  end
45
47
  end
46
48
  end
@@ -1,39 +1,51 @@
1
1
  module It
2
+ # Contains one interpolation and will delegate the work to the It::Tag (or subclass) or
3
+ # ERB::Util.h
2
4
  class Interpolation
3
- attr_accessor :key, :label, :values
4
-
5
+ attr_accessor :key, :label, :value
6
+
5
7
  def initialize(string, values)
6
8
  self.key, self.label = string[2..-2].split(':', 2)
7
- self.values = values
9
+ validate_key_presence(values)
8
10
 
11
+ self.value = values[key]
9
12
  convert_links
10
13
  end
11
-
14
+
12
15
  def process
13
- check_input_values
16
+ validate_value_for_arguments
14
17
 
15
- if label # Normal tags
16
- values[key].process(label.html_safe)
17
- elsif values[key].is_a?(It::Tag) # Empty tag
18
- values[key].process
18
+ if value.is_a?(It::Tag) # Empty tag
19
+ process_it_tag
19
20
  else # Normal interpolations, as I18n.t would do it.
20
- ERB::Util.h(values[key])
21
+ ERB::Util.h(value)
21
22
  end
22
23
  end
23
24
 
24
25
  private
26
+
25
27
  # Convert keys with String arguments into It::Links, if they are named link, link_* or *_link
26
28
  def convert_links
27
- if key =~ /(\Alink\Z|_link\Z|\Alink_)/ && values[key].is_a?(String)
28
- self.values[key] = It::Link.new(values[key])
29
+ if key =~ /(\Alink\z|_link\z|\Alink_)/ && value.is_a?(String)
30
+ self.value = It::Link.new(value)
31
+ end
32
+ end
33
+
34
+ def process_it_tag
35
+ if label
36
+ value.process(label.html_safe)
37
+ else
38
+ value.process
29
39
  end
30
40
  end
31
41
 
32
- def check_input_values
33
- if !values.has_key?(key)
34
- raise KeyError, "key{#{key}} not found"
35
- elsif label && !values[key].is_a?(It::Tag)
36
- raise ArgumentError, "key{#{key}} has an argument, so it cannot resolved with a #{values[key].class}"
42
+ def validate_key_presence(values)
43
+ fail KeyError, "key{#{key}} not found" unless values.key?(key)
44
+ end
45
+
46
+ def validate_value_for_arguments
47
+ if label && !value.is_a?(It::Tag)
48
+ fail ArgumentError, "key{#{key}} has an argument, so it cannot resolved with a #{value.class}"
37
49
  end
38
50
  end
39
51
  end
@@ -2,17 +2,18 @@ module It
2
2
  # A class for links
3
3
  class Link < Tag
4
4
  include ActionView::Helpers::UrlHelper
5
-
5
+
6
6
  # See It.link for details. You can do everything there and save 6 characters.
7
7
  def initialize(href, options = {})
8
- raise TypeError, "Invalid href given" unless href.is_a?(Hash) || href.is_a?(String)
8
+ fail TypeError, 'Invalid href given' unless [Hash, String].include?(href.class)
9
+
9
10
  super(:a, options)
10
11
  @href = href
11
12
  end
12
-
13
+
13
14
  # Will be called from inside the helper to return the tag with the given content.
14
15
  def process(content)
15
16
  link_to(content, @href, @options)
16
17
  end
17
18
  end
18
- end
19
+ end
@@ -1,9 +1,14 @@
1
1
  module It
2
+ # Parses the string and replaces all interpolations accordingly.
2
3
  class Parser
3
4
  attr_accessor :string, :options
4
5
 
5
6
  INTERPOLATION_REGEXP = /%\{[^{}}]+\}/
6
7
 
8
+ def self.backend_options(options)
9
+ options.with_indifferent_access.slice(:default, :locale, :scope)
10
+ end
11
+
7
12
  def initialize(string, options)
8
13
  self.string = string
9
14
  self.options = options
@@ -14,8 +19,8 @@ module It
14
19
  escape_string
15
20
 
16
21
  # For deep nesting, we repeat the process until we have no interpolations anymore
17
- while has_interpolation?
18
- self.string.gsub!(INTERPOLATION_REGEXP) do |interpolation|
22
+ while contains_interpolation?
23
+ string.gsub!(INTERPOLATION_REGEXP) do |interpolation|
19
24
  Interpolation.new(interpolation, options).process
20
25
  end
21
26
  end
@@ -23,12 +28,19 @@ module It
23
28
  end
24
29
 
25
30
  private
26
- def has_interpolation?
31
+
32
+ def contains_interpolation?
27
33
  string =~ INTERPOLATION_REGEXP
28
34
  end
29
35
 
30
36
  def handle_pluralization
31
- self.string = I18n.backend.send(:pluralize, (options['locale'] || I18n.locale), string, options['count']) if string.is_a?(Hash) && options['count']
37
+ return if !string.is_a?(Hash) || !options.key?('count')
38
+
39
+ self.string = I18n.backend.send(:pluralize, locale, string, options['count'])
40
+ end
41
+
42
+ def locale
43
+ options['locale'] || I18n.locale
32
44
  end
33
45
 
34
46
  def escape_string
@@ -1,12 +1,14 @@
1
1
  module It
2
+ # Handles replacements in non HTML templates (e.g. mails)
2
3
  class Plain < Tag
3
- def initialize(template = "%s")
4
- raise TypeError, "expected a String, got #{template.class}" unless template.is_a?(String)
4
+ def initialize(template = '%s')
5
+ fail TypeError, "expected a String, got #{template.class}" unless template.is_a?(String)
6
+
5
7
  @template = template
6
8
  end
7
-
8
- def process(content)
9
- sprintf(@template, content)
9
+
10
+ def process(content = '')
11
+ format(@template, content)
10
12
  end
11
13
  end
12
- end
14
+ end
@@ -2,24 +2,25 @@ module It
2
2
  # A generic class for html tags.
3
3
  class Tag
4
4
  include ActionView::Helpers::TagHelper
5
-
5
+
6
6
  attr_reader :tag_name, :options
7
-
7
+
8
8
  # See It.tag for details. You can do everything there and save 6 characters.
9
9
  def initialize(tag_name, options = {})
10
- raise TypeError, "tag_name must be specified as a String or Symbol" unless tag_name.is_a?(String) || tag_name.is_a?(Symbol)
11
- raise TypeError, "options must be specified as a Hash" unless options.is_a?(Hash)
10
+ fail TypeError, 'tag_name must be a String or Symbol' unless [String, Symbol].include?(tag_name.class)
11
+ fail TypeError, 'options must be a Hash' unless options.is_a?(Hash)
12
+
12
13
  @tag_name = tag_name.to_sym
13
14
  @options = options.symbolize_keys
14
15
  end
15
-
16
+
16
17
  # Will be called from inside the helper to return the tag with the given content.
17
18
  def process(content = nil) # :nodoc:
18
- if content.nil?
19
- tag(@tag_name, @options)
20
- else
19
+ if content
21
20
  content_tag(@tag_name, content, @options)
21
+ else
22
+ tag(@tag_name, @options)
22
23
  end
23
24
  end
24
25
  end
25
- end
26
+ end
@@ -0,0 +1,3 @@
1
+ module It
2
+ VERSION = '0.8.0'
3
+ end
metadata CHANGED
@@ -1,121 +1,170 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: it
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
5
- prerelease:
4
+ version: 0.8.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Johannes Barre
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-07-15 00:00:00.000000000 Z
11
+ date: 2015-06-12 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: actionpack
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: 3.0.0
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: 3.0.0
30
27
  - !ruby/object:Gem::Dependency
31
- name: rspec
28
+ name: bundler
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ~>
31
+ - - "~>"
36
32
  - !ruby/object:Gem::Version
37
- version: '2.11'
33
+ version: '1.7'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ~>
38
+ - - "~>"
44
39
  - !ruby/object:Gem::Version
45
- version: '2.11'
40
+ version: '1.7'
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: rake
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - "~>"
52
46
  - !ruby/object:Gem::Version
53
47
  version: '10.0'
54
48
  type: :development
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - "~>"
60
53
  - !ruby/object:Gem::Version
61
54
  version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: coveralls
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: nokogiri
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: reek
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
62
125
  description:
63
- email: igel@igels.net
126
+ email:
127
+ - igel@igels.net
64
128
  executables: []
65
129
  extensions: []
66
- extra_rdoc_files:
67
- - README.textile
130
+ extra_rdoc_files: []
68
131
  files:
132
+ - CHANGELOG.md
133
+ - Gemfile
69
134
  - MIT-LICENSE
70
- - README.textile
135
+ - README.md
71
136
  - Rakefile
72
- - Gemfile
137
+ - it.gemspec
138
+ - lib/it.rb
73
139
  - lib/it/helper.rb
140
+ - lib/it/interpolation.rb
74
141
  - lib/it/link.rb
75
- - lib/it/plain.rb
76
142
  - lib/it/parser.rb
143
+ - lib/it/plain.rb
77
144
  - lib/it/tag.rb
78
- - lib/it/interpolation.rb
79
- - lib/it.rb
80
- - spec/it/link_spec.rb
81
- - spec/it/interpolation_spec.rb
82
- - spec/it/plain_spec.rb
83
- - spec/it/parser_spec.rb
84
- - spec/it/helper_spec.rb
85
- - spec/it/tag_spec.rb
86
- - spec/spec_helper.rb
87
- - spec/it_spec.rb
145
+ - lib/it/version.rb
88
146
  homepage: https://github.com/igel/it
89
147
  licenses:
90
148
  - MIT
149
+ metadata: {}
91
150
  post_install_message:
92
151
  rdoc_options: []
93
152
  require_paths:
94
153
  - lib
95
154
  required_ruby_version: !ruby/object:Gem::Requirement
96
- none: false
97
155
  requirements:
98
- - - ! '>='
156
+ - - ">="
99
157
  - !ruby/object:Gem::Version
100
- version: 1.9.2
158
+ version: '0'
101
159
  required_rubygems_version: !ruby/object:Gem::Requirement
102
- none: false
103
160
  requirements:
104
- - - ! '>='
161
+ - - ">="
105
162
  - !ruby/object:Gem::Version
106
- version: 1.3.6
163
+ version: '0'
107
164
  requirements: []
108
165
  rubyforge_project:
109
- rubygems_version: 1.8.23
166
+ rubygems_version: 2.4.5
110
167
  signing_key:
111
- specification_version: 3
168
+ specification_version: 4
112
169
  summary: A helper for links and other html tags in your translations
113
- test_files:
114
- - spec/it/link_spec.rb
115
- - spec/it/interpolation_spec.rb
116
- - spec/it/plain_spec.rb
117
- - spec/it/parser_spec.rb
118
- - spec/it/helper_spec.rb
119
- - spec/it/tag_spec.rb
120
- - spec/it_spec.rb
121
- - spec/spec_helper.rb
170
+ test_files: []
@@ -1,76 +0,0 @@
1
- "!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, Rubinius and Jruby)
2
-
3
- h1. What is *it*?
4
-
5
- I18n is baked right into Rails, and it's great. But if you want to place markup or links inside your translated copies, things get a little messy. You need to specify the label of your links separately from the rest of the copy. Writing HTML in your translations is even worse.
6
-
7
- <pre><code>en:
8
- copy: "If you are already registered, %{login_link}!"
9
- copy_login_link: "please sign in"</code></pre>
10
-
11
- <pre><code><%=raw t("copy", login: link_to(t("copy_login_link"), login_path)) %></code></pre>
12
-
13
- Wouldn't it be much nicer and easier to understand for your translator to have the whole copy in single label? *it* lets you do that:
14
-
15
- <pre><code>en:
16
- copy: "If you are already registered, %{login_link:please sign in}!"</code></pre>
17
-
18
- <pre><code><%=it "copy", login_link: login_path %></code></pre>
19
-
20
- You may have noticed in the example above, that *it* doesn't require @raw@ anymore. Of course, all HTML in the translation gets properly escaped, so you don't have to worry about XSS.
21
-
22
- h2. Installation
23
-
24
- Just add the following line to your Gemfile & run @bundle install@:
25
-
26
- <pre><code>gem 'it'</code></pre>
27
-
28
- h2. Usage
29
-
30
- You may have as many links inside your translations as you like, and normal interpolations are possible as well:
31
-
32
- <pre><code>en:
33
- copy: "Did you read the %{guide:Rails I18n guide}? It has more than %{advises} useful advises. You may fork the repo at {repo:github}."</code></pre>
34
-
35
- <pre><code><%=it "copy", guide: It.link("http://guides.rubyonrails.org/i18n.html"), advices: 100, repo: It.link("https://github.com/lifo/docrails/tree/master/railties/guides") %></code></pre>
36
-
37
- As you see above, unless the interpolation name is @link@ or starts with @_link@ or ends with @_link@, you need to call @It.link@ to create a link. The advantage of @It.link@: You may specify options like you would with @link_to@:
38
-
39
- <pre><code><%=it "copy", guide: It.link("http://guides.rubyonrails.org/i18n.html", target: '_blank', class: "important") %></code></pre>
40
-
41
- 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.
42
-
43
- Want to introduce some markup into your sentences? *it* will help you:
44
-
45
- <pre><code>en:
46
- advantages: There are %{b:many advantages} for registered users!</code></pre>
47
-
48
- <pre><code><%=it "advantages", b: It.tag(:b, class: "red") %></code></pre>
49
-
50
- Even nested interpolations are possible:
51
-
52
- <pre><code>en:
53
- copy: "Want to contact %{user}%? %{link:send %{b:%{user} a message}}!"</code></pre>
54
-
55
- <pre><code><%=it "copy", link: "mailto:igel@igels.net", user: 'iGEL', b: It.tag(:b) %></code></pre>
56
-
57
- If you would like to use the same translations in your html and plain text mails, you will like the @It.plain@ method:
58
- <pre><code>en:
59
- mail_copy: "Do you like %{link:Rails}?"</code></pre>
60
-
61
- <pre><code>HTML mail:
62
- <%= it "mail_copy", link: It.link("http://www.rubyonrails.org/") %>
63
-
64
- Plain mail:
65
- <%= it "mail_copy", link: It.plain("%s[http://www.rubyonrails.org/]") %>
66
- </code></pre>
67
-
68
- The @%s@ will be replaced with the label, in the example with Rails. You could provide any other string containing @%s@. The default is just @%s@, so it will return only the label itself.
69
-
70
- h2. Abandoned & outdated?
71
-
72
- Everybody hates old and outdated gems, which don't work on with the latest Rails or Ruby version or haven't been updated for ages. Sadly, rubygems is full of such gems. If you improved *it* send me a pull request. If I have not time to support the lib anymore, I will happily hand the project over to a new maintainer.
73
-
74
- h2. Broken English?
75
-
76
- I'm not a native speaker, so you'll probably find some spelling errors or clumsy sentences above. If you do, please send me a message.
@@ -1,138 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'spec_helper'
4
- require 'it'
5
- require 'active_support/core_ext/string'
6
-
7
- describe It::Helper, "#it" do
8
- before do
9
- I18n.backend.store_translations(:en, test1: "I have a %{link:link to Rails} in the middle.")
10
- I18n.backend.store_translations(:de, test1: "Ich habe einen %{link:Link zu Rails} in der Mitte.")
11
-
12
- @view = ActionView::Base.new
13
- @controller = ActionController::Base.new
14
- @view.controller = @controller
15
- end
16
-
17
- after do
18
- I18n.locale = :en
19
- end
20
-
21
- it "should insert the link into the string" do
22
- 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.')
23
- end
24
-
25
- it "should insert the link into the German translation" do
26
- I18n.locale = :de
27
- 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.')
28
- end
29
-
30
- it "should allow link options to be set" do
31
- 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.')
32
- end
33
-
34
- it "should support the plain thing" do
35
- 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.')
36
- end
37
-
38
- it "should parse other tags as well" do
39
- 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.')
40
- end
41
-
42
- it "should mark the result as html safe" do
43
- expect(@view.it("test1", link: It.link("http://www.rubyonrails.org")).html_safe?).to be_true
44
- end
45
-
46
- it "should escape all html in the translation" do
47
- I18n.backend.store_translations(:en, test2: "<a href=\"hax0r\"> & %{link:link -> Rails} in <b>the middle</b>.")
48
- 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;.')
49
- end
50
-
51
- it "should also work with 2 links" do
52
- I18n.backend.store_translations(:en, test3: "I like %{link1:rails} and %{link2:github}.")
53
- 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>.')
54
- end
55
-
56
- it "should allow normal I18n interpolations" do
57
- I18n.backend.store_translations(:en, test4: "I have a %{link:link to Rails} in the %{position}.")
58
- 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.')
59
- end
60
-
61
- it "should allow Integers as normal interpolation" do
62
- I18n.backend.store_translations(:en, test5: "Hello %{name}.")
63
- expect(@view.it("test5", name: 2)).to eq('Hello 2.')
64
- end
65
-
66
- it "should escape the HTML in normal interpolations" do
67
- I18n.backend.store_translations(:en, test5: "Hello %{name}.")
68
- 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;.')
69
- end
70
-
71
- it "should not escape html_safe interpolations" do
72
- I18n.backend.store_translations(:en, test5: "Hello %{name}.")
73
- 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>.')
74
- end
75
-
76
- it "should allow interpolations inside of links" do
77
- I18n.backend.store_translations(:en, test6: "Did you read our %{link:nice %{article}}?")
78
- expect(@view.it("test6", link: It.link("/article/2"), article: "article")).to eq('Did you read our <a href="/article/2">nice article</a>?')
79
- end
80
-
81
- it "should raise a KeyError, if the key was not given" do
82
- expect { @view.it("test1", blubb: true) }.to raise_error(KeyError, "key{link} not found")
83
- end
84
-
85
- it "should raise an ArgumentError, if a String was given for an interpolation with argument" do
86
- I18n.backend.store_translations(:en, test7: "Sign up %{asdf:here}!")
87
- expect { @view.it("test7", asdf: "Heinz") }.to raise_error(ArgumentError, "key{asdf} has an argument, so it cannot resolved with a String")
88
- end
89
-
90
- it "should allow Strings, if the interpolation name is link" do
91
- I18n.backend.store_translations(:en, test8: "Sign up %{link:here}!")
92
- expect(@view.it("test8", link: "/register")).to eq('Sign up <a href="/register">here</a>!')
93
- end
94
-
95
- it "should allow Strings, if the interpolation name ends with _link" do
96
- I18n.backend.store_translations(:en, test8: "Sign up %{register_link:here}!")
97
- expect(@view.it("test8", register_link: "/register")).to eq('Sign up <a href="/register">here</a>!')
98
- end
99
-
100
- it "should allow Strings, if the interpolation name starts with link_" do
101
- I18n.backend.store_translations(:en, test8: "Sign up %{link_to_register:here}!")
102
- expect(@view.it("test8", link_to_register: "/register")).to eq('Sign up <a href="/register">here</a>!')
103
- end
104
-
105
- it "should work with tags without arguments" do
106
- I18n.backend.store_translations(:en, test9: "We can %{br} do line breaks")
107
- expect(@view.it("test9", br: It.tag(:br))).to eq('We can <br /> do line breaks')
108
- end
109
-
110
- it 'should support dot-prefixed keys' do
111
- I18n.backend.store_translations(:en, widgets: { show: { all_widgets: "See %{widgets_link:all widgets}" } })
112
- @view.instance_variable_set(:"@_virtual_path", "widgets/show") # For Rails 3.0
113
- @view.instance_variable_set(:"@virtual_path", "widgets/show") # For Rails 3.1, 3.2 and probably 4.0
114
- expect(@view.it('.all_widgets', widgets_link: '/widgets')).to eq('See <a href="/widgets">all widgets</a>')
115
- end
116
-
117
- it 'should support the locale option' do
118
- 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.')
119
- end
120
-
121
- context "With a pluralized translation" do
122
- before do
123
- I18n.backend.store_translations(:en, test10: {zero: "You have zero messages.", one: "You have %{link:one message}.", other: "You have %{link:%{count} messages}."})
124
- end
125
-
126
- it 'should work with count = 0' do
127
- expect(@view.it("test10", count: 0, link: "/messages")).to eq('You have zero messages.')
128
- end
129
-
130
- it 'should work with count = 1' do
131
- expect(@view.it("test10", count: 1, link: "/messages")).to eq('You have <a href="/messages">one message</a>.')
132
- end
133
-
134
- it 'should work with count > 1' do
135
- expect(@view.it("test10", count: 2, link: "/messages")).to eq('You have <a href="/messages">2 messages</a>.')
136
- end
137
- end
138
- end
@@ -1,80 +0,0 @@
1
- require 'spec_helper'
2
- require 'it'
3
-
4
- describe It::Interpolation do
5
- subject(:interpolation) { It::Interpolation.new('%{key:label}', { 'key' => It.tag(:b) }) }
6
-
7
- describe '.new' do
8
- it 'throws an error with one argument' do
9
- expect { It::Interpolation.new('%{key:label}') }.to raise_error(ArgumentError)
10
- end
11
-
12
- it 'extracts the key from the string' do
13
- expect(interpolation.key).to eq('key')
14
- end
15
-
16
- it 'extracts the label from the string' do
17
- expect(interpolation.label).to eq('label')
18
- end
19
-
20
- it 'assigns nil to the label if not present' do
21
- expect(It::Interpolation.new('%{key}', {}).label).to be_nil
22
- end
23
-
24
- it 'converts string values to a It::Link if the key is named link' do
25
- interpolation = It::Interpolation.new('%{link:label}', 'link' => 'http://github.com')
26
-
27
- expect(interpolation.values['link']).to be_kind_of(It::Link)
28
- end
29
-
30
- it 'converts string values to a It::Link if the key starts with link_' do
31
- interpolation = It::Interpolation.new('%{link_github:label}', 'link_github' => 'http://github.com')
32
-
33
- expect(interpolation.values['link_github']).to be_kind_of(It::Link)
34
- end
35
-
36
- it 'converts string values to a It::Link if the key ends with _link' do
37
- interpolation = It::Interpolation.new('%{github_link:label}', 'github_link' => 'http://github.com')
38
-
39
- expect(interpolation.values['github_link']).to be_kind_of(It::Link)
40
- end
41
- end
42
-
43
- describe '#process' do
44
- it 'interpolates the string' do
45
- expect(interpolation.process).to eq('<b>label</b>')
46
- end
47
-
48
- it 'interpolates the string to an empty tag if no label is present' do
49
- interpolation.label = nil
50
-
51
- expect(interpolation.process).to eq('<b />')
52
- end
53
-
54
- it 'does the normal interpolation if the value is not a Tag and no label is present' do
55
- interpolation.label = nil
56
- interpolation.values = { 'key' => 'string'}
57
-
58
- expect(interpolation.process).to eq('string')
59
- end
60
-
61
- it 'escapes HTML in the normal interpolation' do
62
- interpolation.label = nil
63
- interpolation.values = { 'key' => '<b>hallo</b>'}
64
-
65
- expect(interpolation.process).to eq('&lt;b&gt;hallo&lt;/b&gt;')
66
- end
67
-
68
- it 'raises an KeyError if the requested key was not provided' do
69
- interpolation.values = {}
70
-
71
- expect { interpolation.process }.to raise_error(KeyError)
72
- end
73
-
74
- it 'raises an ArgumentError, if a string should be interpolated with a label' do
75
- interpolation.values = { 'key' => 'string'}
76
-
77
- expect { interpolation.process }.to raise_error(ArgumentError)
78
- end
79
- end
80
- end
@@ -1,42 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'spec_helper'
4
- require 'it'
5
-
6
- describe It::Link, '.new' do
7
- it "should accept a String as frist param" do
8
- expect { It::Link.new("http://www.rubyonrails.org/") }.not_to raise_error
9
- end
10
-
11
- it "should accept a Hash as first param" do
12
- expect { It::Link.new({controller: "articles", action: "index"}) }.not_to raise_error
13
- end
14
-
15
- it "should raise a TypeError if the first param is an Integer" do
16
- expect { It::Link.new(1) }.to raise_error(TypeError)
17
- end
18
-
19
- it "should accept options as a Hash" do
20
- expect { It::Link.new("http://www.rubyonrails.org/", {id: "identity", class: "classy"}) }.not_to raise_error
21
- end
22
-
23
- it "should raise a TypeError, if the options are a String" do
24
- expect { It::Link.new("http://www.rubyonrails.org/", "classy") }.to raise_error(TypeError)
25
- end
26
-
27
- it "should raise ArgumentError, if called with three params" do
28
- expect { It::Link.new("http://www.rubyonrails.org/", {}, :blubb) }.to raise_error(ArgumentError)
29
- end
30
- end
31
-
32
- describe It::Link, '#tag_name' do
33
- it "should always return a" do
34
- expect(It::Link.new("http://www.rubyonrails.org/").tag_name).to eq(:a)
35
- end
36
- end
37
-
38
- describe It::Link, '#process' do
39
- it "should return a link with the options set and the content as label" do
40
- expect(It::Link.new("http://www.rubyonrails.org", target: "_blank").process("Rails")).to eq('<a href="http://www.rubyonrails.org" target="_blank">Rails</a>')
41
- end
42
- end
@@ -1,38 +0,0 @@
1
- require 'spec_helper'
2
- require 'it'
3
-
4
- describe It::Parser do
5
- describe '#process' do
6
- it 'calls the Interpolation as required' do
7
- values = {'b' => It.tag(:b), 'link' => '/messages'}
8
- parser = It::Parser.new('You have %{b:%{link:new messages}}!', values)
9
-
10
- return1 = double('It::Interpolation', process: '<a href="/messages">new messages</a>')
11
- It::Interpolation.should_receive(:new).with('%{link:new messages}', values).and_return(return1)
12
-
13
- return2 = double('It::Interpolation', process: '<b><a href="/messages">new messages</a></b>')
14
- It::Interpolation.should_receive(:new).with('%{b:<a href="/messages">new messages</a>}', values).and_return(return2)
15
-
16
- expect(parser.process).to eq('You have <b><a href="/messages">new messages</a></b>!')
17
- end
18
-
19
- it 'escapes HTML in the string and the labels' do
20
- parser = It::Parser.new('It is a <b>save</b> %{word:<i>world</i>}', 'word' => It.tag(:i))
21
-
22
- expect(parser.process).to eq('It is a &lt;b&gt;save&lt;/b&gt; <i>&lt;i&gt;world&lt;/i&gt;</i>')
23
- end
24
-
25
- it 'marks the result as html safe' do
26
- parser = It::Parser.new('test', {})
27
-
28
- expect(parser.process).to be_html_safe
29
- end
30
-
31
- it 'delegates pluralization to I18n' do
32
- I18n.backend.stub(:pluralize).with('en', {other: 'You have %{count} messages'}, 2) { 'This is the pluralized string' }
33
- parser = It::Parser.new({other: 'You have %{count} messages'}, 'locale' => 'en', 'count' => 2)
34
-
35
- expect(parser.process).to eq('This is the pluralized string')
36
- end
37
- end
38
- end
@@ -1,30 +0,0 @@
1
- require 'spec_helper'
2
- require 'it'
3
-
4
- describe It::Plain, '.new' do
5
- it "should work with no params" do
6
- expect { It::Plain.new }.not_to raise_error
7
- end
8
-
9
- it "should work with a String" do
10
- expect { It::Plain.new("%s[http://www.rubyonrails.org]") }.not_to raise_error
11
- end
12
-
13
- it "should raise ArgumentError with 2 params" do
14
- expect { It::Plain.new("asdf", "asdf")}.to raise_error(ArgumentError)
15
- end
16
-
17
- it "should raise a TypeError, if the first param is not a String" do
18
- expect { It::Plain.new(1)}.to raise_error(TypeError)
19
- end
20
- end
21
-
22
- describe It::Plain, '#process' do
23
- it "should return 'Ruby on Rails', if no param was given" do
24
- expect(It::Plain.new.process("Ruby on Rails")).to eq('Ruby on Rails')
25
- end
26
-
27
- it "should return 'Ruby on Rails[http://www.rubyonrails.org/]'" do
28
- expect(It::Plain.new("%s[http://www.rubyonrails.org/]").process("Ruby on Rails")).to eq('Ruby on Rails[http://www.rubyonrails.org/]')
29
- end
30
- end
@@ -1,68 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'spec_helper'
4
- require 'it'
5
-
6
- describe It::Tag, '.new' do
7
- it "should work with a parameter (Symbol)" do
8
- expect { It::Tag.new(:b) }.not_to raise_error
9
- end
10
-
11
- it "should work with a paramter (String)" do
12
- expect { It::Tag.new("b") }.not_to raise_error
13
- end
14
-
15
- it "should raise TypeError if called with an Integer" do
16
- expect { It::Tag.new(1) }.to raise_error(TypeError)
17
- end
18
-
19
- it "should accept an options Hash" do
20
- expect { It::Tag.new(:b, class: "very_bold") }.not_to raise_error
21
- end
22
-
23
- it "should raise a TypeError if called with a String" do
24
- expect { It::Tag.new(:b, "very_bold") }.to raise_error(TypeError)
25
- end
26
-
27
- it "should raise an ArgumentError if called with three params" do
28
- expect { It::Tag.new(:b, {}, :blubb) }.to raise_error(ArgumentError)
29
- end
30
- end
31
-
32
- describe It::Tag, '#tag_name' do
33
- it "should return the tag as a Symbol if given as a Symbol" do
34
- expect(It::Tag.new(:i).tag_name).to eq(:i)
35
- end
36
-
37
- it "should return the tag_name as a Symbol if given as a String" do
38
- expect(It::Tag.new("i").tag_name).to eq(:i)
39
- end
40
- end
41
-
42
- describe It::Tag, '#options' do
43
- it "should return the options with symbolized keys" do
44
- expect(It::Tag.new(:i, "id" => "cool", class: "classy").options).to eq({id: "cool", class: "classy"})
45
- end
46
- end
47
-
48
- describe It::Tag, '#process' do
49
- it "should return a tag with the options as attributes and the param as content" do
50
- expect(It::Tag.new(:i, "id" => "cool", "class" => "classy").process("some text")).to eq('<i class="classy" id="cool">some text</i>')
51
- end
52
-
53
- it "should be marked as html safe" do
54
- expect(It::Tag.new(:i).process("some text").html_safe).to be_true
55
- end
56
-
57
- it "should escape HTML" do
58
- expect(It::Tag.new(:i).process("some text & <b>html</b>")).to eq('<i>some text &amp; &lt;b&gt;html&lt;/b&gt;</i>')
59
- end
60
-
61
- it "should not escape strings marked as HTML safe" do
62
- expect(It::Tag.new(:i).process("some text & <b>html</b>".html_safe)).to eq('<i>some text & <b>html</b></i>')
63
- end
64
-
65
- it "should return an empty tag, if no content is provided" do
66
- expect(It::Tag.new(:br, "id" => "cool").process).to eq('<br id="cool" />')
67
- end
68
- end
@@ -1,75 +0,0 @@
1
- # encoding: UTF-8
2
-
3
- require 'spec_helper'
4
- require 'it'
5
-
6
- describe It, '.it' do
7
- it "should translate inside the controller as well" do
8
- I18n.backend.store_translations(:en, test1: "I have a %{link:link to Rails} in the middle.")
9
- expect(It.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.')
10
- end
11
-
12
- it 'should use default key if no translation is present on specified key' do
13
- I18n.backend.store_translations(:en, fallback: 'this is a fallback')
14
- expect(It.it('a.missing.key', default: :fallback)).to eq('this is a fallback')
15
- end
16
-
17
- it 'should use default string if key is missing' do
18
- expect(It.it('a.missing.key', default: 'this is a fallback string')).to eq('this is a fallback string')
19
- end
20
-
21
- end
22
-
23
- describe It, '.link' do
24
- it "should return an It::Link object" do
25
- expect(It.link("https://www.github.com").class).to eq(It::Link)
26
- end
27
-
28
- it "should accept one param" do
29
- expect { It.link("http://www.rubyonrails.org/") }.not_to raise_error
30
- end
31
-
32
- it "should accept two params" do
33
- expect { It.link("http://www.rubyonrails.org/", {id: "identity", class: "classy"}) }.not_to raise_error
34
- end
35
-
36
- it "should raise ArgumentError, if called with three params" do
37
- expect { It.link("http://www.rubyonrails.org/", {}, :blubb) }.to raise_error(ArgumentError)
38
- end
39
- end
40
-
41
- describe It, '.tag' do
42
- it "should return an It::Tag object" do
43
- expect(It.tag(:b).class).to eq(It::Tag)
44
- end
45
-
46
- it "should work with a param" do
47
- expect { It.tag(:b) }.not_to raise_error
48
- end
49
-
50
- it "should accept two params" do
51
- expect { It.tag(:b, class: "very_bold") }.not_to raise_error
52
- end
53
-
54
- it "should raise an ArgumentError if called with three params" do
55
- expect { It.tag(:b, {}, :blubb) }.to raise_error(ArgumentError)
56
- end
57
- end
58
-
59
- describe It, '.plain' do
60
- it "should return an It::Plain object" do
61
- expect(It.plain.class).to eq(It::Plain)
62
- end
63
-
64
- it "should work without params" do
65
- expect { It.plain }.not_to raise_error
66
- end
67
-
68
- it "should accept one param" do
69
- expect { It.plain("%s[http://www.rubyonrails.org/]") }.not_to raise_error
70
- end
71
-
72
- it "should raise ArgumentError, if called with two params" do
73
- expect { It.plain("%s[http://www.rubyonrails.org/]", :blubb) }.to raise_error(ArgumentError)
74
- end
75
- end
@@ -1,13 +0,0 @@
1
- require 'rspec'
2
- require 'action_pack'
3
- require 'action_controller'
4
- require 'action_view'
5
-
6
- $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib')
7
-
8
- RSpec.configure do |config|
9
-
10
- config.expect_with :rspec do |c|
11
- c.syntax = :expect
12
- end
13
- end