diff --git a/features/step_definitions/factory_steps.rb b/features/step_definitions/factory_steps.rb index 6484068f820..8e29104989f 100644 --- a/features/step_definitions/factory_steps.rb +++ b/features/step_definitions/factory_steps.rb @@ -10,9 +10,9 @@ def create_user(name, type = 'User') category = Category.where(name: category_name).first_or_create if category_name title ||= "Hello World %i" count.times do |i| - if defined?(ActiveRecord) + if defined?(::ActiveRecord) Post.create! title: title % i, body: body, author: author, published_at: published, custom_category_id: category.try(:id) - elsif defined?(Mongoid) + elsif defined?(::Mongoid) Post.create! title: title % i, body: body, author: author, published_at: published, category_id: category.try(:id) end end diff --git a/features/support/env.rb b/features/support/env.rb index ad47de5c5a0..e81d368fdf7 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -8,11 +8,19 @@ require File.expand_path('../../../spec/spec_helper', __FILE__) -ENV['RAILS_ROOT'] = File.expand_path("../../../spec/rails/rails-#{ENV["RAILS"]}", __FILE__) +require 'rails' +require 'active_record' unless ENV['MONGOID'] +require 'mongoid' if ENV['MONGOID'] + +orm_name = defined?(::ActiveRecord) ? 'activerecord' : 'mongoid' +orm_version = defined?(::ActiveRecord) ? ::ActiveRecord.version : ::Mongoid::VERSION +ENV['RAILS_ROOT'] = File.expand_path("../../../spec/rails/rails-#{Rails::VERSION::STRING}-#{orm_name}-#{orm_version}", __FILE__) # Create the test app if it doesn't exists unless File.exists?(ENV['RAILS_ROOT']) - system 'rake setup' + require 'rake' + load File.expand_path( "../../tasks/test.rake", __FILE__) + Rake::Task["setup"].invoke end require 'rails' @@ -85,7 +93,7 @@ Cucumber::Rails::World.use_transactional_fixtures = false unless ENV['MONGOID'] # How to clean your database when transactions are turned off. See # http://github.com/bmabey/database_cleaner for more info. -if defined?(ActiveRecord::Base) +if defined?(::ActiveRecord::Base) begin require 'database_cleaner' require 'database_cleaner/cucumber' @@ -94,8 +102,8 @@ end end -if defined?(Mongoid) - Mongoid.default_session.drop +if defined?(::Mongoid) + ::Mongoid.default_session.drop end # Warden helpers to speed up login diff --git a/lib/active_admin.rb b/lib/active_admin.rb index 6ee8243e30c..92a5019ab55 100644 --- a/lib/active_admin.rb +++ b/lib/active_admin.rb @@ -128,5 +128,5 @@ def after_load(&block) require 'active_admin/filters' # Require ORM-specific plugins -require 'active_admin/object_mapper/active_record' if defined? ActiveRecord -require 'active_admin/object_mapper/mongoid' if defined? Mongoid +require 'active_admin/object_mapper/active_record' if defined? ::ActiveRecord +require 'active_admin/object_mapper/mongoid' if defined? ::Mongoid diff --git a/spec/unit/belongs_to_spec.rb b/spec/unit/belongs_to_spec.rb index 0366b6ed3c1..c6d903bf17c 100644 --- a/spec/unit/belongs_to_spec.rb +++ b/spec/unit/belongs_to_spec.rb @@ -42,11 +42,11 @@ allow(controller).to receive(:request){ request } end it 'should be able to access the collection' do - if defined?(ActiveRecord) - expect(controller.send :collection).to be_a ActiveRecord::Relation + if defined?(::ActiveRecord) + expect(controller.send :collection).to be_a ::ActiveRecord::Relation end - if defined?(Mongoid) - expect(controller.send :collection).to be_a Mongoid::Criteria + if defined?(::Mongoid) + expect(controller.send :collection).to be_a ::Mongoid::Criteria end end it 'should be able to build a new resource' do diff --git a/spec/unit/comments_spec.rb b/spec/unit/comments_spec.rb index d9ce01c7d80..d20d067bc96 100644 --- a/spec/unit/comments_spec.rb +++ b/spec/unit/comments_spec.rb @@ -1,5 +1,5 @@ require 'rails_helper' -defined?(ActiveRecord) and +defined?(::ActiveRecord) and describe "Comments" do let(:application) { ActiveAdmin::Application.new } diff --git a/spec/unit/filters/filter_form_builder_spec.rb b/spec/unit/filters/filter_form_builder_spec.rb index 2497d2504b6..380140d5ce8 100644 --- a/spec/unit/filters/filter_form_builder_spec.rb +++ b/spec/unit/filters/filter_form_builder_spec.rb @@ -27,7 +27,7 @@ def view.a_helper_method view end - let(:i18n_namespace) { defined?(ActiveRecord) ? :activerecord : :mongoid } + let(:i18n_namespace) { defined?(::ActiveRecord) ? :activerecord : :mongoid } def render_filter(search, filters) render_arbre_component({filter_args: [search, filters]}, helpers) do @@ -143,7 +143,7 @@ def filter(name, options = {}) body end - if defined?(ActiveRecord) + if defined?(::ActiveRecord) it "should remove original ordering to prevent PostgreSQL error" do expect(scope.object.klass).to receive(:reorder).with('title asc') { m = double uniq: double(pluck: ['A Title']) @@ -239,14 +239,14 @@ def filter(name, options = {}) context "when given as the _id attribute name" do let(:body) { Capybara.string(filter :author_id) } - if defined?(ActiveRecord) + if defined?(::ActiveRecord) it "should generate a numeric filter" do expect(body).to have_selector("label", text: "Author") # really this should be Author ID :/) expect(body).to have_selector("option[value=author_id_less_than]") expect(body).to have_selector("input#q_author_id[name='q[author_id_equals]']") end end - if defined?(Mongoid) + if defined?(::Mongoid) it "should generate a select filter" do expect(body).to have_selector("label", text: "Author") expect(body).to have_selector("select#q_author_id[name='q[author_id_eq]']") @@ -297,11 +297,11 @@ def filter(name, options = {}) context "when polymorphic relationship" do let(:scope) { ActiveAdmin::Comment.search } it "should raise an error if a collection isn't provided" do - if defined?(ActiveRecord) + if defined?(::ActiveRecord) expect { filter :resource }.to raise_error \ Formtastic::PolymorphicInputWithoutCollectionError end - if defined?(Mongoid) + if defined?(::Mongoid) skip "not added yet" end end @@ -311,11 +311,11 @@ def filter(name, options = {}) let(:scope) { Post.search } let(:body) { Capybara.string(filter :category) } it "should ignore that foreign key and let Ransack handle it" do - if defined?(ActiveRecord) + if defined?(::ActiveRecord) expect(Post.reflect_on_association(:category).foreign_key).to eq :custom_category_id expect(body).to have_selector("select[name='q[category_id_eq]']") end - defined?(Mongoid) and skip "not added yet" + defined?(::Mongoid) and skip "not added yet" end end end # belongs to @@ -352,23 +352,23 @@ def view.a_helper_method let(:body) { Capybara.string(filter :authors) } it "should generate a select" do - defined?(ActiveRecord) and + defined?(::ActiveRecord) and expect(body).to have_selector("select[name='q[posts_author_id_eq]']") - defined?(Mongoid) and skip "not added yet" + defined?(::Mongoid) and skip "not added yet" end it "should set the default text to 'Any'" do - defined?(ActiveRecord) and + defined?(::ActiveRecord) and expect(body).to have_selector("option[value='']", text: "Any") - defined?(Mongoid) and skip "not added yet" + defined?(::Mongoid) and skip "not added yet" end it "should create an option for each related object" do - if defined?(ActiveRecord) + if defined?(::ActiveRecord) expect(body).to have_selector("option[value='#{john.id}']", text: "John Doe") expect(body).to have_selector("option[value='#{jane.id}']", text: "Jane Doe") end - defined?(Mongoid) and skip "not added yet" + defined?(::Mongoid) and skip "not added yet" end end @@ -376,11 +376,11 @@ def view.a_helper_method let(:body) { Capybara.string(filter :authors, as: :check_boxes) } it "should create a check box for each related object" do - if defined?(ActiveRecord) + if defined?(::ActiveRecord) expect(body).to have_selector("input[name='q[posts_author_id_in][]'][type=checkbox][value='#{john.id}']") expect(body).to have_selector("input[name='q[posts_author_id_in][]'][type=checkbox][value='#{jane.id}']") end - defined?(Mongoid) and skip "not added yet" + defined?(::Mongoid) and skip "not added yet" end end end diff --git a/spec/unit/filters/resource_spec.rb b/spec/unit/filters/resource_spec.rb index 8832ce6659b..b7d7a6c0739 100644 --- a/spec/unit/filters/resource_spec.rb +++ b/spec/unit/filters/resource_spec.rb @@ -18,10 +18,10 @@ class Post end it "should return the defaults if no filters are set" do - ary = if defined?(ActiveRecord) + ary = if defined?(::ActiveRecord) [ :author, :body, :category, :created_at, :custom_searcher, :position, :published_at, :starred, :taggings, :title, :updated_at ] - elsif defined?(Mongoid) + elsif defined?(::Mongoid) [ :author, :body, :category, :created_at, :custom_searcher, :foo_id, :position, :published_at, :starred, :title, :updated_at ] end @@ -107,10 +107,10 @@ class Post resource.preserve_default_filters! resource.add_filter :count, as: :string - ary = if defined?(ActiveRecord) + ary = if defined?(::ActiveRecord) [ :author, :body, :category, :count, :created_at, :custom_searcher, :position, :published_at, :starred, :taggings, :title, :updated_at ] - elsif defined?(Mongoid) + elsif defined?(::Mongoid) [ :author, :body, :category, :count, :created_at, :custom_searcher, :foo_id, :position, :published_at, :starred, :title, :updated_at ] end diff --git a/spec/unit/form_builder_spec.rb b/spec/unit/form_builder_spec.rb index 58005414eca..d56f7d12331 100644 --- a/spec/unit/form_builder_spec.rb +++ b/spec/unit/form_builder_spec.rb @@ -90,10 +90,10 @@ def build_form(options = {}, form_object = Post.new, &block) expect(body).to have_selector("input[type=text][name='post[title]']") end it "should generate a textarea" do - if defined?(ActiveRecord) + if defined?(::ActiveRecord) expect(body).to have_selector("textarea[name='post[body]']") end - if defined?(Mongoid) + if defined?(::Mongoid) expect(body).to have_selector("input[type=text][name='post[body]']") end end @@ -224,10 +224,10 @@ def build_form(options = {}, form_object = Post.new, &block) expect(body).to have_selector("input[type=text][name='post[title]']") end it "should have a body textarea" do - if defined?(ActiveRecord) + if defined?(::ActiveRecord) expect(body).to have_selector("textarea[name='post[body]']") end - if defined?(Mongoid) + if defined?(::Mongoid) expect(body).to have_selector("input[type=text][name='post[body]']") end end @@ -353,16 +353,16 @@ def build_form(options = {}, form_object = Post.new, &block) end f.inputs do f.input :author - f.input :published_at if defined?(ActiveRecord) + f.input :published_at if defined?(::ActiveRecord) # formtastic doesn't generate datetime_select for DateTime field - f.input :published_at, as: :datetime_select if defined?(Mongoid) + f.input :published_at, as: :datetime_select if defined?(::Mongoid) end end end it "should render four inputs" do expect(body).to have_selector("input[name='post[title]']", count: 1) - expect(body).to have_selector("textarea[name='post[body]']", count: 1) if defined?(ActiveRecord) - expect(body).to have_selector("input[type=text][name='post[body]']", count: 1) if defined?(Mongoid) + expect(body).to have_selector("textarea[name='post[body]']", count: 1) if defined?(::ActiveRecord) + expect(body).to have_selector("input[type=text][name='post[body]']", count: 1) if defined?(::Mongoid) expect(body).to have_selector("select[name='post[author_id]']", count: 1) expect(body).to have_selector("select[name='post[published_at(1i)]']", count: 1) expect(body).to have_selector("select[name='post[published_at(2i)]']", count: 1) @@ -384,7 +384,7 @@ def build_form(options = {}, form_object = Post.new, &block) end end - let(:i18n_namespace) { defined?(ActiveRecord) ? :activerecord : :mongoid } + let(:i18n_namespace) { defined?(::ActiveRecord) ? :activerecord : :mongoid } let(:valid_html_id) { /^[A-Za-z]+[\w\-\:\.]*$/ } it "should translate the association name in header" do @@ -415,8 +415,8 @@ def build_form(options = {}, form_object = Post.new, &block) it "should render the nested form" do expect(body).to have_selector("input[name='category[posts_attributes][0][title]']") - expect(body).to have_selector("textarea[name='category[posts_attributes][0][body]']") if defined?(ActiveRecord) - expect(body).to have_selector("input[name='category[posts_attributes][0][body]']") if defined?(Mongoid) + expect(body).to have_selector("textarea[name='category[posts_attributes][0][body]']") if defined?(::ActiveRecord) + expect(body).to have_selector("input[name='category[posts_attributes][0][body]']") if defined?(::Mongoid) end it "should add a link to remove new nested records" do diff --git a/spec/unit/helpers/collection_spec.rb b/spec/unit/helpers/collection_spec.rb index 6257d4ce9d1..c0fb999029a 100644 --- a/spec/unit/helpers/collection_spec.rb +++ b/spec/unit/helpers/collection_spec.rb @@ -25,19 +25,19 @@ end it "should return the collection size for a collection with group by" do - if defined?(ActiveRecord) + if defined?(::ActiveRecord) expect(collection_size(Post.group(:title))).to eq 2 end - if defined?(Mongoid) + if defined?(::Mongoid) skip "not implemented yet." end end it "should return the collection size for a collection with group by, select and custom order" do - if defined?(ActiveRecord) + if defined?(::ActiveRecord) expect(collection_size(Post.select("title, count(*) as nb_posts").group(:title).order("nb_posts"))).to eq 2 end - if defined?(Mongoid) + if defined?(::Mongoid) skip "not implemented yet." end end diff --git a/spec/unit/order_clause_spec.rb b/spec/unit/order_clause_spec.rb index 05361d78f48..9eec241e084 100644 --- a/spec/unit/order_clause_spec.rb +++ b/spec/unit/order_clause_spec.rb @@ -23,10 +23,10 @@ end specify '#to_sql prepends table name' do - if defined?(ActiveRecord) + if defined?(::ActiveRecord) expect(subject.to_sql(config)).to eq '"posts"."id" asc' end - if defined?(Mongoid) + if defined?(::Mongoid) expect(subject.to_sql(config)).to eq '"id" asc' end end diff --git a/spec/unit/resource/naming_spec.rb b/spec/unit/resource/naming_spec.rb index b706c043881..d171c5ac1c3 100644 --- a/spec/unit/resource/naming_spec.rb +++ b/spec/unit/resource/naming_spec.rb @@ -14,8 +14,8 @@ def config(options = {}) @config ||= Resource.new(namespace, Category, options) end - module ::Mock class Resource < ActiveRecord::Base; end; end if defined?(ActiveRecord) - module ::Mock class Resource; include Mongoid::Document; end; end if defined?(Mongoid) + module ::Mock class Resource < ::ActiveRecord::Base; end; end if defined?(::ActiveRecord) + module ::Mock class Resource; include ::Mongoid::Document; end; end if defined?(::Mongoid) module NoActiveModel class Resource; end; end describe "singular resource name" do diff --git a/spec/unit/resource_controller/data_access_spec.rb b/spec/unit/resource_controller/data_access_spec.rb index 0196663dc40..c84daf6fd6c 100644 --- a/spec/unit/resource_controller/data_access_spec.rb +++ b/spec/unit/resource_controller/data_access_spec.rb @@ -29,10 +29,10 @@ it "reorders chain" do chain = double "ChainObj" - if defined?(ActiveRecord) + if defined?(::ActiveRecord) expect(chain).to receive(:reorder).with('"posts"."id" asc').once.and_return(Post.search) end - if defined?(Mongoid) + if defined?(::Mongoid) expect(chain).to receive(:reorder).with('"id" asc').once.and_return(Post.search) end controller.send :apply_sorting, chain diff --git a/spec/unit/resource_controller_spec.rb b/spec/unit/resource_controller_spec.rb index eb4418fae7a..b77d8add7b7 100644 --- a/spec/unit/resource_controller_spec.rb +++ b/spec/unit/resource_controller_spec.rb @@ -200,8 +200,8 @@ def call_after_destroy(obj); end subject { controller.send :collection } it { - is_expected.to be_a ActiveRecord::Relation if defined?(ActiveRecord) - is_expected.to be_a Mongoid::Criteria if defined?(Mongoid) + is_expected.to be_a ::ActiveRecord::Relation if defined?(::ActiveRecord) + is_expected.to be_a ::Mongoid::Criteria if defined?(::Mongoid) } it "returns a collection of posts" do diff --git a/spec/unit/resource_spec.rb b/spec/unit/resource_spec.rb index 4eca5ac90fa..a21966098a9 100644 --- a/spec/unit/resource_spec.rb +++ b/spec/unit/resource_spec.rb @@ -232,10 +232,10 @@ def config(options = {}) let(:resource) { namespace.register(Post) } let(:post) { double } before do - if defined?(ActiveRecord) + if defined?(::ActiveRecord) allow(Post).to receive(:find_by_id).with('12345') { post } end - if defined?(Mongoid) + if defined?(::Mongoid) allow(Post).to receive(:find).with('12345') { post } end end @@ -259,10 +259,10 @@ def config(options = {}) end it 'can find the post by the custom primary key' do - if defined?(ActiveRecord) + if defined?(::ActiveRecord) expect(resource.find_resource('55555')).to eq different_post end - if defined?(Mongoid) + if defined?(::Mongoid) skip "not implemented yet." end end @@ -280,10 +280,10 @@ def config(options = {}) it 'can find the post by controller finder' do # If document not found, by default Mongoid's `find_by` raises an error, # ActiveRecord's `find_by` always returns nil, but `find_by!` raises an error. - if defined?(ActiveRecord) + if defined?(::ActiveRecord) allow(Post).to receive(:find_by_title!).with('title-name').and_return(post) end - if defined?(Mongoid) + if defined?(::Mongoid) allow(Post).to receive(:find_by).with(title: 'title-name').and_return(post) end diff --git a/spec/unit/view_helpers/display_name_spec.rb b/spec/unit/view_helpers/display_name_spec.rb index a0ca425a3e4..7bb98487df2 100644 --- a/spec/unit/view_helpers/display_name_spec.rb +++ b/spec/unit/view_helpers/display_name_spec.rb @@ -44,7 +44,7 @@ context "when no display name method is defined" do context "on a Rails model" do - let(:i18n_namespace) { defined?(ActiveRecord) ? :activerecord : :mongoid } + let(:i18n_namespace) { defined?(::ActiveRecord) ? :activerecord : :mongoid } it "should show the model name" do class ThisModel diff --git a/spec/unit/views/components/attributes_table_spec.rb b/spec/unit/views/components/attributes_table_spec.rb index 283887cb19e..a7ba9ef2c3f 100644 --- a/spec/unit/views/components/attributes_table_spec.rb +++ b/spec/unit/views/components/attributes_table_spec.rb @@ -218,7 +218,7 @@ end context "with defined attribute name translation" do - let(:i18n_namespace) { defined?(ActiveRecord) ? :activerecord : :mongoid } + let(:i18n_namespace) { defined?(::ActiveRecord) ? :activerecord : :mongoid } it "should have the translated attribute name in the title" do with_translation i18n_namespace => {attributes: {post: {title: 'Translated Title', id: 'Translated Id'}}} do diff --git a/spec/unit/views/components/paginated_collection_spec.rb b/spec/unit/views/components/paginated_collection_spec.rb index 5c9fc2dbb40..073ef78d13e 100644 --- a/spec/unit/views/components/paginated_collection_spec.rb +++ b/spec/unit/views/components/paginated_collection_spec.rb @@ -165,10 +165,10 @@ def paginated_collection(*args) end it "should display proper message (including number and not hash)" do - if defined?(ActiveRecord) + if defined?(::ActiveRecord) expect(pagination.find_by_class('pagination_information').first.content).to eq "Displaying all 2 posts" end - if defined?(Mongoid) + if defined?(::Mongoid) skip "not implemented yet." end end @@ -181,11 +181,11 @@ def paginated_collection(*args) end it "should display proper message (including number and not hash)" do - if defined?(ActiveRecord) + if defined?(::ActiveRecord) expect(pagination.find_by_class('pagination_information').first.content.gsub(' ',' ')). to eq "Displaying posts 1 - 2 of 3 in total" end - if defined?(Mongoid) + if defined?(::Mongoid) skip "not implemented yet." end end diff --git a/tasks/test.rake b/tasks/test.rake index 8e9da27f0b3..4f5d4c0baaa 100644 --- a/tasks/test.rake +++ b/tasks/test.rake @@ -5,8 +5,8 @@ task :setup, :parallel do |t, args| puts "test app #{ENV['RAILS_ROOT']} already exists; skipping" else system("mkdir spec/rails") unless File.exists?("spec/rails") - template_file = defined?(ActiveRecord) ? 'rails_template.rb' : 'rails_template_mongoid.rb' - opts = defined?(ActiveRecord) ? nil : '--skip-active-record' + template_file = defined?(::ActiveRecord) ? 'rails_template.rb' : 'rails_template_mongoid.rb' + opts = defined?(::ActiveRecord) ? nil : '--skip-active-record' system "#{'INSTALL_PARALLEL=yes' if args[:parallel]} bundle exec rails new #{ENV['RAILS_ROOT']} -m spec/support/#{template_file} --skip-bundle #{opts}" Rake::Task['parallel:after_setup_hook'].invoke if args[:parallel] end