extjs-mvc 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,5 +1,6 @@
1
1
  = mvc
2
2
 
3
+
3
4
  A collection of helpers, MVC mixins and PORs (plain-old-ruby-object) to assist with auto-generating ExtJS javascript component definitions.
4
5
 
5
6
 
@@ -23,6 +24,11 @@ fields with will be used to render the <tt>Ext.data.Record.create</tt> field-def
23
24
  include ExtJS::Model
24
25
 
25
26
  extjs_fields :exclude => [:password, :password_confirmation]
27
+ # OR
28
+ extjs_fields :name, :description
29
+ # OR
30
+ extjs_fields :name, :description, :parent => [:name, :date]
31
+ # this would configure associated columns with names like "parent__name" and "parent__date")
26
32
  end
27
33
 
28
34
  In script/console
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.5
1
+ 0.2.6
@@ -55,7 +55,7 @@ class ExtJS::Component
55
55
  end
56
56
  end
57
57
 
58
- def to_json(*args)
58
+ def to_json
59
59
  config.to_json
60
60
  end
61
61
 
@@ -57,7 +57,7 @@ module ExtJS::Data
57
57
  if @writer # <-- ugly hack because 3.0.1 can't deal with Writer as config-param
58
58
  json = @config.to_json
59
59
  json[json.length-1] = ','
60
- json += "writer:new Ext.data.#{@format.capitalize}Writer(#{@writer.to_json})}"
60
+ json += "\"writer\":new Ext.data.#{@format.capitalize}Writer(#{@writer.to_json})}"
61
61
  "<script>new #{@type}(#{json});#{script}</script>"
62
62
  else
63
63
  "<script>new #{@type}(#{@config.to_json});#{script}</script>"
data/lib/extjs-mvc.rb CHANGED
@@ -7,14 +7,15 @@ module ExtJS
7
7
  cattr_accessor :message_property
8
8
  cattr_accessor :root
9
9
 
10
+ require 'model/base'
10
11
 
11
12
  # Detect orm, include appropriate mixin.
12
13
  if defined?(ActiveRecord)
13
- require 'model/active_record/model'
14
+ require 'model/active_record'
14
15
  elsif defined?(DataMapper)
15
- require 'model/dm/model'
16
+ require 'model/data_mapper'
16
17
  elsif defined?(MongoMapper)
17
- require 'model/mongo_mapper/model'
18
+ require 'model/mongo_mapper'
18
19
  end
19
20
 
20
21
  # Rails-style Array#extract_options! used heavily
