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

Refactor Comments UI #2113

Merged
merged 1 commit into from
Apr 19, 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
4 changes: 2 additions & 2 deletions features/comments/commenting.feature
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Feature: Commenting
config.comments = false
end
"""
Then I should not see "Comments"
Then I should not see the element "div.comments.panel"

Scenario: View a resource in a namespace that doesn't have comments
Given a configuration of:
Expand Down Expand Up @@ -95,7 +95,7 @@ Feature: Commenting
ActiveAdmin.register Post
"""
When I add a comment "Hello from Comment"
When I am on the index page for admin_comments
When I am on the index page for comments
Then I should see a table header with "Body"
And I should see "Hello from Comment"

Expand Down
2 changes: 1 addition & 1 deletion features/comments/viewing_index.feature
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Feature: Viewing Index of Comments

Scenario: Viewing all commments for a namespace
When I add a comment "Hello from Comment"
When I am on the index page for admin_comments
When I am on the index page for comments
Then I should see a table header with "Body"
And I should see a table header with "Resource"
And I should see a table header with "Author"
Expand Down
6 changes: 4 additions & 2 deletions features/step_definitions/web_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ def with_scope(locator)
attach_file(field, File.expand_path(path))
end

Then /^(?:I )should( not)? see "([^"]*)"$/ do |negate, text|
page.should negate ? have_no_content(text) : have_content(text)
Then /^(?:I )should( not)? see( the element)? "([^"]*)"$/ do |negate, is_css, text|
should = negate ? :should_not : :should
have = is_css ? have_css(text) : have_content(text)
page.send should, have
end

