Skip to content

Commit 2edf113

Browse files
Merge pull request activeadmin#5826 from activeadmin/fix_renamed_resources_and_optional_belongs_to
Fix renamed resources and optional `belongs_to`
2 parents 7e092d3 + d12dc9a commit 2edf113

File tree

7 files changed

+116
-3
lines changed

7 files changed

+116
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Fix CSVBuilder not respecting `ActiveAdmin.application.csv_options = { humanize_name: false }` setting. [#5800] by [@HappyKadaver]
88
* Fix crash when displaying current filters after filtering by a nested resource. [#5816] by [@deivid-rodriguez]
99
* Fix pagination when `pagination_total` is false to not show a "Last" link, since it's incorrect because we don't have the total pages information. [#5822] by [@deivid-rodriguez]
10+
* Fix optional nested resources causing incorrect routes to be generated, when renamed resources (through `:as` option) are involved. [#5826] by [@ndbroadbent], [@Kris-LIBIS] and [@deivid-rodriguez]
1011

1112
## 2.2.0 [](https://github.com/activeadmin/activeadmin/compare/v2.1.0..v2.2.0)
1213

@@ -485,6 +486,7 @@ Please check [0-6-stable] for previous changes.
485486
[#5802]: https://github.com/activeadmin/activeadmin/pull/5802
486487
[#5816]: https://github.com/activeadmin/activeadmin/pull/5816
487488
[#5822]: https://github.com/activeadmin/activeadmin/pull/5822
489+
[#5826]: https://github.com/activeadmin/activeadmin/pull/5826
488490

489491
[@5t111111]: https://github.com/5t111111
490492
[@aarek]: https://github.com/aarek
@@ -554,3 +556,5 @@ Please check [0-6-stable] for previous changes.
554556
[@panasyuk]: https://github.com/panasyuk
555557
[@jscheid]: https://github.com/jscheid
556558
[@violeta-p]: https://github.com/violeta-p
559+
[@ndbroadbent]: https://github.com/ndbroadbent
560+
[@Kris-LIBIS]: https://github.com/Kris-LIBIS

features/renamed_resource.feature

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Feature: Renamed Resource
22

3-
Strong attributes for resources renamed with as: 'NewName'
3+
Resources renamed with as: 'NewName'
44

55
Scenario: Default form with no config
66
Given a category named "Music" exists
@@ -14,7 +14,6 @@ Feature: Renamed Resource
1414
end
1515
"""
1616
When I am on the index page for posts
17-
1817
And I follow "New Post"
1918
And I fill in "Title" with "Hello World"
2019
And I fill in "Body" with "This is the body"
@@ -24,5 +23,48 @@ Feature: Renamed Resource
2423
Then I should see "Post was successfully created."
2524
And I should see the attribute "Title" with "Hello World"
2625
And I should see the attribute "Body" with "This is the body"
27-
#And I should see the attribute "Category" with "Music"
26+
And I should see the attribute "Category" with "Music"
27+
And I should see the attribute "Author" with "John Doe"
28+
29+
Scenario: With a belongs_to optional association
30+
Given a category named "Music" exists
31+
And a user named "John Doe" exists
32+
And I am logged in
33+
And a configuration of:
34+
"""
35+
ActiveAdmin.register User, as: 'Author' do
36+
show do |author|
37+
attributes_table do
38+
row :articles do
39+
link_to 'Author Articles', admin_author_articles_path(author)
40+
end
41+
end
42+
end
43+
end
44+
45+
ActiveAdmin.register Post, as: 'Article' do
46+
belongs_to :author, optional: true
47+
permit_params :custom_category_id, :author_id, :title,
48+
:body, :position, :published_date, :starred
49+
end
50+
51+
ActiveAdmin.register Post, as: 'News', namespace: :admin2
52+
"""
53+
When I am on the index page for articles
54+
And I follow "New Article"
55+
And I fill in "Title" with "Hello World"
56+
And I fill in "Body" with "This is the body"
57+
And I select "Music" from "Category"
58+
And I select "John Doe" from "Author"
59+
And I press "Create Post"
60+
Then I should see "Post was successfully created."
61+
And I should see the attribute "Title" with "Hello World"
62+
And I should see the attribute "Body" with "This is the body"
63+
And I should see the attribute "Category" with "Music"
2864
And I should see the attribute "Author" with "John Doe"
65+
When I click "John Doe"
66+
And I click "Author Articles"
67+
Then I should see a table header with "Title"
68+
And I should see a table header with "Body"
69+
And I should see "Hello World"
70+
And I should see "This is the body"

lib/active_admin/resource.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
require 'active_admin/resource/sidebars'
1313
require 'active_admin/resource/belongs_to'
1414
require 'active_admin/resource/ordering'
15+
require 'active_admin/resource/model'
1516

1617
module ActiveAdmin
1718

@@ -104,6 +105,10 @@ def decorator_class
104105
ActiveSupport::Dependencies.constantize(decorator_class_name) if decorator_class_name
105106
end
106107

108+
def resource_name_extension
109+
@resource_name_extension ||= define_resource_name_extension(self)
110+
end
111+
107112
def resource_table_name
108113
resource_class.quoted_table_name
109114
end
@@ -133,6 +138,7 @@ def defined_actions
133138
def belongs_to(target, options = {})
134139
@belongs_to = Resource::BelongsTo.new(self, target, options)
135140
self.menu_item_options = false if @belongs_to.required?
141+
options[:class_name] ||= @belongs_to.resource.resource_class_name if @belongs_to.resource
136142
controller.send :belongs_to, target, options.dup
137143
end
138144

@@ -203,5 +209,12 @@ def default_csv_builder
203209
@default_csv_builder ||= CSVBuilder.default_for_resource(self)
204210
end
205211

212+
def define_resource_name_extension(resource)
213+
Module.new do
214+
define_method :model_name do
215+
resource.resource_name
216+
end
217+
end
218+
end
206219
end # class Resource
207220
end # module ActiveAdmin

lib/active_admin/resource/model.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module ActiveAdmin
2+
class Model
3+
def initialize(resource, record)
4+
@record = record
5+
6+
if resource
7+
@record.extend(resource.resource_name_extension)
8+
end
9+
end
10+
11+
def to_model
12+
@record
13+
end
14+
end
15+
end

lib/active_admin/resource_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'active_admin/resource_controller/action_builder'
22
require 'active_admin/resource_controller/data_access'
33
require 'active_admin/resource_controller/decorators'
4+
require 'active_admin/resource_controller/polymorphic_routes'
45
require 'active_admin/resource_controller/scoping'
56
require 'active_admin/resource_controller/streaming'
67
require 'active_admin/resource_controller/sidebars'
@@ -18,6 +19,7 @@ class ResourceController < BaseController
1819
include ActionBuilder
1920
include Decorators
2021
include DataAccess
22+
include PolymorphicRoutes
2123
include Scoping
2224
include Streaming
2325
include Sidebars
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require "active_admin/resource"
2+
require "active_admin/resource/model"
3+
4+
module ActiveAdmin
5+
class ResourceController < BaseController
6+
module PolymorphicRoutes
7+
def polymorphic_url(record_or_hash_or_array, options = {})
8+
super(map_named_resources_for(record_or_hash_or_array), options)
9+
end
10+
11+
def polymorphic_path(record_or_hash_or_array, options = {})
12+
super(map_named_resources_for(record_or_hash_or_array), options)
13+
end
14+
15+
private
16+
17+
def map_named_resources_for(record_or_hash_or_array)
18+
return record_or_hash_or_array unless record_or_hash_or_array.is_a?(Array)
19+
20+
record_or_hash_or_array.map { |record| to_named_resource(record) }
21+
end
22+
23+
def to_named_resource(record)
24+
if record.is_a?(resource_class)
25+
return ActiveAdmin::Model.new(active_admin_config, record)
26+
end
27+
28+
if record.is_a?(parent.class)
29+
return ActiveAdmin::Model.new(active_admin_config.belongs_to_config.resource, record)
30+
end
31+
32+
record
33+
end
34+
end
35+
end
36+
end

spec/support/templates/models/user.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ class User < ActiveRecord::Base
22
class VIP < self
33
end
44
has_many :posts, foreign_key: 'author_id'
5+
has_many :articles, class_name: 'Post', foreign_key: 'author_id'
56
has_one :profile
67
accepts_nested_attributes_for :profile, allow_destroy: true
78
accepts_nested_attributes_for :posts, allow_destroy: true

0 commit comments

Comments
 (0)