christocracy-merb-extjs-direct 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2009-01-14
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,15 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/merb-ext-direct.rb
7
+ lib/merb-ext-direct/xrequest.rb
8
+ lib/merb-ext-direct/xresponse.rb
9
+ lib/merb-ext-direct/xexception.rb
10
+ lib/merb-ext-direct/mixins/remoting_provider.rb
11
+ script/console
12
+ script/destroy
13
+ script/generate
14
+ test/test_helper.rb
15
+ test/test_merb-ext-direct.rb
data/PostInstall.txt ADDED
@@ -0,0 +1,7 @@
1
+
2
+ For more information on merb-ext-direct, see http://merb-ext-direct.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
data/README.rdoc ADDED
@@ -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.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/merb-ext-direct'
3
+
4
+ # Generate all the Rake tasks
5
+ # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
+ $hoe = Hoe.new('merb-ext-direct', Merb::Ext::Direct::VERSION) do |p|
7
+ p.developer('Chris Scott', 'chris.scott@extjs.com')
8
+ p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
+ p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
10
+ p.rubyforge_name = p.name # TODO this is default value
11
+ # p.extra_deps = [
12
+ # ['activesupport','>= 2.0.2'],
13
+ # ]
14
+ p.extra_dev_deps = [
15
+ ['newgem', ">= #{::Newgem::VERSION}"]
16
+ ]
17
+
18
+ p.clean_globs |= %w[**/.DS_Store tmp *.log]
19
+ path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
20
+ p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
21
+ p.rsync_args = '-av --delete --ignore-errors'
22
+ end
23
+
24
+ require 'newgem/tasks' # load /tasks/*.rake
25
+ Dir['tasks/**/*.rake'].each { |t| load t }
26
+
27
+ # TODO - want other tests/tasks run by default? Add them to the list
28
+ # task :default => [:spec, :features]
@@ -0,0 +1,13 @@
1
+ module Merb
2
+ module Ext
3
+ module Direct
4
+ VERSION = '0.0.1'
5
+ end
6
+ end
7
+ end
8
+
9
+ require File.join(File.dirname(__FILE__), 'merb-ext-direct', 'xrequest')
10
+ require File.join(File.dirname(__FILE__), 'merb-ext-direct', 'xresponse')
11
+ require File.join(File.dirname(__FILE__), 'merb-ext-direct', 'xexception')
12
+ require File.join(File.dirname(__FILE__), 'merb-ext-direct', 'mixins', 'remoting_provider')
13
+
@@ -0,0 +1,156 @@
1
+ ##
2
+ # Ext::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::Ext::Direct
68
+ module RemotingProvider
69
+
70
+ # standard ruby method called when some class does:
71
+ # include Ext::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
data/script/console ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # File: script/console
3
+ irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
+
5
+ libs = " -r irb/completion"
6
+ # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
+ # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
+ libs << " -r #{File.dirname(__FILE__) + '/../lib/merb-ext-direct.rb'}"
9
+ puts "Loading merb-ext-direct gem"
10
+ exec "#{irb} #{libs} --simple-prompt"
data/script/destroy ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/destroy'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
+
4
+ begin
5
+ require 'rubigen'
6
+ rescue LoadError
7
+ require 'rubygems'
8
+ require 'rubigen'
9
+ end
10
+ require 'rubigen/scripts/generate'
11
+
12
+ ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
+ RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
+ RubiGen::Scripts::Generate.new.run(ARGV)
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/merb-ext-direct'
@@ -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,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: christocracy-merb-extjs-direct
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Scott
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-27 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: mime-types
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "1.15"
23
+ version:
24
+ description: Merb implementation of ExtJS's Ext.Direct router
25
+ email: chris.scott@extjs.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ - PostInstall.txt
36
+ - README.rdoc
37
+ - Rakefile
38
+ - lib/merb-ext-direct.rb
39
+ - lib/merb-ext-direct/xrequest.rb
40
+ - lib/merb-ext-direct/xresponse.rb
41
+ - lib/merb-ext-direct/xexception.rb
42
+ - lib/merb-ext-direct/mixins/remoting_provider.rb
43
+ - script/console
44
+ - script/destroy
45
+ - script/generate
46
+ - test/test_helper.rb
47
+ - test/test_merb-ext-direct.rb
48
+ has_rdoc: true
49
+ homepage: http://github.com/christocracy/merb-ext-direct
50
+ post_install_message:
51
+ rdoc_options:
52
+ - --inline-source
53
+ - --charset=UTF-8
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.2.0
72
+ signing_key:
73
+ specification_version: 2
74
+ summary: Merb implementation of ExtJS's Ext.Direct router
75
+ test_files: []
76
+