Adding Numeric Range Filtering to ActiveAdmin - activeadmin

Greetings and Happy Holidays 2015 --
I tried to add numeric range filtering per the excellent blog post by Boris Stall.
I'm running:
Rails 4.2.4
Ruby 2.2.3
ActiveAdmin 1.0.0pre2
I keep running into this error:
Unable to find input class NumericRangeInput
Here is my config/initializers/active_admin/filter_numeric_range_input.rb
module ActiveAdmin
module Inputs
class FilterNumericRangeInput < ::Formtastic::Inputs::StringInput # Add filter module wrapper
include ActiveAdmin::Inputs::Filters::Base
def to_html
input_wrapping do
[ label_html,
builder.text_field(gt_input_name, input_html_options(gt_input_name)),
template.content_tag(:span, "-", :class => "seperator"),
builder.text_field(lt_input_name, input_html_options(lt_input_name)),
].join("\n").html_safe
end
end
def gt_input_name
"#{method}_gteq"
end
alias :input_name :gt_input_name
def lt_input_name
"#{method}_lteq"
end
def input_html_options(input_name = gt_input_name)
current_value = #object.send(input_name)
{ :size => 10, :id => "#{input_name}_numeric" , :value => current_value }
end
end
end
end
I'm simply trying:
filter :id, as: :numeric_range
I've researched the potential issues integrating AA, Ransack, Formtastic, etc., but I'm not advanced enough to know where to go from here. Any help is greatly appreciated.

It seems newer versions of ActiveAdmin might have a different strategy for loading or naming other classes in the input module. I noticed that the class name in the file is called FilterNumericRangeInput. So AA must be doing something to convert the name, we just need to figure out which symbol to give to convert properly. So as an experiment, I tried to rename the symbol in app/admin/test.rb to:
filter :id, as: :numeric_range_2
And I got the error:
Unable to find input class NumericRange2Input
So with that hint I tried changing the symbol name to:
filter :id, as: :filter_numeric_range
And it worked.

At this point you just need filter :id, as: :numeric

Related

drools adding fact types in excel file (declaring types)

I am looking on some example excel files to declare a Fact Type in the rule file. How can I add a a fact type in the excel file. I can do it in the drl file as given below.
package KieRule;
global java.util.List names;
declare Invoice
cardType : String
price : int
end
rule "HDFC"
when
invoiceObject : Invoice(cardType=="HDFC" && price>10000);
then
names.add( "discount for HDFC = 10" );
end;
I got a solution for that. Posting it here if it help others,

Origen test_ids next in range accept a Proc/Lambda?

I am working with the Origen test_ids gem 'next in range' feature. When I setup the softbin configuration in the test interface I find out dynamically how many different hardbins have a unique softbin range. This is known but it varies depending on the test module being tested. Some test modules may have 3 hardbin to softbin combinations and some have 5. Is it possible to pass a Proc/Lambda to the softbin config shown below?
config.softbins needs: :bin do |options|
    if options[:bin] == 1
      TestIds.next_in_range((1000..2000))
    elsif options[:bin] == 11
      TestIds.next_in_range((10000..99999))
    end
  end
Such that the number of elsif statements, the bin and the softbin range are all dynamically stitched together. I know eval could work but it seems to be frowned upon.
EDIT
OK after reviewing Ginty's answer I tried the solution but it seems like the options are not getting passed into the next_in_range method. Here is the config:
TestIds.configure current_test_insertion do |config|
config.bins.include << binning_obj.configs(:all).hbin
config.softbins needs: :bin do |options|
bin_map = Hash[test_type_hardbins.zip(binning_test_types)]
TestIds.next_in_range(bin_map[options[:bin]])
end
config.send_to_ate = false
end
Here is the error:
COMPLETE CALL STACK
-------------------
wrong number of arguments (given 1, expected 2)
/users/user/origen/github/test_ids/lib/test_ids.rb:236:in `next_in_range'
When I pass in the options as so:
TestIds.next_in_range(bin_map[options[:bin]], options)
I get this error:
COMPLETE CALL STACK
-------------------
undefined method `map' for nil:NilClass
Did you mean? tap
/users/user/origen/github/test_ids/lib/test_ids/allocator.rb:45:in `range_item'
/users/user/origen/github/test_ids/lib/test_ids/allocator.rb:32:in `next_in_range'
Given that the docs say this feature is in beta, should I move this to a Github issue?
thx
When defining a softbin with a block, you have complete freedom to put whatever you want in the block, so adding an additional Proc into the equation doesn't make sense to me.
There are effectively two APIs here that you can combine, one is the ability to define a function to work out the number:
config.softbins do |options|
# Put any logic you like in here, return the number at the end
end
The other API is the ability to have TestIds keep track of a position in a range:
TestIds.next_in_range((1000..2000))
You can use that, or not, within your block however you wish.
That you should give you full freedom to define whatever rules you like:
config.softbins needs: bin do |options|
if Time.now.tuesday?
bin_map = { 5: (1..10), 11: (11..20) }
else
bin_map = { 6: (10..20), 12: (21..30) }
end
TestIds.next_in_range(bin_map[options[:bin]])
end
Note that if you refer to the same next_in_range within different branches then they will both consume from the same set of numbers.
If you wanted them to each independently count within that range, then you would need to setup different configurations so that they each have their own database:
if Time.now.tuesday?
TestIds.configure :rule1 do |config|
end
else
TestIds.configure :rule2 do |config|
end
end

