Thursday 23 February 2012

Leonardo: update to 1.10 version

With 1.10 version I introduced the following changes:


  • In the index page, I moved the filters on the right and layout now seems more clean. For numeric fields, and dates can be filtered more easily by inserting the operator <, >, <>, <= o >= directly in the text field.
  • The generator detects the field named "state" and imposed a series of operations to manage the state (intended as the process status) in accordance with the gem state_machine.
  • Still in the index page, clicking the arrow beside the name of the nested resource, will perform an ajax call that adds information dynamically under the parent resource. Clicking on the name instead, you run a http call as before.
  • I improved the automatic routes creation

But we get into detail with some examples.
Create a new application and name it as suggested by your imagination, I call it Leonardo:

rails new Leonardo -m template.rb

At the moment is recommended to use the template locally, so download it or take it from the root where you installed the gem.

Respond y everything you want to include. You can choose whether to install the gems locally, by specifying the name of the folder, or if you install it in the default path by simply pressing Enter. I also added the choise to install activeadmin and if you decide to include it you need:

rename the file app\assets\stylesheets\application.css to application.css.scss

inside that file remove the line
*= require_tree .

and add at the bottom
@import "cloudy";
cloudy is the style name, currently also the only one

if we now start the server and visit the address
http://127.0.0.1:3000

You should see the graphic layout without defects created by the interference of the activeadmin css
and here
http://127.0.0.1:3000/admin

you can sign in activeadmin using the default credentials as currently reported in the documentation

 admin@example.com e password

will be accessible only to the dashboard and I would refer you to the documentation for how to use it.
To sign in the leonardo interface instead, you have the usual three users with three different roles that can be managed as provided by the gem cancan:user@leonardo.com
manager@leonardo.com
admin@leonardo.com

replace leonardo with the name of your application
user@yourapp.com

for all these the password is รจ abcd1234

now create the product resource taking advantage of rails 3.2:

rails g leosca product name description:text 'price:decimal{9,2}' 'in_stock:integer{2}' available:boolean from:date 'state:integer{1}'

create the table
rake db:migrate

If we added the state field, Leonardo tries to involve the gem state_machine so you have to add the states into the model:

class Product < ActiveRecord::Base
  state_machine :state, :initial => :new do
    state :new,         :value => 10
    state :working,     :value => 20
    state :completed,   :value => 30
    state :closed,      :value => 40
    state :canceled,    :value => 50
  end
end

before populate the new table, edit the file db \ seed.rb customizing state values ​​as included in the model after which we can perform:
rake db:seed

Visit the product list:


Note the filter on the price for values ​​above 10 pound, with the operator directly in the text box before the value

Note also the State column with the default color and description I18n both easily customizable.

To change the language, you just pass the parameter lang anywhere in the application:
http://127.0.0.1:4000/products?lang=it

will change the standard text and the names will be translated manually into the file:
\config\locales\it.yml

In edit view, the states can be update by a select box:



Now create comment, a nested resource under product:

rails g leosca comment product:references body:text approved:boolean 'score:integer{1}' --under=product

Also in this case, before to populate the table you can customize the file db \ seed.rb inserting for example more records associated with the first product. Furthermore I usually comment out the entries have already been used to avoid creating duplicates, then perform again:
rake db:seed

if we visit the previous list, you will find the reference to nested resources and if you click on the small arrow next to the name the list will be updated directly with the list of related resource:


Of course leonardo provides only a starting point and nothing more than an unconventional scaffolds

Unfortunately, I was not able to update rspec tests which do not pass in the presence of the state field, but will be in next version.

I hope that this work will come in handy. There is still much to do and anyone interested to help me out can send me an email.