Skip to content

Commit

Permalink
Added prefix '::' to ActiveRecord and Mongoid
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhomart committed Jan 31, 2015
1 parent 8c58247 commit 5b4a3e3
Show file tree
Hide file tree
Showing 18 changed files with 79 additions and 71 deletions.
4 changes: 2 additions & 2 deletions features/step_definitions/factory_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
# https://github.com/bmabey/database_cleaner for more info.
if defined?(ActiveRecord::Base)
if defined?(::ActiveRecord::Base)
begin
require 'database_cleaner'
require 'database_cleaner/cucumber'
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/active_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions spec/unit/belongs_to_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/comments_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'rails_helper'
defined?(ActiveRecord) and
defined?(::ActiveRecord) and
describe "Comments" do
let(:application) { ActiveAdmin::Application.new }

Expand Down
32 changes: 16 additions & 16 deletions spec/unit/filters/filter_form_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'])
Expand Down Expand Up @@ -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]']")
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -352,35 +352,35 @@ 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

context "as check boxes" do
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
Expand Down
8 changes: 4 additions & 4 deletions spec/unit/filters/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 11 additions & 11 deletions spec/unit/form_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions spec/unit/helpers/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/order_clause_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/resource/naming_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/resource_controller/data_access_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/resource_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions spec/unit/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down
Loading

0 comments on commit 5b4a3e3

Please sign in to comment.