@@ -0,0 +1,44 @@
1
+ module ExtJS
2
+ module Model
3
+ module ClassMethods
4
+
5
+ def extjs_primary_key
6
+ self.primary_key
7
+ end
8
+
9
+ def extjs_column_names
10
+ self.column_names
11
+ end
12
+
13
+ def extjs_columns_hash
14
+ self.columns_hash
15
+ end
16
+
17
+ def extjs_render_column(col)
18
+ type = col.type
19
+ case type
20
+ when :datetime || :date || :time || :timestamp
21
+ type = :date
22
+ when :text
23
+ type = :string
24
+ when :integer
25
+ type = :int
26
+ end
27
+ {:name => col.name, :allowBlank => (col.primary) ? true : col.null, :type => type}
28
+ end
29
+
30
+ def extjs_associations
31
+ if @extjs_associations.nil?
32
+ @extjs_associations = {}
33
+ self.reflections.keys.each do |key|
34
+ assn = self.reflections[key]
35
+ type = (assn.macro === :has_many) ? :many : assn.macro
36
+ @extjs_associations[key.to_sym] = {:name => key, :type => type}
37
+ end
38
+ end
39
+ @extjs_associations
40
+ end
41
+ end
42
+ end
43
+ end
44
+
data/lib/model/base.rb ADDED
@@ -0,0 +1,118 @@
1
+ module ExtJS
2
+ module Model
3
+
4
+ def self.included(model)
5
+ model.send(:extend, ClassMethods)
6
+ model.send(:include, InstanceMethods)
7
+ model.class_eval do
8
+ cattr_accessor :extjs_record_fields
9
+ end
10
+ model.extjs_record_fields = []
11
+ end
12
+
13
+ ##
14
+ # InstanceMethods
15
+ #
16
+ module InstanceMethods
17
+
18
+ def to_record
19
+ pk = self.class.extjs_primary_key
20
+ assns = self.class.extjs_associations
21
+
22
+ data = {pk => self.send(pk)}
23
+ self.class.extjs_record_fields.each do |f|
24
+ if refl = assns[f]
25
+ if refl[:type] === :belongs_to
26
+ assn = self.send(f)
27
+ data[f] = (assn) ? assn.to_record : {} # <-- a thing was requested, give emtpy thing.
28
+ elsif refl[:type] === :many
29
+ data[f] = self.send(f).collect {|r| r.to_record} #CAREFUL!!!!!!!!!!!!1
30
+ end
31
+ elsif f.is_a? Array
32
+ value = self
33
+ f.each do |method|
34
+ value = value.send(method)
35
+ break if value.nil?
36
+ end
37
+ data[f.join('__')] = value
38
+ else
39
+ data[f] = self.send(f)
40
+ end
41
+ end
42
+ data
43
+ end
44
+ end
45
+
46
+ ##
47
+ # ClassMethods
48
+ #
49
+ module ClassMethods
50
+ ##
51
+ # render AR columns to Ext.data.Record.create format
52
+ # eg: {name:'foo', type: 'string'}
53
+ #
54
+ def extjs_record
55
+
56
+ if self.extjs_record_fields.empty?
57
+ self.extjs_record_fields = self.extjs_column_names
58
+ end
59
+
60
+ pk = self.extjs_primary_key
61
+ columns = self.extjs_columns_hash
62
+ associations = self.extjs_associations
63
+
64
+ return {
65
+ "fields" => self.extjs_record_fields.collect {|f|
66
+ if columns[f.to_sym] || columns[f.to_s]
67
+ field = self.extjs_render_column(columns[f.to_sym] || columns[f.to_s])
68
+ field[:dateFormat] = "c" if field[:type] === :date # <-- ugly hack for date
69
+ field
70
+ elsif assn = associations[f]
71
+ field = {:name => f, :allowBlank => true, :type => 'auto'}
72
+ elsif f.is_a? Array
73
+ field = {:name => f.join('__'), :type => 'auto'}
74
+ else # property is a method?
75
+ field = {:name => f, :allowBlank => true, :type => 'auto'}
76
+ end
77
+ },
78
+ "idProperty" => pk
79
+ }
80
+ end
81
+
82
+ def extjs_fields(*params)
83
+ options = params.extract_options!
84
+ if !options.keys.empty?
85
+ if excludes = options.delete(:exclude)
86
+ self.extjs_record_fields = self.extjs_column_names.reject {|c| excludes.find {|ex| c === ex.to_s}}.collect {|c| c}
87
+ elsif only = options.delete(:only)
88
+ self.extjs_record_fields = only
89
+ end
90
+ self.extjs_record_fields.concat(process_association_fields(options))
91
+ elsif params.empty?
92
+ return self.extjs_record_fields
93
+ end
94
+
95
+ self.extjs_record_fields.concat(params) if !params.empty?
96
+
97
+ #Append primary key if it's not included
98
+ # I don't think we want to automatically include :id
99
+ # Chris
100
+ #self.extjs_record_fields << self.primary_key.to_sym if !self.extjs_record_fields.include?(self.primary_key.to_sym)
101
+ end
102
+
103
+ ##
104
+ # Prepare the config for fields with '.' in their names
105
+ #
106
+ def process_association_fields(options)
107
+ results = []
108
+ options.each do |assoc, fields|
109
+ fields.each do |field|
110
+ results << [assoc, field]
111
+ end
112
+ end
113
+ results
114
+ end
115
+ end
116
+ end
117
+ end
118
+
@@ -0,0 +1,55 @@
1
+ module ExtJS
2
+ module Model
3
+ module ClassMethods
4
+
5
+ def extjs_primary_key
6
+ self.key.first.name
7
+ end
8
+
9
+ def extjs_column_names
10
+ self.properties.collect {|p| p.name.to_s }
11
+ end
12
+
13
+ def extjs_columns_hash
14
+ if @extjs_columns_hash.nil?
15
+ @extjs_columns_hash = {}
16
+ self.properties.each do |p|
17
+ @extjs_columns_hash[p.name] = p
18
+ end
19
+ end
20
+ @extjs_columns_hash
21
+ end
22
+
23
+ def extjs_render_column(col)
24
+ pk = self.key.first
25
+ type = ((col.type.respond_to?(:primitive)) ? col.type.primitive : col.type).to_s
26
+ case type
27
+ when "DateTime", "Date", "Time"
28
+ type = :date
29
+ when "String"
30
+ type = :string
31
+ when "Float"
32
+ type = :float
33
+ when "Integer", "BigDecimal"
34
+ type = :int
35
+ else
36
+ type = "auto"
37
+ end
38
+ {:name => col.name.to_s, :type => type, :allowBlank => (col === pk) ? true : col.nullable? }
39
+ end
40
+
41
+ def extjs_associations
42
+ if @extjs_associations.nil?
43
+ @extjs_associations = {}
44
+ self.relationships.keys.each do |key|
45
+ assn = self.relationships[key]
46
+ type = (assn.options[:max].nil? && assn.options[:min].nil?) ? :belongs_to : (assn.options[:max] > 1) ? :many : nil
47
+ @extjs_associations[key.to_sym] = {:name => key, :type => type}
48
+ end
49
+ end
50
+ @extjs_associations
51
+ end
52
+ end
53
+ end
54
+ end
55
+
@@ -0,0 +1,49 @@
1
+ module ExtJS
2
+ module Model
3
+ ##
4
+ # ClassMethods
5
+ #
6
+ module ClassMethods
7
+
8
+ def extjs_primary_key
9
+ "id"
10
+ end
11
+
12
+ def extjs_column_names
13
+ self.column_names
14
+ end
15
+
16
+ def extjs_columns_hash
17
+ self.keys
18
+ end
19
+
20
+ def extjs_associations
21
+ if @extjs_associations.nil?
22
+ @extjs_associations = {}
23
+ self.associations.keys.each do |key|
24
+ @extjs_associations[key.to_sym] = {:name => key, :type => self.associations[key].type}
25
+ end
26
+ end
27
+ @extjs_associations
28
+ end
29
+
30
+ def extjs_render_column(col)
31
+ type = col.type.to_s
32
+ case type
33
+ when "DateTime", "Date", "Time"
34
+ type = :date
35
+ when "String"
36
+ type = :string
37
+ when "Float"
38
+ type = :float
39
+ when "Integer", "BigDecimal"
40
+ type = :int
41
+ else
42
+ type = "auto"
43
+ end
44
+ {:name => col.name, :type => type, :allowBlank => (col.name === '_id') ? true : (col.options[:required] === true) ? false : true }
45
+ end
46
+ end
47
+ end
48
+ end
49
+
data/test/model_test.rb CHANGED
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+
3
+ class TestParent < ActiveRecord::Base
4
+
5
+ end
6
+
7
+ class TestModel < ActiveRecord::Base
8
+ include ExtJS::Model
9
+ belongs_to :test_parent
10
+
11
+ #for the EXT Store configuration
12
+ extjs_fields :name, :test_parent => [:name]
13
+ end
14
+
15
+ class ModelTest < Test::Unit::TestCase
16
+ def test_field_list_for_associations
17
+ assert_equal [[:test_parent, :name], :name, :id], TestModel.extjs_fields
18
+ end
19
+ end
20
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: extjs-mvc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Scott
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-25 00:00:00 -04:00
12
+ date: 2009-11-17 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -45,9 +45,10 @@ files:
45
45
  - lib/extjs/data/store.rb
