extjsizable 1.0.0.alpha1 → 1.0.1.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
@@ -1,7 +1,8 @@
1
1
  = extjsizable
2
2
 
3
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/).
4
+ Messages generated are valid to be used against <b>Ext JS 4</b> (http://www.sencha.com/products/extjs/).
5
+ Now it's possible to generate <b>Ext JS 3</b> JSON data although it's in experimental state.
5
6
  It requires Rails 3.0 or latter to work properly.
6
7
 
7
8
  == ActiveRecord
@@ -18,7 +19,17 @@ To load a form, the data extjs expect to meet is like so:
18
19
  "success": true
19
20
  }
20
21
 
21
- <tt>* Previous versions of extjs needed attributes wrapped with the model name. Now this gem doesn't allow this.</tt>
22
+ If <em>ActiveRecord::Base.wrap_with_brackets</em> is true then, the model name wraps all attributes:
23
+ {
24
+ "data": {
25
+ "post[id]": 1,
26
+ "post[title]": "First Post",
27
+ "post[body]": "This is my first post.",
28
+ "post[published]": true, ...
29
+ },
30
+ "success": true
31
+ }
32
+ It allows to use this gem with Ext JS 3.
22
33
 
23
34
  If <em>ActiveRecord::Base.include_root_in_json</em> is true then, the model name is used instead of data key:
24
35
  {
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0.alpha1
1
+ 1.0.1.alpha1
data/extjsizable.gemspec CHANGED
@@ -4,14 +4,14 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{extjsizable}
8
- s.version = "1.0.0.alpha1"
7
+ s.name = "extjsizable"
8
+ s.version = "1.0.1.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-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
- s.email = %q{ungue79@yahoo.es}
12
+ s.date = "2011-10-18"
13
+ s.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."
14
+ s.email = "ungue79@yahoo.es"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
17
17
  "README.rdoc"
@@ -31,11 +31,11 @@ Gem::Specification.new do |s|
31
31
  "spec/extjsizable_spec.rb",
32
32
  "spec/spec_helper.rb"
33
33
  ]
34
- s.homepage = %q{http://github.com/ungue/extjsizable}
34
+ s.homepage = "http://github.com/ungue/extjsizable"
35
35
  s.licenses = ["MIT"]
36
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}
37
+ s.rubygems_version = "1.8.10"
38
+ s.summary = "Allow your models and collections to generate the JSON structure accepted by Ext JS 4"
39
39
 
40
40
  if s.respond_to? :specification_version then
41
41
  s.specification_version = 3
@@ -3,10 +3,17 @@ module Extjsizable
3
3
  module ExtJs
4
4
  extend ActiveSupport::Concern
5
5
 
6
+ included do
7
+ class_attribute :wrap_with_brackets
8
+
9
+ self.wrap_with_brackets = false
10
+ end
11
+
6
12
  module InstanceMethods
7
13
 
8
14
  def to_extjs(options = {})
9
15
  success = options.delete(:success)
16
+ underscored_class_name = self.class.to_s.demodulize.underscore
10
17
 
11
18
  if success || (success.nil? && valid?)
12
19
  # returns success/data to load a form:
@@ -30,8 +37,21 @@ module Extjsizable
30
37
  # },
31
38
  # "success": true
32
39
  # }
33
-
34
- h_json_data = ::ActiveRecord::Base.include_root_in_json? ? as_json(options) : { :data => as_json(options) }
40
+ # If ActiveRecord::Base.wrap_with_brackets is true then, the model name is prefixed into data key and all keys are surrounded with brackets:
41
+ # {
42
+ # "data": {
43
+ # "post[id]": 1,
44
+ # "post[title]": "First Post",
45
+ # "post[body]": "This is my first post.",
46
+ # "post[published]": true, ...
47
+ # },
48
+ # "success": true
49
+ # }
50
+
51
+ h_json_data = as_json(options)
52
+ h_json_data = { :data => h_json_data } unless ::ActiveRecord::Base.include_root_in_json?
53
+ h_json_data[h_json_data.keys.first] = wrap_hash_with_brackets(h_json_data.values.first, underscored_class_name) if ::ActiveRecord::Base.wrap_with_brackets?
54
+
35
55
  { :success => true }.merge(h_json_data)
