Skip to content

Commit

Permalink
#2537: autoload the comments model
Browse files Browse the repository at this point in the history
By not loading the Comments model until Rails is initialized, we can
rely on the user's changes to Kaminari settings.
  • Loading branch information
seanlinsley committed Oct 13, 2013
1 parent 3d0fb31 commit 3d23682
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/active_admin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ def after_load(&block)
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)
require 'active_admin/orm/active_record' if defined? ActiveRecord
require 'active_admin/orm/mongoid' if defined? Mongoid

# Load gem-specific code only if that gem is being used
require 'active_admin/cancan_adapter' if Gem.loaded_specs['cancan']
8 changes: 6 additions & 2 deletions lib/active_admin/orm/active_record/comments.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
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'
Expand All @@ -16,7 +15,12 @@
# Add the module to the show page
ActiveAdmin.application.view_factory.show_page.send :include, ActiveAdmin::Comments::ShowPageHelper

# Walk through all the loaded resources after they are loaded
# Load the model as soon as it's referenced. By that point, Rails & Kaminari will be ready
module ActiveAdmin
autoload :Comment, 'active_admin/orm/active_record/comments/comment'
end

# Walk through all the loaded namespaces after they're loaded
ActiveAdmin.after_load do |app|
app.namespaces.values.each do |namespace|
if namespace.comments?
Expand Down
16 changes: 3 additions & 13 deletions lib/active_admin/orm/active_record/comments/comment.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
require 'kaminari/models/active_record_extension'

module ActiveAdmin

# manually initialize kaminari for this model
::ActiveRecord::Base.send :include, Kaminari::ActiveRecordExtension

class Comment < ActiveRecord::Base

belongs_to :resource, :polymorphic => true
belongs_to :author, :polymorphic => true

Expand All @@ -20,13 +15,9 @@ def self.resource_type(record)
record.class.base_class.name.to_s
end

# Postgres adapters won't compare strings to numbers (issue 34)
def self.resource_id_cast(record)
# Postgres adapters won't compare strings to numbers (issue 34)
if resource_id_type == :string
record.id.to_s
else
record.id
end
resource_id_type == :string ? record.id.to_s : record.id
end

def self.find_for_resource_in_namespace(resource, namespace)
Expand All @@ -44,6 +35,5 @@ def self.table_name
end

end

end

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Comments

module NamespaceHelper

# Returns true of the namespace allows comments
# Returns true if the namespace allows comments
def comments?
allow_comments == true
end
Expand Down

0 comments on commit 3d23682

Please sign in to comment.