Skip to content
Open
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
19 changes: 19 additions & 0 deletions features/decorators.feature
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ Feature: Decorators
And I should see "A very unique post"
And I should see "No"

Scenario: Index page with PORO decorator
Given a configuration of:
"""
ActiveAdmin.register Post do
decorate_with PostPoroDecorator

index do
column(:id)
column(:title)
column(:decorator_method)
column(:starred)
end
end
"""
When I am on the index page for posts
Then I should see "A method only available on the PORO decorator"
And I should see "A very unique post"
And I should see "No"

Scenario: Show page with decorator
Given a configuration of:
"""
Expand Down
17 changes: 17 additions & 0 deletions features/index/batch_actions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ Feature: Batch Actions
Then I should see a flash with "Successfully deleted 2 posts"
And I should see 3 posts in the table

Scenario: Use default (destroy) batch action on a PORO decorated resource
Given 5 posts exist
And an index configuration of:
"""
ActiveAdmin.register Post do
decorate_with PostPoroDecorator
end
"""
When I check the 2nd record
And I check the 4th record
And I follow "Batch Actions"
Then I should see the batch action :destroy "Delete Selected"

Given I submit the batch action form with "destroy"
Then I should see a flash with "Successfully deleted 2 posts"
And I should see 3 posts in the table

@javascript
Scenario: Use default (destroy) batch action on a nested resource
Given I am logged in
Expand Down
2 changes: 2 additions & 0 deletions lib/active_admin/resource_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class ResourceController < BaseController
include ViewHelpers::DownloadFormatLinksHelper
extend ResourceClassMethods

helper_method :apply_collection_decorator

def self.active_admin_config=(config)
if @active_admin_config = config
defaults resource_class: config.resource_class,
Expand Down
3 changes: 1 addition & 2 deletions lib/active_admin/resource_controller/data_access.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ def self.included(base)
:filtering,
:scoping,
:includes,
:pagination,
:collection_decorator
:pagination
].freeze

# Retrieve, memoize and authorize the current collection from the db. This
Expand Down
4 changes: 2 additions & 2 deletions lib/active_admin/resource_controller/decorators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ def self.wrap!(parent, name)
# Draper::CollectionDecorator was introduced in 1.0.0
# Draper::Decorator#collection_decorator_class was introduced in 1.3.0
def self.find_collection_decorator(decorator)
if Dependency.draper? '>= 1.3.0'
if Dependency.draper?('>= 1.3.0') && decorator <= Draper::Decorator
decorator.collection_decorator_class
elsif Dependency.draper? '>= 1.0.0'
elsif Dependency.draper?('>= 1.0.0') && decorator <= Draper::Decorator
draper_collection_decorator
else
decorator
Expand Down
2 changes: 1 addition & 1 deletion lib/active_admin/views/pages/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def render_index
per_page: per_page,
pagination_total: pagination_total) do
div class: 'index_content' do
insert_tag(renderer_class, config, collection)
insert_tag(renderer_class, config, apply_collection_decorator(collection))
end
end
end
Expand Down
1 change: 1 addition & 0 deletions spec/support/rails_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

copy_file File.expand_path('templates/models/post.rb', __dir__), 'app/models/post.rb'
copy_file File.expand_path('templates/post_decorator.rb', __dir__), 'app/models/post_decorator.rb'
copy_file File.expand_path('templates/post_poro_decorator.rb', __dir__), 'app/models/post_poro_decorator.rb'

generate :migration, 'create_blog_posts title:string body:text published_date:date author_id:integer ' +
"position:integer custom_category_id:integer starred:boolean foo_id:integer #{timestamps}"
Expand Down
23 changes: 23 additions & 0 deletions spec/support/templates/post_poro_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class PostPoroDecorator
delegate_missing_to :post

def initialize(post)
@post = post
end

def self.decorate(record, *_)
if record.is_a?(Enumerable)
record.map { |individual_record| new(individual_record) }
else
new(record)
end
end

def decorator_method
'A method only available on the PORO decorator'
end

private

attr_reader :post
end
24 changes: 20 additions & 4 deletions spec/unit/resource_controller/decorators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,18 @@

context "in show action" do
let(:action) { 'show' }
let(:decorator_class) { PostDecorator }

it { is_expected.to be_kind_of(PostDecorator) }
context 'with a Draper decorator' do
let(:decorator_class) { PostDecorator }

it { is_expected.to be_kind_of(PostDecorator) }
end

context 'with a PORO decorator' do
let(:decorator_class) { PostPoroDecorator }

it { is_expected.to be_kind_of(PostPoroDecorator) }
end
end

context "in update action" do
Expand Down Expand Up @@ -54,8 +63,15 @@
end

context 'when the form is configured to decorate' do
let(:decorator_class) { PostDecorator }
it { is_expected.to be_kind_of(PostDecorator) }
context 'with a Draper decorator' do
let(:decorator_class) { PostDecorator }
it { is_expected.to be_kind_of(PostDecorator) }
end

context 'with a PORO decorator' do
let(:decorator_class) { PostPoroDecorator }
it { is_expected.to be_kind_of(PostPoroDecorator) }
end
end
end
end
39 changes: 21 additions & 18 deletions spec/unit/resource_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,29 @@ def call_after_destroy(obj); end

context 'with a decorator' do
let(:config) { controller.class.active_admin_config }
before { config.decorator_class_name = '::PostDecorator' }
it 'returns a PostDecorator' do
expect(subject).to be_kind_of(PostDecorator)

context 'with a Draper decorator' do
before { config.decorator_class_name = '::PostDecorator' }

it 'returns a PostDecorator' do
expect(subject).to be_kind_of(PostDecorator)
end

it 'returns a PostDecorator that wraps the post' do
expect(subject.title).to eq post.title
end
end

it 'returns a PostDecorator that wraps the post' do
expect(subject.title).to eq post.title
context 'with a PORO decorator' do
before { config.decorator_class_name = '::PostPoroDecorator' }

it 'returns a PostDecorator' do
expect(subject).to be_kind_of(PostPoroDecorator)
end

it 'returns a PostDecorator that wraps the post' do
expect(subject.title).to eq post.title
end
end
end
end
Expand All @@ -213,19 +229,6 @@ def call_after_destroy(obj); end
it "returns a collection of posts" do
expect(subject.first).to be_kind_of(Post)
end

context 'with a decorator' do
before { config.decorator_class_name = 'PostDecorator' }

it 'returns a collection decorator using PostDecorator' do
expect(subject).to be_a Draper::CollectionDecorator
expect(subject.decorator_class).to eq PostDecorator
end

it 'returns a collection decorator that wraps the post' do
expect(subject.first.title).to eq Post.first.title
end
end
end

describe "performing batch_action" do
Expand Down