Unverified Commit c710caac by Andrew W. Lee Committed by GitHub

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.
parent 6de3ed80
...@@ -4,6 +4,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__)) ...@@ -4,6 +4,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
require 'annotate/version' require 'annotate/version'
require 'annotate/annotate_models' require 'annotate/annotate_models'
require 'annotate/annotate_routes' require 'annotate/annotate_routes'
require 'annotate/constants'
begin begin
# ActiveSupport 3.x... # ActiveSupport 3.x...
...@@ -16,8 +17,6 @@ rescue StandardError ...@@ -16,8 +17,6 @@ rescue StandardError
end end
module Annotate module Annotate
TRUE_RE = /^(true|t|yes|y|1)$/i
## ##
# The set of available options to customize the behavior of Annotate. # The set of available options to customize the behavior of Annotate.
# #
...@@ -107,15 +106,15 @@ module Annotate ...@@ -107,15 +106,15 @@ module Annotate
end end
def self.skip_on_migration? def self.skip_on_migration?
ENV['ANNOTATE_SKIP_ON_DB_MIGRATE'] =~ TRUE_RE || ENV['skip_on_db_migrate'] =~ TRUE_RE ENV['ANNOTATE_SKIP_ON_DB_MIGRATE'] =~ Constants::TRUE_RE || ENV['skip_on_db_migrate'] =~ Constants::TRUE_RE
end end
def self.include_routes? def self.include_routes?
ENV['routes'] =~ TRUE_RE ENV['routes'] =~ Constants::TRUE_RE
end end
def self.include_models? def self.include_models?
ENV['models'] =~ TRUE_RE ENV['models'] =~ Constants::TRUE_RE
end end
def self.loaded_tasks=(val) def self.loaded_tasks=(val)
...@@ -199,7 +198,7 @@ module Annotate ...@@ -199,7 +198,7 @@ module Annotate
def self.true?(val) def self.true?(val)
return false if val.blank? return false if val.blank?
return false unless val =~ TRUE_RE return false unless val =~ Constants::TRUE_RE
true true
end end
end end
...@@ -2,9 +2,9 @@ ...@@ -2,9 +2,9 @@
require 'bigdecimal' require 'bigdecimal'
module AnnotateModels require 'annotate/constants'
TRUE_RE = /^(true|t|yes|y|1)$/i
module AnnotateModels
# Annotate Models plugin use this header # Annotate Models plugin use this header
COMPAT_PREFIX = '== Schema Info'.freeze COMPAT_PREFIX = '== Schema Info'.freeze
COMPAT_PREFIX_MD = '## Schema Info'.freeze COMPAT_PREFIX_MD = '## Schema Info'.freeze
...@@ -590,7 +590,7 @@ module AnnotateModels ...@@ -590,7 +590,7 @@ module AnnotateModels
def matched_types(options) def matched_types(options)
types = MATCHED_TYPES.dup types = MATCHED_TYPES.dup
types << 'admin' if options[:active_admin] =~ TRUE_RE && !types.include?('admin') types << 'admin' if options[:active_admin] =~ Annotate::Constants::TRUE_RE && !types.include?('admin')
types << 'additional_file_patterns' if options[:additional_file_patterns].present? types << 'additional_file_patterns' if options[:additional_file_patterns].present?
types types
......
module Annotate
module Constants
TRUE_RE = /^(true|t|yes|y|1)$/i.freeze
end
end
require_relative '../spec_helper' require_relative '../spec_helper'
describe Annotate do describe Annotate do
it 'should have a version' do describe '.version' do
expect(Annotate.version).to be_instance_of(String) it 'has version' do
expect(Annotate.version).to be_instance_of(String)
end
end
describe '.skip_on_migration?' do
it "checks ENV for 'ANNOTATE_SKIP_ON_DB_MIGRATE' or 'skip_on_db_migrate'" do
expect(ENV).to receive(:[]).twice
described_class.skip_on_migration?
end
end
describe '.include_routes?' do
it "checks ENV with 'routes'" do
expect(ENV).to receive(:[]).with('routes')
described_class.include_routes?
end
end
describe '.include_models?' do
it "checks ENV with 'models'" do
expect(ENV).to receive(:[]).with('models')
described_class.include_models?
end
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment