How to display ActiveAdmin menu tabs in the dashboard? - ruby-on-rails

I have too many models in my rails app. And they don't fit in the top menu bar in active admin.
I would like to know how to display the top menu tabs, in a vertical menu via the dashboard?
Thanks in advance.
Eg:
Model 1
Model 2
Model 3
...
Dashboard.rb
ActiveAdmin.register_page "Dashboard" do
menu :priority => 1, :label => proc{ I18n.t("active_admin.dashboard") }
content :title => proc{ I18n.t("active_admin.dashboard") } do
div :class => "blank_slate_container", :id => "dashboard_default_message" do
span :class => "blank_slate" do
span I18n.t("active_admin.dashboard_welcome.welcome")
small I18n.t("active_admin.dashboard_welcome.call_to_action")
end
end
# Here is an example of a simple dashboard with columns and panels.
#
# columns do
# column do
# panel "Recent Posts" do
# ul do
# Post.recent(5).map do |post|
# li link_to(post.title, admin_post_path(post))
# end
# end
# end
# end
# column do
# panel "Info" do
# para "Welcome to ActiveAdmin."
# end
# end
# end
end # content
end

You can use the :parent option to the menu method to assign each model into a menu drop down in each model's admin file.
So:
ActiveAdmin.register User do
menu :parent => "User Data"
end
If you want to create a list of links on the dashboard page, your code would look something like this:
content :title => proc{ I18n.t("active_admin.dashboard") } do
columns do
column do
panel "Models" do
ul do
li link_to("Users", admin_users_path)
li link_to("Admin Users", admin_admin_users_path)
end
end
end
end

Related

Getting error while rendering form in active admin dashboard - rails?

I am getting error while rendering the form from partial in active admin dashboard.My form partial look like:
app/views/admin/dashboard/_form.html.arb
insert_tag active_admin_form_for :bill do |f|
inputs do
input :from_date
input :to_date
input :expiry_date
end
actions
end
And my dashboard code looks like :
ActiveAdmin.register_page "Dashboard" do
menu priority: 1, label: proc{ I18n.t("active_admin.dashboard") }
content title: proc{ I18n.t("active_admin.dashboard") } do
columns do
column do
panel "Generate Bills" do
render partial: 'form' #rendering form partial
end
end
end
end
I am getting following error while clicking on dashboard menu link :
help me out
thnxx!

ActiveAdmin Custom page with index table

I have created a custom page with ActiveAdmin as follows:
ActiveAdmin.register_page "message_list" do
controller do
def index
#collection = client().account.messages.list.sort_by{ |message| Date.rfc2822(message.date_sent) }.reverse
render :layout => 'active_admin'
end
end
end
I have created an index.html.erb file with a table that I want to display on this page. This however is not optimal. How do I use the active admin standard table layout that also comes with pagination and display it with my table info? I know that ActiveAdmin PageDSL Class does not include #index and therefore I can't simply do:
index do
selectable_column
id_column
column :to
column :from
default_actions
end
In addition to achieving the ActiveAdmin table layout on a custom page, how do I change the Title of the page itself? As of now it is called "Index".
An easier method would be to define an ActiveAdmin resource for your message class, Message, and limit the actions to only allow :index.
ActiveAdmin.register Message do
actions :index
index do
selectable_column
id_column
column :to
column :from
default_actions
end
controller do
def scoped_collection
super.where(account_id: account.id).order(:date_sent)
# Or provide a custom collection similar to the current implementation:
# client().account.messages.list.sort_by{ |message| Date.rfc2822(message.date_sent) }.reverse
end
end
end
It is also possible to rename the resource if necessary by providing an :as option to the #register method:
ActiveAdmin.register Message, as: "Account Message" do
# ...
end
While the accepted answer works well if you can use an ActiveAdmin resource instead of a custom page, it is possible to get an index-style table on a custom page via Arbre:
<%=
Arbre::Context.new({}, self) do
table_for(client().account.messages, sortable: true, class: 'index_table') do
column :id
column :created_at
end
end
%>

Customise layout (components ) for activeadmin