46
46
  - lib/helpers/component.rb
47
47
  - lib/helpers/store.rb
48
- - lib/model/active_record/model.rb
49
- - lib/model/dm/model.rb
50
- - lib/model/mongo_mapper/model.rb
48
+ - lib/model/active_record.rb
49
+ - lib/model/base.rb
50
+ - lib/model/data_mapper.rb
51
+ - lib/model/mongo_mapper.rb
51
52
  - test/component_test.rb
52
53
  - test/controller_test.rb
53
54
  - test/model_test.rb
@@ -1,90 +0,0 @@
1
- module ExtJS
2
- module Model
3
- def self.included(model)
4
- model.send(:extend, ClassMethods)
5
- model.send(:include, InstanceMethods)
6
- model.class_eval do
7
- cattr_accessor :extjs_record_fields
8
- end
9
- model.extjs_record_fields = []
10
- end
11
-
12
- ##
13
- # InstanceMethods
14
- #
15
- module InstanceMethods
16
- def to_record
17
- data = {self.class.primary_key => self.send(self.class.primary_key)}
18
- self.class.extjs_record_fields.each do |f|
19
- if refl = self.class.reflections[f]
20
- if refl.macro === :belongs_to
21
- assn = self.send(f)
22
- data[f] = (assn) ? assn.to_record : {} # <-- a thing was requested, give emtpy thing.
23
- elsif refl.macro === :has_many
24
- #data[f] = self.send(f).collect {|r| r.to_record} CAREFUL!!!!!!!!!!!!1
25
- end
26
- else
27
- data[f] = self.send(f)
28
- end
29
- end
30
- data
31
- end
32
- end
33
- ##
34
- # ClassMethods
35
- #
36
- module ClassMethods
37
- ##
38
- # Defines the subset of AR columns used to create Ext.data.Record def'n.
39
- # @param {Array/Hash} list-of-fields to include, :only, or :exclude
40
- #
41
- def extjs_fields(*params)
42
- options = params.extract_options!
43
- if !options.keys.empty?
44
- if options[:exclude]
45
- self.extjs_record_fields = self.columns.reject {|c| options[:exclude].find {|ex| c.name.to_sym === ex}}.collect {|c| c.name.to_sym}
46
- end
47
- elsif !params.empty?
48
- self.extjs_record_fields = params
49
- else
50
- self.extjs_record_fields
51
- end
52
- end
53
-
54
- ##
55
- # render AR columns to Ext.data.Record.create format
56
- # eg: {name:'foo', type: 'string'}
57
- #
58
- def extjs_record
59
- if self.extjs_record_fields.empty?
60
- self.extjs_record_fields = self.columns.collect {|c| c.name.to_sym }
61
- self.extjs_record_fields.concat(self.reflect_on_all_associations.collect {|assn| assn.name})
62
- end
63
-
64
- return {
65
- "fields" => self.extjs_record_fields.collect {|f|
66
- if col = self.columns_hash[f.to_s]
67
- type = col.type
68
- case col.type
69
- when :datetime || :date || :time || :timestamp
70
- type = :date
71
- when :text
72
- type = :string
73
- when :integer
74
- type = :int
75
- end
76
- field = {:name => col.name, :allowBlank => (col.primary) ? true : col.null, :type => type}
77
- field[:dateFormat] = "c" if col.type === :datetime || col.type === :date # <-- ugly hack for date
78
- field
79
- elsif self.reflections[f]
80
- assn = self.reflections[f]
81
- field = {:name => assn.name, :allowBlank => true, :type => 'auto'}
82
- end
83
- },
84
- "idProperty" => self.primary_key
85
- }
86
- end
87
- end
88
- end
89
- end
90
-
@@ -1,98 +0,0 @@
1
- module ExtJS
2
- module Model
3
- def self.included(model)
4
- model.send(:extend, ClassMethods)
5
- model.send(:include, InstanceMethods)
6
- model.class_eval do
7
- cattr_accessor :extjs_record_fields
8
- end
9
- model.extjs_record_fields = []
10
- end
11
-
12
- ##
13
- # InstanceMethods
14
- #
15
- module InstanceMethods
16
- def to_record
17
- properties = self.class.properties
18
- pk = self.class.key.first.name
19
-
20
- data = {pk => self.send(pk)}
21
- self.class.extjs_record_fields.each do |f|
22
- if refl = self.class.relationships[f]
23
- if refl.options[:max].nil? && refl.options[:min].nil? # belongs_to
24
- assn = self.send(f)
25
- data[f] = (assn) ? assn.to_record : {} # <-- a thing was requested, give emtpy thing.
26
- elsif refl.options[:max] > 1
27
- data[f] = self.send(f).collect {|r| r.to_record} #CAREFUL!!!!!!!!!!!!1
28
- end
29
- else
30
- data[f] = self.send(f)
31
- end
32
- end
33
- data
34
- end
35
- end
36
- ##
37
- # ClassMethods
38
- #
39
- module ClassMethods
40
- ##
41
- # Defines the subset of AR columns used to create Ext.data.Record def'n.
42
- # @param {Array/Hash} list-of-fields to include, :only, or :exclude
43
- #
44
- def extjs_fields(*params)
45
- options = params.extract_options!
46
- if !options.keys.empty?
47
- if options[:exclude]
48
- self.extjs_record_fields = self.properties.reject {|p| options[:exclude].find {|ex| p.name === ex}}.collect {|p| p.name}
49
- end
50
- elsif !params.empty?
51
- self.extjs_record_fields = params
52
- else
53
- self.extjs_record_fields
54
- end
55
- end
56
-
57
- ##
58
- # render AR columns to Ext.data.Record.create format
59
- # eg: {name:'foo', type: 'string'}
60
- #
61
- def extjs_record
62
- if self.extjs_record_fields.empty?
63
- self.extjs_record_fields = self.properties.collect {|p| p.name}
64
- end
65
-
66
- pk = self.key.first
67
-
68
- return {
69
- "fields" => self.extjs_record_fields.collect {|f|
70
- if self.properties.has_property?(f)
71
- col = self.properties[f]
72
- type = ((col.type.respond_to?(:primitive)) ? col.type.primitive : col.type).to_s
73
- case type
74
- when "DateTime", "Date", "Time"
75
- type = :date
76
- when "String"
77
- type = :string
78
- when "Float"
79
- type = :float
80
- when "Integer", "BigDecimal"
81
- type = :int
82
- end
83
- field = {:name => col.name, :allowBlank => (col === pk) ? true : col.nullable?, :type => type}
84
- field[:dateFormat] = "c" if type === :date # <-- ugly hack for date
85
- field
86
- elsif assn = self.relationships[f]
87
- field = {:name => f, :allowBlank => true, :type => 'auto'}
88
- else # property is a method?
89
- field = {:name => f, :allowBlank => true, :type => 'auto'}
90
- end
91
- },
92
- "idProperty" => pk.name
93
- }
94
- end
95
- end
96
- end
97
- end
98
-
@@ -1,102 +0,0 @@
1
- module ExtJS
2
- module Model
3
-
4
- def self.included(model)
5
- model.send(:extend, ClassMethods)
6
- model.send(:include, InstanceMethods)
7
- model.class_eval do
8
- cattr_accessor :extjs_record_fields
9
- end
10
- model.extjs_record_fields = []
11
- end
12
-
13
- ##
14
- # InstanceMethods
15
- #
16
- module InstanceMethods
17
- def to_record
18
- properties = self.class.column_names
19
- pk = "id"
20
-
21
- data = {pk => self.send(pk)}
22
- self.class.extjs_record_fields.each do |f|
23
- if refl = self.class.associations[f]
24
- if refl.type === :belongs_to
25
- assn = self.send(f)
26
- data[f] = (assn) ? assn.to_record : {} # <-- a thing was requested, give emtpy thing.
27
- elsif refl.type === :many
28
- data[f] = self.send(f).collect {|r| r.to_record} #CAREFUL!!!!!!!!!!!!1
29
- end
30
- else
31
- data[f] = self.send(f)
32
- end
33
- end
34
- data
35
- end
36
- end
37
- ##
38
- # ClassMethods
39
- #
40
- module ClassMethods
41
- ##
42
- # Defines the subset of AR columns used to create Ext.data.Record def'n.
43
- # @param {Array/Hash} list-of-fields to include, :only, or :exclude
44
- #
45
- def extjs_fields(*params)
46
- options = params.extract_options!
47
- if !options.keys.empty?
48
- if options[:exclude]
49
- self.extjs_record_fields = self.column_names.reject {|p| options[:exclude].find {|ex| p === ex.to_s}}.collect {|p| p}
50
- end
51
- elsif !params.empty?
52
- self.extjs_record_fields = params
53
- else
54
- self.extjs_record_fields
55
- end
56
- end
57
-
58
- ##
59
- # render AR columns to Ext.data.Record.create format
60
- # eg: {name:'foo', type: 'string'}
61
- #
62
- def extjs_record
63
- if self.extjs_record_fields.empty?
64
- self.extjs_record_fields = self.column_names
65
- end
66
-
67
- pk = "_id"
68
-
69
- return {
70
- "fields" => self.extjs_record_fields.collect {|f|
71
- if self.keys[f]
72
-
73
- col = self.keys[f]
74
- type = col.type.to_s
75
- case type
76
- when "DateTime", "Date", "Time"
77
- type = :date
78
- when "String"
79
- type = :string
80
- when "Float"
81
- type = :float
82
- when "Integer", "BigDecimal"
83
- type = :int
84
- else
85
- type = "auto"
86
- end
87
- field = {:name => col.name, :allowBlank => (col.name === pk) ? true : !col.options[:required] === true, :type => type}
88
- field[:dateFormat] = "c" if type === :date # <-- ugly hack for date
89
- field
90
- elsif assn = self.associations[f.to_sym]
91
- field = {:name => f, :allowBlank => true, :type => 'auto'}
92
- else # property is a method?
93
- field = {:name => f, :allowBlank => true, :type => 'auto'}
94
- end
95
- },
96
- "idProperty" => "id"
97
- }
98
- end
99
- end
100
- end
101
- end
102
-