extjsizable 1.0.0.alpha
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/Gemfile +18 -0
- data/Gemfile.lock +46 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +62 -0
- data/Rakefile +49 -0
- data/VERSION +1 -0
- data/extjsizable.gemspec +67 -0
- data/lib/extjsizable/active_record/extjs.rb +50 -0
- data/lib/extjsizable/core_ext/array/extjs.rb +78 -0
- data/lib/extjsizable.rb +8 -0
- data/spec/extjsizable_spec.rb +123 -0
- data/spec/spec_helper.rb +48 -0
- metadata +137 -0
data/.document
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
gem "activesupport", ">= 3.0"
|
5
|
+
gem "activerecord", ">= 3.0"
|
6
|
+
|
7
|
+
# Add dependencies to develop your gem here.
|
8
|
+
# Include everything needed to run rake, tests, features, etc.
|
9
|
+
group :development do
|
10
|
+
gem "rspec", "~> 2.3.0"
|
11
|
+
gem "bundler", "~> 1.0.0"
|
12
|
+
gem "jeweler", "~> 1.6.3"
|
13
|
+
gem "rcov", ">= 0"
|
14
|
+
end
|
15
|
+
|
16
|
+
group :test do
|
17
|
+
gem "sqlite3"
|
18
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.0.5)
|
5
|
+
activesupport (= 3.0.5)
|
6
|
+
builder (~> 2.1.2)
|
7
|
+
i18n (~> 0.4)
|
8
|
+
activerecord (3.0.5)
|
9
|
+
activemodel (= 3.0.5)
|
10
|
+
activesupport (= 3.0.5)
|
11
|
+
arel (~> 2.0.2)
|
12
|
+
tzinfo (~> 0.3.23)
|
13
|
+
activesupport (3.0.5)
|
14
|
+
arel (2.0.9)
|
15
|
+
builder (2.1.2)
|
16
|
+
diff-lcs (1.1.2)
|
17
|
+
git (1.2.5)
|
18
|
+
i18n (0.5.0)
|
19
|
+
jeweler (1.6.3)
|
20
|
+
bundler (~> 1.0)
|
21
|
+
git (>= 1.2.5)
|
22
|
+
rake
|
23
|
+
rake (0.9.2)
|
24
|
+
rcov (0.9.9)
|
25
|
+
rspec (2.3.0)
|
26
|
+
rspec-core (~> 2.3.0)
|
27
|
+
rspec-expectations (~> 2.3.0)
|
28
|
+
rspec-mocks (~> 2.3.0)
|
29
|
+
rspec-core (2.3.1)
|
30
|
+
rspec-expectations (2.3.0)
|
31
|
+
diff-lcs (~> 1.1.2)
|
32
|
+
rspec-mocks (2.3.0)
|
33
|
+
sqlite3 (1.3.3)
|
34
|
+
tzinfo (0.3.25)
|
35
|
+
|
36
|
+
PLATFORMS
|
37
|
+
ruby
|
38
|
+
|
39
|
+
DEPENDENCIES
|
40
|
+
activerecord (>= 3.0)
|
41
|
+
activesupport (>= 3.0)
|
42
|
+
bundler (~> 1.0.0)
|
43
|
+
jeweler (~> 1.6.3)
|
44
|
+
rcov
|
45
|
+
rspec (~> 2.3.0)
|
46
|
+
sqlite3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Ungue
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
= extjsizable
|
2
|
+
|
3
|
+
This gem insert the method to_extjs into ActiveRecord and Array.
|
4
|
+
Messages generated are valid to be used against <b>Ext JS 4</b> (http://www.sencha.com/products/extjs/).
|
5
|
+
It requires Rails 3.0 or latter to work properly.
|
6
|
+
|
7
|
+
== ActiveRecord
|
8
|
+
|
9
|
+
To load a form, the data extjs expect to meet is like so:
|
10
|
+
|
11
|
+
{
|
12
|
+
"data": {
|
13
|
+
"id": 1,
|
14
|
+
"title": "First Post",
|
15
|
+
"body": "This is my first post.",
|
16
|
+
"published": true, ...
|
17
|
+
},
|
18
|
+
"success": true
|
19
|
+
}
|
20
|
+
|
21
|
+
<tt>* Previous versions of extjs needed attributes wrapped with the model name. Now this gem doesn't allow this.</tt>
|
22
|
+
|
23
|
+
If <em>ActiveRecord::Base.include_root_in_json</em> is true then, the model name is used instead of data key:
|
24
|
+
{
|
25
|
+
"post": {
|
26
|
+
"id": 1,
|
27
|
+
"title": "First Post",
|
28
|
+
"body": "This is my first post.",
|
29
|
+
"published": true, ...
|
30
|
+
},
|
31
|
+
"success": true
|
32
|
+
}
|
33
|
+
|
34
|
+
So that it is as easy as do:
|
35
|
+
|
36
|
+
@post.to_extjs
|
37
|
+
|
38
|
+
In case the model has some failures, the sentence above will return:
|
39
|
+
{
|
40
|
+
"errors": { "title": ["Title can't be blank"] },
|
41
|
+
"success": false
|
42
|
+
}
|
43
|
+
|
44
|
+
Parameters accepted are the same as <b>as_json</b> from ActiveModel.
|
45
|
+
|
46
|
+
== Array
|
47
|
+
|
48
|
+
When to_extjs is called from ActiveRecord (or hashes) collections, Ext JS expect to receive a structure like that:
|
49
|
+
|
50
|
+
{ 'total' : 2,
|
51
|
+
'data' : [
|
52
|
+
{ 'id' : 1, :title : 'First Post' },
|
53
|
+
{ 'id' : 2, :title : 'Second Post' }
|
54
|
+
]
|
55
|
+
}
|
56
|
+
|
57
|
+
It is generated executing
|
58
|
+
Post.all.to_extjs
|
59
|
+
|
60
|
+
== TODO
|
61
|
+
|
62
|
+
* Tests, tests, tests....
|
data/Rakefile
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "extjsizable"
|
18
|
+
gem.homepage = "http://github.com/ungue/extjsizable"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Allow your models and collections to generate the JSON structure accepted by Ext JS 4}
|
21
|
+
gem.description = %Q{You can create REST services to be used for Ext JS 4 in a easy manner by calling to_extjs in your models or arrays.}
|
22
|
+
gem.email = "ungue79@yahoo.es"
|
23
|
+
gem.authors = ["Ungue"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
require 'rake/rdoctask'
|
42
|
+
Rake::RDocTask.new do |rdoc|
|
43
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
44
|
+
|
45
|
+
rdoc.rdoc_dir = 'rdoc'
|
46
|
+
rdoc.title = "extjsizable #{version}"
|
47
|
+
rdoc.rdoc_files.include('README*')
|
48
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
49
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0.alpha
|
data/extjsizable.gemspec
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{extjsizable}
|
8
|
+
s.version = "1.0.0.alpha"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Ungue"]
|
12
|
+
s.date = %q{2011-08-03}
|
13
|
+
s.description = %q{You can create REST services to be used for Ext JS 4 in a easy manner by calling to_extjs in your models or arrays.}
|
14
|
+
s.email = %q{ungue79@yahoo.es}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
"Gemfile",
|
22
|
+
"Gemfile.lock",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"extjsizable.gemspec",
|
28
|
+
"lib/extjsizable.rb",
|
29
|
+
"lib/extjsizable/active_record/extjs.rb",
|
30
|
+
"lib/extjsizable/core_ext/array/extjs.rb",
|
31
|
+
"spec/extjsizable_spec.rb",
|
32
|
+
"spec/spec_helper.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/ungue/extjsizable}
|
35
|
+
s.licenses = ["MIT"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.6.2}
|
38
|
+
s.summary = %q{Allow your models and collections to generate the JSON structure accepted by Ext JS 4}
|
39
|
+
|
40
|
+
if s.respond_to? :specification_version then
|
41
|
+
s.specification_version = 3
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
44
|
+
s.add_runtime_dependency(%q<activesupport>, [">= 3.0"])
|
45
|
+
s.add_runtime_dependency(%q<activerecord>, [">= 3.0"])
|
46
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
|
47
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
48
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.3"])
|
49
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<activesupport>, [">= 3.0"])
|
52
|
+
s.add_dependency(%q<activerecord>, [">= 3.0"])
|
53
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
54
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
55
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.3"])
|
56
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
57
|
+
end
|
58
|
+
else
|
59
|
+
s.add_dependency(%q<activesupport>, [">= 3.0"])
|
60
|
+
s.add_dependency(%q<activerecord>, [">= 3.0"])
|
61
|
+
s.add_dependency(%q<rspec>, ["~> 2.3.0"])
|
62
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
63
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.3"])
|
64
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module Extjsizable
|
2
|
+
module ActiveRecord
|
3
|
+
module ExtJs
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module InstanceMethods
|
7
|
+
|
8
|
+
def to_extjs(options = {})
|
9
|
+
success = options.delete(:success)
|
10
|
+
|
11
|
+
if success || (success.nil? && valid?)
|
12
|
+
# returns success/data to load a form:
|
13
|
+
# {
|
14
|
+
# "data": {
|
15
|
+
# "id": 1,
|
16
|
+
# "title": "First Post",
|
17
|
+
# "body": "This is my first post.",
|
18
|
+
# "published": true, ...
|
19
|
+
# },
|
20
|
+
# "success": true
|
21
|
+
# }
|
22
|
+
#
|
23
|
+
# If ActiveRecord::Base.include_root_in_json is true then, the model name is used instead of data key:
|
24
|
+
# {
|
25
|
+
# "post": {
|
26
|
+
# "id": 1,
|
27
|
+
# "title": "First Post",
|
28
|
+
# "body": "This is my first post.",
|
29
|
+
# "published": true, ...
|
30
|
+
# },
|
31
|
+
# "success": true
|
32
|
+
# }
|
33
|
+
|
34
|
+
h_json_data = ::ActiveRecord::Base.include_root_in_json? ? as_json(options) : { :data => as_json(options) }
|
35
|
+
{ :success => true }.merge(h_json_data)
|
36
|
+
else
|
37
|
+
# retrieves no-success/errors to the form:
|
38
|
+
# {
|
39
|
+
# "errors": { "title": "Title can't be blank", ... },
|
40
|
+
# "success": false
|
41
|
+
# }
|
42
|
+
{ :success => false, :errors => errors.as_json(options).with_indifferent_access }
|
43
|
+
end.with_indifferent_access
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
@@ -0,0 +1,78 @@
|
|
1
|
+
module Extjsizable
|
2
|
+
module CoreExt
|
3
|
+
module Array
|
4
|
+
module ExtJs
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
module InstanceMethods
|
8
|
+
|
9
|
+
# Creates a JSON object by specifying which attributes we want to be shown.
|
10
|
+
# Ej:
|
11
|
+
# { 'total' : 2,
|
12
|
+
# 'data' : [
|
13
|
+
# { 'id' : 1, :nombre : 'Juan' },
|
14
|
+
# { 'id' : 2, :nombre : 'Pedro' }
|
15
|
+
# ]
|
16
|
+
# }
|
17
|
+
|
18
|
+
def to_extjs(options = {})
|
19
|
+
options.reverse_merge!(
|
20
|
+
:methods => [],
|
21
|
+
:total => self.length,
|
22
|
+
:only => [],
|
23
|
+
:except => [],
|
24
|
+
:include => {}
|
25
|
+
)
|
26
|
+
|
27
|
+
result = self.map do |r|
|
28
|
+
case r
|
29
|
+
when Hash
|
30
|
+
record_from_hash(r, options)
|
31
|
+
when ::ActiveRecord::Base
|
32
|
+
record_from_model(r, options)
|
33
|
+
else
|
34
|
+
r
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
{ :total => options[:total], :data => result }
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
|
44
|
+
def record_from_model(reg, options)
|
45
|
+
options.reverse_merge!(
|
46
|
+
:methods => [],
|
47
|
+
:only => [],
|
48
|
+
:except => [],
|
49
|
+
:include => {}
|
50
|
+
)
|
51
|
+
|
52
|
+
data = []
|
53
|
+
|
54
|
+
attrs = options[:only].empty? ? reg.attributes.symbolize_keys.keys - options[:except] : options[:only]
|
55
|
+
attrs.each do |attr|
|
56
|
+
data << ["#{attr}", reg.send(attr)] if reg.respond_to? attr
|
57
|
+
end
|
58
|
+
|
59
|
+
options[:methods].each do |method|
|
60
|
+
data << ["#{method}", reg.send(method)] if reg.respond_to? method
|
61
|
+
end
|
62
|
+
|
63
|
+
options[:include].each do |(k, v)|
|
64
|
+
data.concat record_from_model(reg.send(k), v).map { |l| ["#{k}_#{l.first}", l.last] } if reg.respond_to?(k) && reg.send(k)
|
65
|
+
end
|
66
|
+
|
67
|
+
Hash[*data.flatten(1)]
|
68
|
+
end
|
69
|
+
|
70
|
+
def record_from_hash(reg, options)
|
71
|
+
attrs = options[:only] || reg.keys - options[:except]
|
72
|
+
reg.delete_if { |k,v| attrs.member? k}
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/lib/extjsizable.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
require 'active_support/concern'
|
4
|
+
require 'extjsizable/active_record/extjs'
|
5
|
+
require 'extjsizable/core_ext/array/extjs'
|
6
|
+
|
7
|
+
ActiveRecord::Base.send :include, Extjsizable::ActiveRecord::ExtJs
|
8
|
+
Array.send :include, Extjsizable::CoreExt::Array::ExtJs
|
@@ -0,0 +1,123 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Extjsizable" do
|
4
|
+
|
5
|
+
describe Category do
|
6
|
+
|
7
|
+
describe "valid" do
|
8
|
+
before do
|
9
|
+
@category = Category.new :name => 'The category'
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should return success : true and a data section when not ActiveRecord::Base.include_root_in_json" do
|
13
|
+
ActiveRecord::Base.include_root_in_json = false
|
14
|
+
@category.should be_valid
|
15
|
+
|
16
|
+
json_hash = @category.to_extjs
|
17
|
+
|
18
|
+
json_hash.should_not be_empty
|
19
|
+
json_hash.should have_key(:success)
|
20
|
+
json_hash[:success].should be_true
|
21
|
+
|
22
|
+
json_hash.should have_key(:data)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should return success : true and a category section when ActiveRecord::Base.include_root_in_json" do
|
26
|
+
ActiveRecord::Base.include_root_in_json = true
|
27
|
+
@category.should be_valid
|
28
|
+
|
29
|
+
json_hash = @category.to_extjs
|
30
|
+
|
31
|
+
json_hash.should_not be_empty
|
32
|
+
json_hash.should have_key(:success)
|
33
|
+
json_hash[:success].should be_true
|
34
|
+
json_hash.should have_key(:category)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should return the attribute name of category" do
|
38
|
+
json_hash = @category.to_extjs
|
39
|
+
json_hash[:category].should have_key(:name)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should return all atributes and the result of the methods specified when called with :methods => [...]" do
|
43
|
+
json_hash = @category.to_extjs(:methods => :my_method)
|
44
|
+
json_hash[:category].should have_key(:name)
|
45
|
+
json_hash[:category].should have_key(:my_method)
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should return success : false and an empty errors section when called with :success => false" do
|
49
|
+
json_hash = @category.to_extjs(:success => false)
|
50
|
+
|
51
|
+
json_hash.should have_key(:success)
|
52
|
+
json_hash[:success].should be_false
|
53
|
+
|
54
|
+
json_hash.should_not have_key(:data)
|
55
|
+
json_hash.should_not have_key(:category)
|
56
|
+
|
57
|
+
json_hash.should have_key(:errors)
|
58
|
+
json_hash[:errors].should be_empty
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "not valid" do
|
63
|
+
before do
|
64
|
+
@category = Category.new
|
65
|
+
@category.save
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should return success : false and an errors section" do
|
69
|
+
@category.should_not be_valid
|
70
|
+
|
71
|
+
json_hash = @category.to_extjs
|
72
|
+
|
73
|
+
json_hash.should_not be_empty
|
74
|
+
json_hash.should have_key(:success)
|
75
|
+
json_hash[:success].should be_false
|
76
|
+
|
77
|
+
json_hash.should have_key(:errors)
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should return the failed attribute name" do
|
81
|
+
json_hash = @category.to_extjs
|
82
|
+
json_hash[:errors].should have_key(:name)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
describe Array do
|
88
|
+
describe 'empty' do
|
89
|
+
before do
|
90
|
+
@array = []
|
91
|
+
end
|
92
|
+
|
93
|
+
it 'should return { total => 0, data => [] }' do
|
94
|
+
json_hash = @array.to_extjs
|
95
|
+
json_hash.should have_key(:total)
|
96
|
+
json_hash[:total].should == 0
|
97
|
+
json_hash.should have_key(:data)
|
98
|
+
json_hash[:data].should be_empty
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe 'with 4 categories' do
|
103
|
+
before do
|
104
|
+
@array = Array.new(4) { |i| Category.create :name => "Category #{i}" }
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'should return { :total => 4, :data => [{ "id" => ..., "name" => "Category ..."}, ...] }' do
|
108
|
+
json_hash = @array.to_extjs
|
109
|
+
json_hash.should have_key(:total)
|
110
|
+
json_hash[:total].should == 4
|
111
|
+
|
112
|
+
json_hash.should have_key(:data)
|
113
|
+
json_hash[:data].should have(4).categories
|
114
|
+
json_hash[:data].each do |h|
|
115
|
+
h.should have_key('id')
|
116
|
+
h.should have_key('name')
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
|
4
|
+
require 'active_record'
|
5
|
+
require 'active_support'
|
6
|
+
require 'rspec'
|
7
|
+
require 'extjsizable'
|
8
|
+
|
9
|
+
# Requires supporting files with custom matchers and macros, etc,
|
10
|
+
# in ./support/ and its subdirectories.
|
11
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
12
|
+
|
13
|
+
ActiveRecord::Base.establish_connection(
|
14
|
+
:adapter => 'sqlite3',
|
15
|
+
:database => ":memory:"
|
16
|
+
)
|
17
|
+
|
18
|
+
ActiveRecord::Schema.define(:version => 1) do
|
19
|
+
create_table "products", :force => true do |t|
|
20
|
+
t.integer "category_id"
|
21
|
+
t.string "name"
|
22
|
+
end
|
23
|
+
|
24
|
+
create_table "categories", :force => true do |t|
|
25
|
+
t.string "name"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Testing models
|
30
|
+
class Category < ActiveRecord::Base
|
31
|
+
has_many :products
|
32
|
+
|
33
|
+
validates_presence_of :name
|
34
|
+
|
35
|
+
def my_method
|
36
|
+
return 'the result of my method'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class Product < ActiveRecord::Base
|
41
|
+
belongs_to :category
|
42
|
+
|
43
|
+
validates_presence_of :name
|
44
|
+
end
|
45
|
+
|
46
|
+
RSpec.configure do |config|
|
47
|
+
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: extjsizable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: 6
|
5
|
+
version: 1.0.0.alpha
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ungue
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-08-03 00:00:00 +02:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: activesupport
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "3.0"
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ">="
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: "3.0"
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rspec
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.3.0
|
46
|
+
type: :development
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: bundler
|
51
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ~>
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 1.0.0
|
57
|
+
type: :development
|
58
|
+
prerelease: false
|
59
|
+
version_requirements: *id004
|
60
|
+
- !ruby/object:Gem::Dependency
|
61
|
+
name: jeweler
|
62
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ~>
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 1.6.3
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: *id005
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: rcov
|
73
|
+
requirement: &id006 !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
version: "0"
|
79
|
+
type: :development
|
80
|
+
prerelease: false
|
81
|
+
version_requirements: *id006
|
82
|
+
description: You can create REST services to be used for Ext JS 4 in a easy manner by calling to_extjs in your models or arrays.
|
83
|
+
email: ungue79@yahoo.es
|
84
|
+
executables: []
|
85
|
+
|
86
|
+
extensions: []
|
87
|
+
|
88
|
+
extra_rdoc_files:
|
89
|
+
- LICENSE.txt
|
90
|
+
- README.rdoc
|
91
|
+
files:
|
92
|
+
- .document
|
93
|
+
- Gemfile
|
94
|
+
- Gemfile.lock
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.rdoc
|
97
|
+
- Rakefile
|
98
|
+
- VERSION
|
99
|
+
- extjsizable.gemspec
|
100
|
+
- lib/extjsizable.rb
|
101
|
+
- lib/extjsizable/active_record/extjs.rb
|
102
|
+
- lib/extjsizable/core_ext/array/extjs.rb
|
103
|
+
- spec/extjsizable_spec.rb
|
104
|
+
- spec/spec_helper.rb
|
105
|
+
has_rdoc: true
|
106
|
+
homepage: http://github.com/ungue/extjsizable
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
hash: -676178687
|
120
|
+
segments:
|
121
|
+
- 0
|
122
|
+
version: "0"
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
|
+
none: false
|
125
|
+
requirements:
|
126
|
+
- - ">"
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: 1.3.1
|
129
|
+
requirements: []
|
130
|
+
|
131
|
+
rubyforge_project:
|
132
|
+
rubygems_version: 1.6.2
|
133
|
+
signing_key:
|
134
|
+
specification_version: 3
|
135
|
+
summary: Allow your models and collections to generate the JSON structure accepted by Ext JS 4
|
136
|
+
test_files: []
|
137
|
+
|