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__))
require 'annotate/version'
require 'annotate/annotate_models'
require 'annotate/annotate_routes'
require 'annotate/constants'
begin
# ActiveSupport 3.x...
......@@ -16,8 +17,6 @@ rescue StandardError
end
module Annotate
TRUE_RE = /^(true|t|yes|y|1)$/i
##
# The set of available options to customize the behavior of Annotate.
#
......@@ -107,15 +106,15 @@ module Annotate
end
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
def self.include_routes?
ENV['routes'] =~ TRUE_RE
ENV['routes'] =~ Constants::TRUE_RE
end
def self.include_models?
ENV['models'] =~ TRUE_RE
ENV['models'] =~ Constants::TRUE_RE
end
def self.loaded_tasks=(val)
......@@ -199,7 +198,7 @@ module Annotate
def self.true?(val)
return false if val.blank?
return false unless val =~ TRUE_RE
return false unless val =~ Constants::TRUE_RE
true
end
end
......@@ -2,9 +2,9 @@
require 'bigdecimal'
module AnnotateModels
TRUE_RE = /^(true|t|yes|y|1)$/i
require 'annotate/constants'
module AnnotateModels
# Annotate Models plugin use this header
COMPAT_PREFIX = '== Schema Info'.freeze
COMPAT_PREFIX_MD = '## Schema Info'.freeze
......@@ -590,7 +590,7 @@ module AnnotateModels
def matched_types(options)
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
......
module Annotate
module Constants
TRUE_RE = /^(true|t|yes|y|1)$/i.freeze
end
end
require_relative '../spec_helper'
describe Annotate do
it 'should have a version' do
describe '.version' do
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
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