Customise layout (components ) for activeadmin - ruby-on-rails

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

Related

Remove header conditionnally in active admin on rails5

I need to embed my active admin pages in a parent application.
The active admin page will be shown in an iframe and I need to remove the header and footer on the default index, show and edit pages if the url query param embedded=true.
I proceed like this in my active admin page :
ActiveAdmin.register Followup, as: 'Followup_affectation' do
controller do
def setLayout
if params['embedded'] == 'true'
#layout = false
else
#layout = 'active_admin'
end
end
end
And in some custom action
render template: "admin/followups/mytemplate.html.haml", layout: #layout
But I want to do it for index.
I tried
def index
render :index, layout: #layout
end
and
def index
super
render :index, layout: #layout
end
My index definition is really classic :
index do
id_column
column :step
column :cycle
column :tag_rfid
column :quoteline
column :product_name
column :location
column :active
actions
end
Thanks in advance for your help
I finally find a solution.
1 - Create header.rb in app/admin/
module ActiveAdmin
module Views
class Header < Component
def build(namespace, menu)
super(id: "header")
#add a class hide wich diplay none on the menu
if params['embedded'] == 'true'
super(class: "hide")
end
#namespace = namespace
#menu = menu
#utility_menu = #namespace.fetch_menu(:utility_navigation)
build_site_title
build_global_navigation
build_utility_navigation
end
def build_site_title
insert_tag view_factory.site_title, #namespace
end
def build_global_navigation
#do not insert tag if params embedded is true
insert_tag view_factory.global_navigation, #menu, class: 'header-item tabs' unless params['embedded'] == 'true'
end
def build_utility_navigation
insert_tag view_factory.utility_navigation, #utility_menu, id: "utility_nav", class: 'header-item tabs'
end
end
end
end

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
%>

How to display ActiveAdmin menu tabs in the dashboard?

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

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.

Using View page, Controller and Helper

I am new in Rails. And I have a project that;
I should get a value from user in View page (for example index), and I should use the value in Helper then send the result of Helper to Controller and show the result in a new View page (for example details). Additionally I have to save results to database. Right now I have helper, controller and view pages but I can't connect these three part to each other. I need help
Controller;
def index
#user = Mpeople.new[:user]
redirect_to "secondstep"
end
def secondstep
# helper should have controled here
redirect_to "following"
end
def following
#user = Mpeople.all
end
Model;
class Mpeople < ActiveRecord::Base
has_one :username
accepts_nested_attributes_for :username
end
View;
<% form_for :user, :url => {:action => "index"} do |pform| %>
<% pform.fields_for :person do |namefield| %>
Twitter Name : <%= namefield.text_field :username %>
<%= button_to "OK", :action => "following" %>
<% end %>
<% end %>
And helper is more longer, it sends twitter name to twitter and get following of a user from api.twitter.com
This is some of my helper; I edit it after your comment but I am not sure if it is correct or not.
module FafHelper
class PeopleController
require 'people_helper'
# txtname = indexteki textbox'un adına eşitle
#txtname = tname
txtname = namefiled.text_field
.....
a_get("1/users/lookup.#{json}").
with(:query => {:screen_name => txtname, :user_id => id_list}).
end
end
..
You no need to connect views and helper as by default all the helper modules are included in the views.
And do include the helper in you controller. Helper is a module and controller is a class. Just include the module in the class.
To get the clear picture please post exactly your structure.

Resources