How do I make Active Admin show code DRY across models? - activeadmin

I have a number of models within Active Admin that have very similar (but not exactly the same) show pages along the lines of:
show do |ad|
attributes_table do
row :name
row :length
row :width
row :height
...
end
panel "Images" do
text_node link_to 'Add Image', new_admin_image_path(...)
table_for ad.images do
column "Image" do |image|
image_tag(...)
end
column do |data|
link_to :edit, edit_admin_image_path(...)
end
column do |data|
link_to :delete, admin_image_path(data), method: :delete
end
end
end
end
The 'panel "Images" do' code will be duplicated exactly within each model, so I'd like to put it somewhere else. I've been going down the ViewHelper and render partial paths, but in both cases I end up with something that doesn't know what "panel", "text_node", "table_for", etc. is. Guidance as to what is the right way to do this?

Arbre, the template language ActiveAdmin uses, does support partials. You can
move the duplicated code into an arb partial such as
app/views/admin/_images_panel.html.arb. Then your ActiveAdmin resources
can simply call render with the partial path and any needed local
variables.
show do
attributes_table do
# ...
end
render 'admin/images_panel', data: data
end
The partial may also reference the generic method resource to eliminate
the need to pass in local variables. Resource is whatever resource the
admin is managing. For example:
panel "Images" do
table_for resource.images do
# Note use of `resource` instead of `ad` above.
# ...
end
end

Related

How to repeat a string 'x' amount of times in Livecode

I have a program which sets a variable "x" to the length of a random dictionary word, and then should put "a" into a field x amount of times. However I am unsure whether my syntax is right or wrong. The variable randomword is already defined and works. My non-working code is as follows:
global x
on mouseUp
put length(randomword) into x
put repeatedString("a",x) into field "wordDisplay"
end mouseUp
However, when I look at wordDisplay after clicking my button, it is blank. An explanation of why, and code to fix this would be really beneficial.
Cheers.
You don't say if 'repeatedString' is a function you're calling from somewhere else, but if I understand what you're trying to do, you can try something like this, where you place 'a' into temporary variable:
put length(randomword) into x
repeat x
put "a" after temp
end repeat
set text of field "wordDisplay" to temp
Also, am guessing this is the case, but the only reason to use the global is if you plan to use the value of x across the scripts of multiple objects. If you're just using 'x' in this script, you don't need the variable declaration.
Page 227 of my book "Programming LiveCode for the Real Beginner" contains the following useful function, which does exactly what you want:
function repeatChar theChar,theAmount
local myLongString
set the itemDel to theChar
put theChar into item theAmount of myLongString
return myLongString
end repeatChar
Note that a repeat loop is not necessary.
Use the function in your script like this:
global randomWord
on mouseUp
local x
put length(randomWord) into x
put repeatChar("a",x) into field "wordDisplay"
end mouseUp

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

writing an excel formula with multiple options for a single parameter

I would like to access information from a HTTP based API and manipulate it with excel.
The API returns about 20 pieces of information, and you can get that information by looking up any number of about ten lookup fields: name, serial number etc.
I want to write a function similar to the Match Function in excel where one of the parameters (in this case MATCH TYPE) has multiple possible values.
I have a list of values (the 20 pieces of information the API can return) and I want to make these pieces of information the possible return values for one of the Functions parameters.
How do I do I create a function where one parameter has a list of possible values?
And how do I add tooltip help statements to those parameter options so people know what they are?
You want to use an Enum.
In the declarations part of your module (before any subs or functions) you can place code like this.
Enum MyFunctionsArgValue
LessThan
Equal
GreaterThan
End Enum
This will assign each of these keywords an integer value, starting at zero and counting up. So LessThan = 0, Equal = 1, and GreaterThan = 2. (You can actually start at any number you want, but the default is usually fine.)
Now you can use it in your function something like this.
Function MySuperCoolFunction(matchType as MyFunctionsArgValue)
Select Case matchType
Case LessThan
' do something
Case Equal
' do it different
Case GreaterThan
' do the opposite of LessThan
End Select
End Function
To get the tool tip, you need to use something called an Attribute. In order to add it to your code, you'll need to export the *.bas (or *.cls) file and open it in a regular text editor. Once you've added it, you'll need to import it back in. These properties are invisible from inside of the VBA IDE. Documentation is sketchy (read "nonexistent"), so I'm not sure this works for an Enum, but I know it works for functions and module scoped variables.
Function/Sub
Function MySuperCoolFunction(matchType as MyFunctionsArgValue)
Attribute MySuperCoolFunction.VB_Description = "tool tip text"
Module Scoped Var
Public someVar As String
Attribute someVar.VB_VarDescription = "tooltip text"
So, you could try this to see if it works.
Enum MyFunctionsArgValue
Attribute MyFunctionsArgValue.VB_VarDescription = "tool tip text"
LessThan
Equal
GreaterThan
End Enum
Resources
https://www.networkautomation.com/automate/urc/resources/help/definitions/Sbe6_000Attribute_DefinintionStatement.htm
http://www.cpearson.com/excel/CodeAttributes.aspx

RailsAdmin multiple lines in list view cell

I'd like to get RailsAdmin to show two fields in one cell.
config.model Model do
list do
field :id do
label '#'
end
field :email do
label 'Customer'
formatted_value do
view = bindings[:view]
bindings[:view].content_tag(:div, [view.content_tag(:p, bindings[:object].email), view.content_tag(:p, bindings[:object].phone)])
end
end
end
end
This gives me the following output (escaped HTML basically).
[<p>...(email)...</p> <p>...(phone)...</p>]
I'm not sure what I'm doing wrong - being a rails beginner, maybe I'm not using the content_tag method correctly?

JqGrid - losing ability to sort and perform multipleSearch when column header text is changed programmatically on gridComplete

This is my jqgrid - $("#list1")
When it loads, i.e. on the gridComplete event, I need to rename the column header texts.
Original column header texts are in this format - Colomn1, Column2 ...
On gridComplete, I change these header texts like this:
$("#list1_Column" + someNumber).text(someText);
However on doing this, I lose the ability to sort columns. Column headers are no longer clickable and hence I cannot sort the grid after this custom programmatic editing.
Similar thing happens when I try changing the texts in the search dropdown list (search modal - using multipleSearch: true)
When, on gridComplete, I change text values in the select list as per the grid column headers, like this -
var select = $('#grid_wrapper #fbox_list1 .ui-widget-content .sf .fields select');
$('#grid_wrapper #fbox_list1 .ui-widget-content .sf .fields select option').remove();
$.each(data, function (i, item) {
select.append('<option value="Column' + item.id + '">' + item.ColumnName + '</option>')
});
...I lose the ability to perform multiple search, i.e. the + and - buttons in the search modal disappear.
How do I get around these two issues? Retaining ability to sort and perform multiple search after having changed column header and search list text values on load.
Please guide.
The column header <th> element has two child elements: one <span> with the text of the column header and another with the sort icons which are hidden the most time. So if you want to change the text manually you should use another selector
$("#list1_Column" + someNumber+ " > span").text(someText);
If you do so you will change the text on the page, but not change the text in the colNames or in the colModel (if you use label property instead of colNames). The text will be used for example to create Multisearch dialog. You can make changes in the colModel with respect of setColProp method or use getGridParam to get reference to any internal parameter of jqGrid (which are objects like inclusive colNames and colModel) and then make any changes which you need.
The best way in my opinion to solve the described problems is to use setLabel method to change the text in the column header:
$("#list1").jqGrid('setLabel','ColumnName','My new Name');
this will solve both problems.

Resources