How to add a report/custom page to ActiveAdmin? - ruby-on-rails

I am using ActiveAdmin for my Ruby on Rails application. My model consists of a couple entites such as "Plan", "Country", etc. I would like to have custom page for reporting purposes. This page should work on a collection of selected plans. So, I need scopes and filters. Currently one page associated with "Plan" model for CRUD operations. I don't want to add anything else to my data model or database since I have everything I need in my current model. How could I make such a page and access it through the menu?
Could I associate another page with my "Plan" model as follows?
ActiveAdmin.register Plan do
menu label: 'Report', parent: 'Planning', priority: 2
...
end
Could I have a custom page working on set of plans selected using scopes and filters?
ActiveAdmin.register_page 'Report' do
menu parent: 'Planning', priority: 2
...
end
Or, how could I add a new page/route to the currently associated page with "Plan" model for reporting? Can collection_action help me? If yes, how could I exactly bring a collection of Plans and show them?

You can register your ActiveRecord model several times in ActiveAdmin as a new resource:
ActiveAdmin.register Plan, as: 'PlanReport' do
menu label: 'Report', parent: 'Planning', priority: 2
...
end
In this file you can use all of the ActiveAdmin features like scope and filters.
https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#rename-the-resource

Related

Rails Active Admin Select or Create New

I have a list of products and i have a supplier associated with the product. So when you add the product select the supplier from a dropdown. But If the supplier doesn't exist, i want the ability for the user to add a new supplier via a + icon next to the dropdown box which opens a new tab with suppliers/new. I've looked into the select2 addon, but it doesn't give me what I need.
https://select2.org/tagging
Please look at the select2's dynamic option creation. However documentation is hard to understand.
May be you can think of using modals like this type of situations.
I solved this with the activeadmin_addons gem. It turns select fields into select2 by default and with the tags option you can enable the creation of new options:
app/admin/products.rb
form do |f|
f.input :supplier, tags: true
end
I would've expected Select2 to send a create request when selecting a new option, however, it will send the name of the new option in the supplier_id param. Nevertheless we can work with that:
app/admin/products.rb
controller do
def create_supplier
return unless params.dig('product', 'supplier_id').is_a?(String)
params['product']['supplier_id'] = Supplier.create!(name: params['product']['supplier_id']).id
end
def update
create_supplier
super
end
def create
create_supplier
super
end
end

Ransack and display by category

I want to display users based on their type, so on the user overview page I have three headers: admins / moderators / members. Under each header I show the users that are part of that group. So I query on user_type and find all users that are belonging to this group.
Each member is also works at a certain company. I'm creating a filter that uses ransack to filter on that company:
= f.input :users_company_id_in, collection: Company.order(:name), label: 'Company'
In my controller I'm using ransack like this:
def index
#q = UserType.includes(:users, :active_users).ransack(params[:q])
#user_types = #q.result(distinct: true)
end
Since in my view I'm fetching the users by using #user_types.users, this query is not respecting the ransack parameters, so I'm seeing all users for every category that has someone working for that company.
What I'm trying to achieve is obviously to see only the users that work for a specific company and only display a certain user_type if there are people in the category after filtering.
Any ideas on how to achieve this?

Active Admin Model Index Page (Model List View) define order of displayed fields

Is there an easy way to change the order of displayed field of a Model in the Active Admin index view for a model? I have far more fields then Active Admin can display but i want to be able to define the most relevant fields for the user in this view.
The relevant fields in my case (see screenshot) are hidden (..) by Active Admin and only can be seen then i change to the show view of a model instance.
You should be able to define the fields to display in the index view inside the ActiveAdmin model configuration, i.e.
ActiveAdmin.register Place do
config.sort_order = 'name_asc'
index do
column :name
column :address
column :place_type
column :published
default_actions
end
end
You can read more about ActiveAdmin options here: http://www.activeadmin.info/docs/3-index-pages.html

Rails 3 and ActiveAdmin. How can I add a virtual model?

Is ther a way to add a virtual model? Something such as a second dashboard where you can display more than one different resources?
I ask because I need to create a page with nothing but links to documents, like customer invoices, corporate invoices and other documents from other models.
The only way I know is to create some empty model:
app/models/fake.rb:
class Fake
end
then create generator app/models/fake.rb:
ActiveAdmin.register Fake do
config.comments = false
config.clear_sidebar_sections!
config.clear_action_items!
collection_action :index do
# here you can set you template
render 'you_template', :layout => 'active_admin'
end
end
The last changes in the master branch of ActiveAdmin deprecates the Dashboard and creates the concept of custom pages. See this .

How to let user modifying content of pages with rails ? Query for text?

I'm developing a website with Ruby on Rails.
I want to find the better way to let users (not developers) to edit text on some pages (like the index...). (like a CMS ?)
Actually they had to get the page through FTP, to edit the text and to put the new file on the server (through FTP).
It's a very very bad practice and I wanted to know if someone has an idea to solve this problem ?
Many thanks
It would be the same as the basic Rails CRUD operations. Just make a model/controller representing page content, and an edit view for the controller. Then on the pages you want text to be editable, instead of having the content directly on the page just use a view partial.
Of course, you would probably also want to implement some type of authentication to make sure not just everyone can edit pages.
Well, one thing you could do is add a model to your database called "Content" or "Copy" which represents some text on a page. Then you could use polymorphic association to link the content/copy to your actual model. For instance, if you had a page with a list of products on it, you'd likely have a Product model in your database. You could do something like this:
class Content < ActiveRecord::Base
belongs_to :contentable, :polymorphic => true # excuse my lame naming here
# this model would need two fields to make it polymorphic:
# contentable_id <-- Integer representing the record that owns it
# contentable_type <-- String representing the kind of model (Class) that owns it
# An example would look like:
# contentable_id: 4 <--- Product ID 4 in your products table
# contentable_type: Product <--- Tells the rails app which model owns this record
# You'd also want a text field in this model where you store the page text that your
# users enter.
end
class Product < ActiveRecord::Base
has_many :contents, :as => :contentable # again forgive my naming
end
In this scenario, when the product page renders, you could call #product.contents to retrieve all the text users have entered for this product. If you don't want to use two separate models like this, you could put a text field directly on the Product model itself and have users enter text there.

Resources