Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some rubocop issues. #859

Merged
merged 2 commits into from
Oct 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 1 addition & 53 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,63 +1,11 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2019-10-02 17:54:14 +0200 using RuboCop version 0.68.1.
# on 2019-10-09 14:16:04 +0200 using RuboCop version 0.68.1.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 9
# Cop supports --auto-correct.
# Configuration parameters: TreatCommentsAsGroupSeparators, Include.
# Include: **/*.gemfile, **/Gemfile, **/gems.rb
Bundler/OrderedGems:
Exclude:
- 'gemfiles/4.0.gemfile'
- 'gemfiles/4.1.gemfile'
- 'gemfiles/4.2.gemfile'
- 'gemfiles/5.0.gemfile'
- 'gemfiles/5.1.gemfile'
- 'gemfiles/5.2.gemfile'
- 'gemfiles/rails_edge.gemfile'

# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: TreatCommentsAsGroupSeparators, Include.
# Include: **/*.gemspec
Gemspec/OrderedDependencies:
Exclude:
- 'wicked_pdf.gemspec'

# Offense count: 16
# Cop supports --auto-correct.
Layout/EmptyLineAfterGuardClause:
Exclude:
- 'Rakefile'
- 'lib/wicked_pdf.rb'
- 'lib/wicked_pdf/pdf_helper.rb'
- 'lib/wicked_pdf/wicked_pdf_helper/assets.rb'

# Offense count: 2
# Cop supports --auto-correct.
Layout/LeadingBlankLines:
Exclude:
- 'test/unit/wkhtmltopdf_location_test.rb'
- 'wicked_pdf.gemspec'

# Offense count: 1
# Cop supports --auto-correct.
Layout/RescueEnsureAlignment:
Exclude:
- 'lib/wicked_pdf.rb'

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: final_newline, final_blank_line
Layout/TrailingBlankLines:
Exclude:
- 'gemfiles/4.0.gemfile'

# Offense count: 11
Metrics/AbcSize:
Max: 45
Expand Down
7 changes: 1 addition & 6 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ end

desc 'Run RuboCop'
task :rubocop do
next unless RUBY_VERSION >= '2.0.0'
require 'rubocop/rake_task'
RuboCop::RakeTask.new
end
Expand All @@ -34,11 +33,7 @@ desc 'Generate dummy application for test cases'
task :dummy_generate do
Rake::Task[:dummy_remove].invoke
puts 'Creating dummy application to run tests'
if Rails::VERSION::MAJOR > 2
system('rails new test/dummy --database=sqlite3')
else
system('rails test/dummy')
end
system('rails new test/dummy --database=sqlite3')
system('touch test/dummy/db/schema.rb')
FileUtils.cp 'test/fixtures/database.yml', 'test/dummy/config/'
FileUtils.rm_r Dir.glob('test/dummy/test/*')
Expand Down
4 changes: 2 additions & 2 deletions gemfiles/4.0.gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source "https://rubygems.org"

gem 'rdoc'
gem 'rails', '~> 4.0.0'
gem 'rdoc'

gemspec path: '../'
gemspec path: '../'
2 changes: 1 addition & 1 deletion gemfiles/4.1.gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'

gem 'rdoc'
gem 'rails', '~> 4.1.0'
gem 'rdoc'

gemspec path: '../'
2 changes: 1 addition & 1 deletion gemfiles/4.2.gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'

gem 'rdoc'
gem 'rails', '~> 4.2.0'
gem 'rdoc'

gemspec path: '../'
2 changes: 1 addition & 1 deletion gemfiles/5.0.gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'

gem 'rdoc'
gem 'rails', '~> 5.0.0'
gem 'rdoc'

gemspec path: '../'
2 changes: 1 addition & 1 deletion gemfiles/5.1.gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'

gem 'rdoc'
gem 'rails', '~> 5.1.0'
gem 'rdoc'

gemspec path: '../'
6 changes: 3 additions & 3 deletions gemfiles/5.2.gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
source 'https://rubygems.org'

gem 'rdoc'
gem 'rails', '~> 5.2'
gem 'sqlite3', '~> 1.3.6'
gem 'bootsnap' # required to run `rake test` in Rails 5.2
gem 'mocha', '= 1.3' # newer versions blow up
gem 'rails', '~> 5.2'
gem 'rdoc'
gem 'sqlite3', '~> 1.3.6'

gemspec path: '../'
6 changes: 3 additions & 3 deletions gemfiles/rails_edge.gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
source 'https://rubygems.org'

gem 'rdoc'
gem 'rails', git: 'https://github.com/rails/rails.git'
gem 'sqlite3', '~> 1.3.6'
gem 'bootsnap' # required to run `rake test` in Rails 6.0
gem 'mocha', '= 1.3' # newer versions blow up
gem 'rails', git: 'https://github.com/rails/rails.git'
gem 'rdoc'
gem 'sqlite3', '~> 1.3.6'

gemspec path: '../'
18 changes: 13 additions & 5 deletions lib/wicked_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def pdf_from_url(url, options = {})
pdf = generated_pdf_file.read
raise "Error generating PDF\n Command Error: #{err}" if options[:raise_on_all_errors] && !err.empty?
raise "PDF could not be generated!\n Command Error: #{err}" if pdf && pdf.rstrip.empty?

pdf
rescue StandardError => e
raise "Failed to execute:\n#{command}\nError: #{e}"
Expand All @@ -90,6 +91,7 @@ def pdf_from_url(url, options = {})

def in_development_mode?
return Rails.env == 'development' if defined?(Rails.env)

RAILS_ENV == 'development' if defined?(RAILS_ENV)
end

