type="number" to type="text" and add class in ActiveAdmin (globally) - activeadmin

I have tables with fields with type numeric(12,2).
ActiveAdmin (with help of formastic gem) renders them as type="number".
I want it to render them as type="text" and add class float-field. Is it possible to do it globally? I'm not against of monkey-patching it.

Active Admin builds on Formtastic. The Fomtastic README documents Modified & Custom Inputs. You would implement an apps/inputs/float_input and reference it as f.input :total, as: :float

I've decided to monkeypatch Formastic gem:
module Formtastic
module Inputs
class NumberInput
include Base
include Base::Stringish
include Base::Placeholder
def to_html
input_wrapping do
label_html << builder.text_field(method, input_html_options.merge(class: 'float-field'))
end
end
end
end
end

Related

How can I write this xpath expression in WATIR methods

I have to form the following xpath via watir methods,
//span[#class='fieldTitle-small required z-label'][contains(.,'Address Type')]/../following-sibling::div//input
Is there a way to call the watir methods one after another to form this xpath?
That XPath could be done using Watir methods as:
browser.span(class: %w(fieldTitle-small required z-label), text: /Address Type/) # //span[#class='fieldTitle-small required z-label'][contains(.,'Address Type')]
.parent # /..
.following_sibling(tag_name: 'div') # /following-sibling::div
.input # //input
Note that when using a :class locator, an Array can be used to specify multiple classes that can appear in any order. The element only has to include those classes, but it can also include any others. For example, browser.span(class: %w(fieldTitle-small z-label), text: /Address Type/), which has the "required" class removed, would match <span class="fieldTitle-small required z-label"> and <span class="fieldTitle-small z-label">.

mat-table can't bind dataSource

I am trying to use angular material table MatTableModule, but when passing the data to the [dataSource] input I am getting the Can't bind to 'dataSource' since it isn't a known property of 'table'. error. I have already imported the MatTableModule and as pointed at GitHub issue #5577 also imported CdkTableModule but still no luck. Any ideas?
The problem was that the material angular version I'm using is v5.2.4, so the template should be <mat-table #table [dataSource]="dataSource"></mat-table> instead of <table mat-table #table [dataSource]="dataSource"></table> according to Angular material v5.2.4 documentation.
https://v5.material.angular.io/components/table/overview#1-write-your-mat-table-and-provide-data

Google Closure Templates variable variables

Situation:
context contains such fields as testExecutionKey1, testExecutionKey2 .. testExecutionKey10
I want to check them for null, but do it the nice way - in for loop
I need to access $context.testExecutionKey1..10 inside the loop, where number is $i
{for $i in range(1, 11)}
{if {$context.testExecutionKey}{$i}}
<div class="aui-message aui-message-info">
<p>A Test instance execution is already linked to this issue: ${context.testExecutionKey{$i}}.</p>
</div>
<input type="hidden" name="skip-test-execution-${$i}" value="true">
{/if}
{/for}
Problem:
PHP has got variable variables feature, but I could no similar find for closure templates in docs.
Reimplementing jira context the proper way is the way to go

Jest - how to test if a component does not exist?

How do I check if a component is not present, i.e. that a specific component has not been rendered?
.contains receives a React Node or array of Nodes as an argument. Instead, use .find:
expect(wrapper.find('selector').exists()).toBeTruthy()
You can use enzymes contains to check if the component was rendered:
expect(component.contains(<ComponentName />)).toBe(false)
If you're using react-testing-library (I know the OP wasn't but I found this question via web search) then this will work:
expect(component.queryByText("Text I care about")).not.toBeInTheDocument();
You can query by Text, Role, and several others. See docs for more info.
Note: queryBy* will return null if it is not found. If you use getBy* then it will error out for elements not found.
Providing a slightly updated answer based on the documentation for enzyme-matchers's toExist. This will require you to install the enzyme-matchers package.
function Fixture() {
return (
<div>
<span className="foo" />
<span className="bar baz" />
</div>
);
}
const wrapper = mount(<Fixture />); // mount/render/shallow when applicable
expect(wrapper.find('span')).toExist();
expect(wrapper.find('ul')).not.toExist();
.contains does not expect a selector, unlike find. You can look at the length attribute of the ShallowWrapper
expect(wrapper.find('...')).toHaveLength(0)
I found I needed to use this syntax with Enzyme and Jest to test if a Connected Component existed in the rendered output.
We use Jest and Enzyme, and I've found the only good test is to import the sub-component and test this way:
expect(component.find(SubComponent).length).toEqual(0); // or (1) for exists, obvs
I tried all the other answers and none worked reliably.
If you are using react-testing-library, then this also will work:
expect(component.queryByText("Text I care about").toBeNull());
expect(within(component).queryByText("Text I care about")).toBeNull();
Note: In my case, I needed to use queryBy* because it doesn´t error out when the text element (that contains the text: Text I care about) does not exist. Therefore, I could evaluate whether there is an existence of a text component or not.

Optional Partials in DustJS

Is it possible to have optional partials in Dust? Let's say I define a layout like this:
<div>
{>"{module}"/>
</div>
I have been defining the module in Express's res.locals object. However, what if I forgot to define a module, or I actually want a default module for use when I do not define one? Worse, what if I did define one but it's the incorrect module, meaning there's no template file in the view folder with the name of that module? I don't want the user to see the ugly error message, which seems to be something like:
Error: ENOENT, open 'view_path\{module}.dust'
where {module} is the name of the module, or an empty string if I did not specify a res.locals.module. Should I resort to try-catch blocks (not even sure how to do them in dust), or is there a method for making templates optional, rather than required? NOTE: The template would be optional, but the module variable would (usually) still be a string. It seems that dust sections are optional, meaning if the exact key is not available, the section is simply not included. For example, say I have the context {friends: [{name: "Harry"}, {name: "Ron"}, {name: "Hermione"}]}. If I define the section:
{#friends}
{name} is {age} years old.
{/friends}
it will output
Harry is years old.
Ron is years old.
Hermione is years old.
Notice there are 2 spaces between is and years in each case, where the age would be if we defined any ages. If this functionality is included, how is it that neither the original creators of dust nor LinkedIn thought to not require partials? How do I specify optional partials in dust?
You could use a conditional like so,
<div>
{?module}
{>"{module}"/>
{:else}
{>"{YourDefaultPartial}"}
{/module}
</div>
This will work when {module} is either undefined or an empty string or a falsey value.
But, in case its a string pointing to a partial that does not exist, it'll throw an error. Hope this helps.

Resources