HAML nesting tags - haml

I'm trying to nest this
%h1 Admin Menu
%small logged in as: #{session[:username]}
To get something like this
<h1>Admin Menu <small>logged in as: something</small></h1>
But the only way I can get it to display without firing an error is putting them at the same level, say
%h1 Admin Menu
%small logged in as: #{session[:username]}
Which outputs:
<h1>Admin Menu</h1>
<small>logged in as: something</small>
It's probaby something silly but I have no idea this why this wouldn't work?

Haml only allows inline nesting if everything that will be nested is inline. So you can do this:
%h1 Admin Menu
or this:
%h1
Admin Menu
%small logged in as: #{session[:username]}
but not this:
%h1 Admin Menu
%small logged in as: #{session[:username]}
The first form should really be thought of as a convenient abbreviation. It exists so you can take something like this:
%li
One
%li
Two
%li
Three
and just say:
%li One
%li Two
%li Three

I haven't tried, but won't the following work for you?
%h1
Admin Menu
%small logged in as: #{session[:username]}

The other trick is this.
%h1
Admin Menu
%small
logged in as:
=session[:username]
because last 2 lines will be rendered inside "small"-tag

Or you could just use simple css to do the trick, as:
%h1
Admin Menu
.small{ style: 'font-size: smaller;' }
logged in as:
= session[:username]
this way you can specify other attributes or the specific size of the font.

Related

Active Admin Rename the resource in different places

The documentation allows you to rename a page pretty easily, but not exactly in the way I'd like.
Given this code: ActiveAdmin.register User, :as => "Static" gives a menu name of statics
I was wondering if there was a way to customize it so the name in the menu is not pluralized => static
Rename the menu item: menu :label => "Static"
Rename the title in the top left corner: index :title => "Static"
Didn't really see them when looking through the documentation, but saw them being used in other questions/code

How to hide the resource in activeadmin gem

I create an admin page by using activeadmin gem http://activeadmin.info/
I use cancan to authorize the privilege for 2 kind of user, user manage and book manage user.
My question is: How I hide the resource User when a book manage user log in to admin page?
I tried something like that but it didn't work
menu false if can? :manage, BookHeader
or
menu false if authorize! :manage, BookHeader
Thanks for your help!
You have to use proc for build menu dynamicly
Examples
dynamic label
menu :label => proc{current_admin_user.admin? ? "Accounts" : "My Account"}
Display/Hide
menu :if => proc{ can?(:manage, BookHeader ) }
For more info about integrating AA and CanCan read this article
https://github.com/gregbell/active_admin/wiki/How-to-work-with-cancan
i use this:
for me was users for adminuser,
password for none admin user
menu label: proc{I18n.t "#{current_user.is_admin? ? 'users':'password'}"}
Following your example
menu label: proc{I18n.t "#{current_admin_user.admin? ? 'Accounts' : 'My Account'}"}
will help

Conditionally change resource name Activeadmin

