Best practices for passing params in Grails 3 - grails

I'm building a Grails application that starts with loading XML file and based on it generates (dynamically) a form that has checkboxes and a submit button. When a user submits a form, a controller receives the params (checkboxes) and calls a method that reads an Excel file to extract records that matches the params.
In other words, the scenario I'm trying to implement is that the user submits a form that has some checkboxes (generated dynamically from an XML file), and the params will be passed to a controller that runs a script or a service, which uses these params as criteria to retrieve some rows from an xlsx file and displays these rows in a view.
My question is what are the best practices for doing that?
I pass the params from a controller to a service (although I don't
have a domain class for the data --it can be updated everyday), And
inject this service into BootStrap.
I call a method in an external groovy class (in the src folder) that
uses the params to retrieve data from the Excel file.
Also, what is the most efficient way to parse an Excel or CSV file and extract records with data that match the params? (I can't use DB or domain classes because the Excel file can be updated with new columns over time).

params in Grails do a good job. However, I would suggest that you consider using command objects. Command objects are like Grails domains but they do not persist data in a physical database. You can easily pass the instantiated command object to the external groovy class (in the src folder) instead of passing params.
You can find more details here: http://guides.grails.org/command-objects-and-forms/guide/index.html

Related

Dynamically construct url in swagger for each operation

I Check the swagger and we can have URL dynamically for various site based on Server specified. But i have doubt whether is it possible to construct URL dynamically for each operation similar like this
http://{serverurl}/{tagName}/{operation}
I know that server url can be configured at the top of swagger.json but how can we configure/add tag name to the URL dynamically.
Example:
consider i have following scenario
I have following tags,
customer
user
admin
Each tags have various operations associated with them, I need to add the tag name along with operation name dynamically in swagger URL as following
If the operation is getRecentActivity it need to be like
http://www.example.com/{tagname}/{operation}
which is similar like
http://www.example.com/customer/getRecentActivity
Note:- My configuration is in such a way, i know i can directly specify it in path itself by i required it to generate dynamically based on tag name

Symfony linking to an admin Table record edit page

kind of finding it difficult to wrap my head around this. For those who have used the Symfony admin generator extensively, for each module based of a backend module, there is an edit page for all the records. Typically this can be accessed like this:
module/primarykey/edit. (assume questions/1/edit)
which is strange because typically the primary key would be passed in as a URI parameter like:
questions/edit/1. Anyways, that maybe irrelevant. What is important is how do I manage to generate a link_to for the above URI. I am linking the editSuccess page through an external page which does not belong to the UI. The syntax I use is
link_to('Edit','questions/'.$primary_key.'/edit') // (where $primary_key = 1 as in this case)
However that auto modifies itself to :
/backend_dev.php/questions/1/action note the action instead of edit
No such action exists and it returns a 404 error stating that questions/action does not exist
To summarize, How do you link to an admin page that renders specifically for a record?
The url_for (and thus the link_to) helpers deal with internal urls, not external ones. The syntax is module/action?parameters. In your case this would be question/edit?id=$primarykey (assuming the action looks for the id parameter).
If you give a name to your route, that makes generating the link faster (hashtable lookup vs. linear search):
echo url_for("#question_edit?id=$primary_key");
If you set up your route as an sfDoctrineRoute, it gets even simpler:
echo url_for("question_edit", $question);
note how you need not pass the id, but the question object - the route class will fetch all neccessary parameters.

Testing "HTML fixtures" with RSpec and rails

I have a web scraper built to parse html from a website and I'm trying to write tests for it.
The class I'm trying to test receives a Nokogiri HTML object and extracts the required data from it. Now as usual the html can vary, sometimes elements will be missing or whatnot. I need to test these different situations.
So what I'd like to do is make a bunch of html files, each one representing a case with a particular element missing etc. For each html file, I wish to also construct an associated hash of the data I would expect the scraper to extract, assuming it is working correctly.
So I would like to write a test which will iterate over these html files and compare the data extracted by the class being tested against the expected data and report whether or not it is correct.
Any suggestions as to how to do this?
Have a look at the Artifice, fakeweb or webmock gems, which override net/http in order to supply testable results.

How to access AJAX hash values in ASP.NET MVC?

I'm considering using the hash method to create static urls to content that is managed by ajax calls in a Asp.Net MVC. The proof of concept i'm working on is a profile page /user/profile where one can browse and edit different sections. You could always ask for the following url /user/profile#password to access directly to you profile page, in the change password section
However, i'm wondering if i'm not starting this the bad way, since apparently i can't access the part after the hash in any way, except by declaring a route value for the hash in global.asax. So i'm wondering if this is the right way to access this part of the url?
Am i supposed to declare a route value, or is there another way to work with hash values (a framework, javascript or mvc)?
Edited to add:
In pure javascript, i have no problem using the window.location.hash property, i'm not sure though how standard it is in today's browsers, hence the question about a javascript framework/plugin that would use it.
The thing is that the part that follows the hash (#) is never sent to the server into the HTTP request so the server has absolutely no way of reading it. So no need to waste time in searching for something that doesn't exist.
You could on the other hand tune your routes to generate links that contain the hash part so that client scripts can read it.
Send the hash value document.location.hash as a parameter to the controller action of your choice.
This can be done in the code if needed...
RedirectResult(Url.Action("profile") + "#password");
should work fine

Generate xml from form inputs

i have a form in Asp.net MVC. On submitting i want to generate the xml of the input. Whats the best way of doing it?
I can do it by capturing the names/id of form in Post event and can generate xml.
Is there any better automated way?
A form could be submitted using either application/x-www-form-urlencoded or multipart/form-data. In both cases you get name=value pairs which cannot automatically be mapped to an hierarchical structure such as XML without any manual indication. As far as XML creation is concerned you may take a look at the XDocument class or XmlWriter. Another alternative is to rely on the default model binder which will instantiate a view model passed as an action parameter that you could serialize to XML.
Are you trying to submit XML over HTTP from the form? If so, you'll need javascript. Jquery can be very helpful here too.
If you are trying to generate XML on the server-side, check out XmlSerialization, its the only way to fly.

Resources