Skip to content

Commit c710caa

Browse files
authored
Refactor and tidy lib/annotate.rb (#653)
Adds tests for `.include_routes?`, `.include_models?`, `.skip_on_migration?`. Also moves the `TRUE_RE` under the `Annotate::Constants` namespace.
1 parent 6de3ed8 commit c710caa

File tree

4 files changed

+38
-11
lines changed

4 files changed

+38
-11
lines changed

lib/annotate.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
require 'annotate/version'
55
require 'annotate/annotate_models'
66
require 'annotate/annotate_routes'
7+
require 'annotate/constants'
78

89
begin
910
# ActiveSupport 3.x...
@@ -16,8 +17,6 @@
1617
end
1718

1819
module Annotate
19-
TRUE_RE = /^(true|t|yes|y|1)$/i
20-
2120
##
2221
# The set of available options to customize the behavior of Annotate.
2322
#
@@ -107,15 +106,15 @@ def self.reset_options
107106
end
108107

109108
def self.skip_on_migration?
110-
ENV['ANNOTATE_SKIP_ON_DB_MIGRATE'] =~ TRUE_RE || ENV['skip_on_db_migrate'] =~ TRUE_RE
109+
ENV['ANNOTATE_SKIP_ON_DB_MIGRATE'] =~ Constants::TRUE_RE || ENV['skip_on_db_migrate'] =~ Constants::TRUE_RE
111110
end
112111

113112
def self.include_routes?
114-
ENV['routes'] =~ TRUE_RE
113+
ENV['routes'] =~ Constants::TRUE_RE
115114
end
116115

117116
def self.include_models?
118-
ENV['models'] =~ TRUE_RE
117+
ENV['models'] =~ Constants::TRUE_RE
119118
end
120119

121120
def self.loaded_tasks=(val)
@@ -199,7 +198,7 @@ def self.fallback(*args)
199198

200199
def self.true?(val)
201200
return false if val.blank?
202-
return false unless val =~ TRUE_RE
201+
return false unless val =~ Constants::TRUE_RE
203202
true
204203
end
205204
end

lib/annotate/annotate_models.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
require 'bigdecimal'
44

5-
module AnnotateModels
6-
TRUE_RE = /^(true|t|yes|y|1)$/i
5+
require 'annotate/constants'
76

7+
module AnnotateModels
88
# Annotate Models plugin use this header
99
COMPAT_PREFIX = '== Schema Info'.freeze
1010
COMPAT_PREFIX_MD = '## Schema Info'.freeze
@@ -590,7 +590,7 @@ def remove_annotation_of_file(file_name, options = {})
590590

591591
def matched_types(options)
592592
types = MATCHED_TYPES.dup
593-
types << 'admin' if options[:active_admin] =~ TRUE_RE && !types.include?('admin')
593+
types << 'admin' if options[:active_admin] =~ Annotate::Constants::TRUE_RE && !types.include?('admin')
594594
types << 'additional_file_patterns' if options[:additional_file_patterns].present?
595595

596596
types

lib/annotate/constants.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Annotate
2+
module Constants
3+
TRUE_RE = /^(true|t|yes|y|1)$/i.freeze
4+
end
5+
end

spec/lib/annotate_spec.rb

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,30 @@
11
require_relative '../spec_helper'
22

33
describe Annotate do
4-
it 'should have a version' do
5-
expect(Annotate.version).to be_instance_of(String)
4+
describe '.version' do
5+
it 'has version' do
6+
expect(Annotate.version).to be_instance_of(String)
7+
end
8+
end
9+
10+
describe '.skip_on_migration?' do
11+
it "checks ENV for 'ANNOTATE_SKIP_ON_DB_MIGRATE' or 'skip_on_db_migrate'" do
12+
expect(ENV).to receive(:[]).twice
13+
described_class.skip_on_migration?
14+
end
15+
end
16+
17+
describe '.include_routes?' do
18+
it "checks ENV with 'routes'" do
19+
expect(ENV).to receive(:[]).with('routes')
20+
described_class.include_routes?
21+
end
22+
end
23+
24+
describe '.include_models?' do
25+
it "checks ENV with 'models'" do
26+
expect(ENV).to receive(:[]).with('models')
27+
described_class.include_models?
28+
end
629
end
730
end

0 commit comments

Comments
 (0)