How to test the value of ensure in a custom type?

I've been writing custom types for Puppet, and I've run into a case where for both 'latest' and 'present' I require the existence of certain parameters, but for 'absent' I would like those parameters to be optional.
Unfortunately, I haven't been able to figure out how to test the value of 'ensure' within the ruby code.
Puppet::Type.type(:fubar) do
ensurable do
desc 'Has three states: absent, present, and latest.'
newvalue(:absent) do
# ...
end
newvalue(:latest) do
# ...
end
newvalue(:present) do
# ...
end
def insync?(is)
# ...
end
defaultto :latest
end
# ...
validate do
# This if condition doesn't work. The error is still raised.
if :ensure != :absent
unless value(:myprop)
raise ArgumentError, "Property 'myprop' is required."
end
end
end
end
So, my question is simple... How do I test the value of 'ensure' so that when it is 'absent', the validation is NOT performed?
Thanks to both Matt Schuchard and John Bollinger for their help.
The problem is that:
if :ensure != :absent
is indeed comparing two symbols, where I need to be comparing the value of a property to a symbol:
if self[:ensure] != :absent
I am sufficiently new to both Puppet and Ruby that I didn't realize the difference. John stated it clearly, and Matt provided a good example.
Again, Thanks to both Matt and John.

Get field location U2 Unidata

It is very rare to find any help for U2 Unidata/Universe database and searches online are not much of a help. So I am trying to make dynamic field change (based on input):
OPEN FILE.NAME TO WORKING.FILE ELSE STOP
READV FIELD_VAR FROM WORKING.FILE,RECORD.ID,FIELD.LOCATION THEN
PRINT FIELD_VAR
END ELSE
PRINT "No records found"
END
CLOSE WORKING.FILE
Problem is user executing this program don't know field location, field location could be 10, could be 5, could be any number (except 0 which is ID). I've been reading rocket documentation and I cannot find anything similar. Closest I've got was with writing query that looks like this:
SELECT DICT WORKING.FILE WITH #ID EQ 'FIELD.NAME'
LIST DICT WORKING.FILE LOC or ELE DICT WORKING.FILE where 2nd line shows location
This is a workaround that I just have to translate into code but I hope that there is something much easier then this.

Can't access an element by a data- attribute with an underscore

Good day everyone!
I have an element
<tbody class="cp-ads-list__table-item _sas-offers-table__item cp-ads-list__table- item_state-deposit" data-card_id="16676514">
I'd like to access it by the data-card_id tag, but when I try the following
#browser.tbody(:data_card_id => "16676514").hover
I get an error
unable to locate element, using {:data_card_id=>"16676514", :tag_name=>"tbody"} (Watir::Exception::UnknownObjectException)
I guess my code would have worked if the tag were "data-card-id", but it's "data-card_id".
How do I access my element by this attribute?
Problem
You are right that the problem is the underscore in the data attribute. As seen in the ElementLocator, when building the XPath expression, all underscores are converted to dashes (in the else part of the statement):
def lhs_for(key)
case key
when :text, 'text'
'normalize-space()'
when :href
# TODO: change this behaviour?
'normalize-space(#href)'
when :type
# type attributes can be upper case - downcase them
# https://github.com/watir/watir-webdriver/issues/72
XpathSupport.downcase('#type')
else
"##{key.to_s.gsub("_", "-")}"
end
end
Solution - One-Off
If this is the only data attribute that is using underscores (rather than dashes), I would probably manually build the XPath or CSS expression.
#browser.tbody(:css => '[data-card_id="16676514"]').hover
Solution - Monkey Patch
If using underscores is a standard on the website, I would probably consider monkey patching the lhs_for method. You could monkey patch the method so that you only change the first underscore for data attributes:
module Watir
class ElementLocator
def lhs_for(key)
puts 'hi'
case key
when :text, 'text'
'normalize-space()'
when :href
# TODO: change this behaviour?
'normalize-space(#href)'
when :type
# type attributes can be upper case - downcase them
# https://github.com/watir/watir-webdriver/issues/72
XpathSupport.downcase('#type')
else
if key.to_s.start_with?('data')
"##{key.to_s.sub("_", "-")}"
else
"##{key.to_s.gsub("_", "-")}"
end
end
end
end
end
This would then allow your original code to work:
#browser.tbody(:data_card_id => "16676514").hover

Resources