Custom filter in rails ActiveAdmin using ransack - filter

I have created an activeadmin filter that has the following choices on filtering the table data in its drop-down menu.
Choice A
Choice B
Choice C
Choice D
I want to add a fifth choice F that would either choice B or C (that is the results of both B and C).
Choice A
Choice B
Choice C
Choice D
Choice F = either B or C
The choices are stored in a table called Coupons in its title field (i created a row having the choice F). So far, I tried to employ a ransack to do the trick, without being quite sure.
//app/admin/user.rb
filter :has_right_choice, label: 'Filter by the right choice', collection: Coupon.all, as: :select
//app/models/user.rb
ransacker :has_right_choice, :formatter => ->(title) {
if (title == 'Choice F')
Coupon.all.where("title = 'Choice B' OR title = 'Choice C'")
else
Coupon.all
end
} do |parent|
parent.table[:id]
end
But my solution doesn't work. Is there a better approach instead of ransack ? If not, any suggestion ?
==========================================================================
EDIT: solution
Choice A # lets say that this choice has id = 1 in Coupons table
Choice B # id = 2
Choice C # id = 3
Choice D # id = 4
Choice F # id = 5
Also lets say that Users table has a field choice_id that refers to the Choices table.
We modify the faulty code as follows:
//app/admin/user.rb
filter :choice_id_in, label: 'Filter by the right choice', collection: Coupon.all, as: :select
//app/models/user.rb
ransacker :choice_id,
:formatter => -> (coupon_id) {
if (coupon_id == "5")
ids = User.all.where("choice_id = 3 OR choice_id = 4").map(&:id)
else
ids = User.where("choice_id = ?", coupon_id).map(&:id)
end
(ids.empty?) ? ids << 0: ids #activeadmin translates the queries into IN operator, may get syntax error if empty
# id = 0 is non-existent in Users as id >= 1
ids #maybe is not needed
} do |parent|
parent.table[:id]
end
Suggestions for improvement are welcome. Thanks #berker for the guidance.

Related

Setting a "subrange" list of values in a dropdown (data validation), based on selected value from a range in Google Sheets

glorious Internet. I seek your help.
I've imported a spreadsheet from OpenOffice, and the "subrange" dropdown list (from data validation) is broken.
Basically, Column B = "Category", and Column C = "Subcategory" -- Column B data validation works properly, populates the standard Category dropdown list (it's just a Range within the same sheet, in the example below D2:E2). But then, based on the Category value, Column C should update a dropdown list of subcategories.
i.e.
row/col
D
E
2
Fruit
Vegetable
3
Apple
Carrot
4
Banana
Onion
B7 = dropdown list "Fruit / Vegetable"
I pick fruit
C7 should then update to a dropdown list "Apple / Banana"
I feel like a potential avenue might be using a Script (was looking to this for inspiration: Getting a range from a range in Google Apps scripting)
but never having used Google Scripting before, I need some basic help (like, even if I have a properly working function CreateSubranges defined in macros.gs -- but how do I get it to trigger every time a Fruit is selected?)
Any advice you have is greatly appreciated! Thank you :-)
If you don't need anything too fancy and it is for just one cell, then this formula will do. Just change the ranges to the correct sheet name/range and it will work for you. The formula gets the value selected from sheet 1 and checks if it matches Fruits or Vegetable. If it matches either of those then B3:B or C3:C is selected. I then constrain the array to only go as far as the count in B3:B...assuming that your two columns have the same number of elements. I then add this range to the data validation for C2 on Sheet one.
=Array_Constrain(ArrayFormula(if(Sheet1!B2="Fruit",B3:B,if(Sheet1!B2="Vegetable",C3:C,""))),countif(B3:B,"<>"),1)
Demo of the category and sub category
Showing each separate tab and what happens when something is selected.
If you are needing a dynamic subcategory for each row then the following onEdit script will set the data validation rules based on what was selected. This does require a small setup of creating the data validation for fruit and vegetable on Sheet2 D2 and E2.
function onEdit(e) {
var ss = e.source;
var value = e.value;
var activeSheet = ss.getSheetName();
var range = e.range;
var currentRow = range.getRow();
var currentColumn = range.getColumn();
if(activeSheet == 'Sheet1' && currentRow > 1 && currentColumn == 2 && value == 'Fruit') {
//IF THE ROW IS > 1 AND THE COLUMN = 2 AND FRUIT WAS SELECTED, THEN IT WILL COPY THE VALIDATION RULES FROM
//SHEET2 D2.
var fruitRule = ss.getSheetByName('Sheet2').getRange(2, 4).getDataValidation().copy();
range.offset(0,1).setDataValidation(fruitRule);
return;
} else if(activeSheet == 'Sheet1' && currentRow > 1 && currentColumn == 2 && value == 'Vegetable') {
//IF THE ROW IS > 1 AND THE COLUMN = 2 AND VEGETABLE WAS SELECTED, THEN IT WILL COPY THE VALIDATION RULES FROM
//SHEET2 E2.
var vegRule = ss.getSheetByName('Sheet2').getRange(2, 5).getDataValidation().copy();
range.offset(0,1).setDataValidation(vegRule);
return;
} else if(activeSheet == 'Sheet1' && currentRow > 1 && currentColumn == 2 && value == null) {
//IF THE ROW IS > 1 AND THE COLUMN = 2 AND VALUE WAS DELETED, THEN IT WILL CLEAR THE VALIDATION RULES
//FROM THE ADJACENT CELL.
range.offset(0,1).clearDataValidations();
}
}
Hope this helps! In the future, it would be much easier to show an example with a demo sheet provided.

