extjs-mvc 0.1.33 → 0.2.0

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
@@ -11,11 +11,8 @@ A collection of helpers, MVC mixins and PORs (plain-old-ruby-object) to assist w
11
11
  In <tt>environment.rb</tt>
12
12
 
13
13
  Rails::Initializer.run do |config|
14
- .
15
- .
16
- .
14
+ config.gem "extjs-mvc"
17
15
  end
18
- require 'extjs-mvc'
19
16
 
20
17
 
21
18
  === An ActiveRecord mixin: ExtJS::Model
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.33
1
+ 0.2.0
File without changes
@@ -0,0 +1,15 @@
1
+ ##
2
+ # add Rails-style Array#extract_options! method
3
+ #
4
+ module ExtJS
5
+ module CoreExtensions
6
+ module Array
7
+ module ExtractOptions
8
+ def extract_options!
9
+ last.is_a?(::Hash) ? pop : {}
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ Array.send(:include, ExtJS::CoreExtensions::Array::ExtractOptions)
@@ -7,7 +7,7 @@ class ExtJS::Component
7
7
  @config = params#params.extract_options!
8
8
  @controller = @config.delete(:controller) unless @config[:controller].nil?
9
9
 
10
- @config[:items] = [] if config[:items].nil?
10
+ @config["items"] = [] if config["items"].nil?
11
11
 
12
12
  if container = @config.delete(:container)
13
13
  container.add(self)
@@ -25,25 +25,32 @@ class ExtJS::Component
25
25
  # instance will be returned.
26
26
  # @return {String/ExtJS::Component}
27
27
  def add(*config)
28
+
28
29
  options = config.extract_options!
29
30
  if !options.keys.empty?
30
31
  if url = options.delete(:partial)
31
32
  # rendering a partial, cache the config until partial calls #add method. @see else.
32
33
  @partial_config = options
33
- return @controller.render(:partial => url, :locals => {:container => self})
34
+ if (@controller.respond_to?(:partial))
35
+ # Merb
36
+ return @controller.partial(url, :with => self, :as => :container)
37
+ else
38
+ # Rails
39
+ return @controller.render(:partial => url, :locals => {:container => self})
40
+ end
34
41
  else
35
42
  options.merge!(@partial_config) unless @partial_config.nil?
36
43
  options[:controller] = @controller unless @controller.nil?
37
44
  cmp = ExtJS::Component.new(options)
38
45
  @partial_config = nil
39
- @config[:items] << cmp
46
+ @config["items"] << cmp
40
47
  return cmp
41
48
  end
42
49
  elsif !config.empty? && config.first.kind_of?(ExtJS::Component)
43
50
  cmp = config.first
44
51
  cmp.apply(@partial_config) unless @partial_config.nil?
45
52
  @partial_config = nil
46
- @config[:items] << cmp.config
53
+ @config["items"] << cmp.config
47
54
  return cmp
48
55
  end
49
56
  end
@@ -53,6 +60,8 @@ class ExtJS::Component
53
60
  end
54
61
 
55
62
  def render
63
+ @config.delete("items") if @config["items"].empty?
64
+
56
65
  # If there are any listeners attached in json, we have to get rid of double-quotes in order to expose
57
66
  # the javascript object.
58
67
  # eg: "listeners":"SomeController.listeners.grid" -> {"listeners":SomeController.listeners.grid, ...}
@@ -15,8 +15,8 @@ module ExtJS::Data
15
15
  @proxy = options[:proxy] || 'http'
16
16
  @writer = options[:writer]
17
17
  @type = (@proxy === 'direct' ? @proxy : @format).capitalize
18
- @controller = "#{options[:controller].to_s.camelize}Controller".constantize
19
- @model = ((options[:model]) ? options[:model] : @controller.controller_name.singularize).camelize.constantize
18
+ @controller = self.get_controller(options[:controller])
19
+ @model = self.get_model(options[:controller], options[:model])
20
20
 
21
21
  # Merge Reader/Proxy config
22
22
  @config.merge!(@controller.extjs_reader(@model))
@@ -63,5 +63,21 @@ module ExtJS::Data
63
63
  "<script>new Ext.data.#{@type}Store(#{@config.to_json});#{script}</script>"
64
64
  end
65
65
  end
66
+
67
+ def get_controller(name)
68
+ if (defined?(Rails))
69
+ "#{name.to_s.camelize}Controller".constantize
70
+ else
71
+ Extlib::Inflection.constantize("#{Extlib::Inflection.camelize(name)}")
72
+ end
73
+ end
74
+
75
+ def get_model(controller, model)
76
+ if (defined?(Rails))
77
+ ((model) ? model : controller.singularize).camelize.constantize
78
+ else
79
+ Extlib::Inflection.constantize(Extlib::Inflection.camelize(((model) ? model : Extlib::Inflection.singularize(controller))))
80
+ end
81
+ end
66
82
  end
67
83
  end
data/lib/extjs-mvc.rb CHANGED
@@ -7,16 +7,28 @@ module ExtJS
7
7
  cattr_accessor :message_property
8
8
  cattr_accessor :root
9
9
 
10
+ # Detect orm, include appropriate mixin.
10
11
  if defined?(ActiveRecord)
11
- require 'active_record/model'
12
+ require 'model/active_record/model'
13
+ elsif defined?(DataMapper)
14
+ require 'model/dm/model'
12
15
  end
