Skip to content

Commit

Permalink
Refactor AnnotateRoutes.routes_file_exist? (ctran#716)
Browse files Browse the repository at this point in the history
I refactored `AnnotateRoutes.routes_exists?` and methods using this.
The points are as follows.

*   Removing `puts` in `AnnotateRoutes.routes_exists?`
*   Using `File.exist?` instead of `File.exists?` because `File.exists?` is deprecated
*   Renaming `AnnotateRoutes.routes_exists?` to `AnnotateRoutes.routes_file_exists?` in order to make the name of method more explanatory
  • Loading branch information
nard-tech authored and vfonic committed May 8, 2020
1 parent 851b0e7 commit d9eca25
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
37 changes: 19 additions & 18 deletions lib/annotate/annotate_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,34 @@ module AnnotateRoutes

class << self
def do_annotations(options = {})
return unless routes_exists?
existing_text = File.read(routes_file)

if rewrite_contents_with_header(existing_text, header(options), options)
puts "#{routes_file} annotated."
if routes_file_exist?
existing_text = File.read(routes_file)
if rewrite_contents_with_header(existing_text, header(options), options)
puts "#{routes_file} annotated."
end
else
puts "Can't find routes.rb"
end
end

def remove_annotations(_options={})
return unless routes_exists?
existing_text = File.read(routes_file)
content, header_position = strip_annotations(existing_text)
new_content = strip_on_removal(content, header_position)
new_text = new_content.join("\n")

if rewrite_contents(existing_text, new_text)
puts "Removed annotations from #{routes_file}."
if routes_file_exist?
existing_text = File.read(routes_file)
content, header_position = strip_annotations(existing_text)
new_content = strip_on_removal(content, header_position)
new_text = new_content.join("\n")
if rewrite_contents(existing_text, new_text)
puts "Removed annotations from #{routes_file}."
end
else
puts "Can't find routes.rb"
end
end

private

def routes_exists?
routes_exists = File.exists?(routes_file)
puts "Can't find routes.rb" unless routes_exists

routes_exists
def routes_file_exist?
File.exist?(routes_file)
end

def routes_file
Expand Down
12 changes: 6 additions & 6 deletions spec/lib/annotate/annotate_routes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def magic_comments_list_each
end

it 'should check if routes.rb exists' do
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(false)
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(false)
expect(AnnotateRoutes).to receive(:puts).with("Can't find routes.rb")
AnnotateRoutes.do_annotations
end
Expand All @@ -42,7 +42,7 @@ def magic_comments_list_each
end

before(:each) do
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true).at_least(:once)
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true).at_least(:once)

expect(File).to receive(:read).with(ROUTE_FILE).and_return("").at_least(:once)

Expand Down Expand Up @@ -164,7 +164,7 @@ def magic_comments_list_each

describe 'When adding' do
before(:each) do
expect(File).to receive(:exists?).with(ROUTE_FILE)
expect(File).to receive(:exist?).with(ROUTE_FILE)
.and_return(true).at_least(:once)
expect(AnnotateRoutes).to receive(:`).with('rake routes')
.and_return('').at_least(:once)
Expand Down Expand Up @@ -243,7 +243,7 @@ def magic_comments_list_each

describe 'When adding with older Rake versions' do
before(:each) do
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true)
expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return("(in /bad/line)\ngood line")
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
Expand All @@ -264,7 +264,7 @@ def magic_comments_list_each

describe 'When adding with newer Rake versions' do
before(:each) do
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true)
expect(AnnotateRoutes).to receive(:`).with('rake routes').and_return("another good line\ngood line")
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_ADDED)
Expand All @@ -291,7 +291,7 @@ def magic_comments_list_each

describe 'When removing' do
before(:each) do
expect(File).to receive(:exists?).with(ROUTE_FILE).and_return(true)
expect(File).to receive(:exist?).with(ROUTE_FILE).and_return(true)
expect(File).to receive(:open).with(ROUTE_FILE, 'wb').and_yield(mock_file)
expect(AnnotateRoutes).to receive(:puts).with(ANNOTATION_REMOVED)
end
Expand Down

0 comments on commit d9eca25

Please sign in to comment.