Sass-lint, ignore nesting - sass-lint

How to ignore this error:
Class should be nested within its parent Class force-element-nesting.
I don't knwo how to ingore this error when I'm overriding soem classes with a lot of nesting in one line like:
// sass-lint:disable ???
.one.two.three.four .soemthing .else,
.one.three.hello .another. else
{
//
}
Currently I'm using // sass-lint:disable-all for all blocks.

It should work with this:
// sass-lint:disable force-element-nesting
.one.two.three.four .soemthing .else,
.one.three.hello .another. else
{
//
}
// sass-lint:enable force-element-nesting
Enabling again to restore the rule.

Related

Different Behaviour of defer statement

I have code on logout button, where I'm using defer statement.
I'm wondering when changing position of defer statement code inside action method scope.
I have added defer statement at the end of method, it's show me warning.
'defer' statement before end of scope always executes immediately;
replace with 'do' statement to silence this warning
Code:
override func sendButtonTapped(sender: Any) {
self.deleteCoreData()
self.clearUserDefaults()
// Clear view context
AppDelegate.shared.persistentContainer.viewContext.reset()
....
....
// Call after all code execution completed in this block's Scope
defer {
// Set isUserLoggedIn and change root view controller.
UserDefaults.Account.set(false, forKey: .isUserLoggedIn)
AppDelegate.shared.setRootViewController()
}
}
Then, I have added defer statement at start of method, it's show nothing.
Code:
override func sendButtonTapped(sender: Any) {
// Call after all code execution completed in this block's Scope
defer {
// Set isUserLoggedIn and change root view controller.
UserDefaults.Account.set(false, forKey: .isUserLoggedIn)
AppDelegate.shared.setRootViewController()
}
self.deleteCoreData()
self.clearUserDefaults()
// Clear view context
AppDelegate.shared.persistentContainer.viewContext.reset()
....
....
}
Can anyone explain what exactly happening with defer statement?
To sum it up, the defer statement will be executed at the end of the scope you are in. ( .apple doc : https://docs.swift.org/swift-book/ReferenceManual/Statements.html#grammar_defer-statement )
from apple doc
func f() {
defer { print("First defer") }
defer { print("Second defer") }
print("End of function")
}
f()
// Prints "End of function"
// Prints "Second defer"
// Prints "First defer"
The defer statement allow you to define an action that will be executed after the rest of the operation you want to be done, i.e. at the end of the scope.
The warning is pretty explicit too, considering you place the defer statement just at the end of the scope, it doesnt serve any purpose :
func f() {
print("TIC")
defer { print("TAC") } // will be print at the end of the function
}
f()
// Prints "TIC"
// Prints "TAC""
which is the very same as :
func f() {
print("TIC")
print("TAC") // no defer, same result
}
f()
// Prints "TIC"
// Prints "TAC""
To go further
Then why do the warning propose you a do block ?
Actually, the two previous example are not 100% the same, when you use the defer statement, it creates its own scope
func f() {
// here you are in the scope of the `f()` function
print("TIC")
defer {
// here you are the scope of the `defer` statement
print("First defer")
}
}
And the closest way to manually create a scope is the do statement
func f() {
// here you are in the scope of the `f()` function
print("TIC")
do {
// here you are the scope of the `do` statement
print("First defer")
}
}
From apple docs
The do statement is used to introduce a new scope and can optionally contain one or more catch clauses, which contain patterns that match against defined error conditions. Variables and constants declared in the scope of a do statement can be accessed only within that scope.
if you want to learn more about scopes, here is some lecture: https://andybargh.com/lifetime-scope-and-namespaces-in-swift/
Essentially, an objects scope defines the areas of our program from which the item can be accessed.
According to Swift documentation:
A defer statement is used for executing code just before transferring program control outside of the scope that the defer statement appears in. This means that a defer statement can be used, for example, to perform manual resource management such as closing file descriptors, and to perform actions that need to happen even if an error is thrown.
In your example, using a defer at the end of the code is useless because the code will be executed exactly in the same way as if it was outside the defer, as a defer executes the code just before exiting the current scope (method).

Grails update one to many from the parent domain model

I am updating a one to many from the parent domain model in a controller.
The code looks like this:
def update() {
def parentInstance = Parent.get(params.id)
params.childerenDetails.each {
parentInstance.addToChildrens(
newChildren(
name:params.get(name_"${it}"),
age:params.get(age_"${it}"))
}
}
if(parentInstance.validate()) {
parentInstance.save(flush:true)
} else {
render "error found at"+parentInstance.errors
}
}
....here I have custom validation in Parent class when adding the children values getting that parent validation error but children objects are saving into the db ..how to prevent if validation error comes in parent domain
If you want to prevent children to be stored into the DB you need to either move this code into a Service, which is transactional by default, or encapsulate all the code inside a Parent.withTransaction { // }. If you choose to use the service you can throw a RuntimeException when you detect the validation error to force a rollback. If you use the withTransaction, you can manually rollback it with status.setRollbackOnly:
def parentInstance = Parent.get(params.id)
Parent.withTransaction { status ->
params.childerenDetails.each {
parentInstance.addToChildrens(
newChildren(
name:params.get(name_"${it}"),
age:params.get(age_"${it}"))
}
}
if(parentInstance.validate()) {
parentInstance.save(flush:true)
} else {
status.setRollbackOnly()
}
}
Not sure if a parentInstance.save(flush: true, failOnError: true) would trigger a rollback as well.
I would prefer though to move the business logic to a Service. See withTransaction and Services docs in the Ref Guide.

How does grails pass arguments to controller methods?

In grails controller examples, I have seen save(Model modelInstance) and save(). I tried them both, both of them works. I imagine grails instantiates the modelInstance with the params. Is my assumption correct?
I also noticed in index(Integer max), does the param has to be named max? or any name would work as long as it is a number?
How does these passing of arguments work underneath?
If you write a controller like this...
class MyController {
def actionOne() {
// your code here
}
def actionTwo(int max) {
// your code here
}
def actionThree(SomeCommandObject co) {
// your code here
}
}
The Grails compiler will turn that in to something like this (not exactly this, but this describes effectively what is happening in a way that I think addresses your question)...
class MyController {
def actionOne() {
// Grails adds some code here to
// do some stuff that the framework needs
// your code here
}
// Grails generates this method...
def actionTwo() {
// the parameter doesn't have to be called
// "max", it could be anything.
int max = params.int('max')
actionTwo(max)
}
def actionTwo(int max) {
// Grails adds some code here to
// do some stuff that the framework needs
// your code here
}
// Grails generates this method...
def actionThree() {
def co = new SomeCommandObject()
bindData co, params
co.validate()
actionThree(co)
}
def actionThree(SomeCommandObject co) {
// Grails adds some code here to
// do some stuff that the framework needs
// your code here
}
}
There is other stuff going on to do things like impose allowedMethods checks, impose error handling, etc.
I hope that helps.

Grails 2 - How to dynamically call multiple datasources

I have two named data sources in my Grails app (Grails 2.0.3)...
dataSource_a {
// ...
}
dataSource_b {
// ...
}
I'd like the ability to dynamically change what datasource I'm accessing, based on some kind of parameter. I could do something like this...
def findPeople(datasource) {
if (datasource == 'a') {
return Person.a.list()
} else if (datasource == 'b') {
return Person.b.list()
}
}
What I was really hoping to be able to do, though, is something like this...
def findPeople(datasource) {
return Person."$datasource".list()
}
Unfortunately, I get an error when I try and do that. "Fatal error occurred apply query transformations: null 1 error".
Any thoughts on how to accomplish this? Or am I just stuck with if/switch blocks?
I figured it out, this is how you have to do it.
def findPeople(datasource) {
def p = People.class
p."${datasource}".list()
}
For some reason, if you call it like that, it works.

Grails Property Expression instead of Domain Object in Web Flow?

We are currently trying to build some stuff with Grails Web Flows.
We are setting an object in the Flow (using flow.objectName = objectInstance), but when we try to access it in the next step of the Flow (using flow.objectName), the Object is not set, but instead there is a org.codehaus.groovy..... .PropertyExpression, that has none of the methods we want to use.
The Code we used to set and get works in other cases, and we cannot find any differences.
What is a Property Expression?
What are we doing wrong, any clues or Problems that happen often with Webflows?
Thank you in advance for your time.
Make sure your Webflow DSL syntax is correct.
For example
def someFlow = {
eventAction {
flow.value = someValue // This is incorrect
action {
flow.value = someValue // This is correct
}
on("success").to "eventDisplay"
}
eventDisplay {
on("finish").to "end"
flow.anotherValue = somethingElse // This usually causes the behavior you are seeing.
// Proper way of setting flow.anotherValue
on("finish2") {
flow.anotherValue = somethingElse
}.to "end"
}
end{}
}

Resources