Skip to content

Commit

Permalink
Merge pull request activeadmin#5826 from activeadmin/fix_renamed_reso…
Browse files Browse the repository at this point in the history
…urces_and_optional_belongs_to

Fix renamed resources and optional `belongs_to`
  • Loading branch information
deivid-rodriguez authored Aug 19, 2019
2 parents 7e092d3 + d12dc9a commit 2edf113
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Fix CSVBuilder not respecting `ActiveAdmin.application.csv_options = { humanize_name: false }` setting. [#5800] by [@HappyKadaver]
* Fix crash when displaying current filters after filtering by a nested resource. [#5816] by [@deivid-rodriguez]
* 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]
* 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]

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

Expand Down Expand Up @@ -485,6 +486,7 @@ Please check [0-6-stable] for previous changes.
[#5802]: https://github.com/activeadmin/activeadmin/pull/5802
[#5816]: https://github.com/activeadmin/activeadmin/pull/5816
[#5822]: https://github.com/activeadmin/activeadmin/pull/5822
[#5826]: https://github.com/activeadmin/activeadmin/pull/5826

[@5t111111]: https://github.com/5t111111
[@aarek]: https://github.com/aarek
Expand Down Expand Up @@ -554,3 +556,5 @@ Please check [0-6-stable] for previous changes.
[@panasyuk]: https://github.com/panasyuk
[@jscheid]: https://github.com/jscheid
[@violeta-p]: https://github.com/violeta-p
[@ndbroadbent]: https://github.com/ndbroadbent
[@Kris-LIBIS]: https://github.com/Kris-LIBIS
48 changes: 45 additions & 3 deletions features/renamed_resource.feature
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Feature: Renamed Resource

Strong attributes for resources renamed with as: 'NewName'
Resources renamed with as: 'NewName'

Scenario: Default form with no config
Given a category named "Music" exists
Expand All @@ -14,7 +14,6 @@ Feature: Renamed Resource
end
"""
When I am on the index page for posts

And I follow "New Post"
And I fill in "Title" with "Hello World"
And I fill in "Body" with "This is the body"
Expand All @@ -24,5 +23,48 @@ Feature: Renamed Resource
Then I should see "Post was successfully created."
And I should see the attribute "Title" with "Hello World"
And I should see the attribute "Body" with "This is the body"
#And I should see the attribute "Category" with "Music"
And I should see the attribute "Category" with "Music"
And I should see the attribute "Author" with "John Doe"

Scenario: With a belongs_to optional association
Given a category named "Music" exists
And a user named "John Doe" exists
And I am logged in
And a configuration of:
"""
ActiveAdmin.register User, as: 'Author' do
show do |author|
attributes_table do
row :articles do
link_to 'Author Articles', admin_author_articles_path(author)
end
end
end
end
ActiveAdmin.register Post, as: 'Article' do
belongs_to :author, optional: true
permit_params :custom_category_id, :author_id, :title,
:body, :position, :published_date, :starred
end
ActiveAdmin.register Post, as: 'News', namespace: :admin2
"""
When I am on the index page for articles
And I follow "New Article"
And I fill in "Title" with "Hello World"
And I fill in "Body" with "This is the body"
And I select "Music" from "Category"
And I select "John Doe" from "Author"
And I press "Create Post"
Then I should see "Post was successfully created."
And I should see the attribute "Title" with "Hello World"
And I should see the attribute "Body" with "This is the body"
And I should see the attribute "Category" with "Music"
And I should see the attribute "Author" with "John Doe"
When I click "John Doe"
And I click "Author Articles"
Then I should see a table header with "Title"
And I should see a table header with "Body"
And I should see "Hello World"
And I should see "This is the body"
13 changes: 13 additions & 0 deletions lib/active_admin/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require 'active_admin/resource/sidebars'
require 'active_admin/resource/belongs_to'
require 'active_admin/resource/ordering'
require 'active_admin/resource/model'

module ActiveAdmin

Expand Down Expand Up @@ -104,6 +105,10 @@ def decorator_class
ActiveSupport::Dependencies.constantize(decorator_class_name) if decorator_class_name
end

def resource_name_extension
@resource_name_extension ||= define_resource_name_extension(self)
end

def resource_table_name
resource_class.quoted_table_name
end
Expand Down Expand Up @@ -133,6 +138,7 @@ def defined_actions
def belongs_to(target, options = {})
@belongs_to = Resource::BelongsTo.new(self, target, options)
self.menu_item_options = false if @belongs_to.required?
options[:class_name] ||= @belongs_to.resource.resource_class_name if @belongs_to.resource
controller.send :belongs_to, target, options.dup
end

Expand Down Expand Up @@ -203,5 +209,12 @@ def default_csv_builder
@default_csv_builder ||= CSVBuilder.default_for_resource(self)
end

def define_resource_name_extension(resource)
Module.new do
define_method :model_name do
resource.resource_name
end
end
end
end # class Resource
end # module ActiveAdmin
15 changes: 15 additions & 0 deletions lib/active_admin/resource/model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module ActiveAdmin
class Model
def initialize(resource, record)
@record = record

if resource
@record.extend(resource.resource_name_extension)
end
end

def to_model
@record
end
end
end
2 changes: 2 additions & 0 deletions lib/active_admin/resource_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'active_admin/resource_controller/action_builder'
require 'active_admin/resource_controller/data_access'
require 'active_admin/resource_controller/decorators'
require 'active_admin/resource_controller/polymorphic_routes'
require 'active_admin/resource_controller/scoping'
require 'active_admin/resource_controller/streaming'
require 'active_admin/resource_controller/sidebars'
Expand All @@ -18,6 +19,7 @@ class ResourceController < BaseController
include ActionBuilder
include Decorators
include DataAccess
include PolymorphicRoutes
include Scoping
include Streaming
include Sidebars
Expand Down
36 changes: 36 additions & 0 deletions lib/active_admin/resource_controller/polymorphic_routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "active_admin/resource"
require "active_admin/resource/model"

module ActiveAdmin
class ResourceController < BaseController
module PolymorphicRoutes
def polymorphic_url(record_or_hash_or_array, options = {})
super(map_named_resources_for(record_or_hash_or_array), options)
end

def polymorphic_path(record_or_hash_or_array, options = {})
super(map_named_resources_for(record_or_hash_or_array), options)
end

private

def map_named_resources_for(record_or_hash_or_array)
return record_or_hash_or_array unless record_or_hash_or_array.is_a?(Array)

record_or_hash_or_array.map { |record| to_named_resource(record) }
end

def to_named_resource(record)
if record.is_a?(resource_class)
return ActiveAdmin::Model.new(active_admin_config, record)
end

if record.is_a?(parent.class)
return ActiveAdmin::Model.new(active_admin_config.belongs_to_config.resource, record)
end

record
end
end
end
end
1 change: 1 addition & 0 deletions spec/support/templates/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class User < ActiveRecord::Base
class VIP < self
end
has_many :posts, foreign_key: 'author_id'
has_many :articles, class_name: 'Post', foreign_key: 'author_id'
has_one :profile
accepts_nested_attributes_for :profile, allow_destroy: true
accepts_nested_attributes_for :posts, allow_destroy: true
Expand Down

0 comments on commit 2edf113

Please sign in to comment.