Activeadmin layout is not just a file, its a collection of components.
How can I override some of the components like, logo, navigation with activeadmin.
The activeadmin layout components are very easy to customize. What you need to do: Just define a module that opens ActiveAdmin::View.
you can have a custom_activeadmin_components.rb in initializers or admin directory where you defined all of your activeadmin resources. I prefer to put it in the directory where your activeadmin resources are. Then override any module you want:
here is a sample:
module ActiveAdmin
module Views
class Header < Component
def build(namespace, menu)
super(:id => "header")
#namespace = namespace
#menu = menu
#utility_menu = #namespace.fetch_menu(:utility_navigation)
build_site_title
build_global_navigation
build_utility_navigation
#you can add any other component here in header section
end
def build_site_title
render "admin/parts/logo"
end
def build_global_navigation
render "admin/parts/main_nav"
end
def build_utility_navigation
render 'admin/parts/language_options'
insert_tag view_factory.global_navigation, #utility_menu, :id => "utility_nav", :class => 'header-item tabs'
render 'admin/parts/branch_in_header'
end
end
module Pages
class Base
def build_page_content
build_flash_messages
div :id => :wizard_progress_bar do
render 'admin/parts/wizard_progress_bar'
end
div :id => "active_admin_content", :class => (skip_sidebar? ? "without_sidebar" : "with_sidebar") do
build_main_content_wrapper
build_sidebar unless skip_sidebar?
end
end
end
end
end
end
You can customise active admin page in admin/your_model.rb file.
Sample code for active admin is as follows.
ActiveAdmin.register User do
menu :label => "Our User", :priority => 3 #rename menu & set priority#
#for index page#
index :title => 'Our User' do #set page title#
# index :download_links => false do
selectable_column
column :title
column :category do |e| #want to change name of category
e.categoryone
end
column :address
default_actions#this will add default action i.e. show|edit|delete#
end
#end index#
#for controller#
controller do
actions :all, :except => [:edit, :new] # you can decide which all methods to be shown in show page.
end
#end controller#
#show start page#
show do |user|
h3 "User Details"
attributes_table do
row :title
row :description
row :address, :label=>"User Address" #overide address label
row :country
row :approval
end
h3 "Activity Photoes"
attributes_table do
user.uploads.each do |img| #call associated model. Here i want to display uploaded image of upload.rb file
row(" ") do
link_to image_tag(img.to_jq_upload['small_url']), admin_upload_path(img)#here i have added upload module to admin thats y i can directly give path to the upload record of admin module#
end
end
end
active_admin_comments # to show comment block of active admin
end
#show end#
#filer(search) start righthand side panel#
filter :title
filter :category
filter :address
#end filter#
#for menu on show page#
action_item only:[:show] do # this add "Approve User" button on the right hand side of show menu only as we have specified only show method
if User.find(params[:id]).approval == true
link_to "Approve User", approv_user_path(:id => params[:id])#call custom method of normal rails controller#
end
end
#end menu#
#start add side bar on page#
sidebar "Project Details" do
ul do
li link_to("Profile", new_project_path)
end
end
#end side bar#
end

Two pages for the same resource - ActiveAdmin

