activeadmin, remove empty message - ruby-on-rails

In ActiveAdmin, when there are no items for the model (in my example User), it shows a default 'There are no Users yet. Create one'.
How can I remove this message?
Is there the possibility of customizinig it on per-page basis, that is having a particular message for a particular ActiveAdmin page?

This is a MonkeyPatch:
Create a new file in lib folder and copy:
module ActiveAdmin
module Views
# Build a Blank Slate
class BlankSlate < ActiveAdmin::Component
builder_method :blank_slate
def default_class_name
'blank_slate_container'
end
def build(content)
super(span(content.html_safe, class: "blank_slate"))
end
end
end
end
Customize the content variable in build method to change the default message.

For now you cannot do it by ActiveAdmin settings. Look issues in
repository.
You can render any html or erb.html in your resources

You may use poorly documented (but not so monkey-path) blank_slate_link index page option. It lets you override this message in any of your ActiveAdmin's resource index page.
See https://stackoverflow.com/a/72529909/1744707 for details.

Related

How to get the path for a collection in ActiveAdmin

I made custom index view for ActiveAdmin, and I would need to get the admin path for the given collection.
Here is what I have:
module ActiveAdmin
module Views
class IndexAsSpecial < ::ActiveAdmin::Component
def build(page_presenter, collection)
//...
// should be something like /admin/posts
path_to_collection = ???
//...
end
def self.index_name
"special"
end
end
I have searched and tried, but all I found is paths for a resource, not for the collection (e.g. resource.route_collection_path )
I also found this, but here I am still missing the right parameters, or maybe the class name of the active admin collection:
Rails.application.routes.url_helpers.polymorphic_path([:admin,:posts])
Just that I need to replace :posts somehow dynamically.
Any ideas?
I must say, that the documentation of ActiveAdmin is very vague here, but after debugging a bit more I found the answer to my question:
helpers.collection_path # will generate /admin/posts
or if you need a member action on the collection:
helpers.resource_path(:action_name) # will generate /admin/posts/action_name

ActiveAdmin - blank slate - customized message - Rails

How can i change the blank_slate message on ActiveAdmin. Each of my models would have a different blank_slate message.
Example:
Transport: No transports, do this and this
Car: Did you forget, no cars until this
You can monkey patch ActiveAdmin to load the message as desired:
require 'active_admin/helpers/collection'
module ActiveAdmin
module Views
module Pages
class Index < Base
protected
def render_blank_slate
# for example only, you can define your own I18n structure
# You can use active_admin_config.resource_label too if not mistaken
blank_slate_content = I18n.t("active_admin.blank_slate.content.#{active_admin_config.plural_resource_label}")
insert_tag(view_factory.blank_slate, blank_slate_content)
end
end
end
end
end
load this file to the initializer folder and put the messages in your language YAML file following the structure as defined in the blank_slate_content assignment line.
You can use (poorly documented but not so monkey-path) PagePresenter.blank_slate_link option responsible for rendering a link to new resource on an empty index page (see GitHub source). You may override it in your ActiveAdmin's resource index page using blank_slate_link keyword in the header of the index page definition. E.g. (app/admin/transport.rb):
ActiveAdmin.register Transport do
# Some cool initialization: scope, filters, ...
index blank_slate_link:
-> { "No transports, "+
link_to("do this and this", new_admin_transport_path) }
do
# ...other index page details go here...
end
# ...rest of pages: show, edit, ...
end
This way you get as sophisticated link as you want. And the I18n's active_admin.blank_slate.link is no longer used for generating link to a new resource.
But as you see render_blank_slate still uses the hard-coded active_admin.blank_slate.content string to render text right before the link (e.g., "There are no %{resource_name} yet."). If you want to get rid of this text you should override it in your YAML localization file. You may simply leave it empty (app/config/locales/en.yml):
en:
active_admin:
blank_slate:
content: ""
Or, in your case I'd suggest:
en:
active_admin:
blank_slate:
content: "%{resource_name}: "
which gives you the desired "Transport: No transports, do this and this" message.

How to hide Add new option in Rails Admin