Expand Down Expand Up @@ -135,6 +137,7 @@ def parse_options(options)
def parse_extra(options)
return [] if options[:extra].nil?
return options[:extra].split if options[:extra].respond_to?(:split)

options[:extra]
end

Expand All @@ -151,6 +154,7 @@ def make_option(name, value, type = :string)
if value.is_a?(Array)
return value.collect { |v| make_option(name, v, type) }
end

if type == :name_value
parts = value.to_s.split(' ')
["--#{name.tr('_', '-')}", *parts]
Expand All @@ -175,6 +179,7 @@ def valid_option(name)

def make_options(options, names, prefix = '', type = :string)
return [] if options.nil?

names.collect do |o|
if options[o].blank?
[]
Expand All @@ -191,6 +196,7 @@ def parse_header_footer(options)
unless options.blank?
[:header, :footer].collect do |hf|
next if options[hf].blank?

opt_hf = options[hf]
r += make_options(opt_hf, [:center, :font_name, :left, :right], hf.to_s)
r += make_options(opt_hf, [:font_size, :spacing], hf.to_s, :numeric)
Expand All @@ -214,6 +220,7 @@ def parse_header_footer(options)
def parse_cover(argument)
arg = argument.to_s
return [] if arg.blank?

# Filesystem path or URL - hand off to wkhtmltopdf
if argument.is_a?(Pathname) || (arg[0, 4] == 'http')
[valid_option('cover'), arg]
Expand All @@ -228,6 +235,7 @@ def parse_cover(argument)

def parse_toc(options)
return [] if options.nil?

r = [valid_option('toc')]
unless options.blank?
r += make_options(options, [:font_name, :header_text], 'toc')
Expand Down Expand Up @@ -331,11 +339,11 @@ def find_wkhtmltopdf_binary_path
possible_locations += %w[~/bin] if ENV.key?('HOME')
exe_path ||= WickedPdf.config[:exe_path] unless WickedPdf.config.empty?
exe_path ||= begin
detected_path = (defined?(Bundler) ? Bundler.which('wkhtmltopdf') : `which wkhtmltopdf`).chomp
detected_path.present? && detected_path
rescue StandardError
nil
end
detected_path = (defined?(Bundler) ? Bundler.which('wkhtmltopdf') : `which wkhtmltopdf`).chomp
detected_path.present? && detected_path
rescue StandardError
nil
end
exe_path ||= possible_locations.map { |l| File.expand_path("#{l}/#{EXE_NAME}") }.find { |location| File.exist?(location) }
exe_path || ''
end
Expand Down
3 changes: 3 additions & 0 deletions lib/wicked_pdf/pdf_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ def render_to_string_with_wicked_pdf(options = nil, *args, &block)
def set_basic_auth(options = {})
options[:basic_auth] ||= WickedPdf.config.fetch(:basic_auth) { false }
return unless options[:basic_auth] && request.env['HTTP_AUTHORIZATION']

request.env['HTTP_AUTHORIZATION'].split(' ').last
end

def clean_temp_files
return unless defined?(@hf_tempfiles)

@hf_tempfiles.each(&:close!)
end

Expand Down Expand Up @@ -121,6 +123,7 @@ def make_and_send_pdf(pdf_name, options = {})
def prerender_header_and_footer(options)
[:header, :footer].each do |hf|
next unless options[hf] && options[hf][:html] && options[hf][:html][:template]

@hf_tempfiles = [] unless defined?(@hf_tempfiles)
@hf_tempfiles.push(tf = WickedPdfTempfile.new("wicked_#{hf}_pdf.html"))
options[hf][:html][:layout] ||= options[:layout]
Expand Down
4 changes: 4 additions & 0 deletions lib/wicked_pdf/wicked_pdf_helper/assets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Assets
def wicked_pdf_asset_base64(path)
asset = find_asset(path)
raise "Could not find asset '#{path}'" if asset.nil?

base64 = Base64.encode64(asset.to_s).gsub(/\s+/, '')
"data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
end
Expand All @@ -31,6 +32,7 @@ def wicked_pdf_stylesheet_link_tag(*sources)

def wicked_pdf_stylesheet_pack_tag(*sources)
return unless defined?(Webpacker)

if running_in_development?
stylesheet_pack_tag(*sources)
else
Expand Down Expand Up @@ -162,6 +164,7 @@ def gzip(asset)

def webpacker_source_url(source)
return unless defined?(Webpacker) && defined?(Webpacker::VERSION)

# In Webpacker 3.2.0 asset_pack_url is introduced
if Webpacker::VERSION >= '3.2.0'
asset_pack_url(source)
Expand All @@ -174,6 +177,7 @@ def webpacker_source_url(source)

def running_in_development?
return unless defined?(Webpacker)

# :dev_server method was added in webpacker 3.0.0
if Webpacker.respond_to?(:dev_server)
Webpacker.dev_server.running?
Expand Down
2 changes: 0 additions & 2 deletions test/unit/wkhtmltopdf_location_test.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


class WkhtmltopdfLocationTest < ActiveSupport::TestCase
setup do
@saved_config = WickedPdf.config
Expand Down
5 changes: 2 additions & 3 deletions wicked_pdf.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'wicked_pdf/version'
Expand Down Expand Up @@ -32,11 +31,11 @@ DESC

spec.add_dependency 'activesupport'

spec.add_development_dependency 'rails'
spec.add_development_dependency 'bundler', RUBY_VERSION >= '2.5' ? '~> 2' : '~> 1.3'
spec.add_development_dependency 'mocha', '= 1.3'
spec.add_development_dependency 'rails'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'rubocop', '~> 0.68.0'
spec.add_development_dependency 'sqlite3', '~> 1.3.6'
spec.add_development_dependency 'mocha', '= 1.3'
spec.add_development_dependency 'test-unit'
end