I have been using this little plugin I wrote for some time now.
The main idea is that I always start working in the migrations, and there I define: not null fields, length for string fields, what fields are numbers and what are not, references to other models (this at least can be guessed with the _id ending), unique fields or field groups and things like that.
After the migration is executed, I need to rewrite almost it all, with even more information in the model, for example: the maximum length for a string field is not enough anymore, now you need the minimum length too.
And I really do not like idea of doing the same thing more than once (I think some of you agree with me, even if you do not agree with my solution).
To solve this problem, I created this little plugin, that is really a generator
You want to see some code? So, let’s start with a migration: ./script/generate migration example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class Example < ActiveRecord::Migration def self.up create_table :examples do |t| t.string :name, :null => false t.text :description t.integer :a_number t.references :example end end def self.down drop_table :examples end end |
Now, let’s run rake db:migrate && ./script/generate drymodel example
And it will generate the following code for the model:
1 2 3 4 5 6 7 8 9 | class Example < ActiveRecord::Base #Relationships belongs_to :example #Validations validates_presence_of :name validates_numericality_of :a_number, :allow_nil => true validates_length_of :name, :maximum => 255, :allow_nil => true validates_associated :example, :allow_nil => true end |
Up to now, it generates validations for uniques, non nulls, numerics, associations, …
pretty much, every thing that you have already wrote on the migration.
Liked it?
to install, just add the github gem source (you need to do it only once):
gem sources -a http://gems.github.com/
and to install the plugin gem:
gem install urubatan-mydry_generator
Now you are ready to play!
If you want to take a look at the code, it is hosted in GitHub.
There is also a dryscaffold generator, that will do the same as the standard scaffold (with REST and all), but it list the columns from the database and you do not need to name them on the command line.
But I do not think it is that useful
If you enjoyed this post, make sure you subscribe to my RSS feed!
Have you ever read about easyb? and about Grails? May be you answer yes to one or both previous questions, but this is the first time you will find them together I guarantee, at least in a plugin form.
This is the first post I’m writing about my latest toy project (at least for now it is only a toy project).
The project is hosted at GitHub, and any help will be very welcome since I do not work every day with Grails, but I did a presentation about grails in the last weekend and wanted to use BDD instead of the traditional GUnit based unit tests, I already use Easyb for some of my Java projects, then I wanted to make Easyb and Grails to work together.
It was not an easy task, because of some craziness of Grails class loader, but it is working fine now.
You can write a test with the easyb sintaxe, and the test will look line this:
scenario "Project creation", { given "A new project", { project = new Project() } when "the project name is supplied", { project.name = 'My test project' } and "there is no other project with that name", { validateproject = { project.validate().shouldBe true } } then "a new project must be created", { validateproject() project.save(flush:true).shouldNotBe null } } scenario "User association with a project", { given "Any existing project", { project = Project.findAll()[0] } when "a list of users is supplied", { users = [new User(login:'testUser1',password:'testUser1',email:'test1@test.com').save(),new User(login:'testUser2',password:'testUser2',email:'test2@test.com').save()] } then "the users must be associated with that project", { users.each{ ProjectMembership.link(it,project) } } }
You can even show it to your boss, because the output of this test is some thing like this:
Story: project management
scenario Project creation
given A new project
when the project name is supplied
when there is no other project with that name
then a new project must be createdscenario User association with a project
given Any existing project
when a list of users is supplied
then the users must be associated with that project
And the best part of it, is that you can use your tests as the specifications of your next grails project!
If you want to know more about the easyb sintax, take a look at the project site.
If you want to know more about grails, take a look at the project site.
If you want a new functionality in this plugin, I’ll probably accept all patches and push requests you send-me ![]()
And finally, if you want to install the plugin, download it from here and run:
grails install-plugin grails-easybtest-latest.zip
You will need to place the tests you write in the folder tests/behavior
And to run the tests, just type: grails easyb-test
Any doubts or suggestions leave a comment here ![]()
I hope it can be useful to others.
If you enjoyed this post, make sure you subscribe to my RSS feed!
Tags: easyb, easyb-test, grails, petproject, plugin, test
Google has launched a very cool application server, it is called Google App Engine.
Kudos to google again
But this application server started a big war on the internet, lots and lots of developers around the world, are very sad because the only supported language in the application server is Python.
Google App Engine has free accounts with limitations, like heroku for example, which is a great application server that supports only Ruby On Rails.
So, what is the problem?
The big problem is that because App Engine is from Google, most of the developers that haven’t not learned Python yet are yelling around because Google App Engine does not support their language.
But the real problem here is that these developers do not understand that a programming language is only a tool you need for work.
Most of the money I get every month is from Java programming, but I have to use many other languages, like Ruby, Bash, ActionScript/Flex, Java, C++, Object Pascal/Delphi, …
<opinion>
If you think yourself as a developer and you can write programs using only one language, you is probably a very bad developer.
</opinion>
I think this bunch of “why did you choose python instead of my beloved Java/Ruby/Perl” is just because it is from google, and everyone wants some google love.
Why no one yells about Heroku supporting only Ruby On Rails? Not even the Merb programmers …
But if google created Heroku (almost it did now), every one would complain about the chosen language.
I liked the idea of Google App Engine, probably this was the push I needed to learn Python (I never liked it, and never had any good reason for that, I just do not want the idea of spaces defining code blocks
), the only boring thing is all this yelling about the supported language.
If you enjoyed this post, make sure you subscribe to my RSS feed!