rails-extjs-direct 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,5 +1,5 @@
1
1
  = rails-extjs-direct
2
- THIS GEM IS UNDER DEVELOPMENT AND WILL NOT YET WORK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
2
+ THIS GEM IS UNDER DEVELOPMENT AND NOT A FULL IMLPLEMENTATION OF THE Ext.Direct specification
3
3
 
4
4
  http://rubyforge.org/projects/rails-extjs/
5
5
  http://www.extjs.com
@@ -13,12 +13,48 @@ Ext.Direct in Rails is implementede with Rack Middleware.
13
13
 
14
14
  == SYNOPSIS:
15
15
 
16
+ * STEP1: Attach Rack MiddleWare to Rails config in environment.rb. The "/direct" bit is the url
17
+ designated as Ext.Direct request.
18
+
19
+ require 'rails-extjs-direct'
20
+ Rails::Initializer.run do |config|
21
+ config.middleware.use Rails::ExtJS::Direct::RemotingProvider, "/direct"
22
+ .
23
+ .
24
+ .
25
+ end
26
+
27
+ * STEP2: Include Rails::ExtJS::Direct::Controller mixin into each controller you wish to be "Directable":
28
+
29
+ class CompanyController < ApplicationController
30
+ include Rails::ExtJS::Direct::Controller
31
+
32
+ def destroy
33
+ # The Direct::Controller mixin will provide an instance variable @xrequest to each action.
34
+ # @xrequest is an instance of class XRequest provided by the rails-extjs-direct gem.
35
+ # XRequest contains accessor methods for each parameter in the Ajax request, including "tid", "type",
36
+ # "data", and "id"
37
+ #
38
+ # You must always render an instance of XResponse. Use the @request instance when instantiating
39
+ # your XResponse. This will ensure that Ext.Direct receives the correct "tid" (transaction id).
40
+ #
41
+ res = XResponse.new(@xrequest)
42
+ res.status = true
43
+ res.message = "Company#destroy"
44
+ res.result = {:foo => 'bar'}
45
+ render(:json => res)
46
+ end
47
+ end
48
+
49
+ * STEP 3 -- Define your Ext.Direct client-API
50
+
16
51
  <script>
