Skip to content
This repository has been archived by the owner on Mar 12, 2024. It is now read-only.

Commit

Permalink
Simplify render_as_pdf?
Browse files Browse the repository at this point in the history
  • Loading branch information
sigmavirus24 committed Aug 10, 2014
1 parent 8392f77 commit 2b75aa6
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions lib/pdfkit/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,16 @@ def rendering_pdf?
end

def render_as_pdf?
request_path_is_pdf = @request.path.match(%r{\.pdf$})
request_path = @request.path
request_path_is_pdf = request_path.match(%r{\.pdf$})

if request_path_is_pdf && @conditions[:only]
rules = [@conditions[:only]].flatten
rules.any? do |pattern|
if pattern.is_a?(Regexp)
@request.path =~ pattern
else
@request.path[0, pattern.length] == pattern
end
conditions_as_regexp(@conditions[:only]).any? do |pattern|
request_path =~ pattern
end
elsif request_path_is_pdf && @conditions[:except]
rules = [@conditions[:except]].flatten
rules.map do |pattern|
if pattern.is_a?(Regexp)
return false if @request.path =~ pattern
else
return false if @request.path[0, pattern.length] == pattern
end
conditions_as_regexp(@conditions[:except]).each do |pattern|
return false if request_path =~ pattern
end

return true
Expand All @@ -99,5 +90,10 @@ def concat(accepts, type)
(accepts || '').split(',').unshift(type).compact.join(',')
end

def conditions_as_regexp(conditions)
[conditions].flatten.map do |pattern|
pattern.is_a?(Regexp) ? pattern : Regexp.new('^' + pattern)
end
end
end
end

0 comments on commit 2b75aa6

Please sign in to comment.