36
56
  else
37
57
  # retrieves no-success/errors to the form:
@@ -39,11 +59,25 @@ module Extjsizable
39
59
  # "errors": { "title": "Title can't be blank", ... },
40
60
  # "success": false
41
61
  # }
42
- { :success => false, :errors => errors.as_json(options).with_indifferent_access }
62
+
63
+ h_json_data = errors.as_json(options)
64
+ h_json_data = wrap_hash_with_brackets(h_json_data, underscored_class_name) if ::ActiveRecord::Base.wrap_with_brackets?
65
+ { :success => false, :errors => h_json_data.with_indifferent_access }
43
66
  end.with_indifferent_access
44
67
 
45
68
  end
46
69
  end
70
+
71
+ private
72
+
73
+ # Wrap with brackets so that {:a => {:b => :c} } becomes to { 'model[a][b]' => :c }
74
+ def wrap_hash_with_brackets(h, bracket_key = '')
75
+ return { bracket_key => h } unless h.is_a?(Hash)
76
+
77
+ h.reduce({}) do |nh, (k, v)|
78
+ nh.merge(wrap_hash_with_brackets(v, bracket_key + "[#{k.to_s}]"))
79
+ end
80
+ end
47
81
  end
48
82
  end
49
83
  end
@@ -33,8 +33,17 @@ describe "Extjsizable" do
33
33
  json_hash[:success].should be_true
34
34
  json_hash.should have_key(:category)
35
35
  end
36
+
37
+ it "should return a category section with category[name] key when ActiveRecord::Base.wrap_with_brackets?" do
38
+ ActiveRecord::Base.wrap_with_brackets = true
39
+
40
+ json_hash = @category.to_extjs
41
+ json_hash[:category].should have_key('category[name]')
42
+ end
43
+
44
+ it "should return the attribute name of category when not ActiveRecord::Base.wrap_with_brackets?" do
45
+ ActiveRecord::Base.wrap_with_brackets = false
36
46
 
37
- it "should return the attribute name of category" do
38
47
  json_hash = @category.to_extjs
39
48
  json_hash[:category].should have_key(:name)
40
49
  end
metadata CHANGED
@@ -1,94 +1,91 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: extjsizable
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1.alpha1
4
5
  prerelease: 6
5
- version: 1.0.0.alpha1
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Ungue
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-08-04 00:00:00 +02:00
14
- default_executable:
15
- dependencies:
16
- - !ruby/object:Gem::Dependency
12
+ date: 2011-10-18 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
17
15
  name: activesupport
18
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &25676860 !ruby/object:Gem::Requirement
19
17
  none: false
20
- requirements:
21
- - - ">="
22
- - !ruby/object:Gem::Version
23
- version: "3.0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
24
22
  type: :runtime
25
23
  prerelease: false
26
- version_requirements: *id001
27
- - !ruby/object:Gem::Dependency
24
+ version_requirements: *25676860
25
+ - !ruby/object:Gem::Dependency
28
26
  name: activerecord
29
- requirement: &id002 !ruby/object:Gem::Requirement
27
+ requirement: &25676260 !ruby/object:Gem::Requirement
30
28
  none: false
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
34
- version: "3.0"
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
35
33
  type: :runtime
36
34
  prerelease: false
37
- version_requirements: *id002
38
- - !ruby/object:Gem::Dependency
35
+ version_requirements: *25676260
36
+ - !ruby/object:Gem::Dependency
39
37
  name: rspec
40
- requirement: &id003 !ruby/object:Gem::Requirement
38
+ requirement: &25675700 !ruby/object:Gem::Requirement
41
39
  none: false
42
- requirements:
40
+ requirements:
43
41
  - - ~>
44
- - !ruby/object:Gem::Version
42
+ - !ruby/object:Gem::Version
45
43
  version: 2.3.0
46
44
  type: :development
47
45
  prerelease: false
