linkificator 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cd55f7b84fe5b8cc95452c4233f849cc56fb4b06
4
- data.tar.gz: c4761b2b55ed9cc0f54a36f49a976db5f6b7e776
3
+ metadata.gz: 8d701023fd8a8105a73009361fcbd4bb3c37c913
4
+ data.tar.gz: 883dcb257b7e2d4a6817eb6de9589b552bd833d9
5
5
  SHA512:
6
- metadata.gz: 2e47037d7c5caa01dfb5bb96176e54d7fc98149435beb72055127ce4c5a20650a872665ad7d2dd77f1d6a9b7ce7487b86af4b9dff68a29486db5801de2591f73
7
- data.tar.gz: 10db3e4381d5dddadce0387d8a4ec2c60d2d507d58945b080073e660cfdfa807531e58b4a933413350a3963063eae70ba153bef391cbf93131ae3599461a42af
6
+ metadata.gz: 0138f1086f91eeef3c8b22cb056e268f5f9da9edd2a05a2574d84cc95ceed3665be7befdfd6579ecbe2d67d4b1ae51dd79b48eb6a600abbb2e9302f678020518
7
+ data.tar.gz: bf36ebce95a639dede5edab96a1110416cc79b94e82fa247a417396ca32931185ccf4f9f8009c6448a67675354a35500aeeda564b61f98d511b48fb52ac6487e
data/README.md CHANGED
@@ -95,7 +95,7 @@ seo_link_to('text', 'http://other.com') # => <a href="http://other.com">text</
95
95
 
96
96
  ## Contributing
97
97
 
98
- 1. Fork itр
98
+ 1. Fork it
99
99
  2. Create your feature branch (`git checkout -b my-new-feature`)
100
100
  3. Commit your changes (`git commit -am 'Add some feature'`)
101
101
  4. Push to the branch (`git push origin my-new-feature`)
@@ -4,8 +4,8 @@ require 'linkificator/conditions_storage/condition'
4
4
 
5
5
  module Linkificator
6
6
 
7
- def for_conditions(conditions)
8
- ConditionsStorage.new(conditions)
7
+ def self.for_conditions(conditions, options={})
8
+ ConditionsStorage.new(conditions, options)
9
9
  end
10
10
 
11
11
  end
@@ -2,10 +2,11 @@ require 'base64'
2
2
 
3
3
  class Linkificator::ConditionsStorage
4
4
 
5
- attr_reader :conditions
5
+ attr_reader :conditions, :options
6
6
 
7
- def initialize(conditions)
8
- @conditions = conditions.map { |hash| Condition.new(hash) }
7
+ def initialize(conditions, options={})
8
+ @conditions = conditions.map { |hash| Condition.new(hash.merge(options)) }
9
+ @options = options
9
10
  end
10
11
 
11
12
  def condition_for(options={})
@@ -14,17 +15,18 @@ class Linkificator::ConditionsStorage
14
15
  end
15
16
  end
16
17
 
17
- def link_to(*params, &block)
18
- context = extract_context(params)
19
- condition = condition_for(context) || Condition.new
20
- condition.link_to(*params, &block)
18
+ def link_to(name = nil, options = nil, html_options = nil, &block)
19
+ context = extract_context(name, options, html_options, &block)
20
+ condition = condition_for(context) || Condition.new(self.options)
21
+ condition.link_to(name, options, html_options, &block)
21
22
  end
22
23
 
23
24
  private
24
25
 
25
- def extract_context(params)
26
+ def extract_context(name = nil, options = nil, html_options = nil, &block)
27
+ html_options, options = options, name if block_given?
26
28
  context = {}
27
- context[:target] = params[1] if params[1].is_a?(String)
28
- context.merge!((params[2] || {}).delete(:context) || {})
29
+ context[:target] = options if options.is_a?(String)
30
+ context.merge!((html_options || {}).delete(:context) || {})
29
31
  end
30
32
  end
@@ -1,8 +1,8 @@
1
1
  class Linkificator::ConditionsStorage::Condition
2
- attr_reader :conditions
2
+ attr_reader :options
3
3
 
4
- def initialize(conditions={})
5
- @conditions = conditions
4
+ def initialize(options={})
5
+ @options = options
6
6
  end
7
7
 
8
8
  def match?(options)
@@ -14,21 +14,27 @@ class Linkificator::ConditionsStorage::Condition
14
14
  ].all?
15
15
  end
