Skip to content
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

Make ActiveAdmin ORM agnostic (i.e. works without loading ActiveRecord) #2545

Merged
merged 2 commits into from
Oct 9, 2013
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
7 changes: 5 additions & 2 deletions lib/active_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,13 @@ def after_load(&block)

end

# Require internal Plugins
require 'active_admin/comments'
# Require internal plugins
require 'active_admin/batch_actions'
require 'active_admin/filters'

# Require ORM-specific plugins
require 'active_admin/orm/active_record' if defined?(::ActiveRecord)
require 'active_admin/orm/mongoid' if defined?(::Mongoid)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What will happen if both ActiveRecord and Mongoid are present?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, both will load. As long as the plugins within the directories don't conflict with each other, should not be a problem. (Currently there are no Mongoid plugins, so no conflicts). We could add a global config option to AA so user can control which set(s) to load.


# Load gem-specific code only if that gem is being used
require 'active_admin/cancan_adapter' if Gem.loaded_specs['cancan']
2 changes: 0 additions & 2 deletions lib/active_admin/comments/views.rb

This file was deleted.

3 changes: 3 additions & 0 deletions lib/active_admin/orm/active_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# ActiveRecord-specific plugins should be required here

require 'active_admin/orm/active_record/comments'
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require 'active_admin/comments/comment'
require 'active_admin/comments/views'
require 'active_admin/comments/show_page_helper'
require 'active_admin/comments/namespace_helper'
require 'active_admin/comments/resource_helper'
require 'active_admin/orm/active_record/comments/comment'
require 'active_admin/orm/active_record/comments/views'
require 'active_admin/orm/active_record/comments/show_page_helper'
require 'active_admin/orm/active_record/comments/namespace_helper'
require 'active_admin/orm/active_record/comments/resource_helper'

# Add the comments configuration
ActiveAdmin::Application.inheritable_setting :allow_comments, true
Expand Down
2 changes: 2 additions & 0 deletions lib/active_admin/orm/active_record/comments/views.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require 'active_admin/views'
require 'active_admin/orm/active_record/comments/views/active_admin_comments'
1 change: 1 addition & 0 deletions lib/active_admin/orm/mongoid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Mongoid-specific plugins should be required here
Empty file.
6 changes: 4 additions & 2 deletions lib/active_admin/view_helpers/display_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def association_methods_for(resource)

# Return a pretty string for any object
# Date Time are formatted via #localize with :format => :long
# ActiveRecord objects are formatted via #auto_link
# ActiveRecord and Mongoid objects are formatted via #auto_link
# We attempt to #display_name of any other objects
def pretty_format(object)
case object
Expand All @@ -35,7 +35,9 @@ def pretty_format(object)
object
when Date, Time
localize(object, :format => :long)
when ActiveRecord::Base
when ->(obj){defined?(::ActiveRecord) && obj.is_a?(ActiveRecord::Base)}
auto_link(object)
when ->(obj){defined?(::Mongoid) && obj.class.included_modules.include?(Mongoid::Document)}
auto_link(object)
else
display_name(object)
Expand Down