extjs-mvc 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.3
1
+ 0.2.4
@@ -55,7 +55,7 @@ class ExtJS::Component
55
55
  end
56
56
  end
57
57
 
58
- def to_json
58
+ def to_json(*args)
59
59
  config.to_json
60
60
  end
61
61
 
data/lib/extjs-mvc.rb CHANGED
@@ -6,12 +6,15 @@ module ExtJS
6
6
  cattr_accessor :success_property
7
7
  cattr_accessor :message_property
8
8
  cattr_accessor :root
9
-
9
+
10
+
10
11
  # Detect orm, include appropriate mixin.
11
12
  if defined?(ActiveRecord)
12
13
  require 'model/active_record/model'
13
14
  elsif defined?(DataMapper)
14
15
  require 'model/dm/model'
16
+ elsif defined?(MongoMapper)
17
+ require 'model/mongo_mapper/model'
15
18
  end
16
19
 
17
20
  # Rails-style Array#extract_options! used heavily
@@ -16,7 +16,7 @@ module ExtJS
16
16
  def to_record
17
17
  properties = self.class.properties
18
18
  pk = self.class.key.first.name
19
-
19
+
20
20
  data = {pk => self.send(pk)}
21
21
  self.class.extjs_record_fields.each do |f|
22
22
  if refl = self.class.relationships[f]
@@ -71,13 +71,13 @@ module ExtJS
71
71
  col = self.properties[f]
72
72
  type = ((col.type.respond_to?(:primitive)) ? col.type.primitive : col.type).to_s
73
73
  case type
74
- when "DateTime" || "Date" || "Time"
74
+ when "DateTime", "Date", "Time"
75
75
  type = :date
76
76
  when "String"
77
77
  type = :string
78
78
  when "Float"
79
79
  type = :float
80
- when "Integer" || "BigDecimal"
80
+ when "Integer", "BigDecimal"
81
81
  type = :int
82
82
  end
83
83
  field = {:name => col.name, :allowBlank => (col === pk) ? true : col.nullable?, :type => type}
@@ -0,0 +1,102 @@
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
+
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.3
4
+ version: 0.2.4
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-14 00:00:00 -04:00
12
+ date: 2009-10-23 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -47,6 +47,7 @@ files:
47
47
  - lib/helpers/store.rb
48
48
  - lib/model/active_record/model.rb
49
49
  - lib/model/dm/model.rb
50
+ - lib/model/mongo_mapper/model.rb
50
51
  - test/component_test.rb
51
52
  - test/controller_test.rb
52
53
  - test/model_test.rb