extjs-direct 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,6 @@
1
+ === 1.0.0 / 2009-04-09
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+
@@ -0,0 +1,10 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.rdoc
4
+ Rakefile
5
+ lib/merb-extjs-direct.rb
6
+ lib/merb-extjs-direct/xrequest.rb
7
+ lib/merb-extjs-direct/xresponse.rb
8
+ lib/merb-extjs-direct/xexception.rb
9
+ lib/merb-extjs-direct/mixins/remoting_provider.rb
10
+ test/test_merb-extjs-direct.rb
@@ -0,0 +1,48 @@
1
+ = merb-ext-direct
2
+
3
+ * FIX (url)
4
+
5
+ == DESCRIPTION:
6
+
7
+ FIX (describe your package)
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * FIX (list of features or problems)
12
+
13
+ == SYNOPSIS:
14
+
15
+ FIX (code sample of usage)
16
+
17
+ == REQUIREMENTS:
18
+
19
+ * FIX (list of requirements)
20
+
21
+ == INSTALL:
22
+
23
+ * FIX (sudo gem install, anything else)
24
+
25
+ == LICENSE:
26
+
27
+ (The MIT License)
28
+
29
+ Copyright (c) 2009 FIXME full name
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,12 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/merb-extjs-direct.rb'
6
+
7
+ Hoe.new('extjs-direct', Merb::ExtJS::Direct::VERSION) do |p|
8
+ # p.rubyforge_name = 'merb_extjs_directx' # if different than lowercase project name
9
+ p.developer('christocracy', 'chris.scott@extjs.com')
10
+ end
11
+
12
+ # vim: syntax=Ruby
@@ -0,0 +1,13 @@
1
+ module Merb
2
+ module ExtJS
3
+ module Direct
4
+ VERSION = '0.0.1'
5
+ end
6
+ end
7
+ end
8
+
9
+ require File.join(File.dirname(__FILE__), 'merb-extjs-direct', 'xrequest')
10
+ require File.join(File.dirname(__FILE__), 'merb-extjs-direct', 'xresponse')
11
+ require File.join(File.dirname(__FILE__), 'merb-extjs-direct', 'xexception')
12
+ require File.join(File.dirname(__FILE__), 'merb-extjs-direct', 'mixins', 'remoting_provider')
13
+
@@ -0,0 +1,156 @@
1
+ ##
2
+ # Meb::ExtJS::Direct::RemotingProvider
3
+ # @mixin
4
+ # This is a mixin which provides support Merb support for ExtJS's Ext.Direct mechanism in Ext3.x
5
+ # <h3>Defining and registering the Ext.Direct api</h3>
6
+ # <code>
7
+ # <script>
8
+ # var api : {
9
+ # url: 'my_controller/rpc',
10
+ # type: 'remoting',
11
+ # actions: []
12
+ # };
13
+ #
14
+ # Ext.onReady(function() {
15
+ # // Add some actions from ArtistPart
16
+ # api.actions.push({
17
+ # ArtistPart : [
18
+ # {"name" : "create"},
19
+ # {"name" : "destroy"}
20
+ # ];
21
+ # });
22
+ # .
23
+ # .
24
+ # .
25
+ # // add some more actions from AlbumPart
26
+ # api.actions.push({
27
+ # AlbumPart : [
28
+ # {"name" : "create"},
29
+ # {"name" : "destroy"}
30
+ # ];
31
+ # });
32
+ #
33
+ # // register api with Ext.Direct
34
+ # Ext.Direct.addProvider(api);
35
+ #
36
+ # // Executing Ext.Direct actions. (very much like using irb console)
37
+ # ArtistPart.destroy(1);
38
+ # AlbumCreate.create({name : "Joe Puke and the Chunky Bits"});
39
+ #
40
+ # });
41
+ # </script>
42
+ # </code>
43
+ #
44
+ # <h3>Configuring your Merb Ext.Direct controller</h3>
45
+ # <code>
46
+ # class MyController
47
+ # include Merb::Ext::Direct::RemotingProvider # <-- include this mixin.
48
+ # end
49
+ # </code>
50
+ #
51
+ # <p>By including RemotingProvider mixin, your controller will have added to it a single action called "rpc"
52
+ # with which you can send all you Ext.Direct actions to. Take note of the url specified in the first step.</p>
53
+ #
54
+ # <h3>Merb-parts</h3>
55
+ # Ext.Direct is implemented in Merb by taking advantage of "Parts". The nice thing about parts is that they
56
+ # have both a view (for rendering you Ext controls and api along with a controller for executing your api actions.
57
+ #
58
+ #
59
+ #
60
+ # The Merb RemotingProvider mixin takes advantage of the merb-parts gem.
61
+ #
62
+ # Ext.Direct router
63
+ # @usage include Ext::Direct::RemotingProvider
64
+ # @author Chris Scott
65
+ # @TODO before_actions
66
+ #
67
+ module Merb::ExtJS::Direct
68
+ module RemotingProvider
69
+
70
+ # standard ruby method called when some class does:
71
+ # include Merb::ExtJS::Direct::RemotingProvider
72
+ def self.included(base)
73
+ # must explicity specify controller-actions to make callable. security-wise, this is a *good* thing.
74
+ # just one action is added.
75
+ base.show_action(:rpc)
76
+ end
77
+
78
+ ##
79
+ # rpc
80
+ # remote procedure call handler for Ext.direct requests.
81
+ #
82
+ def rpc
83
+ if !request.ajax? && !params["extAction"]
84
+ return "Ext::Direct::RemotingProvider#rpc -- This method is an ajax-handler only"
85
+ end
86
+
87
+ is_form = false
88
+ is_upload = false
89
+ if params["extAction"] && params["extMethod"]
90
+ # create fake response here.
91
+ is_form = true
92
+ is_upload = params.delete("extUpload") == 'true'
93
+
94
+ request = {
95
+ "xcontroller" => params.delete("extAction"),
96
+ "xaction" => params.delete("extMethod"),
97
+ "type" => "rpc",
98
+ "id" => params.delete("id"),
99
+ "tid" => params.delete("extTID"),
100
+ "format" => params.delete("format"),
101
+ "data" => params
102
+ }
103
+ params.delete('controller')
104
+ params.delete('action')
105
+ res = "<html><body><textarea>#{handle_request(request).gsub(/&quot;/, "\&quot;")}</textarea></body></html>"
106
+ return res
107
+ elsif (params[:inflated_object])
108
+ # multiple requests found. I'm not sure how this "inflated_object" mechanism works. This is Merb magic?
109
+ # controllers always return a string, so each response has already been json-encoded.
110
+ # since we're dealing with multiple requests here, we have to manually string-concat-wrap the retured
111
+ # string-of-json.
112
+ #
113
+ return "[" + params[:inflated_object].collect {|req| handle_request(req)}.join(',') + "]"
114
+ else
115
+ ##
116
+ # just a single request found
117
+ #
118
+ return handle_request(params)
119
+ end
120
+ end
121
+
122
+ ##
123
+ #
124
+ protected
125
+ #
126
+ ##
127
+
128
+ ##
129
+ # handle_request
130
+ # # @throws XException when anything goes wrong to gracefully handle on client.
131
+ # @see Merb::Controller#part method, google: "merb parts". A Merb "part" is a kind of sub-controller
132
+ # with its onwn view capabilites but cannot be directly routed-to. Using part() method here is similar
133
+ # to using Ext.getCmp(id), eg: part(req.controller => req.action) ~ Ext.getCmp(controller).doSomething(action);
134
+ # However, unlike Ext.getCmp(), part(controller => action) returns a string to be added to the render stream
135
+ # and not a component instance, just as all merb render actions.
136
+ #
137
+ def handle_request(req)
138
+ req = XRequest.new(req)
139
+
140
+ begin
141
+ # here be your Ext.direct.Transaction as a ruby Hash.
142
+ # turn it into an offical ruby XRequest instance.
143
+ # constantize the "controller" (ie: String "ArtistPart" becomes Class ArtistPart) and send "action" to it.
144
+ # NOTE: Extlib has nothing to do with ExtJS -- just a coincidence. Extlib is a Merb gem.
145
+ return part(Extlib::Inflection.constantize(req.controller) => req.action, :xrequest => req)
146
+ rescue XException => e
147
+ # caught an XException...return an Ext.Direct-friendly XExceptionResponse
148
+ return XExceptionResponse.new(req, e).to_json
149
+ rescue StandardError => e
150
+ # might be an invalid controller and/or action if we got here.
151
+ # we can be more specific here and trap on NameError if we wish, intead of base StandardError
152
+ return XExceptionResponse.new(req, e).to_json
153
+ end
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,36 @@
1
+ ##
2
+ # XException
3
+ # General exception class for all Ext application exceptions.
4
+ # @author Chris Scott
5
+ #
6
+ class XException < StandardError
7
+ end
8
+
9
+ ##
10
+ # XExceptionResponse
11
+ # A special extension of XResponse for returning responses where an Exception was raised. includes a back-trace in the response which
12
+ # should probably be turned on with a debug switch only.
13
+ # @author Chris Scott
14
+ #
15
+ class XExceptionResponse < XResponse
16
+ attr_accessor :where
17
+
18
+ ##
19
+ # initialize
20
+ # @param {XRequest}
21
+ # @param {StandardError}
22
+ #
23
+ def initialize(req, e)
24
+ super(req)
25
+ @type = 'exception'
26
+ @message = e.message
27
+ @where = e.backtrace
28
+ end
29
+
30
+ def to_h
31
+ data = super
32
+ data[:type] = 'exception'
33
+ data[:where] = @where.join("\n")
34
+ data
35
+ end
36
+ end
@@ -0,0 +1,31 @@
1
+ ###
2
+ # XRequest
3
+ # A standard response class suitable for Ext.Direct requests.
4
+ # @author Chris Scott
5
+ #
6
+ class XRequest
7
+ attr_reader :id, :tid, :controller, :action, :type, :params
8
+
9
+ def initialize(params)
10
+ # TODO: simply setting @id, @params
11
+ @id = (params["id"].to_i > 0) ? params["id"].to_i : (params["data"].kind_of?(Array) && (params["data"].first.kind_of?(Integer) || params["data"].first.nil?)) ? params["data"].shift: nil
12
+ @tid = params["tid"]
13
+ @type = params["type"]
14
+ @params = (params["data"].kind_of?(Array) && params["data"].length == 1 && params["data"].first.kind_of?(Hash)) ? params["data"].first : params["data"] || []
15
+ @controller = params["xcontroller"]
16
+ @action = params["xaction"]
17
+ end
18
+
19
+ ##
20
+ # arg
21
+ # return a request argument at index. can be used to access either [] or {}
22
+ # @param {String/Integer} index
23
+ # @raises XException if params doesn't exist.
24
+ #
25
+ def arg(index)
26
+ if params[index].nil?
27
+ raise XException.new("Attempt to access unknown request argument '#{index.to_s}' on transaction #{@tid}")
28
+ end
29
+ @params[index]
30
+ end
31
+ end
@@ -0,0 +1,26 @@
1
+ ###
2
+ # XResponse
3
+ # A standard response class suitable for Ext.Direct AJAX responses.
4
+ # @author Chris Scott
5
+ #
6
+ class XResponse
7
+ attr_accessor :type, :status, :errors, :success, :message, :result
8
+ attr_reader :tid
9
+
10
+ def initialize(req)
11
+ @tid = req.tid
12
+ @type = req.type
13
+ @status = false
14
+ @message = ''
15
+ @result = []
16
+ @errors = []
17
+ end
18
+
19
+ def to_h
20
+ {:tid => @tid, :status => @status, :type => @type, :message => @message, :result => @result, :errors => @errors}
21
+ end
22
+
23
+ def to_json
24
+ self.to_h.to_json
25
+ end
26
+ end
@@ -0,0 +1,11 @@
1
+ require File.dirname(__FILE__) + '/test_helper.rb'
2
+
3
+ class TestMerb-ext-direct < Test::Unit::TestCase
4
+
5
+ def setup
6
+ end
7
+
8
+ def test_truth
9
+ assert true
10
+ end
11
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: extjs-direct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - christocracy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-09 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.12.1
24
+ version:
25
+ description: FIX (describe your package)
26
+ email:
27
+ - chris.scott@extjs.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ files:
36
+ - History.txt
37
+ - Manifest.txt
38
+ - README.rdoc
39
+ - Rakefile
40
+ - lib/merb-extjs-direct.rb
41
+ - lib/merb-extjs-direct/xrequest.rb
42
+ - lib/merb-extjs-direct/xresponse.rb
43
+ - lib/merb-extjs-direct/xexception.rb
44
+ - lib/merb-extjs-direct/mixins/remoting_provider.rb
45
+ - test/test_merb-extjs-direct.rb
46
+ has_rdoc: true
47
+ homepage: FIX (url)
48
+ post_install_message:
49
+ rdoc_options:
50
+ - --main
51
+ - README.txt
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project: extjs-direct
69
+ rubygems_version: 1.3.1
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: FIX (describe your package)
73
+ test_files:
74
+ - test/test_merb-extjs-direct.rb