I'd like to conditionally change the label of a resource in ActiveAdmin based on the user role. I'd like to keep the default pluralization of the name for admins, but for regular users, I need to change the name.
I.e. I have a resource Users, which I would like to change to My Account for the regular user (since they won't see #index).
I'm trying something like
ActiveAdmin.register User do
menu :if => proc {if !current_user.admin?
menu :label => "My Account"
else
menu :label => "I hate Users"
end}
Anyone know how to conditionally name the resource?
Thanks!
This works:
menu :label => proc { true ? "I Hate Users" : "My Account" }
But then to access the
current_admin_user
or the
current_user
object from within the proc won't work. I haven't found a way to get the logged in user object from within the ActiveAdmin::MenuBuilder scope. There are suggestions, see e.g. https://stackoverflow.com/a/2513456/790737 where you set a variable in
Thread.current
after succesfull login. I guess you will have to hook in to the post-authentication work of devise. Good luck.

Activeadmin disabling the "new resource" method

I'm using Activeadmin for the admin interface on an app I'm working on (loving it) and I am curious if there is a way to disable the "New Resource" link in the upper-right corner of the resource show page?
The particular resource I'm using is nested inside another resource and I have a partial that allows it to be created from the show page on that parent resource.
I have disabled the resource in the menu, but I'd rather leave the resource in the menu so I can see/edit/delete those resources without having to find it by looking through its parent resource.
Previous solution didn`t work for me, so here is general solutions, that works always:
ActiveAdmin.register Book do
actions :index
#or like that
#actions :all, :except => [:destroy]
index do
column :title
column :author
end
end
Try config.clear_action_items! to remove the link to New and other links on top of the table
This removed the "New Resource" button from the top-right:
config.clear_action_items!
This removed both the "New Resource" button as well as the box "There are no resources yet - create one".
actions :all, :except => [:new]
Thank you, Irio
config.clear_action_items!
Will remove all the actions.
If you only want to remove the new action link you can also use:
config.remove_action_item(:new)
I know this is an old question, but I just came up to it (had the same problem), and realized that config.clear_action_items! and actions :all, :except => [:new] are fundamentally different.
config.clear_action_items! will remove the New button from the index page, while actions :all, :except => [:new] will remove both the button, AND the route, meaning you can't call it from another place (which, in my case, is needed).
I did this:
controller do
def action_methods
if some_condition
super
else
super - ['new', 'create', 'destroy']
end
end
end
To disable some of the possible actions. action_methods returns an array of the 7 standard CRUD actions, so you can subtract those you don’t want
Or even:
ActiveAdmin.register Purchase do
config.clear_action_items!
actions :index
end
Worked for me too ! :-)
ActiveAdmin.register AssetSumView do
menu :label => "Asset Summary View", :parent => "Things"
# no button for NEW (since this is a db view)
#---------------------------------------------------------------------------------------------
config.clear_action_items!
enter code here
action_item do
link_to "Assets" , "/admin/assets"
end
action_item do
link_to "AssetCatgCodes", "/admin/asset_catg_codes"
end
#---------------------------------------------------------------------------------------------
config.clear_action_items! does only half of the job. There is one issue though.
In case of empty index table, active admin show this message
There are no [Resources] yet. Create one
which doesn't get hidden by the above command and I don't want to entirely disable the action. So, I kept the link and edited the new action to redirect to the parent resource index with a message.
controller do
def new
if params[:parent_id].present?
super
else
redirect_to parent_resources_path, notice: "Create Resource through ParentResource"
end
end
end

Multiple Haml Elements on Same Line

I want to be able to have two Haml elements on the same line. For example:
%h1 %a{:href => '/'} Professio.
That doesn't work. How would I get something like this to work without borking?
Late answer, but I was just tackling the same problem myself and knew HAML had a way to do this as part of the language. See the White Space Removal section of the HAML reference.
You could do it this way:
%h1<
%a{ :href => '/' } Professio.
Or this way:
%h1
%a{ :href => '/' }> Professio.
Unfortunately, you cannot put two haml elements on the same line.
You can achieve something similar by using inline html elements:
%h1 <a href='/'>Lorem ipsum</a>
Why don't you like this format?
%h1
%a{:href => '/'} Professio.
Another option is to write special 'helper' method (that generate an html link). For example, the link_to in Rails:
%h1= link_to 'Professio', root_url
Haml cannot do this. Slim can:
h1: a( href='/' ) Professio.
is the same as:
h1
a( href="/ ) Professio
You can write as deeper tree as you need:
ul.nav
li.active: a( href="/home" ): strong Home
li: a( href="/contact" ): span Contact
Jade also has similar syntax and support this feature, but it's been designed for Node.js environment.
If you're looking to preserve the HTML on the same line you could try something like this:
irb> print Haml::Engine.new("%h1<\n %a{:href => '/'} Profession.").render()
<h1><b href='/'>Profession.</a></h1>
Found here: HAML whitespace removal
[Edit: I know that's says b href above...]