plugin « Intelligrape Groovy & Grails Blogs

Posts Tagged ‘ plugin ’

Bootstrap using faker plugin

Posted by on September 23rd, 2012

Every now and then we have to bootstrap data in our application. It takes lot of time thinking what name to give, what description to add and other stuff.
To overcome this, we can use faker plugin which can generate random tokens as per your need.

To use this, add following in BuildConfig.groovy

 compile ":faker:0.6"
 

And in BootStrap.groovy(or wherever we want to use it), just inject fakerservice:


def fakerService

It gives us options to generate name, city, zip code, sentences with given word count, paragraphs with predefined sentence count and a lot more.

Some samples:


fakerService.name() -- Santos Koelpin

fakerService.city() -- West Domenic fort

fakerService.zipCode() -- 60985

fakerService.sentence(10) -- Et quod expedita in qui eligendi est error quos eos aperiam quia libero

It saves a lot of time that we spend while bootstrapping data.

It helped me a lot.

Hope it helps you too.

Regards.

Vivek Sachdeva

vivek.sachdeva@intelligrape.com

Twitter : @vivek_sach

Posted in Grails, Plugin

Getting started with jQuery Mobile

Posted by on September 15th, 2012

JQuery mobile provides set of features which are supported on almost all smartphones such as touch UI, ajax navigation, animated page transitions. Building jQuery mobile page is very simple, here’s how:

  1. A jQuery Mobile site must start with an HTML5 ‘doctype’ to take full advantage of all of the framework’s features.
  2. First of all you need jQuery, jQuery mobile, mobile theme stylesheets from CDN.
  3. Reference all styles & scripts in the head of page.
  4. <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Page Title</title>
    <link rel="stylesheet" href="css/jquery.mobile-1.1.1.css"/>
    <script src="js/jquery-1.8.0.min.js"></script>
    <script src="js/jquery.mobile-1.1.1.min.js"></script>
    </head>
  5. A meta viewport tag in the head sets the screen width to the pixel width of the device.
  6. Inside body of html page a div with data-role of page is used as a wrapper for whole page.
  7. For header bar add a div with data-role of header.
  8. For content region add a div with a data-role of content & add content between this div.
  9. <body>
    <div data-role="page">
            <div data-role="header">
                    Header
            </div>
            <div data-role="content">
                    <p>This is jquery mobile test page</p>
            </div>
            <div data-role="footer">
                   ©intelligrape
            </div>
    </div>
    </body>
    
  10. There are many features provided by jQuery mobile. Here i am explaining one of them which is list. For list, simply add ul tag with attribute data-role of listview between

    .
  11. To make it look like an inset module add an attribute data-inset=”true”.
  12. For dynamic search filter just add another attribute data-filter=”true”.
  13. <div data-role="content">
        <ul data-role="listview" data-inset="true" data-filter="true">
              <li>Blue</li>
              <li>Black</li>
              <li>Green</li>
              <li>Yellow</li>
              <li>White</li>
        </ul>
    </div>
    

Grails Console, execute from file.

Posted by on September 7th, 2012

Grails Console is one of the most useful plugins available, it provides a console to application to which it’s installed. This plugin can be used to test code snippets amazingly fast, debug app, create patches and scripts.

And the latest update has made it even more fantastic. Now it has an option to execute code from a file. All one needs to do is specify the path and it will run code snippet from that file. Now this is of extreme use because, I used to store my scripts in files for later use. And now I don’t have to copy paste code every time, I can just specify absolute path of the file and it will do rest for me.

Sample Image below shows path of the file as “/home/hitesh/scriptFile” this is the file with the code.
And on the right side, the output screen shows code that was in file and its output below.

Grails Console 1.2

_________________________________
Hitesh Bhatia
Mail LinkedIn,Facebook,Twitter
_________________________________
Posted in Grails, Groovy, Plugin

Searchable plugin – Lucene search on numeric values

Posted by on July 14th, 2010

While using searchable plugin for the first time, I wasn’t aware that lucene implements only lexicographic comparisons for searching on indexed values (even on numeric fields). Before I learned this, I kept on wondering why a Person of age 6 always appeared in search results when I look for People more than 20 years of age. The solution was to define a transient field ageIndexValue in domain class Person and implement its getter as:

String getAgeIndexValue() {
    return org.apache.lucene.document.NumberTools.NumberTools.longToString(this.age)
}

Also, you will need to change the search query from

q=age:[20 TO *] to q=ageIndexValue:[0000000000000u TO *]
where, 0000000000000u == org.apache.lucene.document.NumberTools.longToString(20L)

~Aman Aggarwal
aman@intelligrape.com

http://www.IntelliGrape.com/

Posted in Grails

Configuring multiple senders in Grails Mail plugin

Posted by on June 14th, 2010

In one of the recent projects, we had to set up a system where emails for different purposes (i.e registration, error reporting etc.) were to be sent out from different email accounts. We were using the Grails Mail plugin which does not provide a clear way to set-up or configure multiple email addresses. With the help of my colleague, Chandan Luthra, I tried to dig into the workings of plugin and found out that Mail plugin injects the properties such as username, password etc. through a bean named mailSender. So we just did the following to customize/configure these properties on the fly from the controller or a service:

CH.config.grails.mail.default.from = "username@domain.com" // Mail plugin uses this to connect to the account in e-mail provider's server. So it should be set.
mailSender.username = "username@domain.com"
mailSender.password = "sample123!"

And yes, once you are done with your email sending functionality you can set mailSender properties back to the default settings set originally in Config.groovy. Your service/controller code could look something like this:

// Store the default mail settings in variables
def defaultFrom = CH.config.grails.mail.default.from
String oldUsername = mailSender.username
String oldPassword = mailSender.password

// Change the properties here; send the email
try {
CH.config.grails.mail.default.from = "username@domain.com"
mailSender.username = "username@domain.com"
mailSender.password = "sample123!"
mailService.sendMail {
to(['exampple1@domain1.com', 'example2@domain2.com'].toArray())
subject("Example Subject")
body("Sample body")
}
}
catch (Exception e) {
// catch block code
}
// Set the original settings back
finally {
CH.config.grails.mail.default.from = defaultFrom
mailSender.username = oldUsername
mailSender.password = oldPassword
}

I guess this scenario can be quite prevalent in many projects. Hope you find the blog useful.

- Abhishek Tejpaul

Intelligrape Software Pvt. Ltd.

[www.intelligrape.com]

Posted in Grails, Java tools, Plugin