extjsizable

This gem insert the method to_extjs into ActiveRecord and Array. Messages generated are valid to be used against Ext JS 4 (www.sencha.com/products/extjs/). Now it’s possible to generate Ext JS 3 JSON data although it’s in experimental state. It requires Rails 3.0 or latter to work properly.

ActiveRecord

To load a form, the data extjs expect to meet is like so:

{
  "data": { 
    "id": 1, 
    "title": "First Post",
    "body": "This is my first post.",
    "published": true, ...
   }, 
   "success": true
}

If ActiveRecord::Base.wrap_with_brackets is true then, the model name wraps all attributes:

{
  "data": { 
    "post[id]": 1, 
    "post[title]": "First Post",
    "post[body]": "This is my first post.",
    "post[published]": true, ...
   }, 
   "success": true
}

It allows to use this gem with Ext JS 3.

If ActiveRecord::Base.include_root_in_json is true then, the model name is used instead of data key:

{
  "post": { 
    "id": 1, 
    "title": "First Post",
    "body": "This is my first post.",
    "published": true, ...
   }, 
   "success": true
}

So that it is as easy as do:

@post.to_extjs

In case the model has some failures, the sentence above will return:

{
  "errors": { "title": ["Title can't be blank"] },
  "success": false 
}

Parameters accepted are the same as as_json from ActiveModel.

Array

When to_extjs is called from ActiveRecord (or hashes) collections, Ext JS expect to receive a structure like that:

{ 'total' : 2,
  'data' : [
     { 'id' : 1, :title : 'First Post'  },
     { 'id' : 2, :title : 'Second Post' }
   ]
}

It is generated executing

Post.all.to_extjs

Parameters accepted are the same as as_json from Array, nesting into each record.

if Array.dasherize_keys is true, then nested Hashes are plained:

{ 'total' : 2,
  'data' : [
     { 'id' : 1, 'title' : 'First Post', 'owner' : { 'name' : 'John'}  },
     { 'id' : 2, 'title' : 'Second Post', 'owner' : { 'name' : 'Frank'} }
   ]
}

, becomes to

{ 'total' : 2,
  'data' : [
     { 'id' : 1, 'title' : 'First Post', 'owner_name' : 'John' },
     { 'id' : 2, 'title' : 'Second Post', 'owner_name' : 'Frank' }
   ]
}

For now, it works in belongs_to associations. has_many requires duplication of each line when a nested Array appears. I dont think you need that.

TODO

  • Tests, tests, tests.…