Optional part in rails routing path - rails-routing

I am trying to create a route for a page which can have param1 and param2. Param2 is optional part and the code will take a default value if the value is not present. The following is what I am trying to do
match '/school/:dept/:staff/show' => staff#show
match '/school/:staff/show' => staff#show
I have a bunch of statements like the above which seems to be too much of repetition. Is there a better way to do this. This link has an approach using a third party option. Considering it is an older post, looking to see if this is currently supported with rails.

As per: http://asciicasts.com/episodes/203-routing-in-rails-3
We can do this by defining a route like this, directing any matching route to our info#about action. Note that we can nest parentheses when creating optional parameters.
match "/:year(/:month(/:day))" => "info#about"

Related

Cypress, page content and variables

Right or wrong: In Cypress, its impossible to read a value on page X, then keep this value and compare it to a value on page Y.
I can read a value from the page and log it:
cy.get('[data-e2e-selector=whatever]').then(elm => cy.log('Value from page X : ' + elm))
or, for instance, the number of elements with similar or partially matchin selectors:
cy.get('[data-e2e-selector=^whatever]').then(elm => cy.log('Number of elements like this on page X: ' + elm.length))
Hoever, I cannot create a variable of this, because of the asynchronous way Cypress runs. Right? Any value created to just be blank.
Nor can I pass the value read to a method, which in turn compares it to the value on the next page:
compareToValueOnNextPage(cy.get('[data-e2e-selector=^whatever]').then(elm => elm.length));
compareToValueOnNextPage(value: number) { // NB: Not sure if it's a number or string yet...
cy.get('[data-e2e-selector=^whateverNextPage]').then(elm => elm.length).should('have.length', 4)
}
Basically, if I want to compare values, the either have to be on the same page, or they need to be hard-coded. This is a huge limitation when actually end-to-end testing some applications. Very often, a value is created on page X, based on input which should in many cases ba random, thus creating a dynamic value in the test. Then, on page Y, that same value is fetch (from the backend) or shown in some other way, in a Summary etc. And, naturally, I want to compare the value shown on page X to the one shown on page Y/Summary. But this is not possible, due to the very unit-testing thinking that seems to be the foundation for Cypress.
Or am I missing something here? Are there ways around this that aren't ugly/smelly? I think it's possible to store the value on page X in a file, then read that file on page Y. However, Cypress seems to only have one option when reading the file, and that's reading the whole file and looking for a match. So that file would be a mess. Or I'd need several files.
I realize this is kind of trying to impose non-functional ways on a quite functional and asynchroeous technology. However, if it's not possible to "keep" a value and use it later, it's very limiting when it comes to end-to-end testing (even though it's not when the testing is unit-based on frontend components).
UPDATE:
As per Kerrry's suggestion in the answer below:
cy.get('[data-e2e-selector=dellan-accordion]')
.then(elm => cy.wrap(elm.length).as("myVariableName"));
-GO TO NEXT PAGE-
cy.get('[data-e2e-selector=betalingsplan-dellan]')
.then(elm => elm.length)
.should('have.length', myVariableName)
This yeilds "expected 4 to have property 'length'.
This means, obviously, that I cannot get the length of the length.
So I replace 'have.length' with 'eq':
cy.get('[data-e2e-selector=betalingsplan-dellan]')
.then(elm => elm.length)
.should('have.length', myVariableName)
And I get the following error:
expected 4 to equal 0
So, it seems that the first variable - myVariable - is gone after the first cy.get().
If I do everything inside one get (have another get inside that, where I go to the next page AND get the count of the elements), then it works. But the way Kerry shows it, it would be much more flexible. But, alas, the above error.
As jonrsharpe mentioned in the comments, please read the Cypress document on variables and aliases thoroughly. This is a core concept of Cypress, and it will give you a solid understanding of how to implement variables and carry the values between test steps.
The reader's digest example of what you how you can achieve is this:
cy.get('[data-e2e-selector=^whatever]')
.then(elm => cy.wrap(elm.length).as("myVariableName"));
What this is doing is cy.wrap will yield the value from elm.length in a Cypress command chain, which then allows you to assign it to an alias "myVariableName".
In your following test step where you want to compare the value on a separate page, you would then access the alias' value in one of two ways:
Using this.
cy.get('[data-e2e-selector=^whateverNextPage]')
.then(elm => elm.length)
.should('have.length', this.myVariableName)
OR
via cy.get()
cy.get("#myVariableName").then(function(variableValue){
cy.get('[data-e2e-selector=^whateverNextPage]')
.then(elm => elm.length)
.should('have.length', variableValue)
})

How to use us/en instead of en_US with JMSI18nRoutingBundle

I'm using JMSI18nRoutingBundle for locale routing on our new site, but our existing site uses language + country in the following format and I need to keep the URLs looking the same.
example.com/us/en/hello (en_US)
example.com/be/fr/bonjour (fr_BE)
Is there any way to do this using config? If not, where is the best place to start customizing?
It doesn't look it's possible to do through config, but it can be done by replacing default implementation of PatternGenerationStrategyInterface by your own implementation.
You can check out default implementation that bundle uses here.
After you create your own implementation, just make bundle use your own implementation by setting the config parameter. If you're using YAML for example:
parameters:
jms_i18n_routing.pattern_generation_strategy.class: YourBundle\YourImplementationClass
Hint: you can basically copy/paste from default implementation and change line 69 to use str_replace('_', '/', $locale) instead of just $locale. That way, newly generated route pattern will contain a / if locale contains an _.
Not very elegant solution, but bundle unfortunately doesn't provide enough configuration to make it prettier.

Meteor Iron Router does not get Current Path from Route

In a Template Helper I get the current path from Iron.Router (iron:router) as follows:
Router.current().route.path(this);
This works fine, unless the route path does contain parameters (e.g. /client/:_id/edit). In that case the path() function returns null.
How do I get the current path within a Template Helper, when the route contains parameters?
There are posts around covering the issue but the solution mentioned there seem not to fit.
I'm using Meteor 1.1.5 with iron:router1.0.7
According to this iron-router/issues/289 there are problems when the path contains parameters. The suggestion to use Iron.Location.get().path This works well for me.

RssDisplay Extension / Simple Pie

I'm just a little desperate.
I installed the RSS Display extension.
http://typo3.org/extensions/repository/view/rss_display
Everything works fine, but I have just a little understanding problem.
I pull myself with fluid the Author for the current feed.
<feed:item.get value="author"/>
Than i look whats inside.
<f:debug><feed:item.get value="author"/></f:debug>
Thats the result.
SimplePie_Author prototype object
name => 'Name Name' (12 chars)
link => NULL
email => NULL
So what i need it's to get the name of the author.
Unfortunately i am not able to get the value.
I am really new in Fluid, Typo3.
Hopefully someone can help me.
One way to do this, is to create a fluid variable author and assign the author object to it. Then you can access the name using {author.name}.
To create a variable, you could use the ViewHelper <f:alias>, like this:
<f:alias map="{author: '{feed:item.get(value: \'author\')}'}">
{author.name}
</f:alias>
Another way would be to use the extension "vhs", which provides many tools for use with fluid. One of these tools is the ViewHelper <v:variable.set>, which could be used like this:
<v:variable.set name="author" value="{feed:item.get(value: 'author\'}"/>
{author.name}
This has the advantage that you don't need to use the variable within the tags of the ViewHelper.
There are other ways to reach the same goal, without defining variables, but this seems to be the easiest one to me.

Remove or Filter Options on Exposed Filter in Drupal 7

I'm working on a website that uses Domain Access and for a view I expose a filter that lists all domains. I want to list only some domains but not all. I know that it is posible to do with the filter configuration, but if I limit the options this way there is a problem with ajax which changes all the options text to "1", and in any case I want to know how to do it, with either hook form alter or with themes, but preferably using some hook from a module.
In general I'm trying to figure out how to remove some of the options for an exposed filter, I managed to do it using JQuery, but I'd like to do it with php, thanks!
It should be fairly straight forward using hook_form_FORM_ID_alter as you suggest. First inspect the <form> element of the filter form (using Firebug or similar) and get it's ID attribute. The form's ID in Drupal will be that string but with all '-' characters replaced with '_' (so 'form-exposed-filter-form' would become 'form_exposed_filter_form').
The thing to remember is that the submit handler for the form will probably be expecting the elements you're trying to remove to be there, so you'll probably get unexpected results if you just yank them out of there.
The way round it is to change the type of the elements to value instead (so they're available to the submit function in $form_state['values']), and choose a default value for each. Something like this:
// Replace 'FILTER_FORM_ID' with the form's ID
function mymodule_form_FILTER_FORM_ID_alter(&$form, &$form_state, $form_id) {
$form['some_existing_element'] = array(
'#type' => 'value',
'#value' => $default_value_for_filter
);
}

Resources