rails+rails_admin customize content of a specific field - ruby-on-rails

I'm using rails_admin gem.
I'm customizing the edit action of a model, so there are many fields that display by default rails_admin.
There is a field to select some value using a select box for multiple value.
Is possible to customize the content of this field? by default rails_admin show me the name of the attribute, but i want to display the name and other attribute like: id and created_at.
Is possible?
SOLUTION
in the model of the field to customize:
#app/models/my_model.rb
rails_admin do
object_label_method :to_label
end
def to_label
"#{name}-#{attribute}-#{other-attribute}"
end

Related

Multiple attributes in activeadmin associated dropdown

I am using Active Admin where I have two models User and Post. The relationship is User has many Post and Post belongs to User. The attributes of User are name, id, phone-number, email and address. In new form of Post, associated dropdown of User is only showing name of each user but I want to show name,phone-number and address of every user. I am new to RoR. So any help will be highly appreciated.
To modify the associated dropdown you should edit your form association's input in the ActiveAdmin form. ActiveAdmin uses formtastic to generate forms for your models. When you set your User model in your Post's form as f.input :user ActiveAdmin set the input type to :select.
So, you can use f.input :user, as: :select and it'll work in the same way.
Formtastic Select let us edit the label rendered in each option through the member_label property. First add the property to your input in the ActiveAdmin form
f.input :user, member_label: :full_label, then declare the full_label method in your User model returning the joined string with the user attributes.
def full_label
"#{name} - #{phone-number} - #{address}"
end

Create a custom string filter in ActiveAdmin that defaults to Contains and doesn't show a string filter choice drop down - Formtastic & Ransack

The default string filter in ActiveAdmin has a select the options of:
Contains
Equals
Starts with
Ends with
This shows a dropdown next to the string search where you are able to choose these options.
My requirement is to have the filter to use contains search condition BUT not show the drop down/select to do this. So it will just have an input box for search, with no select to choose contains.
I originally achieved this by creating a partial, but that was problematic as it was then unable to work with the other filters that ActiveAdmin provides. Here is an image of the partial:
My current thinking is to create a custom filter that does this.
So below is the standard code for the string filter in ActiveAdmin. How could this be modified to default to contains and not show the drop down? I've tried removing the filter options but that doesn't work. Any ideas?
ActiveAdmin uses Ransack and Formtastic
module ActiveAdmin
module Inputs
class FilterStringInput < ::Formtastic::Inputs::StringInput
include FilterBase
include FilterBase::SearchMethodSelect
filter :contains, :equals, :starts_with, :ends_with
# If the filter method includes a search condition, build a normal string search field.
# Else, build a search field with a companion dropdown to choose a search condition from.
def to_html
if seems_searchable?
input_wrapping do
label_html <<
builder.text_field(method, input_html_options)
end
else
super # SearchMethodSelect#to_html
end
end
end
end
end
To remove the need for a predicate drop down, you can use the Ransack's string-based query interface. For example, if you have an ActiveRecord User class with an attribute name and you want a containing filter on it, you could do something like:
ActiveAdmin.register User do
filter :name_cont, label: 'User name'
end
This will generate:
And it will search for Users containing the introduced input in its name
From your question I can conclude, that you are looking for to have a filter, that searches by :contains, and not having a drop down for other options (:equals, :starts_with, :ends_with).
If I do understand you right, you can simple use this (and not having to monkeypatch the ActiveAdmin):
filter :attribute_name, as: :string, label: 'Your custom label, if default doesn't fit'
As a bonus I can suggest you nice gem ('chosen-rails' I've discovered it today), which allows you to do autocomplete on filter (originally it is used to autocomplete the associated model in new/edit form, but I've easily adjusted it for my needs).
So for filters it is as easy as:
filter :title, as: :select, collection: -> {ClientApplication.all.map{|s| s.title}.uniq}, input_html: { class: 'chosen-input' } #or as you've shown before, using pluck :)
The only disadvantage, is that it works only if your string starts with the same letters, as the searched thing, e.g. if name is "Hello", it will hint you when you type "H", "He" and so on, but won't if you type "el", "llo" etc.
EDIT
Ok, The only thing you need to adjust the filtering of ActiveAdmin to your need is to change (comment the line responsible for adding dropdown) the to_html method in module [SearchMethodSelect][2]:
module Inputs
module FilterBase
module SearchMethodSelect
#other methods
def to_html
input_wrapping do
label_html << # your label
#select_html << # THIS LINE -- the dropdown that holds the available search methods
input_html # your input field
end
end
#other methods
end
end
end
I've test it out, and it still works as :contains, check it out :)

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

How can I implement sunspot search when it's nested

How can I extend my search?
I'd like to search User at users_controllers#index when user pressed search button.
Now I have 3 models.
User > User_profile > Prefecture
User_profile has these column such as user_id, and prefecture_id.
In Prefecture model, it has its id, and name (Exp: California, New York)
Now I have setup models/user.rb like this. If I want to add prefecture search, what should I add to this? User should be able to type in California, and it hits search.
searchable do
text :user_profile_nickname do
user_profile.nickname
end
text :user_profile_introduction do
user_profile.introduction
end
text :tag_list do
tag_list
end
end
Each user will be stored as a document in Solr and you need to give the prefacture information to the user document in order for it to be searchable.
Try:
searchable do
text :user_profile_nickname do
user_profile.nickname
end
text :user_profile_introduction do
user_profile.introduction
end
text :tag_list do
tag_list
end
string :prefacture do
user_profile.prefacture.name
end
end
I would use string instead of text since you don't need to apply text processing such as stemming on the prefacture. And with Sunspot I don't think it's possible to build facets on text fields.

Rails 3 ActiveAdmin. How to use ONE field for SIX columns on a search in Active Admin?

How can I add a custom filter or create a custom search field with ActiveAdmin?
I need to generate a report on wire transfers. There are these fields in the Invoices model
wire_transfer_1_ar
wire_transfer_2_ar
wire_transfer_3_ar
wire_transfer_1_customer
wire_transfer_2_customer
wire_transfer_3_customer
So I have this
filter :wire_transfer_1_ar
filter :wire_transfer_2_ar
filter :wire_transfer_3_ar
filter :wire_transfer_1_customer
filter :wire_transfer_2_customer
filter :wire_transfer_2_customer
So when a wire transfer is found, then I can click a link to generate a report.
How can I use only ONE field, to search for all those 6 fields?
ActiveAdmin is using metasearch gem inside, so you can do this way:
ActiveAdmin.register Store do
filter :title_or_name_or_description_contains, :as => :string
end

Resources