ActiveAdmin: access to the result of filters - ruby-on-rails

I want to display few stats on top of an ActiveAdmin index page.
Let's say I have a car model, with a color attribute and a year of fabrication. On the top of my index page, I want to display the count of red cars. But I want that count to reflect the use of filters; if I choose to show only cars built in 1995, I want the red car count to change accordingly.
How can I access the filtered list? I guessed the existence of a variable containing the result of filter action, but I can't find it.

Turns out you can use the variable collection anywhere in your index.
Something like this is working perfectly, and reflects the state of filters:
index do
div "Total: #{collection.count}"
column :attribute1
column :attribute2
actions
end
Another way is to use ransack, with this:
ModelName.ransack(params[:q]).result
But it does a useless query, call ransack again for no reason.
Inspiration found in this question.

Use this method
apply_filtering(collection)
Inspiration found in this question

Related

Filter view behaviour when filtered list changes

I have a google sheet which summarises work for staff, and a filter view for each staff member so they can quickly see what they need to do. This works well.
However, if a new person is added to the staff list, every filter view now includes that new person's tasks as well.
How can I prevent this to avoid having to update every filter view each time a new staff member is added?
The solution (which occurred to me while typing the question) is to create the filter using a condition (eg "text starts with FirstName").
you could use regular formula instead:
=QUERY(A:Z; "where A = 'FirstName'")
or if you wana play with "start with":
=QUERY(A:Z; "where A starts with 'FirstName'")

In JIRA is there a way to create a nested table?

The JIRA documentation does not mention a way to create a nested table something like
|outer-col1|outer-col2|
|col1 val1||inner-col1|inner-col2|
|icol1 val1|icol2 val1|
|icol1 val2|icol2 val2|
|col1 val2||col2 val2|
(or) atleast something where I can span a multiple rows for a column value
i.e.
|outer-col1|outer-col2|outer-col-3
|col1 val1 |col2 val1a|col3 val1a|
|col2 val1b|col3 val1b|
|col1 val2||col2 val2 |col3 val2 |
The only way I can span now is to have empty values in the column and have the reader assume that it represents the previous rows value for that column.
Found a way around to make nested tables at below linked page
https://community.atlassian.com/t5/Confluence-questions/Nested-table-in-wiki-markup-Confluence-4/qaq-p/18355
The solutions suggest many tags that can be used to achieve this. A simple one is {panel} tag. Just wrap the Inner table inside two {panel} tags.
Below is a sample and its result
Sample
||Tabe1Column1||Table1Column2||
|item1.1|item1.2|
|item2.1|Sub table {panel}
||Table2Coumn1||Table2Column2||
|sub item1.1|sub item 1.2|
|sub item 2.1|sub item 2.2|{panel}|
No, this is not supported in JIRA out of the box.
However, the JEditor add-on that is available on the marketplace does support this.
Since JEditor replaces JIRA's wiki editor it can have quite some impact on a JIRA instance, depending on how your users are used to work with mark-up. It can also affect other add-ons, so best to test properly before buying.

How to display all values of a custom field in a given category?

In a WordPress category that has posts in it, each post has a custom field that has a value (usually a number).
The question:
How do I get all the custom field values from all the posts from that category?
(I want to be able to use those values independently (to strip non-numerical characters, make average, etc).
Thank you,
Puiu D. - Romania
get_post_custom returns a multidimensional array with all custom fields of a particular post or page.
EG:
$custom_fields = get_post_custom($post_id);
You can get all your posts in a specific category using get_posts and loop through them to get their specific custom fields.
Function Reference: http://codex.wordpress.org/Function_Reference/get_post_custom

Is it possible to access full index scope at sidebar in activeadmin?

I have created ActiveAdmin administration interface for Bill model in my application, and want to display statistic information in sidebar on index and filtered searches (like total amount of current filtered bills). In sidebar seaction I have access to bills relation like that:
sidebar do
bills.sum(:amount)
end
bills correctly scoped with current filters: date, number etc.
But 'bills' relation contains only 30 records (which displayed on page, 30 is default pagination size), but in total it's more than 300. How can I get "unpaged" relation for current filtered bills (with current selected filters)?
Get it myself, I can use bills.limit(nil), which override current page selection in relation, and returns full scope.

Rails 3: Predictive filtering (faceted navigation)

Users can currently apply filters to a database query by setting checkboxes, e.g.
Colour
[] Red
[] Green
[] Blue
Shape
[] Round
[] Square
Size
[] Small
[] Medium
[] Large
Clicking on a checkbox triggers an AJAX request which appends the where condition to the query, e.g.
#mymodel = MyModel.scoped
#mymodel = #mymodel.where(:colour => params[:colour]) if params[:colour]
...
What I would like to do now is add a count after each checkbox to indicate how many results would be displayed if a given checkbox is selected. e.g.
Colour
[] Red (148)
[] Green (121)
[] Blue (136)
...
Upon clicking a checkbox, the counts next to all checkboxes should be refreshed, and options pertaining to a zero result will not be displayed. This needs to work across all filter categories, so for example, if there is nothing small and red in the database, the 'Small' option should disappear when 'Red' is selected, and re-appear when it is deselected.
What is the best way to achieve this type of predictive filtering?
Many thanks.
Update
More specifically, my question is: What is the best way to implement faceted navigation in Rails?
(Thanks #Pasta for the identifying the term for this)
There are quite a few open-source search engines that will give you faceted search. We use Thinking Sphinx and have been very happy with it. It looks like they have preliminary support for Rails 3, but it might be worth a shot. The facet syntax is fairly straightforward:
Example:
# This will return any search results for 'pancakes'
Article.search 'pancakes'
# This will return any facets for 'pancakes'
Article.facets 'pancakes'
You might also want to look at Solr and Ferret -- both of which I believe support faceted search.
Take a look at faceted search.
Cheers;
although I can't answer fully, I suggest you look into updating this "form" or partial through ajax. Ryan had a railscast 43 will give you a few ideas on how it can work, but then you can research how to do all the fancy stuff with jquery and rails3.
I wrote a gem for ActiveRecord based filtering and facet calculation, that does not depend on external services/deamons: https://github.com/fortytools/forty_facets

Resources