48
- version_requirements: *id003
49
- - !ruby/object:Gem::Dependency
46
+ version_requirements: *25675700
47
+ - !ruby/object:Gem::Dependency
50
48
  name: bundler
51
- requirement: &id004 !ruby/object:Gem::Requirement
49
+ requirement: &25675180 !ruby/object:Gem::Requirement
52
50
  none: false
53
- requirements:
51
+ requirements:
54
52
  - - ~>
55
- - !ruby/object:Gem::Version
53
+ - !ruby/object:Gem::Version
56
54
  version: 1.0.0
57
55
  type: :development
58
56
  prerelease: false
59
- version_requirements: *id004
60
- - !ruby/object:Gem::Dependency
57
+ version_requirements: *25675180
58
+ - !ruby/object:Gem::Dependency
61
59
  name: jeweler
62
- requirement: &id005 !ruby/object:Gem::Requirement
60
+ requirement: &25674620 !ruby/object:Gem::Requirement
63
61
  none: false
64
- requirements:
62
+ requirements:
65
63
  - - ~>
66
- - !ruby/object:Gem::Version
64
+ - !ruby/object:Gem::Version
67
65
  version: 1.6.3
68
66
  type: :development
69
67
  prerelease: false
70
- version_requirements: *id005
71
- - !ruby/object:Gem::Dependency
68
+ version_requirements: *25674620
69
+ - !ruby/object:Gem::Dependency
72
70
  name: rcov
73
- requirement: &id006 !ruby/object:Gem::Requirement
71
+ requirement: &25674120 !ruby/object:Gem::Requirement
74
72
  none: false
75
- requirements:
76
- - - ">="
77
- - !ruby/object:Gem::Version
78
- version: "0"
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
79
77
  type: :development
80
78
  prerelease: false
81
- version_requirements: *id006
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.
79
+ version_requirements: *25674120
80
+ description: You can create REST services to be used for Ext JS 4 in an easy manner
81
+ by calling to_extjs in your models or arrays.
83
82
  email: ungue79@yahoo.es
84
83
  executables: []
85
-
86
84
  extensions: []
87
-
88
- extra_rdoc_files:
85
+ extra_rdoc_files:
89
86
  - LICENSE.txt
90
87
  - README.rdoc
91
- files:
88
+ files:
92
89
  - .document
93
90
  - Gemfile
94
91
  - Gemfile.lock
@@ -102,36 +99,33 @@ files:
102
99
  - lib/extjsizable/core_ext/array/extjs.rb
103
100
  - spec/extjsizable_spec.rb
104
101
  - spec/spec_helper.rb
105
- has_rdoc: true
106
102
  homepage: http://github.com/ungue/extjsizable
107
- licenses:
103
+ licenses:
108
104
  - MIT
109
105
  post_install_message:
110
106
  rdoc_options: []
111
-
112
- require_paths:
107
+ require_paths:
113
108
  - lib
114
- required_ruby_version: !ruby/object:Gem::Requirement
109
+ required_ruby_version: !ruby/object:Gem::Requirement
115
110
  none: false
116
- requirements:
117
- - - ">="
118
- - !ruby/object:Gem::Version
119
- hash: -759270289
120
- segments:
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ segments:
121
116
  - 0
122
- version: "0"
123
- required_rubygems_version: !ruby/object:Gem::Requirement
117
+ hash: -4318746047491848362
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
119
  none: false
125
- requirements:
126
- - - ">"
127
- - !ruby/object:Gem::Version
120
+ requirements:
121
+ - - ! '>'
122
+ - !ruby/object:Gem::Version
128
123
  version: 1.3.1
129
124
  requirements: []
130
-
131
125
  rubyforge_project:
132
- rubygems_version: 1.6.2
126
+ rubygems_version: 1.8.10
133
127
  signing_key:
134
128
  specification_version: 3
135
- summary: Allow your models and collections to generate the JSON structure accepted by Ext JS 4
129
+ summary: Allow your models and collections to generate the JSON structure accepted
130
+ by Ext JS 4
136
131
  test_files: []
137
-