ooorest 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [Raphaël Valyi]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,68 @@
1
+ OOOREST - OpenObject On Rails, REST layer
2
+ ====
3
+
4
+ <table>
5
+ <tr>
6
+ <td><a href="http://github.com/rvalyi/ooor" title="OOOR - OpenObject On Rails"><img src="http://sites.google.com/site/assetserversite/_/rsrc/1257126460164/home/ooor_s.jpg" width="159px" height="124px" /></a></td>
7
+ <td><b>BY</b></td>
8
+ <td><a href="http://www.akretion.com" title="Akretion - open source to spin the world"><img src="http://sites.google.com/site/assetserversite/_/rsrc/1257126470309/home/akretion_s.png" width="228px" height="124px" /></a></td>
9
+ <td>
10
+ OOOR stands for OpenObject On Rails. OpenObject is the RAD framework behind OpenERP,
11
+ the ERP that doesn't hurt, just like Rails is "web development that doesn't hurt".
12
+ So OOOR exposes seamlessly your OpenOpbject application, to your custom Rails application.
13
+ Needless to say, OOOR doubly doesn't hurt.
14
+ Furthermore, OOOR only depends on the "activeresource" gem. So it can even be used
15
+ in any (J)Ruby application without Rails.
16
+ </td>
17
+ </tr>
18
+ </table>
19
+
20
+ OooREST enables REST HTTP routes to your OpenERP models:
21
+ Before anything, make sure you read the doc of [OOOR](http://github.com/rvalyi/ooor)
22
+ Install the gem using gem install ooorest.
23
+
24
+ we assume you created a working Rails application, in your config/environment.rb
25
+ Inside the Rails::Initializer.run do |config| statement, paste the following gem dependency (along with the ooor dependency):
26
+
27
+ $ config.gem "ooorest"
28
+
29
+ in your config/route.rb, you can alternatively enable routes to all your OpenERP models by addding:
30
+ $ OOOREST.load_all_controllers(map)
31
+
32
+ Or only enable the route to some specific model instead (here partners):
33
+ $ map.resources :res_partner
34
+
35
+
36
+ REST HTTP API:
37
+
38
+ $ http://localhost:3000/res_partner
39
+ $ http://localhost:3000/res_partner.xml
40
+ $ http://localhost:3000/res_partner.json
41
+ $ http://localhost:3000/res_partner/2
42
+ $ http://localhost:3000/res_partner/2.json
43
+ $ http://localhost:3000/res_partner/2.xml
44
+ $ http://localhost:3000/res_partner/[2,3,4].xml
45
+ $ http://localhost:3000/res_partner/[2,3,4].json
46
+
47
+ $ TODO http://localhost:3000/res_partner.xml?active=1
48
+
49
+ $ TODO http://localhost:3000/res_partner.xml?domain=TODO
50
+
51
+
52
+ FAQ
53
+ ------------
54
+
55
+
56
+ ### Can I extend the OOOR controllers?
57
+
58
+ Yes, you can do that, see the "how to extends models" section as it's very similar. Instead, in the app/controllers directory, you'll re-open defined controllers,
59
+ for the product.product controllers, it means creating a product_product_controller.rb file with:
60
+
61
+ $ class ProductProduct < OpenObjectsController
62
+ $ def foo
63
+ $ render :text => "bar"
64
+ $ end
65
+ $ end
66
+
67
+ Now, if you register that new method in your route.rb file GET /product_product/1/foo will render "bar" on your browser screen.
68
+ You could instead just customize the existing CRUD methods so you don't need to regiter any other route in route.rb.
@@ -0,0 +1,123 @@
1
+ require 'action_controller'
2
+
3
+ class OpenObjectsController < ActionController::Base
4
+
5
+ #TODO get lang from URL in before filter
6
+ #TODO use timezone from HTTP header and put is in context
7
+
8
+ # ******************** class methods ********************
9
+ class << self
10
+
11
+ cattr_accessor :logger
12
+ attr_accessor :model_class
13
+
14
+ def model_class
15
+ if defined?(@model_class)
16
+ @model_class
17
+ elsif superclass != Object && superclass.model_class
18
+ superclass.model_class.dup.freeze
19
+ end
20
+ end
21
+
22
+ def model_class=(_model_class)
23
+ @model_class = _model_class
24
+ end
25
+
26
+ def ids_from_param(param)
27
+ if param.split(',').size > 0
28
+ return eval param
29
+ else
30
+ return param
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+
37
+ # ******************** instance methods ********************
38
+
39
+ # GET /models
40
+ # GET /models.xml
41
+ def index
42
+ @models = self.class.model_class.find(:all)
43
+
44
+ respond_to do |format|
45
+ format.html # index.html.erb
46
+ format.xml { render :xml => @models }
47
+ end
48
+ end
49
+
50
+ # GET /models/1
51
+ # GET /models/1.xml
52
+ def show
53
+ @models = self.class.model_class.find(self.class.ids_from_param(params[:id]))
54
+
55
+ respond_to do |format|
56
+ format.html # show.html.erb
57
+ format.xml { render :xml => @models }
58
+ format.json { render :json => @models }
59
+ end
60
+ end
61
+
62
+ # GET /models/new
63
+ # GET /models/new.xml
64
+ def new
65
+ @models = self.class.model_class.new
66
+
67
+ respond_to do |format|
68
+ format.html # new.html.erb
69
+ format.xml { render :xml => @models }
70
+ end
71
+ end
72
+
73
+ # GET /models/1/edit
74
+ def edit
75
+ @models = self.class.model_class.find(params[:id])
76
+ end
77
+
78
+ # POST /models
79
+ # POST /models.xml
80
+ def create
81
+ @models = self.class.model_class.new(params[:partners])
82
+
83
+ respond_to do |format|
84
+ if @models.save
85
+ flash[:notice] = 'Model was successfully created.'
86
+ format.html { redirect_to(@models) }
87
+ format.xml { render :xml => @models, :status => :created, :location => @models }
88
+ else
89
+ format.html { render :action => "new" }
90
+ format.xml { render :xml => @models.errors, :status => :unprocessable_entity }
91
+ end
92
+ end
93
+ end
94
+
95
+ # PUT /models/1
96
+ # PUT /models/1.xml
97
+ def update
98
+ @models = self.class.model_class.find(params[:id])
99
+
100
+ respond_to do |format|
101
+ if @models.update_attributes(params[:partners])
102
+ flash[:notice] = 'Partners was successfully updated.'
103
+ format.html { redirect_to(@models) }
104
+ format.xml { head :ok }
105
+ else
106
+ format.html { render :action => "edit" }
107
+ format.xml { render :xml => @models.errors, :status => :unprocessable_entity }
108
+ end
109
+ end
110
+ end
111
+
112
+ # DELETE /models/1
113
+ # DELETE /models/1.xml
114
+ def destroy
115
+ @models = self.class.model_class.find(params[:id])
116
+ @models.destroy
117
+
118
+ respond_to do |format|
119
+ format.html { redirect_to(url_for(:action => index)) }
120
+ format.xml { head :ok }
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,33 @@
1
+ require 'app/controllers/open_objects_controller'
2
+
3
+ class OooREST
4
+
5
+ attr_accessor :ooor
6
+
7
+ def initialize(ooor)
8
+ @ooor = ooor
9
+ models = @ooor.config[:models] && @ooor.all_loaded_models.select {|model| ooor.config[:models].index(model.openerp_model)} || @ooor.all_loaded_models
10
+ OpenObjectsController.logger = @ooor.logger
11
+ models.each {|model| define_openerp_controller(model)}
12
+ end
13
+
14
+ def define_openerp_controller(model_class)
15
+ controller_class_name = model_class.to_s + "Controller"
16
+ OpenObjectsController.logger.info "registering #{controller_class_name} as a Rails ActiveResource Controller wrapper for OpenObject #{model_class} model"
17
+ klass = Class.new(OpenObjectsController)
18
+ klass.model_class = model_class
19
+ Object.const_set(controller_class_name, klass)
20
+ klass
21
+ end
22
+
23
+ def load_all_controllers(map)
24
+ @ooor.all_loaded_models.each do |model|
25
+ map.resources model.openerp_model.gsub('.', '_').to_sym
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+ if defined?(Rails) && defined?(OOOR)
32
+ OOOREST = OooREST.new(OOOR)
33
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ooorest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Raphael Valyi - www.akretion.com
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-19 00:00:00 -02:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ooor
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.2.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: actionpack
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.1
34
+ version:
35
+ description: OOOREST is a thin layer over the OOOR Model layer, an ActiveResource proxy for OpenERP models. OOOREST depends on actionpack and OOOR, thus OpenERP models Restfully over HTTP, using the Rails formats.
36
+ email: rvalyi@akretion.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - README.md
45
+ - MIT-LICENSE
46
+ - lib/ooorest.rb
47
+ - lib/app/controllers/open_objects_controller.rb
48
+ has_rdoc: true
49
+ homepage: http://github.com/rvalyi/ooor
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: "0"
68
+ version:
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.3.5
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: OOOR - OpenObject On Rails
76
+ test_files: []
77
+