Showing posts with label Leonardo. Show all posts
Showing posts with label Leonardo. Show all posts

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.

Tuesday, 13 December 2011

Leonardo: update to version 1.9

I just released the1.9 version which enhances and completes the resources management.



If you use the --under option to nest the new resource under another existing one, must be edited the file config / routes.rb because the operation will create the new routes nested, overwriting the originals.


For example if we create the product resource, leonardo will create these routes:
resources :products do
  post :select,           :on => :collection
  post :edit_multiple,    :on => :collection
  put  :update_multiple,  :on => :collection
  put  :create_multiple,  :on => :collection
end

if then we create the comment resource under product you will get:
resources :products do
  resources :comments do
    post :select,           :on => :collection
    post :edit_multiple,    :on => :collection
    put  :update_multiple,  :on => :collection
    put  :create_multiple,  :on => :collection
  end
end

resources :products do
  post :select,           :on => :collection
  post :edit_multiple,    :on => :collection
  put  :update_multiple,  :on => :collection
  put  :create_multiple,  :on => :collection
end

but you have to join it to get something like this:
resources :products do
  post :select,           :on => :collection
  post :edit_multiple,    :on => :collection
  put  :update_multiple,  :on => :collection
  put  :create_multiple,  :on => :collection
  resources :comments do
    post :select,           :on => :collection
    post :edit_multiple,    :on => :collection
    put  :update_multiple,  :on => :collection
    put  :create_multiple,  :on => :collection
  end
end
Moreover, the list of resources shows a link to the nested resource for the parent who uses the name field. If not present you should replace it with some other field otherwise you can add the method name into the model:
class Product < ActiveRecord::Base

  def name
    self.title
  end
end

Monday, 10 October 2011

Leonardo: Update to version 1.8

With the latest version 1.8 I added the chance to sort the columns by this approach:
http://railscasts.com/episodes/228-sortable-table-columns

I also added by default the auto submit to the search form on the index view when the values ​​are changed for fields like select, radio ... those with a net change of the value, avoiding the text box to not encumbering the database. However it is possible to add or remove this feature, by adding or removing the autosubmit class :

<%= f.collection_select :category_id, Category.all, :id, :name, {:prompt => true}, {:class => 'autosubmit'} %>

In custom.js file (called from application.js) I added a bind (or more precisely, a live):

$('.autosubmit').live('change', function() {
  setTimeout("$('#"+this.id+"').parents('form:first').submit();", 300);
  return false;
});

I use a timeout to run the submit after eventual updates, usually hidden fields coupled to self completions which currently leonardo does not handle but it will do soon.

With version 1.7 released in late September that I have introduced the option leospace:

rails g leosca decode name:string --leospace=admin

I fixed the code for compatibility with Ruby 1.9.2 which has a slightly different String class.

Now the development users are no longer created by migration, being a database population their place is in the file db / seeds.rb used with rake db: seed. Handy for db:reset or db: schema: load utilities that previously they deleted users.

Create the resource "decode " in no namespace and only the management under the namespace specified in the parameter leospace.

The original scaffold has something similar as well, let's take this example:
rails g scaffold admin/decode name:string

This creates the resource and its management in admin namespace.

We examine the differences:

Both will create paths in admin namespace:
/admin/decodes
/admin/decodes/:id
ecc.


With the scaffold, the resource is called Admin:: Decode and table admin_decodes

With leonardo, the resource is called only Decode and it is not under a namespace. The table is called decodes.

The transaction meets the need to create different interfaces for the same resource. Of course you can also use the original leonardo Methodology:
rails g leosca admin/decode name:string


The latest updates details:

1.8.3 (November 8th, 2011) Marco Mastrodonato
  • Controller/sort_column => Fixed an issue with multiple tables
  • Dev users are now added by rake db:seed, no more by migration
1.8.2 (October 10th, 2011) Marco Mastrodonato
  • List: Added id as first column
  • Replaced String.any? with String.size>0 for ruby 1.9.2 support


1.8.1 (October 10th, 2011) Marco Mastrodonato
  • Updated rspec to pass index view test with two new controller helpers
  •  Date managed with datepicker are now in year-month-day format and now works with sqlite3
  • Added autosubmit class by default. A new function in custom.js let you to autosubmit searches on filter fields change event
1.8.0 (October 7th, 2011) Marco Mastrodonato
  •  Added sortable columns
1.7.2 (October 5th, 2011) Marco Mastrodonato
  • Updated formtastic support to version 2.0
  • Updated template.rb
1.7.1 (October 3th, 2011) Marco Mastrodonato
  • Fixed a layout issue
1.7.0 (September 28th, 2011) Marco Mastrodonato
  • New feature to add a resource management within a namescape. Unlike the original way (rails g leosca admin/resource ...which remains available), the resource is not under the namespace but only its management so you could create one for several kind of users. Try it adding the new option like this example: --leospace=admin
  • Huge code improvements