Monday, May 12, 2014

My First Chortle



I worked with my daddy today to make this. This is a start of an app we are making called chortle. I hope this app will be a very motivating way to do chores!

Tuesday, May 6, 2014

Reverse domain name qualifiers

When do you specify a "reverse domain name" qualifier in Grails?

Is it only when creating domain classes? Or controllers? Both?
# Which one is correct?
grails create-domain-class Thing1
grails create-domain-class com.mycompany.myapp.Thing1

# Which one is correct?
grails create-controller Thing1
grails create-controller com.mycompany.myapp.Thing1

Sunday, April 27, 2014

Let's play did you know I'll go first

Let's play "Did You Know?"

I'll go first.

Did you know that we, the Chortlers, have our own Git repo on Bitbucket (a free, web-based Git repo hosting service)? And that said repo has its own wiki? And that I, for my part, am not crystal-clear on the dividing line between "stuff that goes in the blog" and "stuff that goes in the wiki"? Yep!

Check it out: https://bitbucket.org/chortlers/chortle/wiki/Home

(An acronym to help you remember that URL is: HBOCCWH.)

Sunday, April 20, 2014

Default Login Screen with Spring Security

The spring security plugin enables some amount of default application security without any substantial coding. Here was my 30 minute experiment tonight:

> grails create-app sectest

>cd sectest

In grails-app/conf/BuildConfig.groovy I added the following under the "repositories" section:

mavenRepo "http://repo.spring.io/milestone/"

and under the "plugins" section added:

compile ':spring-security-core:2.0-RC2'

Then:

> grails run-app

I did this to import the plugin. I probably could have just build it. Anyway with the plugin installed I did:

> grails stop-app

> grails s2-quickstart com.homesteadgaming User Role

> grails run-app

Then browsing to localhost:8080/sectest, I saw the login controller listed. Clicking on the login controller gave me this default screen:

My login attempts failed with a sensible error, which is expected since there are no users in my default database.

So next I populated a default user which I hear you can do by editing BootStrap.groovy. That looks like this:

class BootStrap {

    def init = { servletContext ->

    def adminUser = new com.homesteadgaming.User(
                username: 'admin',
                password: 'admin',
                enabled: true).save(failOnError: true)
    }
    def destroy = {
    }
}

And now after restarting the app again, I can login with admin/admin. That's nice.


Saturday, April 19, 2014

Grails Twitter Clone in 90 Minutes

For those looking for the next step beyond the basic CRUD style tutorials, Jeff Brown has a great presentation about creating a Twitter clone in grails during a SF Java Users Group meeting.

Sunday, April 13, 2014

Groovy and Sublime

IDEs are wonderful and all, but sometimes a nice text editor is all you need. In preparation for some Groovy training, I've been evaluating Sublime. Sublime doesn't support Groovy out of the box, but with some syntax highlighting from here, and a build setup from here, Groovy is all ready to go. With the mac's easy <cmd>+ for increasing font size, you can get a nice, clean REPL loop, perfect for demonstration purposes.

Here's a screenshot.


Wednesday, April 9, 2014

Wednesday Night Groovy Lesson

I'm going to do this from memory, so there may be errors in this. Also, my information may be dated, and possibly just plain wrong. Or...it might be the most correct -- the essence of correctness.

In groovy you can do something like this:

4.times {  println 'hi' }

Which will, not surprisingly print 'hi' four times.

In Java you would have to do the much more cumbersome:

for (int i = 0; i < 4; i++) { System.out.println("hi"); }

So how does Groovy way even work? '4' is just a java integer. A primitive. You can't call methods on primitives. Just Objects.

Part of the answer is that Java has "autoboxing", which means that primitives will be converted to their Object equivalents at runtime. So that will solve the problem of '4' not being an Object. At runtime it's a 'java.lang.Integer', not an 'int'.

But now there's another problem. There is no "times" method on java.lang.Integer. In fact, java.lang.Integer is final, so you're not even allow to subclass it. What sort of devilry is this?

Groovy being a dynamic language, doesn't care at compile time if you try and execute a method on an object that doesn't exist. In fact, at compile time Integer.times() doesn't exist. When Groovy starts up, at runtime, it will graft on the "times" method to the Integer class.

So what's that argument? It doesn't look like a normal Java parameter. It might look more clear if you saw the entire form:

4.times ( {...})

But we leave out the parenthesis because they are unnecessary. Now it should be clear that "times" is a method that takes a single parameter: a closure. A closure is essentially an anonymous method: a code block without a name.

At the end of it all then you have a way to concisely execute the body of a loop (what would be a "for" loop in Java) a given number of times hijacking the Integer class to make it readable.