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
Fixes #1986 - Menu should register child regardless of AA resource load order #1988
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,22 @@ def items | |
|
||
def add_without_parent(item_options) | ||
menu_item = ActiveAdmin::MenuItem.new(item_options, self) | ||
|
||
# | ||
# Depending on load order, might have an "empty" parent item already | ||
# in the menu, that is just a label. We want to allow you to customize that if | ||
# you are registering a resource that comes later, such as "Users" parent and | ||
# "Posts" child. The "Users" menu is registered after posts, but posts said it | ||
# already was a sub menu of users. | ||
# | ||
# This will correct that by assigning existing children to this new menu item. | ||
# | ||
if children.has_key? menu_item.id | ||
menu_item.send :children=, self[menu_item.id].send(:children) | ||
end | ||
|
||
children[menu_item.id] = menu_item | ||
|
||
end | ||
|
||
def add_with_parent(parent, item_options) | ||
|
@@ -75,13 +90,18 @@ def children | |
@children ||= {} | ||
end | ||
|
||
def children=(kids) | ||
@children = kids | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We now have this: def children
@children ||= {}
end
def children=(kids)
@children = kids
end Why not replace both of these with attr_accessor :children Is there a real need to initialize Since AA depends on Rails, any places in the code base that checks if there are any children can do this instead: children.present? # same as !children.blank? And for inline operations: children.presence.try :to_do_something There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Disregard the above comment. I implemented what I was looking for in e11fd6c for #2031. It goes something like this: module MenuNode
def initialize
@children = {}
end
end
class Menu
include MenuNode
def initialize
super
# ...
end
end I'm thinking we'll get your PR pulled in first, then I'll rebase mine. |
||
|
||
def normalize_id(string) | ||
case string | ||
raw_id = case string | ||
when Proc | ||
string | ||
string.call rescue string | ||
else | ||
string.to_s.downcase.gsub(" ", "_") | ||
string.to_s | ||
end | ||
raw_id.downcase.gsub( " ", '_' ).gsub( /[^a-z0-9_]/, '' ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO this is more readable: def normalize_id(id)
(id.is_a?(Proc) ? id.call : id).to_s.downcase.gsub ' ', '_'
end Note that I removed both the unconditional |
||
end | ||
|
||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's with all the
send
s? Wouldn't this work?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Private method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Private methods are only private if called out of context. A menu item can call its own "private" methods without any
send
ing.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nevermind, I see this code just created a new menu item object.