Skip to content

Commit

Permalink
Merge pull request #207 from kpumuk/dmytro/railtie
Browse files Browse the repository at this point in the history
Moved Rails initialization into a Railtie
  • Loading branch information
kpumuk committed Oct 10, 2019
2 parents ec7ef94 + 8a5351b commit 038c3d3
Show file tree
Hide file tree
Showing 13 changed files with 75 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ engines:

rubocop:
enabled: true
channel: rubocop-0-74
channel: rubocop-0-75

ratings:
paths:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ doc
pkg
*.gem
.idea
log/
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 2.13.0 (October 10, 2019) [](https://github.com/kpumuk/meta-tags/compare/v2.12.0...v2.13.0)

Bugfixes:
- Fixed Rails 6 deprecation warning.

## 2.12.0 (September 10, 2019) [](https://github.com/kpumuk/meta-tags/compare/v2.11.1...v2.12.0)

Features:
Expand Down
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ gemspec

if ENV['RAILS_VERSION']
# Install specified version of actionpack if requested
gem 'actionpack', "~> #{ENV['RAILS_VERSION']}"
gem 'railties', "~> #{ENV['RAILS_VERSION']}"
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.74.0'
gem 'rubocop', '~> 0.75.0'
# Cops for rails apps
gem 'rubocop-rails'
# Apply RSpec rubocop cops
Expand Down
7 changes: 1 addition & 6 deletions lib/meta_tags.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# frozen_string_literal: true

require 'English'
require 'action_controller'
require 'action_view'

# MetaTags gem namespace.
module MetaTags
# Returns MetaTags gem configuration.
Expand Down Expand Up @@ -36,5 +32,4 @@ def self.configure
require 'meta_tags/text_normalizer'
require 'meta_tags/view_helper'

ActionView::Base.include MetaTags::ViewHelper
ActionController::Base.include MetaTags::ControllerHelper
require 'meta_tags/railtie.rb' if defined?(Rails)
17 changes: 17 additions & 0 deletions lib/meta_tags/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module MetaTags
class Railtie < Rails::Railtie
initializer 'meta_tags.setup_action_controller' do
ActiveSupport.on_load :action_controller do
ActionController::Base.include MetaTags::ControllerHelper
end
end

initializer 'meta_tags.setup_action_view' do
ActiveSupport.on_load :action_view do
ActionView::Base.include MetaTags::ViewHelper
end
end
end
end
2 changes: 1 addition & 1 deletion lib/meta_tags/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

module MetaTags
# Gem version.
VERSION = '2.12.0'
VERSION = '2.13.0'
public_constant :VERSION
end
2 changes: 0 additions & 2 deletions lib/meta_tags/view_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,5 @@ def display_meta_tags(defaults = {})
def display_title(defaults = {})
@meta_tags.full_title(defaults)
end

# safe_helper :display_meta_tags if defined?(:safe_helper)
end
end
5 changes: 3 additions & 2 deletions meta-tags.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ Gem::Specification.new do |spec|

spec.add_dependency "actionpack", ">= 3.2.0", "< 6.1"

spec.add_development_dependency "rake", "~> 12.0"
spec.add_development_dependency "rspec", "~> 3.8.0"
spec.add_development_dependency "railties", ">= 3.2.0", "< 6.1"
spec.add_development_dependency "rake", "~> 13.0"
spec.add_development_dependency "rspec", "~> 3.9.0"
spec.add_development_dependency "rspec-html-matchers", "~> 0.9.1"

spec.cert_chain = ["certs/kpumuk.pem"]
Expand Down
18 changes: 1 addition & 17 deletions spec/controller_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,9 @@

require 'spec_helper'

class MetaTagsController < ActionController::Base
def index
@page_title = 'title'
@page_keywords = 'key1, key2, key3'
@page_description = 'description'

if Gem.loaded_specs["actionpack"].version > Gem::Version.new('4.2.0')
render plain: '_rendered_'
else
render text: '_rendered_'
end
end

public :set_meta_tags, :meta_tags
end

describe MetaTags::ControllerHelper do
subject do
MetaTagsController.new.tap do |c|
MetaTagsRailsApp::MetaTagsController.new.tap do |c|
c.response = ActionDispatch::TestResponse.new
end
end
Expand Down
40 changes: 40 additions & 0 deletions spec/support/initialize_rails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

# Pretend we are a normal Rails application to trigger all the initializers
# that are normally happen during Rails boot process. This is done to properly
# test Railtie used by this gem to properly integrate into the Rails.
#
# TLDR. This is a real Rails application

require 'rails'
require 'action_controller/railtie'
require 'action_view/railtie'
require 'meta_tags/railtie'

module MetaTagsRailsApp
class Application < Rails::Application
config.secret_token = '572c86f5ede338bd8aba8dae0fd3a326aabababc98d1e6ce34b9f5'
config.eager_load = false
end

class MetaTagsController < ActionController::Base
include MetaTags::ControllerHelper

def index
@page_title = 'title'
@page_keywords = 'key1, key2, key3'
@page_description = 'description'

render plain: '_rendered_'
end

public :set_meta_tags, :meta_tags
end

class MetaTagsView < ActionView::Base
include MetaTags::ViewHelper
end
end

# Alright, we're all set. Let's boot!
Rails.application.initialize!
2 changes: 1 addition & 1 deletion spec/support/shared_examples_for_set_meta_tags.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

shared_examples_for '.set_meta_tags' do
shared_examples_for '.set_meta_tags' do # rubocop:disable Metrics/BlockLength
context 'with a Hash parameter' do
it 'updates meta tags' do
subject.set_meta_tags(title: 'hello')
Expand Down
4 changes: 2 additions & 2 deletions spec/support/view_helper_setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
end
end

RSpec.shared_examples "initialize a view for the view helpers", type: :view_helper do
RSpec.shared_context "with an initialized view", type: :view_helper do
subject do
ActionView::Base.new(ActionView::LookupContext.new([]))
MetaTagsRailsApp::MetaTagsView.new(ActionView::LookupContext.new([]))
end
end

0 comments on commit 038c3d3

Please sign in to comment.