Regroup By in PigLatin

In PigLatin, I want to group by 2 times, so as to select lines with 2 different laws.
I'm having trouble explaining the problem, so here is an example. Let's say I want to grab the specifications of the persons who have the nearest age as mine ($my_age) and have lot of money.
Relation A is four columns, (name, address, zipcode, age, money)
B = GROUP A BY (address, zipcode); # group by the address
-- generate the address, the person's age ...
C = FOREACH B GENERATE group, MIN($my_age - age) AS min_age, FLATTEN(A);
D = FILTER C BY min_age == age
--Then group by as to select the richest, group by fails :
E = GROUP D BY group; or E = GROUP D BY (address, zipcode);
-- The end would work
D = FOREACH E GENERATE group, MAX(money) AS max_money, FLATTEN(A);
F = FILTER C BY max_money == money;
I've tried to filter at the same time the nearest and the richest, but it doesn't work, because you can have richest people who are oldest as mine.
An another more realistic example is :
You have demands file like : iddem, idopedem, datedem
You have operations file like : idope,labelope,dateope,idoftheday,infope
I want to return operations that matches demands like :
idopedem matches ideope.
The dateope must be the nearest with datedem.
If datedem - date_ope > 0, then I must select the operation with the max(idoftheday), else I must select the operation with the min(idoftheday).
Relation A is 5 columns (idope,labelope,dateope,idoftheday,infope)
Relation B is 3 columns (iddem, idopedem, datedem)
C = JOIN A BY idope, B BY idopedem;
D = FOREACH E GENERATE iddem, idope, datedem, dateope, ABS(datedem - dateope) AS datedelta, idoftheday, infope;
E = GROUP C BY iddem;
F = FOREACH D GENERATE group, MIN(C.datedelta) AS deltamin, FLATTEN(D);
G = FILTER F BY deltamin == datedelta;
--Then I must group by another time as to select the min or max idoftheday
H = GROUP G BY group; --Does not work when dump
H = GROUP G BY iddem; --Does not work when dump
I = FOREACH H GENERATE group, (datedem - dateope >= 0 ? max(idoftheday) as idofdaysel : min(idoftheday) as idofdaysel), FLATTEN(D);
J = FILTER F BY idofdaysel == idoftheday;
DUMP J;
Data in the 2nd example (note date are already in Unix format) :
You have demands file like :
1, 'ctr1', 1359460800000
2, 'ctr2', 1354363200000
You have operations file like :
idope,labelope,dateope,idoftheday,infope
'ctr0','toto',1359460800000,1,'blabla0'
'ctr0','tata',1359460800000,2,'blabla1'
'ctr1','toto',1359460800000,1,'blabla2'
'ctr1','tata',1359460800000,2,'blabla3'
'ctr2','toto',1359460800000,1,'blabla4'
'ctr2','tata',1359460800000,2,'blabla5'
'ctr3','toto',1359460800000,1,'blabla6'
'ctr3','tata',1359460800000,2,'blabla7'
Result must be like :
1, 'ctr1', 'tata',1359460800000,2,'blabla3'
2, 'ctr2', 'toto',1359460800000,1,'blabla4'
Sample input and output would help greatly, but from what you have posted it appears to me that the problem is not so much in writing the Pig script but in specifying what exactly it is you hope to accomplish. It's not clear to me why you're grouping at all. What is the purpose of grouping by address, for example?
Here's how I would solve your problem:
First, design an optimization function that will induce an ordering on your dataset that reflects your own prioritization of money vs. age. For example, to severely penalize large age differences but prefer more money with small ones, you could try:
scored = FOREACH A GENERATE *, money / POW(1+ABS($my_age-age)/10, 2) AS score;
ordered = ORDER scored BY score DESC;
top10 = LIMIT ordered 10;
That gives you the 10 best people according to your optimization function.
Then the only work is to design a function that matches your own judgments. For example, in the function I chose, a person with $100,000 who is your age would be preferred to someone with $350,000 who is 10 years older (or younger). But someone with $500,000 who is 20 years older or younger is preferred to someone your age with just $50,000. If either of those don't fit your intuition, then modify the formula. Likely a simple quadratic factor won't be sufficient. But with a little experimentation you can hit upon something that works for you.

