View show page for menu item in Active Admin - ruby-on-rails

I would like to dynamically change the menu based on the permissions of the viewing user. I would like the superadmin user to have access to the normal Resource actions (index, show, update, etc). So when an admin clicks on a menu item, it would take them to the index of that resource. I would like to restrict the normal admin user to just viewing a specific show page.
The menu route for the superadmin would be: /admin/resource
The menu route for the normal admin would be: /admin/resource/id
I would also like to restrict normal admin access to the index view, or other resources that they don't have access to. I have been able to achieve both these things, but I have yet to be able to map a menu item to a specific show page. I know I could create a custom page and view, but I really would like to share the custom DSL for the show and edit pages between superadmin and the normal admin.
Anyone know how to make this happen.

Ok, so I figured a way to get what I want. I am not sure if exactly fulfills what I wanted. (meaning, it would be nice to create custom menu items that are mapped to specific resources)
I just overwrote the index controller action to redirect to the specific show page. Because the super admin needs access to the original Store resource, I had to alias it with :as.
ActiveAdmin.register Store, :as => 'My Store' do
menu :if => proc{ !current_user.is_admin? },
:label => 'My Store'
actions :show, :edit, :update
controller do
def index
redirect_to(admin_my_store_url(current_user.store))
end
end
end

Related

How do I include a different file if my user is logged in?

I’m using Rails 4.2.5. I have this line in my “app/views/layouts/application.html.erb” file …
<%= yield %>
If the user is logged in, I would like to display the content from the page “./app/views/user_objects/index.html.erb” but if they are not, I would like to display content from the page “app/views/pages/_welcome.html.erb”. I don’t know how to set this up. In my config/routes.rb file I have
constraints lambda { |req| !req.session[:user_id].blank? } do
root :to => 'user_objects#index', as: "dashboard"
end
root to: 'pages#index'
but that is displaying the “./app/views/user_objects/index.html.erb” file at all times, even if the user is not logged in. What do I need to add (or remove) to allow me to display the normal welcome page content if the user is not logged in?
I'd do this a bit different. In my config/routes.rb I'd have just the usual
root 'pages#index'
and then I'd change my index method at app/controllers/pages_controller.rb to
def index
if logged_user?
render 'user_objects/index'
end
end
and then I'd put all this logic to say if the user is logged in a logged_user? method like
def logged_user?
!session[:user_id].blank?
end
There are other ways to to this, of course. But always keep the KISS Principle in mind.
If you really want to understand all the rendering process, it would be better to give some attention to http://guides.rubyonrails.org/layouts_and_rendering.html

ActiveAdmin: Adding pages dynamically to the menu after the application has started

I'm using ActiveAdmin as a CMS. I want my users to be able to create some pages and then these pages should be added dynamically in the active admin menu under a parent page called "Help". However, I can't workout how to register newly created pages, since the register_page method only runs when the server is started.
Is there a way to dynamically build the path to newly created pages? Or is there a way to run register_page dynamically (perhaps in a proc?) without having to restart?
Later edit:
The problem is the fact that I don't have a route for the newly created page so I get an error saying that admin_page_title_path could not be found after I create the page. This is because I'm doing a register_page in an after_create:
ActiveAdmin.register Page do
after_create do |page|
ActiveAdmin.register_page page.title do
menu :parent => 'Help'
content :title => proc { I18n.t("active_admin.manual") } do
columns do
column do
panel "#{raw(page.title)}" do
render :partial => "admin/pages/content", :locals => {page: page}
end
end
end #columns
end
end
end
end
If I use Rails.application.reload_routes! then the route is built, BUT this also clears my menus and I basically get a list of ALL the pages in the place where the menu used to be.
All the methods for rebuilding the menu that I tried seemed a bit hack-ish. So I assume there should be a way to rebuild just that one route instead of meddling with resetting resources and re-adding menu items.

ActiveAdmin: Make index pages link to edit action

ActiveAdmins DSL for index pages provides a simple means to link to associations, like e.g.
ActiveAdmin.register Rental do
index do
column :user
end
The users name in the created column will automatically be linked to the show action of the associated user.
Now if in User we disabled the show action like so:
ActiveAdmin.register User do
actions :all, :except => [:show]
AA will still generate links to the show action instead of to the edit action, as one would expect.
Is there an easy way to specify, that AA should always automatically link to the associations edit action?
Update:
I found out, that the link is created in ActiveAdmin::ViewHelpers::AutoLinkHelper.auto_url_for(resource) but I don't see how that could be made configurable.
This bug just got fixed in activeadmin master in this commit:
https://github.com/activeadmin/activeadmin/pull/3754
ActiveAdmin will now link to the edit action if the show action was disabled.

ActiveAdmin display default view content

I am working with ActiveAdmin and need to make customizations to some views and have come across a couple of scenarios I feel I am doing wrong.
I am adding an additional table to a show view (comments on Posts). This requires me to rewrite the whole attributes table and then add my panel. Is there a way to customize views without losing the default content?
I would also like to add a table of associated items on the show view which doesn't need to be customized is there any way to include the default tale that would normally be on the index view with default actions and paging?
After digging in the source code of Active Admin, I've found a way to patch this
show do
default_main_content
panel "Your Added Stuff" do
# Add stuff here
end
end
Of course this is undocumented and maybe considered a hack, but unless any other solution exists, it works.
Note: To do this in the form action (new and edit):
form do |f|
f.inputs
# Other inputs here
f.actions
end
Instead of using default_main_content, you could also just loop through the columns on the model like so:
ActiveAdmin.register Ad do
show do
attributes_table do
default_attribute_table_rows.each do |field|
row field
end
# Custom bits here
end
end
end
A couple areas of the documentation might help you:
See Customize the Show Page, Customizing the Index Page, Customizing the Form, and Custom Pages. An example of customizing a show screen:
ActiveAdmin.register Ad do
show do |ad|
default_main_content
h3 ad.title
end
end
See Custom Action Items in the Custom Controller Actions section of the documentation. An example:
action_item :only => :show, :if => proc{ current_admin_user.super_admin? } do
"Only display this to super admins on the show screen"
end
NB default_main_content does not exist in the documentation anymore, yet it works fine.
Just figured that out myself:
For the default table index page you can do something like this
index do
h1 "Hello World"
p "get more content"
instance_eval(&default_table)
end

Rails3 - routing: How do you create a root path for a database-driven item?

Background info
I have an app where users can make microsites ala http://myname.the_app.com.
Microsite has_many :pages
Each Microsite belongs_to :landing_page (which is a Page) that determines where the visitor is fowarded when they view the site. Right now the app does a redirect_to #microsite.landing_page. The visitor then sees a url like so: `http://myname.the_app.com/pages/id
How can I render the landing page but keep the root path? I want to be able to see the "about page" but with the url http://myname.the_app.com.
An option
Here's an option: have a home_controller#index action that instantiates all the page variables it needs. However... I'm holding this off because what if I have more than page? What if I want the landing_page to be something else in the future, like a contact_form located at `/contact_forms/id?
Thanks!
Perhaps render :template => "pages/#{whatever_id}" might solve your problem. Just replace your redirect_to with the render...

Resources