16
16
 
17
- def link_to(*params, &block)
18
- updated_params = params.clone
17
+ def link_to(name = nil, options = nil, html_options = nil, &block)
18
+ html_options, options, name = options, name, block if block_given?
19
+ options ||= {}
19
20
 
20
- view_context = view_context((updated_params[2] || {}).delete(:view_context))
21
+ view_context = view_context((html_options || {}).delete(:view_context))
21
22
 
22
23
  if processing[:data_href]
23
- url = view_context.url_for(*updated_params[1])
24
- updated_params[2] = (updated_params[2] || {}).merge('href' => '#', 'data-href' => Base64.strict_encode64(url))
24
+ url = view_context.url_for(*options)
25
+ html_options = (html_options || {}).merge('href' => '#', 'data-href' => Base64.strict_encode64(url))
25
26
  end
26
27
 
27
28
  if processing[:rel_nofollow]
28
- updated_params[2] = (updated_params[2] || {}).merge(rel: 'nofollow')
29
+ html_options = (html_options || {}).merge(rel: 'nofollow')
29
30
  end
30
31
 
31
- link = view_context.link_to(*updated_params, &block)
32
+
33
+ link = if block_given?
34
+ call_link_method(view_context, options, html_options, &block)
35
+ else
36
+ call_link_method(view_context, name, options, html_options)
37
+ end
32
38
 
33
39
  if processing[:wrap_noindex]
34
40
  view_context.content_tag(:noindex) { link }
@@ -45,7 +51,7 @@ class Linkificator::ConditionsStorage::Condition
45
51
  include ActionView::Context
46
52
  end
47
53
 
48
- def view_context(view_context)
54
+ def view_context(view_context=nil)
49
55
  view_context || (@view_context ||= ViewContext.new)
50
56
  end
51
57
 
@@ -63,10 +69,20 @@ class Linkificator::ConditionsStorage::Condition
63
69
  end
64
70
 
65
71
  def context
66
- @context ||= conditions[:context] || {}
72
+ @context ||= options[:context] || {}
67
73
  end
68
74
 
69
75
  def processing
70
- @processing ||= conditions[:processing] || {}
76
+ @processing ||= options[:processing] || {}
77
+ end
78
+
79
+ def call_link_method(view_context, *params, &block)
80
+ @link_method = if options[:link_via] && view_context(view_context).respond_to?(options[:link_via])
81
+ options[:link_via]
82
+ else
83
+ :link_to
84
+ end
85
+
86
+ view_context(view_context).send(*([@link_method] + params), &block)
71
87
  end
72
88
  end
@@ -1,3 +1,3 @@
1
1
  module Linkificator
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
@@ -129,5 +129,29 @@ describe Linkificator::ConditionsStorage::Condition do
129
129
  end
130
130
  end
131
131
 
132
+ context 'with block' do
133
+ let(:condition) {
134
+ Linkificator::ConditionsStorage::Condition.new(
135
+ processing: {rel_nofollow: true}
136
+ )
137
+ }
138
+ it do
139
+ expect(condition.link_to('http://example.com'){ 'text' }).to eq('<a href="http://example.com" rel="nofollow">text</a>')
140
+ end
141
+ end
142
+
143
+ context 'use custom link_to methods' do
144
+ let(:condition) {
145
+ Linkificator::ConditionsStorage::Condition.new(
146
+ processing: {rel_nofollow: true},
147
+ link_via: :custom_link_to
148
+ )
149
+ }
150
+ it do
151
+ expect_any_instance_of(Linkificator::ConditionsStorage::Condition::ViewContext).to receive(:custom_link_to)
152
+ expect(condition.link_to('text', 'http://example.com')).to eq(nil)
153
+ end
154
+ end
155
+
132
156
  end
133
157
  end
@@ -19,7 +19,7 @@ describe Linkificator::ConditionsStorage do
19
19
 
20
20
  context '#link_to' do
21
21
  it 'dont match anything' do
22
- expect(storage.link_to('text', 'http://example.com/c')).to eq('<a href="http://example.com/c">text</a>')
22
+ expect(storage.link_to('text', 'http://example.com/c')).to eq('<a href="http://example.com/c">text</a>')
23
23
  end
24
24
  it 'match by target' do
25
25
  expect(storage.link_to('text', 'http://example.com/a/b')).to eq('<noindex><a href="http://example.com/a/b">text</a></noindex>')
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linkificator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - dizer