how to find number of tag matches in acts as taggable on

I have two entries in my database
Obj1 is tagged with "hello, world, planet"
Obj2 is tagged with "hello"
if I do modelName.tagged_with(["hello", "world", "planet", "earth"], :any=>true)
I want to sort the returned objects in order of highest to lowest number of tags matched.
so in this case i'd like the order to be Obj1, Obj2
how can I do this? is there a way to get number of tags matched for each of the returned results?
You can call tag_list on the objects and use that to figure out how many tags there are:
tags = %w{hello world planet earth}
objs = ModelName.taggedWith(tags, :any => true)
objs.sort_by! { |o| -(tags & o.tag_list).length }
The tags & o.tag_list yields the intersection of the tags you're looking for and the tags found, then we negate the size of the intersection to tell sort_by (which sorts in ascending order) to put larger intersections at the front, negating the result is an easy way to reverse the usual sort order.
Posting this here if someone else is looking for a way to query a model by tags and order by the number of matches. This solution also allows for the usage of any "equality" operator like the % from pg_trgm.
query = <<-SQL
SELECT users.*, COUNT(DISTINCT taggings.id) AS ct
FROM users
INNER JOIN taggings ON taggings.taggable_type = 'User'
AND taggings.context = 'skills'
AND taggings.taggable_id = users.id
AND taggings.tag_id IN
(SELECT tags.id FROM tags
WHERE (LOWER(tags.name) % 'ruby'
OR LOWER(tags.name) % 'java'
OR LOWER(tags.name) % 'sa-c'
OR LOWER(tags.name) % 'c--'
OR LOWER(tags.name) % 'gnu e'
OR LOWER(tags.name) % 'lite-c'
))
GROUP BY users.id
ORDER BY ct DESC;
SQL
User.find_by_sql(query)
Note that the code above will only work if you have pg_trgm enabled. You can also simply replace % with ILIKE.
EDIT: With ActiveRecord and eager loading:
This could be in a scope or class method and can be chained with other ActiveRecord methods.
ActiveRecord::Base.connection
.execute('SET pg_trgm.similarity_threshold = 0.5')
matches = skills.map do
'LOWER(tags.name) % ?'
end.join(' OR ')
select('users.*, COUNT(DISTINCT taggings.id) AS ct')
.joins(sanitize_sql_array(["INNER JOIN taggings
ON taggings.taggable_type = 'User'
AND taggings.context = 'skills'
AND taggings.taggable_id = users.id
AND taggings.tag_id IN
(SELECT tags.id FROM tags WHERE (#{matches}))", *skills]))
.group('users.id')
.order('ct DESC')
.includes(:skills)
Override skill_list from acts-as-taggable-on in the model:
def skill_list
skills.collect(&:name)
end
and proceed normally.

Removing values from a returned linq query

HI there I am hoping for some help with a query I have.
I have this query
var group =
from r in CustomerItem
group r by r.StoreItemID into g
select new { StoreItemID = g.Key,
ItemCount = g.Count(),
ItemAmount = Customer.Sum(cr => cr.ItemAmount),
RedeemedAmount = Customer.Sum(x => x.RedeemedAmount)
};
I am returning my results to a list so I can bind it listbox.
I have a property called EntryType which is an int. There are 2 available numbers 1 or 2
Lets say I had 3 items that my query is working with
2 of them had the EntryType = 1 and the 3rd had EntryType2. The first records had a ItemAmount of 55.00 and the 3rd had a ItemAmount of 50.00
How can I group using something simlar to above but minus the ItemAmount of 50.00 from the grouped amount to return 60.00?
Any help would be great!!
It's not really clear what the question is - are you just trying to ignore all items with an entry type of 2? To put it another way, you only want to keep entries with an entry type of 1? If so, just add a where clause:
var group = from r in CustomerItem
where r.EntryType == 1
group r by r.StoreItemID into g
select new {
StoreItemID = g.Key, ItemCount = g.Count(),
ItemAmount = Customer.Sum(cr => cr.ItemAmount),
RedeemedAmount = Customer.Sum(x => x.RedeemedAmount)
};
Change ItemAmount = ... to:
ItemAmount =
g.Where(x => x.EntryType == 1).Sum(cr => cr.ItemAmount) -
g.Where(x => x.EntryType == 2).Sum(cr => cr.ItemAmount),
I changed Customer to g because this seems to be an error, but it's not clear to me from your question what you mean here, so maybe this change is not what you want.
A slightly more concise method is to use test the entry type in the sum and use the ternary operator to choose whether to add the positive or negative value:
ItemAmount = g.Sum(cr => cr.EntryType == 1 ? cr.ItemAmount : -cr.ItemAmount),
This gives the value of 60.00 as you required.

conditional include in linq to entities?

I felt like the following should be possible I'm just not sure what approach to take.
What I'd like to do is use the include method to shape my results, ie define how far along the object graph to traverse. but... I'd like that traversal to be conditional.
something like...
dealerships
.include( d => d.parts.where(p => p.price < 100.00))
.include( d => d.parts.suppliers.where(s => s.country == "brazil"));
I understand that this is not valid linq, in fact, that it is horribly wrong, but essentially I'm looking for some way to build an expression tree that will return shaped results, equivalent to...
select *
from dealerships as d
outer join parts as p on d.dealerid = p.dealerid
and p.price < 100.00
outer join suppliers as s on p.partid = s.partid
and s.country = 'brazil'
with an emphasis on the join conditions.
I feel like this would be fairly straight forward with esql but my preference would be to build expression trees on the fly.
as always, grateful for any advice or guidance
This should do the trick:
using (TestEntities db = new TestEntities())
{
var query = from d in db.Dealership
select new
{
Dealer = d,
Parts = d.Part.Where
(
p => p.Price < 100.0
&& p.Supplier.Country == "Brazil"
),
Suppliers = d.Part.Select(p => p.Supplier)
};
var dealers = query.ToArray().Select(o => o.Dealer);
foreach (var dealer in dealers)
{
Console.WriteLine(dealer.Name);
foreach (var part in dealer.Part)
{
Console.WriteLine(" " + part.PartId + ", " + part.Price);
Console.WriteLine
(
" "
+ part.Supplier.Name
+ ", "
+ part.Supplier.Country
);
}
}
}
This code will give you a list of Dealerships each containing a filtered list of parts. Each part references a Supplier. The interesting part is that you have to create the anonymous types in the select in the way shown. Otherwise the Part property of the Dealership objects will be empty.
Also, you have to execute the SQL statement before selecting the dealers from the query. Otherwise the Part property of the dealers will again be empty. That is why I put the ToArray() call in the following line:
var dealers = query.ToArray().Select(o => o.Dealer);
But I agree with Darren that this may not be what the users of your library are expecting.
Are you sure this is what you want? The only reason I ask is, once you add the filter on Parts off of Dealerships, your results are no longer Dealerships. You're dealing in special objects that are, for the most part, very close to Dealerships (with the same properties), but the meaning of the "Parts" property is different. Instead of being a relationship between Dealerships and Parts, it's a filtered relationship.
Or to put it another way, if I pull a dealership out of your results and passed to a method I wrote, and then in my method I call:
var count = dealership.Parts.Count();
I'm expecting to get the parts, not the filtered parts from Brazil where the price is less than $100.
If you don't use the dealership object to pass the filtered data, it becomes very easy. It becomes as simple as:
var query = from d in dealerships
select new { DealershipName = d.Name,
CheapBrazilProducts = dealership.Parts.Where(d => d.parts.Any(p => p.price < 100.00) || d.parts.suppliers.Any(s => s.country == "brazil")) };
If I just had to get the filtered sets like you asked, I'd probably use the technique I mentioned above, and then use a tool like Automapper to copy the filtered results from my anonymous class to the real class. It's not incredibly elegant, but it should work.
I hope that helps! It was an interesting problem.
I know this can work with one single Include. Never test with two includes, but worth the try:
dealerships
.Include( d => d.parts)
.Include( d => d.parts.suppliers)
.Where(d => d.parts.All(p => p.price < 100.00) && d.parts.suppliers.All(s => s.country == "brazil"))
Am I missing something, or aren't you just looking for the Any keyword?
var query = dealerships.Where(d => d.parts.Any(p => p.price < 100.00) ||
d.parts.suppliers.Any(s => s.country == "brazil"));
Yes that's what I wanted to do I think the next realease of Data Services will have the possiblity to do just that LINQ to REST queries that would be great in the mean time I just switched to load the inverse and Include the related entity that will be loaded multiple times but in theory it just have to load once in the first Include like in this code
return this.Context.SearchHistories.Include("Handle")
.Where(sh => sh.SearchTerm.Contains(searchTerm) && sh.Timestamp > minDate && sh.Timestamp < maxDate);
before I tried to load for any Handle the searchHistories that matched the logic but don't know how using the Include logic you posted so in the mean time I think a reverse lookup would be a not so dirty solution

Resources