16
+
17
+ # Rails-style Array#extract_options! used heavily
18
+ if defined?(Merb)
19
+ require 'core_ext/array/extract_options'
20
+ end
21
+
22
+ # ExtJS Component and Store wrappers
13
23
  require 'extjs/component'
14
24
  require 'extjs/data/store'
15
25
 
26
+ # Component/Store view-helpers
16
27
  require 'helpers/component'
17
28
  require 'helpers/store'
18
29
 
19
- require 'action_controller/controller'
30
+ # Controller mixin. Works for both Rails and Merb.
31
+ require 'controller/controller'
20
32
 
21
33
  end
22
- end
34
+ end
@@ -6,7 +6,7 @@ module ExtJS::Helpers
6
6
  ##
7
7
  # add class-var @@extjs_on_ready
8
8
  def self.included(helper)
9
-
9
+
10
10
  end
11
11
 
12
12
  def extjs_component(*params)
@@ -21,9 +21,8 @@ module ExtJS::Helpers
21
21
  #
22
22
  def extjs_onready(*params)
23
23
  @onready_queue = [] if @onready_queue.nil?
24
-
25
24
  params.each do |cmp|
26
- @onready_queue << cmp
25
+ @onready_queue << cmp
27
26
  end
28
27
  end
29
28
 
@@ -31,7 +30,7 @@ module ExtJS::Helpers
31
30
  # Empties the on_ready queue. Renders within <script></script> tags
32
31
  #
33
32
  def extjs_render
34
- @onready_queue = [] if @onready_queue.nil? # <--- ugly, ugh...having trouble with initializing my instance vars.
33
+ @onready_queue = [] if @onready_queue.nil?
35
34
  "<script>\nExt.onReady(function() {\n\t#{@onready_queue.collect {|cmp| (cmp.kind_of?(ExtJS::Component)) ? cmp.render : cmp}.join("\n\t")}\n });\n</script>"
36
35
  end
37
36
  end
File without changes
@@ -0,0 +1,96 @@
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
+ end
89
+ },
90
+ "idProperty" => pk.name
91
+ }
92
+ end
93
+ end
94
+ end
95
+ end
96
+
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ class ComponentTest < Test::Unit::TestCase
4
+ context "An ExtJS::Component Instance" do
5
+
6
+ setup do
7
+ @cmp = ExtJS::Component.new("title" => "A Component", "xtype" => "panel")
8
+ end
9
+
10
+ should "Render" do
11
+ assert @cmp.render.match(/Ext.ComponentMgr.create/)
12
+ end
13
+ end
14
+ end
15
+
File without changes
File without changes
File without changes
data/test/test_helper.rb CHANGED
@@ -2,9 +2,13 @@ require 'rubygems'
2
2
  require 'test/unit'
3
3
  require 'shoulda'
4
4
 
5
+ require 'active_record'
6
+ require 'active_support'
7
+
5
8
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
9
  $LOAD_PATH.unshift(File.dirname(__FILE__))
7
- require 'mvc'
10
+
11
+ require 'extjs-mvc'
8
12
 
9
13
  class Test::Unit::TestCase
10
14
  end
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.1.33
4
+ version: 0.2.0
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-09-30 00:00:00 -04:00
12
+ date: 2009-10-05 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -38,16 +38,19 @@ files:
38
38
  - README.rdoc
39
39
  - Rakefile
40
40
  - VERSION
41
- - lib/action_controller/controller.rb
42
- - lib/active_record/model.rb
43
- - lib/dm/model.rb
41
+ - lib/controller/controller.rb
42
+ - lib/core_ext/array/extract_options.rb
44
43
  - lib/extjs-mvc.rb
45
44
  - lib/extjs/component.rb
46
45
  - lib/extjs/data/store.rb
47
46
  - lib/helpers/component.rb
48
47
  - lib/helpers/store.rb
49
- - lib/mvc.rb
50
- - test/mvc_test.rb
48
+ - lib/model/active_record/model.rb
49
+ - lib/model/dm/model.rb
50
+ - test/component_test.rb
51
+ - test/controller_test.rb
52
+ - test/model_test.rb
53
+ - test/store_test.rb
51
54
  - test/test_helper.rb
52
55
  has_rdoc: true
53
56
  homepage: http://github.com/extjs/mvc
data/lib/mvc.rb DELETED
@@ -1,22 +0,0 @@
1
- module ExtJS
2
- class MVC
3
- @@success_property = :success
4
- @@message_property = :message
5
- @@root = :data
6
- cattr_accessor :success_property
7
- cattr_accessor :message_property
8
- cattr_accessor :root
9
-
10
- if defined?(ActiveRecord)
11
- require 'active_record/model'
12
- end
13
- require 'extjs/component'
14
- require 'extjs/data/store'
15
-
16
- require 'helpers/component'
17
- require 'helpers/store'
18
-
19
- require 'action_controller/controller'
20
-
21
- end
22
- end
data/test/mvc_test.rb DELETED
@@ -1,7 +0,0 @@
1
- require 'test_helper'
2
-
3
- class MvcTest < Test::Unit::TestCase
4
- should "probably rename this file and start testing for real" do
5
- flunk "hey buddy, you should probably rename this file and start testing for real"
6
- end
7
- end