Currently I have User model, which is registered in user.rb as a new resource for ActiveAdmin. Generated page displays all users with scopes (all/journalists/startup_employees). Now I want to create another page for the same resource, and the same scopes, but there should be only records with waiting field set to true (and the previous page should displays only this with :waiting => false). How could I do that? I know I could do that with filters, but I need two separate pages, with two links in menu.
// SOLUTION
It was even easier than advices (thanks guys!):
ActiveAdmin.register User, :as => 'Waitlist User' do
menu :label => "Waitlist"
controller do
def scoped_collection
User.where(:waitlist => true)
end
end
# code
scope :all
scope :journalists
scope :startup_employees
end
ActiveAdmin.register User do
controller do
def scoped_collection
User.where(:waitlist => false)
end
end
# code
scope :all
scope :journalists
scope :startup_employees
end
STI (Single table inheritance) can be used to create multiple "sub-resources" of the same table/parent model in Active admin
Add a "type" column in user table as a string
Add this to User model to mirror waiting field with type field
after_commit {|i| update_attribute(:type, waiting ? "UserWaiting" : "UserNotWaiting" )}
Create the new models UserWaiting and UserNotWaiting
class UserWaiting < User
end
class UserNotWaiting < User
end
Create Active Admin resources
ActiveAdmin.register UserWaiting do
# ....
end
ActiveAdmin.register UserNotWaiting do
# ....
end
You can run a first-time sync in console
User.all.each {|user| user.save}
..............
Another way could be to skip the type column (steps 1,2 and 5) and solve the rest with scopes.
Step 3 and 4 above
Then create the scopes
#model/user.rb
scope :waiting, where(:waiting => true)
scope :not_waiting, where(:waiting => false)
Scopes in Active Admin
#admin/user.rb
scope :waiting, :default => true
#admin/user_not_waitings.rb
scope :not_waiting, :default => true
Just make sure the other scopes in these two pages are also filtered on waiting/not_waiting
you could use a parameter to distinguish the cases and render different actions depending on the parameter:
link_to users_path(:kind => 'waiting')
and in the users_controller.rb
def index
if params[:kind]=='waiting'
#users= Users.where(:waiting => true)
render :action => 'waiting' and return
else
# do your other stuff
end
end
then put your new, different page (partial) in app/views/users/waiting.html.erb
If you want to use a different layout for this page add the layout parameter to render:
render :action => 'waiting', :layout => 'other_layout' and return

Custom Settings Form in ActiveAdmin

I'm using rails-settings by Squeegy from https://github.com/Squeegy/rails-settings as well as Activeadmin. What I'm trying to accomplish is making a form in ActiveAdmin that I can let the site admin change the settings for the site, which take a command line syntax of:
Setting.foo = "bar"
Setting.site_title = "My Awesome Site!"
Setting.max_users = 35
I really don't think I've got too far, but I'm already stuck. I'm up to the point of having a custom ActiveAdmin form made:
ActiveAdmin.register_page "Settings" do
action_item do
link_to "View Site", "/"
end
content do
form do |f|
#Inputs for Settings
end
end
end
But I don't even know how to begin laying out the form to directly access the Settings model, or how to make a custom controller to handle the input. I suppose if I could get the input sent to a controller that I could make, I'd be just fine.
This is very simple to do with ActiveAdmin.
Lets say your settings class is Settings :
ActiveAdmin.register_page "Settings" do
content do
table :class => 'settings' do
thead do
th 'Setting'
th 'Value'
th ''
end
Settings.all.each do |key, val|
tr do
td strong key
td val
td do
link_to "delete", admin_settings_delete_path( :key => key ), :method => :post
end
end
end
tr do
form :action => admin_settings_create_path, :method => :post do
td do
input :name => 'key'
end
td do
input :name => 'val'
end
td do
input :type => 'submit', :value => 'Add'
end
end
end
end
end
page_action :create, :method => :post do
Settings[params[:key]] = params[:val]
redirect_to :back, :notice => "#{params[:key]} added"
end
page_action :delete, :method => :post do
Settings.destroy params[:key]
redirect_to :back, :notice => "#{params[:key]} deleted"
end
end
Of course you'll need to add some CSS and maybe some validations but you have your settings page.
Edit:
Note that I wrote this for rails-settings-cached, not rails-settings, but my quick search led here so I guess this could still help someone.
I don't think you want your site's form to directly change the settings in ActiveAdmin, I would ...
Create a new table, eg. adminsettings and add fields for each of the settings you want to store for instance site_title, alternatively you could use each row for a setting which means you can add new settings in the future without changing the database
Put together a form in Activeadmin to maintain your settings
Add some functions to your model to grab the settings so you can do something like ..
Setting.site_title = Adminsetting.getsitetitle
You could be clever with your model method and use the method_missing facility so you need the least amount of code to get a setting ...
class << self
def method_missing(method, *args, &block)
setting = Adminsetting.where(:code => method.to_s).first
if setting
return setting.content
else
return super(method, *args, &block)
end
end
Perhaps you could package this into a Gem as it could be a useful thing for others.

Resources