From 23f2266de60c76933541dd373d133ad665208d10 Mon Sep 17 00:00:00 2001 From: Dmytro Shteflyuk Date: Tue, 10 Sep 2019 18:59:41 -0400 Subject: [PATCH 1/7] Upgrading Rubocop --- .codeclimate.yml | 2 +- Gemfile | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.codeclimate.yml b/.codeclimate.yml index 13fe4d48..da1aa63e 100644 --- a/.codeclimate.yml +++ b/.codeclimate.yml @@ -10,7 +10,7 @@ engines: rubocop: enabled: true - channel: rubocop-0-60 + channel: rubocop-0-67 ratings: paths: diff --git a/Gemfile b/Gemfile index bb2c6a7c..71c4c69a 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,6 @@ # frozen_string_literal: true -source 'http://rubygems.org' +source 'https://rubygems.org' # Specify your gem's dependencies in meta-tags.gemspec gemspec @@ -13,7 +13,7 @@ end group :test do # Lock rubocop to a specific version we use on CI. If you update this, # don't forget to switch rubocop channel in the .codeclimate.yml - gem 'rubocop', '0.60.0' + gem 'rubocop', '~> 0.67.0' # Apply RSpec rubocop cops gem 'rubocop-rspec', require: false # We use this gem on CI to calculate code coverage. From 0a0d30bc0644ed870edb1905c8129045aa0729fe Mon Sep 17 00:00:00 2001 From: Dmytro Shteflyuk Date: Tue, 10 Sep 2019 18:59:56 -0400 Subject: [PATCH 2/7] Upgrading Ruby and Rails --- .circleci/config.yml.erb | 6 +++--- .ruby-version | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.circleci/config.yml.erb b/.circleci/config.yml.erb index 1da26931..39ce64d7 100644 --- a/.circleci/config.yml.erb +++ b/.circleci/config.yml.erb @@ -14,14 +14,14 @@ ['2.5', '4.2.10'], ['2.5', '5.0.7'], ['2.5', '5.1.6'], - ['2.5', '5.2.2', true], - ['2.5', '6.0.0.beta1'], + ['2.5', '5.2.2'], + ['2.5', '6.0.0'], # 2.6 ['2.6', '5.0.7'], ['2.6', '5.1.6'], ['2.6', '5.2.2'], - ['2.6', '6.0.0.beta1'], + ['2.6', '6.0.0', true], ] cc_build = builds.find { |_, _, submit_cc| submit_cc } diff --git a/.ruby-version b/.ruby-version index 73462a5a..ec1cf33c 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.5.1 +2.6.3 From 9e99f466afbdb35d6125b9dca51b0519f65ff352 Mon Sep 17 00:00:00 2001 From: Dmytro Shteflyuk Date: Tue, 10 Sep 2019 19:02:21 -0400 Subject: [PATCH 3/7] Fixing a bug when noindex is "robots" and nofollow is a specific robot name, both are treated as "robots". Closes #195 --- lib/meta_tags/meta_tags_collection.rb | 76 +++++++++------------------ lib/meta_tags/renderer.rb | 2 +- spec/view_helper/noindex_spec.rb | 8 +++ 3 files changed, 35 insertions(+), 51 deletions(-) diff --git a/lib/meta_tags/meta_tags_collection.rb b/lib/meta_tags/meta_tags_collection.rb index 1031111b..9ca6ad0e 100644 --- a/lib/meta_tags/meta_tags_collection.rb +++ b/lib/meta_tags/meta_tags_collection.rb @@ -142,28 +142,30 @@ def extract_separator # # @return [Hash] noindex attributes. # - def extract_noindex - noindex_name, noindex_value = extract_noindex_attribute(:noindex) - index_name, index_value = extract_noindex_attribute(:index) - - nofollow_name, nofollow_value = extract_noindex_attribute(:nofollow) - follow_name, follow_value = extract_noindex_attribute(:follow) - - noindex_attributes = if noindex_name == follow_name && (noindex_value || follow_value) - # noindex has higher priority than index and follow has higher priority than nofollow - [ - [noindex_name, noindex_value || index_value], - [follow_name, follow_value || nofollow_value], - ] - else - [ - [index_name, index_value], - [follow_name, follow_value], - [noindex_name, noindex_value], - [nofollow_name, nofollow_value], - ] - end - append_noarchive_attribute group_attributes_by_key noindex_attributes + def extract_robots + result = Hash.new { |h, k| h[k] = [] } + + [ + # noindex has higher priority than index + [:noindex, :index], + # follow has higher priority than nofollow + [:follow, :nofollow], + :noarchive, + ].each do |attribute| + if attribute.kind_of?(Array) + left, right = attribute + lname, lvalue = extract_robots_attribute(left) + rname, rvalue = extract_robots_attribute(right) + + result[lname] << lvalue if lvalue + result[rname] << rvalue if rvalue && !(lname == rname && lvalue) + else + name, value = extract_robots_attribute(attribute) + result[name] << value if value + end + end + + result.transform_values { |v| v.join(', ') } end protected @@ -190,43 +192,17 @@ def extract_separator_section(name, default) meta_tags[name] == false ? '' : (meta_tags[name] || default) end - # Extracts noindex attribute name and value without deleting it from meta tags list. + # Extracts robots attribute (noindex, nofollow, etc) name and value. # # @param [String, Symbol] name noindex attribute name. # @return [Array] pair of noindex attribute name and value. # - def extract_noindex_attribute(name) + def extract_robots_attribute(name) noindex = extract(name) noindex_name = noindex.kind_of?(String) ? noindex : 'robots' noindex_value = noindex ? name.to_s : nil [ noindex_name, noindex_value ] end - - # Append noarchive attribute if it present. - # - # @param [Hash] noindex noindex attributes. - # @return [Hash] modified noindex attributes. - # - def append_noarchive_attribute(noindex) - noarchive_name, noarchive_value = extract_noindex_attribute :noarchive - if noarchive_value - if noindex[noarchive_name].blank? - noindex[noarchive_name] = noarchive_value - else - noindex[noarchive_name] += ", #{noarchive_value}" - end - end - noindex - end - - # Convert array of arrays to hashes and concatenate values - # - # @param [Array] attributes list of noindex keys and values - # @return [Hash] hash of grouped noindex keys and values - # - def group_attributes_by_key(attributes) - Hash[attributes.group_by(&:first).map { |k, v| [k, v.map(&:last).tap(&:compact!).join(', ')] }] - end end end diff --git a/lib/meta_tags/renderer.rb b/lib/meta_tags/renderer.rb index 504e582d..f2addc16 100644 --- a/lib/meta_tags/renderer.rb +++ b/lib/meta_tags/renderer.rb @@ -97,7 +97,7 @@ def render_with_normalization(tags, name) # @param [Array] tags a buffer object to store tag in. # def render_noindex(tags) - meta_tags.extract_noindex.each do |name, content| + meta_tags.extract_robots.each do |name, content| tags << Tag.new(:meta, name: name, content: content) if content.present? end end diff --git a/spec/view_helper/noindex_spec.rb b/spec/view_helper/noindex_spec.rb index 610156e8..021794f6 100644 --- a/spec/view_helper/noindex_spec.rb +++ b/spec/view_helper/noindex_spec.rb @@ -89,6 +89,14 @@ end end + it 'displays two meta tags when different names used with "set_meta_tags"' do + subject.set_meta_tags(noindex: 'robots', nofollow: 'googlebot') + subject.display_meta_tags(site: 'someSite').tap do |meta| + expect(meta).to have_tag('meta', with: { content: 'noindex', name: 'robots' }) + expect(meta).to have_tag('meta', with: { content: 'nofollow', name: 'googlebot' }) + end + end + it 'uses custom name if string is used' do subject.noindex('some-name') subject.nofollow('some-name') From 7e9dfe6af9b50ee3e1a702a9ea36df9d2500a2f2 Mon Sep 17 00:00:00 2001 From: Dmytro Shteflyuk Date: Tue, 10 Sep 2019 19:02:33 -0400 Subject: [PATCH 4/7] Rubocop fixes --- lib/meta_tags/text_normalizer.rb | 2 +- lib/meta_tags/version.rb | 1 + spec/view_helper/module_spec.rb | 2 +- spec/view_helper/title_spec.rb | 2 -- 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/meta_tags/text_normalizer.rb b/lib/meta_tags/text_normalizer.rb index a54d3bc9..b47dc9c5 100644 --- a/lib/meta_tags/text_normalizer.rb +++ b/lib/meta_tags/text_normalizer.rb @@ -3,7 +3,7 @@ module MetaTags # Module contains helpers that normalize text meta tag values. module TextNormalizer - extend self # rubocop:disable Style/ModuleFunction + extend self # Normalize title value. # diff --git a/lib/meta_tags/version.rb b/lib/meta_tags/version.rb index b125300b..faec7de0 100644 --- a/lib/meta_tags/version.rb +++ b/lib/meta_tags/version.rb @@ -3,4 +3,5 @@ module MetaTags # Gem version. VERSION = '2.11.1' + public_constant :VERSION end diff --git a/spec/view_helper/module_spec.rb b/spec/view_helper/module_spec.rb index 6cf4bd4e..dee94cbe 100644 --- a/spec/view_helper/module_spec.rb +++ b/spec/view_helper/module_spec.rb @@ -6,7 +6,7 @@ subject { ActionView::Base.new } it 'is mixed into ActionView::Base' do - expect(ActionView::Base.included_modules).to include(MetaTags::ViewHelper) + expect(ActionView::Base.included_modules).to include(described_class) end it 'responds to "title" helper' do diff --git a/spec/view_helper/title_spec.rb b/spec/view_helper/title_spec.rb index 91b5cfe6..df252dd4 100644 --- a/spec/view_helper/title_spec.rb +++ b/spec/view_helper/title_spec.rb @@ -105,7 +105,6 @@ expect(title).to eq('TITLE') end - # rubocop:disable Rails/OutputSafety it 'uses custom separator when :separator specified' do subject.title('someTitle') subject.display_meta_tags(site: 'someSite', separator: '-').tap do |meta| @@ -127,7 +126,6 @@ expect(meta).to eq('someSite:someTitle') end end - # rubocop:enable Rails/OutputSafety it 'uses custom prefix and suffix if available' do subject.display_meta_tags(site: 'someSite', title: 'someTitle', prefix: ' -', suffix: '- ').tap do |meta| From 278dff60bbd90ce50dad96b04d78acfa7afbd3c7 Mon Sep 17 00:00:00 2001 From: Dmytro Shteflyuk Date: Tue, 10 Sep 2019 19:22:23 -0400 Subject: [PATCH 5/7] Reduced complexity and added ability to specify multiple robot names in indexing directives --- lib/meta_tags/meta_tags_collection.rb | 30 +++++++++++++---------- spec/view_helper/noindex_spec.rb | 34 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/lib/meta_tags/meta_tags_collection.rb b/lib/meta_tags/meta_tags_collection.rb index 9ca6ad0e..fcd475c8 100644 --- a/lib/meta_tags/meta_tags_collection.rb +++ b/lib/meta_tags/meta_tags_collection.rb @@ -152,17 +152,7 @@ def extract_robots [:follow, :nofollow], :noarchive, ].each do |attribute| - if attribute.kind_of?(Array) - left, right = attribute - lname, lvalue = extract_robots_attribute(left) - rname, rvalue = extract_robots_attribute(right) - - result[lname] << lvalue if lvalue - result[rname] << rvalue if rvalue && !(lname == rname && lvalue) - else - name, value = extract_robots_attribute(attribute) - result[name] << value if value - end + apply_robots_attributes(result, attribute) end result.transform_values { |v| v.join(', ') } @@ -199,10 +189,26 @@ def extract_separator_section(name, default) # def extract_robots_attribute(name) noindex = extract(name) - noindex_name = noindex.kind_of?(String) ? noindex : 'robots' + noindex_name = noindex.kind_of?(String) || noindex.kind_of?(Array) ? noindex : 'robots' noindex_value = noindex ? name.to_s : nil [ noindex_name, noindex_value ] end + + def apply_robots_attributes(result, attributes) + processed = Set.new + Array(attributes).each do |attribute| + names, value = extract_robots_attribute(attribute) + Array(names).each do |name| + next unless value + + name = name.to_s + next if processed.include?(name) + + result[name] << value + processed << name + end + end + end end end diff --git a/spec/view_helper/noindex_spec.rb b/spec/view_helper/noindex_spec.rb index 021794f6..b4035a69 100644 --- a/spec/view_helper/noindex_spec.rb +++ b/spec/view_helper/noindex_spec.rb @@ -27,6 +27,14 @@ end end + it 'accepts multiple custom noindex robots in an array' do + subject.noindex(['some-noindex', 'another-noindex']) + subject.display_meta_tags(site: 'someSite').tap do |meta| + expect(meta).to have_tag('meta', with: { content: "noindex", name: "some-noindex" }) + expect(meta).to have_tag('meta', with: { content: "noindex", name: "another-noindex" }) + end + end + it 'displays nothing by default' do subject.display_meta_tags(site: 'someSite').tap do |meta| expect(meta).not_to have_tag('meta', with: { content: "noindex" }) @@ -66,6 +74,14 @@ end end + it 'accepts multiple custom nofollow robots in an array' do + subject.nofollow(['some-nofollow', 'another-nofollow']) + subject.display_meta_tags(site: 'someSite').tap do |meta| + expect(meta).to have_tag('meta', with: { content: "nofollow", name: "some-nofollow" }) + expect(meta).to have_tag('meta', with: { content: "nofollow", name: "another-nofollow" }) + end + end + it 'displays nothing by default' do subject.display_meta_tags(site: 'someSite').tap do |meta| expect(meta).not_to have_tag('meta', with: { content: "nofollow" }) @@ -187,4 +203,22 @@ end end end + + it 'properly handles priorities and multiple robot names' do + subject.set_meta_tags( + noindex: true, + index: 'yahoo', + follow: ['google', 'yahoo', 'github'], + nofollow: ['yandex', :google], + noarchive: ['yahoo', 'bing'], + ) + subject.display_meta_tags(site: 'someSite').tap do |meta| + expect(meta).to have_tag('meta', with: { name: "robots", content: "noindex" }) + expect(meta).to have_tag('meta', with: { name: "yahoo", content: "index, follow, noarchive" }) + expect(meta).to have_tag('meta', with: { name: "google", content: "follow" }) + expect(meta).to have_tag('meta', with: { name: "github", content: "follow" }) + expect(meta).to have_tag('meta', with: { name: "yandex", content: "nofollow" }) + expect(meta).to have_tag('meta', with: { name: "bing", content: "noarchive" }) + end + end end From a3061eb9d44d3b8d2efca9002ae4d77a4b4d0de8 Mon Sep 17 00:00:00 2001 From: Dmytro Shteflyuk Date: Tue, 10 Sep 2019 19:28:27 -0400 Subject: [PATCH 6/7] Reducing complexity --- lib/meta_tags/meta_tags_collection.rb | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/meta_tags/meta_tags_collection.rb b/lib/meta_tags/meta_tags_collection.rb index fcd475c8..9284517d 100644 --- a/lib/meta_tags/meta_tags_collection.rb +++ b/lib/meta_tags/meta_tags_collection.rb @@ -151,8 +151,8 @@ def extract_robots # follow has higher priority than nofollow [:follow, :nofollow], :noarchive, - ].each do |attribute| - apply_robots_attributes(result, attribute) + ].each do |attributes| + calculate_robots_attributes(result, attributes) end result.transform_values { |v| v.join(', ') } @@ -195,20 +195,24 @@ def extract_robots_attribute(name) [ noindex_name, noindex_value ] end - def apply_robots_attributes(result, attributes) + def calculate_robots_attributes(result, attributes) processed = Set.new Array(attributes).each do |attribute| names, value = extract_robots_attribute(attribute) - Array(names).each do |name| - next unless value - - name = name.to_s - next if processed.include?(name) + next unless value - result[name] << value - processed << name + Array(names).each do |name| + apply_robots_value(result, name, value, processed) end end end + + def apply_robots_value(result, name, value, processed) + name = name.to_s + return if processed.include?(name) + + result[name] << value + processed << name + end end end From e0bf14710a10cd855eefe03a1f555fb7d84b1bea Mon Sep 17 00:00:00 2001 From: Dmytro Shteflyuk Date: Tue, 10 Sep 2019 19:34:21 -0400 Subject: [PATCH 7/7] Preparing a release --- CHANGELOG.md | 8 ++++++++ README.md | 10 +++++----- lib/meta_tags/version.rb | 2 +- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5cffcf3..e0846721 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## 2.12.0 (Development) [☰](https://github.com/kpumuk/meta-tags/compare/v2.11.1...master) + +Features: + - Indexing directives (`noindex`, `nofollow`, etc. now support an array of robot names as a value). + +Bugfixes: + - When `noindex` uses "robots" as a value, `nofollow` ignores a custom robot name, and switches to "robots" as well + ## 2.11.1 (January 19, 2019) [☰](https://github.com/kpumuk/meta-tags/compare/v2.11.0...v2.11.1) Features: diff --git a/README.md b/README.md index 76c5f4d2..ab18efcc 100644 --- a/README.md +++ b/README.md @@ -164,11 +164,11 @@ Use these options to customize the title format: | `:suffix` | text between separator and page title | | `:lowercase` | when true, the page name will be lowercase | | `:reverse` | when true, the page and site names will be reversed | -| `:noindex` | add noindex meta tag; when true, 'robots' will be used, otherwise the string will be used | -| `:index` | add index meta tag; when true, 'robots' will be used, otherwise the string will be used | -| `:nofollow` | add nofollow meta tag; when true, 'robots' will be used, otherwise the string will be used | -| `:follow` | add follow meta tag; when true, 'robots' will be used, otherwise the string will be used | -| `:noarchive` | add noarchive meta tag; when true, 'robots' will be used, otherwise the string will be used | +| `:noindex` | add noindex meta tag; when true, 'robots' will be used; accepts a string with a robot name, or an array of strings | +| `:index` | add index meta tag; when true, 'robots' will be used; accepts a string with a robot name, or an array of strings | +| `:nofollow` | add nofollow meta tag; when true, 'robots' will be used; accepts a string with a robot name, or an array of strings | +| `:follow` | add follow meta tag; when true, 'robots' will be used; accepts a string with a robot name, or an array of strings | +| `:noarchive` | add noarchive meta tag; when true, 'robots' will be used; accepts a string with a robot name, or an array of strings | | `:canonical` | add canonical link tag | | `:prev` | add prev link tag | | `:next` | add next link tag | diff --git a/lib/meta_tags/version.rb b/lib/meta_tags/version.rb index faec7de0..9afb9f44 100644 --- a/lib/meta_tags/version.rb +++ b/lib/meta_tags/version.rb @@ -2,6 +2,6 @@ module MetaTags # Gem version. - VERSION = '2.11.1' + VERSION = '2.12.0' public_constant :VERSION end