17
52
  Ext.Direct.addProvider({
18
- url: '/admin/rpc', //<-- Note the url corresponding to Admin above.
53
+ // Set url corresponding to: config.middleware.use Rails::ExtJS::Direct::RemotingProvider, "/direct"
54
+ url: '/direct',
19
55
  type: 'remoting',
20
56
  actions: {
21
- Users : [
57
+ Company : [
22
58
  {name: 'load', len: 1},
23
59
  {name: 'save', len: 2},
24
60
  {name: 'create', len: 1},
@@ -26,34 +62,26 @@ Ext.Direct.addProvider({
26
62
  ]
27
63
  }
28
64
  });
65
+
66
+ // The following series of actions will generate just 1 Ajax request.
67
+ Company.create({name: "Slate International"});
68
+ Company.destroy(1, function(result, response) { console.info(arguments);});
69
+ Company.destroy(2);
70
+ Company.destroy(3);
71
+
29
72
  </script>
30
73
 
31
74
  == REQUIREMENTS:
32
75
 
33
- merb-parts
34
-
35
76
  == INSTALL:
36
77
 
37
- sudo gem install merb-extjs-parts
38
-
39
- IMPORTANT: This gem includes a required javascript file named "merb-extjs-direct.js" in the root of this gem.
40
- Currently you MUST copy this file to your /public/javascripts directory and included it immediately the ext
41
- framework's assets (ie: immediately after "ext-all.js").
42
-
43
- merb-extjs-direct.js contains a couple of simple overrides of the Ext.Direct library which will rename two
44
- HTTP request parameters to be more Merb/Rails-like. By default, Ext.Direct uses the word "action" where Merb
45
- uses "controller" and "method" vs "action". Confusing, eh? The HTTP params are actually renamed xcontroller
46
- and xaction to not interfere with Merb reserved words. If you don't see "xcontroller" / "xaction" params
47
- in your Ajax requests, make sure you've got merb-ext-direct.js included.
48
-
49
- In the future, I'll have >rake install automaticlly copy this file to /public/javascripts (Would anyone like to
50
- contribute some code to do that?)
78
+ sudo gem install rails-extjs-direct
51
79
 
52
80
  == LICENSE:
53
81
 
54
82
  (The MIT License)
55
83
 
56
- Copyright (c) 2009 FIX
84
+ Copyright (c) 2009 Chris Scott
57
85
 
58
86
  Permission is hereby granted, free of charge, to any person obtaining
59
87
  a copy of this software and associated documentation files (the
@@ -7,6 +7,6 @@ module Rails::ExtJS::Direct::Controller
7
7
  end
8
8
 
9
9
  def extjs_direct_prepare_request
10
- params["xrequest"] = "XRequest.new" # <-- TODO: prepare XRequest
10
+ @xrequest = XRequest.new(params)
11
11
  end
12
12
  end
@@ -1,54 +1,49 @@
1
+
1
2
  require 'json'
2
3
  class Rails::ExtJS::Direct::RemotingProvider
3
4
  def initialize(app, rpath)
4
5
  @app = app
5
6
  @router_path = rpath
6
- @logger = RAILS_DEFAULT_LOGGER
7
- @params = {}
8
7
  end
9
8
 
10
9
  def call(env)
11
- @logger.info('---------------- PARAMS ---------------------')
12
- #@logger.info(env.to_yaml)
13
-
14
-
15
- @logger.info(env["action_controller.request.request_parameters"].to_yaml)
10
+ if env["PATH_INFO"].match("^"+@router_path)
11
+ output = []
12
+ parse(env["action_controller.request.request_parameters"]).each do |req|
13
+ # have to dup the env for each request
14
+ request_env = env.dup
16
15
 
16
+ # pop poorly-named Ext.Direct params off.
17
+ controller = req.delete("action").downcase
18
+ action = req.delete("method")
17
19
 
18
- if env["PATH_INFO"].match("^"+@router_path)
19
- parse_params(env)
20
+ # set env URI and PATH_INFO for each request
21
+ request_env["PATH_INFO"] = "/#{controller}/#{action}"
22
+ request_env["REQUEST_URI"] = "/#{controller}/#{action}"
20
23
 
21
- @logger.info('env: ' + env.to_yaml)
24
+ # set request params
25
+ request_env["action_controller.request.request_parameters"] = req
22
26
 
23
- output = []
24
- ["company", "users"].each do |c|
25
- env["PATH_INFO"] = "/#{c}/load"
26
- env["REQUEST_URI"] = "/#{c}/load"
27
- status, headers, response = @app.call(env)
27
+ # call the app!
28
+ status, headers, response = @app.call(request_env)
28
29
  output << response.body
29
- @logger.info('---------------------- RESPONSE ----------------------------')
30
- @logger.info(response.body)
31
30
  end
31
+ # join all responses together
32
32
  res = output.join(',')
33
+
34
+ # [wrap in array] if multiple responses. Each controller response will have returned json already
35
+ # so we don't want to re-encode.
33
36
  res = "[" + res + "]" if output.length > 1
34
37
 
35
- #status, headers, response = @app.call(env)
38
+ # return response
36
39
  [200, {"Content-Type" => "text/html"}, res]
37
-
38
40
  else
39
41
  @app.call(env)
40
42
  end
41
43
  end
42
44
 
43
- def parse_params(env)
44
- @params = env["REQUEST_METHOD"] == "GET" ? CGI.parse(env["QUERY_STRING"]) : env["rack.request.form_hash"]
45
- #@params.each do |k,v|
46
- # @params[k] = @params[k].first
47
- #end
48
- #@params["data"] = JSON.parse(@params["data"]) if @params["data"]
49
- #@logger.info('params: ' + @params.to_yaml)
50
- #env["QUERY_STRING"] = {}
51
-
45
+ def parse(data)
46
+ return (data["_json"]) ? data["_json"] : [data]
52
47
  end
53
48
 
54
49
  end
@@ -4,7 +4,7 @@
4
4
  module Rails
5
5
  module ExtJS
6
6
  module Direct
7
- VERSION = '0.0.2'
7
+ VERSION = '0.0.3'
8
8
  end
9
9
  end
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-extjs-direct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
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-04-30 00:00:00 -04:00
12
+ date: 2009-05-01 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -63,7 +63,7 @@ files:
63
63
  - test/test_helper.rb
64
64
  - test/test_rails-extjs-direct.rb
65
65
  has_rdoc: true
66
- homepage: THIS GEM IS UNDER DEVELOPMENT AND WILL NOT YET WORK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
66
+ homepage: THIS GEM IS UNDER DEVELOPMENT AND NOT A FULL IMLPLEMENTATION OF THE Ext.Direct specification
67
67
  post_install_message: PostInstall.txt
68
68
  rdoc_options:
69
69
  - --main