I am customizing Rails Admin : https://github.com/sferik/rails_admin , i need to disable/hide "Add new" option for some model.
Any help will save lot time for me. Thanks in advance
I use the following to achieve this on a specific model. Hopefully, this helps:
config.actions do
new do
except ['Some Model']
end
end
The answer is in the configuration documentation for actions. By default, all actions are possible, including new. To customize the possible actions, in config.actions in config/initilizers/rails_admin.rb, list all the actions you want to support, leaving out the ones you don’t want to support. For example, here is a config block that allows all of the default actions except for new:
# config/initilizers/rails_admin.rb
RailsAdmin.config do |config|
config.actions do
# root actions
dashboard
# collection actions
index
# `new` is NOT allowed
export
history_index
bulk_delete
# member actions
show
edit
delete
history_show
show_in_app
end
end
To have multiple models, you must put each model in single quotes. For example, consider the following configuration:
config.actions do
dashboard
index do
except ['Address']
end
new do
except ['Address', 'Employee', 'Setting']
end
export
show
edit do
except ['Employee']
end
end
This means that:
Addresses are not included on the navbar on the left
You cannot add a new address employee or setting with the "add new" button
There is no pencil icon in the index view for Employees for editing.
If you had a User model you could see it in the navbar, edit it, and add a new one on the index page.
You can export every model, but not bulk delete them.
Implemented it with Cancan. You can refer to above answer to do it in rails admin way.
URL : https://github.com/sferik/rails_admin/wiki/CanCan

Permalinks with Ruby on Rails (dynamic routes)

I am currently developing a blogging system with Ruby on Rails and want the user to define his "permalinks" for static pages or blog posts, meaning:
the user should be able to set the page name, eg. "test-article" (that should be available via /posts/test-article) - how would I realize this in the rails applications and the routing file?
for user-friendly permalinks you can use gem 'has_permalink'. For more details http://haspermalink.org
Modifying the to_param method in the Model indeed is required/convenient, like the others said already:
def to_param
pagename.parameterize
end
But in order to find the posts you also need to change the Controller, since the default Post.find methods searches for ID and not pagename. For the show action you'd need something like this:
def show
#post = Post.where(:pagename => params[:id]).first
end
Same goes for the other action methods.
You routing rules can stay the same as for regular routes with an ID number.
I personally prefer to do it this way:
Put the following in your Post model (stick it at the bottom before the closing 'end' tag)
def to_param
permalink
end
def permalink
"#{id}-#{title.parameterize}"
end
That's it. You don't need to change any of the find_by methods. This gives you URL's of the form "123-title-of-post".
You can use the friendly_id gem. There are no special controller changes required. Simple add an attribute for example slug to your model..for more details check out the github repo of the gem.
The #63 and #117 episodes of railscasts might help you. Also check out the resources there.
You should have seolink or permalink attribute in pages' or posts' objects. Then you'd just use to_param method for your post or page model that would return that attribute.
to_param method is used in *_path methods when you pass them an object.
So if your post has title "foo bar" and seolink "baz-quux", you define a to_param method in model like this:
def to_param
seolink
end
Then when you do something like post_path(#post) you'll get the /posts/baz-quux or any other relevant url that you have configured in config/routes.rb file (my example applies to resourceful urls). In the show action of your controller you'll just have to find_by_seolink instead of find[_by_id].

rails - how to override default views

by default, when i ask rails controller to do messages/index, he does
def index
respond_to{|fmt| fmt.html}
end
and shows app/views/messages/index.html.erb
there is a customer which wants his instance of the platform to display views differently (and changes cannot be done with css only).
solution i think of would be
create directory app/views/#{customername}, which would have same structure as app/views, but would only have views which have to override default ones.
setting an array constant containing list of views which have to be overriden (if not, they should load the default views)
CUSTOM_VIEWS["messages"]=["index","show","edit"]
somewhere in the customer-specific config file
in all controller actions do something like
def index
respond_to do |fmt|
fmt.html do
if CUSTOM_VIEWS[params[:controller]].include?(params[:action])
#override default app/views/messages/index.html.erb with app/views/customername/messages/index.html.erb
render "#{customername}/#{params[:controller]}/#{params[:action]}"
end
end
end
end
or is there better/faster solution/plugin to do that?
This should help:
http://railscasts.com/episodes/125-dynamic-layouts
i believe "view_paths" along with "prepend_view_path" can be an answer to my question
for example
http://www.axehomeyg.com/2009/06/10/view-path-manipulation-for-rails-with-aop/
upd:
solved with simple add to application_controller
def override_views
if APP_CONFIG['pr_name']!=nil
ActionController::Base.view_paths=[RAILS_ROOT+"/app/custom_views/"+APP_CONFIG['pr_name'],RAILS_ROOT+"/app/views/"]
end
end
where APP_CONFIG['pr_name'] is specific product name.
basically what it does is loading custom view from app/custom_views/customername/ if it exists for specific controller action, if not it loads default view from app/views/

Resources