Skip to content

Refactor and tidy lib/annotate.rb #653

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions lib/annotate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'annotate/version'
require 'annotate/annotate_models'
require 'annotate/annotate_routes'
require 'annotate/constants'

begin
# ActiveSupport 3.x...
Expand All @@ -16,8 +17,6 @@
end

module Annotate
TRUE_RE = /^(true|t|yes|y|1)$/i

##
# The set of available options to customize the behavior of Annotate.
#
Expand Down Expand Up @@ -107,15 +106,15 @@ def self.reset_options
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)
Expand Down Expand Up @@ -199,7 +198,7 @@ def self.fallback(*args)

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
6 changes: 3 additions & 3 deletions lib/annotate/annotate_models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -590,7 +590,7 @@ def remove_annotation_of_file(file_name, options = {})

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
Expand Down
5 changes: 5 additions & 0 deletions lib/annotate/constants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Annotate
module Constants
TRUE_RE = /^(true|t|yes|y|1)$/i.freeze
end
end
27 changes: 25 additions & 2 deletions spec/lib/annotate_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,30 @@
require_relative '../spec_helper'

describe Annotate do
it 'should have a version' do
expect(Annotate.version).to be_instance_of(String)
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