extjsizable 1.0.0.alpha → 1.0.0.alpha1

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/README.rdoc CHANGED
@@ -57,6 +57,8 @@ When to_extjs is called from ActiveRecord (or hashes) collections, Ext JS expect
57
57
  It is generated executing
58
58
  Post.all.to_extjs
59
59
 
60
+ Parameters accepted are the same as <b>as_json</b> from Array, nesting into each record.
61
+
60
62
  == TODO
61
63
 
62
64
  * Tests, tests, tests....
data/Rakefile CHANGED
@@ -18,7 +18,7 @@ Jeweler::Tasks.new do |gem|
18
18
  gem.homepage = "http://github.com/ungue/extjsizable"
19
19
  gem.license = "MIT"
20
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.}
21
+ gem.description = %Q{You can create REST services to be used for Ext JS 4 in an easy manner by calling to_extjs in your models or arrays.}
22
22
  gem.email = "ungue79@yahoo.es"
23
23
  gem.authors = ["Ungue"]
24
24
  # dependencies defined in Gemfile
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0.alpha
1
+ 1.0.0.alpha1
data/extjsizable.gemspec CHANGED
@@ -5,12 +5,12 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{extjsizable}
8
- s.version = "1.0.0.alpha"
8
+ s.version = "1.0.0.alpha1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
11
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.}
12
+ s.date = %q{2011-08-04}
13
+ s.description = %q{You can create REST services to be used for Ext JS 4 in an easy manner by calling to_extjs in your models or arrays.}
14
14
  s.email = %q{ungue79@yahoo.es}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
@@ -16,61 +16,9 @@ module Extjsizable
16
16
  # }
17
17
 
18
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 }
19
+ { :total => (options.delete(:total) || self.length), :data => as_json(options) }.with_indifferent_access
39
20
  end
40
21
 
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
22
  end
75
23
  end
76
24
  end
@@ -61,6 +61,7 @@ describe "Extjsizable" do
61
61
 
62
62
  describe "not valid" do
63
63
  before do
64
+ Category.delete_all
64
65
  @category = Category.new
65
66
  @category.save
66
67
  end
@@ -85,6 +86,8 @@ describe "Extjsizable" do
85
86
  end
86
87
 
87
88
  describe Array do
89
+ before(:all) { ActiveRecord::Base.include_root_in_json = false }
90
+
88
91
  describe 'empty' do
89
92
  before do
90
93
  @array = []
@@ -99,9 +102,18 @@ describe "Extjsizable" do
99
102
  end
100
103
  end
101
104
 
102
- describe 'with 4 categories' do
105
+ describe 'with 4 categories with some products each' do
103
106
  before do
104
- @array = Array.new(4) { |i| Category.create :name => "Category #{i}" }
107
+ Product.delete_all
108
+ Category.delete_all
109
+
110
+ 4.times do |i|
111
+ c = Category.new :name => "Category #{i}"
112
+ c.products = Array.new(2) { |j| Product.new :name => "Product #{j}" }
113
+ c.save
114
+ end
115
+
116
+ @array = Category.all
105
117
  end
106
118
 
107
119
  it 'should return { :total => 4, :data => [{ "id" => ..., "name" => "Category ..."}, ...] }' do
@@ -116,6 +128,48 @@ describe "Extjsizable" do
116
128
  h.should have_key('name')
117
129
  end
118
130
  end
131
+
132
+ it 'should return only id attributes when called with :only => :id ' do
133
+ json_hash = @array.to_extjs :only => :id
134
+ json_hash.should have_key(:total)
135
+ json_hash[:total].should == 4
136
+
137
+ json_hash.should have_key(:data)
138
+ json_hash[:data].should have(4).categories
139
+ json_hash[:data].each do |h|
140
+ h.should have_key('id')
141
+ h.should_not have_key('name')
142
+ end
143
+ end
144
+
145
+ it 'should return only name attributes when called with :except => :id ' do
146
+ json_hash = @array.to_extjs :except => :id
147
+ json_hash.should have_key(:total)
148
+ json_hash[:total].should == 4
149
+
150
+ json_hash.should have_key(:data)
151
+ json_hash[:data].should have(4).categories
152
+ json_hash[:data].each do |h|
153
+ h.should have_key('name')
154
+ h.should_not have_key('id')
155
+ end
156
+ end
157
+
158
+ it 'should return only related data attributes with :include => :products ' do
159
+ json_hash = @array.to_extjs :include => :products
160
+ json_hash.should have_key(:total)
161
+ json_hash[:total].should == 4
162
+
163
+ json_hash.should have_key(:data)
164
+ json_hash[:data].should have(4).categories
165
+ json_hash[:data].each do |h|
166
+ h.should have_key('name')
167
+ h.should have_key('id')
168
+ h.should have_key('products')
169
+ h[:products].should have(2).items
170
+ end
171
+ end
172
+
119
173
  end
120
174
  end
121
175
 
data/spec/spec_helper.rb CHANGED
@@ -44,5 +44,4 @@ class Product < ActiveRecord::Base
44
44
  end
45
45
 
46
46
  RSpec.configure do |config|
47
-
48
47
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: extjsizable
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease: 6
5
- version: 1.0.0.alpha
5
+ version: 1.0.0.alpha1
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ungue
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-08-03 00:00:00 +02:00
13
+ date: 2011-08-04 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -79,7 +79,7 @@ dependencies:
79
79
  type: :development
80
80
  prerelease: false
81
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.
82
+ description: You can create REST services to be used for Ext JS 4 in an easy manner by calling to_extjs in your models or arrays.
83
83
  email: ungue79@yahoo.es
84
84
  executables: []
85
85
 
@@ -116,7 +116,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
116
116
  requirements:
117
117
  - - ">="
118
118
  - !ruby/object:Gem::Version
119
- hash: -676178687
119
+ hash: -759270289
120
120
  segments:
121
121
  - 0
122
122
  version: "0"