Then /^the "([^"]*)" field(?: within (.*))? should( not)? contain "([^"]*)"$/ do |field, parent, negate, value|
Expand Down
4 changes: 0 additions & 4 deletions lib/active_admin/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,6 @@ def self.deprecated_inheritable_setting(name, default)
# @deprecated The default sort order for index pages
deprecated_setting :default_sort_order, 'id_desc'

# DEPRECATED: This option is deprecated and will be removed. Use
# the #allow_comments_in option instead
attr_accessor :admin_notes

include AssetRegistration

# Event that gets triggered on load of Active Admin
Expand Down
68 changes: 34 additions & 34 deletions lib/active_admin/comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
require 'active_admin/comments/resource_helper'

# Add the comments configuration
ActiveAdmin::Application.inheritable_setting :allow_comments, true
ActiveAdmin::Application.inheritable_setting :allow_comments, true
ActiveAdmin::Application.inheritable_setting :show_comments_in_menu, true
ActiveAdmin::Application.inheritable_setting :comments_registration_name, 'Comment'

# Add the comments module to ActiveAdmin::Namespace
ActiveAdmin::Namespace.send :include, ActiveAdmin::Comments::NamespaceHelper
Expand All @@ -20,66 +22,64 @@
ActiveAdmin.after_load do |app|
app.namespaces.values.each do |namespace|
if namespace.comments?
namespace.register ActiveAdmin::Comment, :as => "AdminComment" do
namespace.register ActiveAdmin::Comment, :as => namespace.comments_registration_name do
actions :index, :show, :create

# Ensure filters are turned on
config.filters = true
menu false unless namespace.show_comments_in_menu

# Don't display in the menu
menu false
config.comments = false # Don't allow comments on comments
config.batch_actions = false # The default destroy batch action isn't showing up anyway...

# Don't allow comments on comments
config.comments = false

# Filter Comments by date
filter :resource_type
if Rails::VERSION::STRING >= '3.2'
filter :resource_type, :as => :select, :collection => proc{ ActiveAdmin::Comment.uniq.pluck :resource_type }
filter :author_type, :as => :select, :collection => proc{ ActiveAdmin::Comment.uniq.pluck :author_type }
else
filter :resource_type
filter :author_type
end
filter :body
filter :created_at

# Only view comments in this namespace
scope :all, :default => true do |comments|
comments.where(:namespace => active_admin_config.namespace.name.to_s)
end

# Always redirect to the resource on show
before_filter :only => :show do
flash[:notice] = flash[:notice].dup if flash[:notice]
comment = ActiveAdmin::Comment.find(params[:id])
resource_config = active_admin_config.namespace.resource_for(comment.resource.class)
redirect_to resource_config.route_instance_path(comment.resource)
scope :all, :show_count => false
# Register a scope for every namespace that exists.
# The current namespace will be the default scope.
app.namespaces.values.map(&:name).each do |name|
scope name, :default => namespace.name == name do
resource_class.where :namespace => name
end
end

# Store the author and namespace
before_save do |comment|
comment.namespace = active_admin_config.namespace.name
comment.author = current_active_admin_user
comment.author = current_active_admin_user
end

# Redirect to the resource show page when failing to add a comment
# TODO: Provide helpers to make such kind of customization much simpler
# Redirect to the resource show page after comment creation
controller do
def create
create! do |success, failure|
# FYI: below we call `resource.resource`. First is the comment, second is the associated resource.
resource_config = active_admin_config.namespace.resource_for resource.resource.class
resource_url = resource_config.route_instance_path resource.resource
success.html{ redirect_to resource_url }
failure.html do
resource_config = active_admin_config.namespace.resource_for(@admin_comment.resource.class)
flash[:error] = I18n.t('active_admin.comments.errors.empty_text')
redirect_to resource_config.route_instance_path(@admin_comment.resource)
redirect_to resource_url
end
end
end
end

# Display as a table
index do
column(I18n.t('active_admin.comments.resource')){|comment| auto_link(comment.resource) }
column(I18n.t('active_admin.comments.author')){|comment| auto_link(comment.author) }
column(I18n.t('active_admin.comments.body')){|comment| comment.body }
column I18n.t('active_admin.comments.resource_type'), :resource_type
column I18n.t('active_admin.comments.author_type'), :author_type
column I18n.t('active_admin.comments.resource'), :resource
column I18n.t('active_admin.comments.author'), :author
column I18n.t('active_admin.comments.body'), :body
actions
end
end
end
end
end

# @deprecated #allow_comments_on - Remove in 0.5.0
ActiveAdmin::Application.deprecated_setting :allow_comments_in, [], 'The "allow_comments_in = []" setting is deprecated and will be remove by Active Admin 0.5.0. Please use "allow_comments = true|false" instead.'
14 changes: 6 additions & 8 deletions lib/active_admin/comments/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@ module ActiveAdmin

class Comment < ActiveRecord::Base
belongs_to :resource, :polymorphic => true
belongs_to :author, :polymorphic => true
belongs_to :author, :polymorphic => true

attr_accessible :resource, :resource_id, :resource_type, :body, :namespace

validates_presence_of :resource
validates_presence_of :body
validates_presence_of :namespace
validates_presence_of :body, :namespace, :resource

# @returns [String] The name of the record to use for the polymorphic relationship
def self.resource_type(record)
Expand All @@ -30,13 +28,13 @@ def self.resource_id_cast(record)
end

def self.find_for_resource_in_namespace(resource, namespace)
where(:resource_type => resource_type(resource),
:resource_id => resource_id_cast(resource),
:namespace => namespace.to_s)
where :resource_type => resource_type(resource),
:resource_id => resource_id_cast(resource),
:namespace => namespace.to_s
end

def self.resource_id_type
columns.select { |i| i.name == "resource_id" }.first.type
columns.detect{ |i| i.name == "resource_id" }.type
end

def self.table_name
Expand Down
10 changes: 5 additions & 5 deletions lib/active_admin/comments/views/active_admin_comments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ def build_empty_message
end

def comment_form_url
if active_admin_namespace.root?
comments_path
else
send(:"#{active_admin_namespace.name}_admin_comments_path")
end
parts = []
parts << active_admin_namespace.name unless active_admin_namespace.root?
parts << active_admin_namespace.comments_registration_name.underscore.pluralize
parts << 'path'
send parts.join '_'
end

def build_comment_form
Expand Down
2 changes: 2 additions & 0 deletions lib/active_admin/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ en:
labels:
destroy: "Delete"
comments:
resource_type: "Resource Type"
author_type: "Author Type"
body: "Body"
author: "Author"
title: "Comment"
Expand Down
5 changes: 0 additions & 5 deletions lib/active_admin/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,6 @@ def defined_actions
controller.instance_methods.map { |m| m.to_sym } & ResourceController::ACTIVE_ADMIN_ACTIONS
end

# Are admin notes turned on for this resource
def admin_notes?
admin_notes.nil? ? ActiveAdmin.admin_notes : admin_notes
end

def belongs_to(target, options = {})
@belongs_to = Resource::BelongsTo.new(self, target, options)
self.menu_item_menu_name = target unless @belongs_to.optional?
Expand Down
25 changes: 12 additions & 13 deletions lib/generators/active_admin/install/templates/active_admin.rb.erb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ ActiveAdmin.setup do |config|
# Default:
# config.logout_link_method = :get


# == Root
#
# Set the action to call for the root path. You can set different
Expand All @@ -94,21 +95,19 @@ ActiveAdmin.setup do |config|
# Default:
# config.root_to = 'dashboard#index'


# == Admin Comments
#
# Admin comments allow you to add comments to any model for admin use.
# Admin comments are enabled by default.
# This allows your users to comment on any resource registered with Active Admin.
#
# Default:
# config.allow_comments = true
# You can completely disable comments:
# config.allow_comments = false
#
# You can turn them on and off for any given namespace by using a
# namespace config block.
# You can disable the menu item for the comments index page:
# config.show_comments_in_menu = false
#
# Eg:
# config.namespace :without_comments do |without_comments|
# without_comments.allow_comments = false
# end
# You can change the name under which comments are registered:
# config.comments_registration_name = 'AdminComment'


# == Batch Actions
Expand All @@ -134,7 +133,7 @@ ActiveAdmin.setup do |config|
#
# To load a stylesheet:
# config.register_stylesheet 'my_stylesheet.css'

#
# You can provide an options hash for more control, which is passed along to stylesheet_link_tag():
# config.register_stylesheet 'my_print_stylesheet.css', :media => :print
#
Expand All @@ -144,7 +143,7 @@ ActiveAdmin.setup do |config|

# == CSV options
#
# Set the CSV builder separator (default is ",")
# Set the CSV builder separator (default is ',')
# config.csv_column_separator = ','
#
# Set the CSV builder options (default is {})
Expand Down Expand Up @@ -172,6 +171,7 @@ ActiveAdmin.setup do |config|
# end
# end


# == Download Links
#
# You can disable download links on resource listing pages,
Expand Down Expand Up @@ -206,5 +206,4 @@ ActiveAdmin.setup do |config|
#
# config.filters = true


end
4 changes: 0 additions & 4 deletions spec/unit/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@
application.view_factory.should be_an_instance_of(ActiveAdmin::ViewFactory)
end

it "should have deprecated admin notes by default" do
application.admin_notes.should be_nil
end

it "should allow comments by default" do
application.allow_comments.should == true
end
Expand Down