Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #312 from qnm/check-ar-base-defined-before-use
Browse files Browse the repository at this point in the history
Verify that ActiveRecord::Base is defined when checking for support
  • Loading branch information
paulelliott committed May 2, 2019
2 parents 909ce8a + b2b5d0d commit 6d5e6ed
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/fabrication/generator/active_record.rb
@@ -1,7 +1,11 @@
class Fabrication::Generator::ActiveRecord < Fabrication::Generator::Base

def self.supports?(klass)
defined?(ActiveRecord) && klass.ancestors.include?(ActiveRecord::Base)
# Some gems will declare an ActiveRecord module for their own purposes
# so we can't assume because we have the ActiveRecord module that we also
# have ActiveRecord::Base. Because defined? can return nil we ensure that nil
# becomes false.
defined?(ActiveRecord) && defined?(ActiveRecord::Base) && klass.ancestors.include?(ActiveRecord::Base) || false
end

def build_instance
Expand Down
17 changes: 17 additions & 0 deletions spec/fabrication/generator/active_record_spec.rb
@@ -1,5 +1,22 @@
require 'spec_helper'

describe Fabrication::Generator::ActiveRecord do
describe ".supports?" do
subject { Fabrication::Generator::ActiveRecord }

# Defines a fakey ActiveRecord module that doesn't also have
# ActiveRecord::Base such as those written by instrumentation
# platforms e.g. Honeycomb
module ActiveRecord; end

let(:active_record_fake) { ActiveRecord }

it "returns false for active record objects without ar::base" do
expect(subject.supports?(active_record_fake)).to be false
end
end
end

describe Fabrication::Generator::ActiveRecord, depends_on: :active_record do

describe ".supports?" do
Expand Down

0 comments on commit 6d5e6ed

Please sign in to comment.