bulk_api 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -9,9 +9,19 @@ to use that.
9
9
 
10
10
  Bulk Rails API plugin makes integrating Sproutcore applications with Rails applications dead simple. It handles all the communication and allows to take advantage of bulk operations, which can make your application much faster. To use that plugin you will also need BulkDataSource, which will handle Sproutcore side of communcation.
11
11
 
12
- ## Installing:
12
+ ## Example
13
13
 
14
- ### Rails app:
14
+ https://github.com/drnic/todos-bulk-api-demo
15
+
16
+ ## Installing
17
+
18
+ After following this guide you will have a setup like this:
19
+
20
+ * `$RAILS_ROOT/app/sproutcore` is the path where your sproutcore application lives
21
+ * `/api/bulk` is routed to serve the API
22
+ * `/_sproutcore` is routed to serve your sproutcore application
23
+
24
+ ### Rails
15
25
 
16
26
  Add this line to Gemfile and run bundle install:
17
27
 
@@ -25,20 +35,37 @@ To set up Bulk API in your Rails app:
25
35
  rails generate bulk:install
26
36
  ```
27
37
 
28
- Now you need to configure it in your application. First thing to do is to use it with your store:
38
+ ### Sproutcore
39
+
40
+ Now you need to configure your application. Install `BulkDataSource`:
41
+
42
+ ```
43
+ cd <sproutcore_root>
44
+ mkdir frameworks # In case it does not exist yet
45
+ git clone https://github.com/drogus/bulk_data_source.git frameworks/bulk_data_source
46
+ ```
47
+
48
+ Change your `Buildfile` to include it:
49
+
50
+ ```ruby
51
+ config :all, :required => [:sproutcore, "sproutcore/core_foundation", "bulk_data_source"]
52
+ ```
53
+
54
+ Configure your store:
29
55
 
30
56
  ```javascript
31
57
  YourApp = SC.Application.create({
32
- store: SC.Store.create().from('SC.BulkDataSource')
58
+ store: SC.Store.create().from('BulkApi.BulkDataSource')
33
59
  });
34
60
  ```
35
61
 
36
- The last thing that you need to do is to set resource names for your models. BulkDataSource assumes that you have resourceName attribute set on your record. If you don't have such attributes, you can add it like that:
62
+ The last thing that you need to do is to set resource names for your models. `BulkDataSource` assumes that you have resourceName attribute set on your model. If you don't have such attributes, you can add it like that:
37
63
 
38
64
  ```javascript
39
65
  Todos.Todo = SC.Record.extend({
40
- resourceName: 'todo'
41
- })
66
+ // your code here
67
+ });
68
+ Todos.Todo.resourceName = 'todo';
42
69
  ```
43
70
 
44
71
  ## Usage
@@ -268,7 +295,7 @@ While overriding records you can use super to handle the actions with default be
268
295
  Bulk::Collection is a container for records and is used to construct response from. It has a few handy methods to easily modify collection, for more please refer to documantation.
269
296
 
270
297
  ```ruby
271
- collection = Bulk::Colection.new
298
+ collection = Bulk::Collection.new
272
299
  collection.set(1, record) # add record with identifier '1', identifier is then used while constructing response
273
300
  # most of the time it's id or _local_id (the latter one is mainly for create)
274
301
 
@@ -297,11 +324,11 @@ The point of using bulk API is to cut the requests number. Because of its nature
297
324
  POST /bulk/api
298
325
  {
299
326
  'todos': [
300
- {'title': "First todo", 'done': false, '_storeKey': '3'},
301
- {'title': "Second todo", 'done': true, '_storeKey': '10'}
327
+ {'title': "First todo", 'done': false, '_local_id': '3'},
328
+ {'title': "Second todo", 'done': true, '_local_id': '10'}
302
329
  ],
303
330
  'projects': [
304
- {'name': "Sproutcore todolist", '_storeKey': '12'}
331
+ {'name': "Sproutcore todolist", '_local_id': '12'}
305
332
  ]
306
333
  }
307
334
  ```
@@ -311,10 +338,10 @@ As you can see we POST some new items to our application. Rails application will
311
338
  ```
312
339
  {
313
340
  'todos': [
314
- {'id': 1, 'title': "First todo", 'done': false, '_storeKey': '3'}
341
+ {'id': 1, 'title': "First todo", 'done': false, '_local_id': '3'}
315
342
  ],
316
343
  'projects': [
317
- {'id': 1, 'name': "Sproutcore todolist", '_storeKey': '12'}
344
+ {'id': 1, 'name': "Sproutcore todolist", '_local_id': '12'}
318
345
  ]
319
346
  'errors': {
320
347
  'todos': {
@@ -36,7 +36,12 @@ module Bulk
36
36
  each do |id, record|
37
37
  next if errors.get(id)
38
38
  response[name] ||= []
39
- response[name] << (only_ids ? record.id : record.as_json(options[:as_json_options]) )
39
+ response[name] << if only_ids
40
+ record.id
41
+ else
42
+ opts = options[:as_json_options] ? options[:as_json_options].dup : {}
43
+ record.as_json(opts)
44
+ end
40
45
  end
41
46
 
42
47
  errors.each do |id, error|
data/lib/bulk/engine.rb CHANGED
@@ -7,18 +7,8 @@ module Bulk
7
7
  ActiveRecord::Base.include_root_in_json = false
8
8
  end
9
9
 
10
- initializer "require sproutcore Rack app" do
11
- # fix some failz from sass and haml, TODO: investigate
12
- require 'haml/util'
13
- require 'sass/util'
14
-
15
- # it needs to be done after rails load, otherwise haml freaks out
16
- require 'bulk/sproutcore'
17
- end
18
-
19
10
  initializer "config paths" do |app|
20
11
  app.config.paths.add "app/bulk", :eager_load => true
21
- app.config.paths.add "app/sproutcore"
22
12
  end
23
13
  end
24
14
  end
data/lib/bulk/resource.rb CHANGED
@@ -76,7 +76,8 @@ module Bulk
76
76
  collection = resource_object.send(method, hash)
77
77
  as_json_options = resource_object.send(:as_json_options, resource_object.send(:klass))
78
78
  options = {:only_ids => (method == 'delete'), :as_json_options => as_json_options}
79
- response.deep_merge! collection.to_hash(resource_object.resource_name.to_sym, options)
79
+ hash = resource_object.send(:as_json, collection.to_hash(resource_object.resource_name.to_sym, options))
80
+ response.deep_merge! hash
80
81
  end
81
82
 
82
83
  { :json => response }
@@ -183,6 +184,10 @@ module Bulk
183
184
  {}
184
185
  end
185
186
 
187
+ def as_json(hash)
188
+ hash
189
+ end
190
+
186
191
  private
187
192
  delegate :abstract?, :to => "self.class"
188
193
 
@@ -261,7 +266,7 @@ module Bulk
261
266
 
262
267
  if filter
263
268
  attributes.delete_if do |k, v|
264
- delete_if = filter.include?(k)
269
+ delete_if = filter.include?(k.to_sym)
265
270
  type == :accessible ? !delete_if : delete_if
266
271
  end
267
272
  end
@@ -4,18 +4,13 @@ module Bulk
4
4
 
5
5
  desc <<DESC
6
6
  Description:
7
- Creates initializer with configuration and adds required routes
7
+ Creates initializer with configuration.
8
8
  DESC
9
9
 
10
10
  def self.source_root
11
11
  @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
12
12
  end
13
13
 
14
- def routes_entry
15
- route 'bulk_routes "/api/bulk"'
16
- route 'mount Bulk::Sproutcore.new => "/_sproutcore"'
17
- end
18
-
19
14
  def copy_app_bulk_application_resource
20
15
  template 'app/bulk/application_resource.rb'
21
16
  end
metadata CHANGED
@@ -1,55 +1,39 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: bulk_api
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.9
4
5
  prerelease:
5
- version: 0.0.8
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Piotr Sarnacki
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
-
13
- date: 2011-05-19 00:00:00 Z
14
- dependencies:
15
- - !ruby/object:Gem::Dependency
12
+ date: 2011-09-20 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
16
15
  name: rails
17
- prerelease: false
18
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &2152755020 !ruby/object:Gem::Requirement
19
17
  none: false
20
- requirements:
18
+ requirements:
21
19
  - - ~>
22
- - !ruby/object:Gem::Version
23
- version: "3.0"
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
24
22
  type: :runtime
25
- version_requirements: *id001
26
- - !ruby/object:Gem::Dependency
27
- name: sproutcore
28
23
  prerelease: false
29
- requirement: &id002 !ruby/object:Gem::Requirement
30
- none: false
31
- requirements:
32
- - - ~>
33
- - !ruby/object:Gem::Version
34
- version: 1.6.0.rc.1
35
- type: :runtime
36
- version_requirements: *id002
24
+ version_requirements: *2152755020
37
25
  description: Easy integration of rails apps with sproutcore.
38
26
  email:
39
27
  executables: []
40
-
41
28
  extensions: []
42
-
43
29
  extra_rdoc_files: []
44
-
45
- files:
30
+ files:
46
31
  - app/controllers/bulk/api_controller.rb
47
32
  - lib/bulk/abstract_collection.rb
48
33
  - lib/bulk/collection.rb
49
34
  - lib/bulk/engine.rb
50
35
  - lib/bulk/resource.rb
51
36
  - lib/bulk/routes.rb
52
- - lib/bulk/sproutcore.rb
53
37
  - lib/bulk_api.rb
54
38
  - lib/generators/bulk/install/install_generator.rb
55
39
  - lib/generators/bulk/install/templates/app/bulk/application_resource.rb
@@ -62,30 +46,26 @@ files:
62
46
  - README.markdown
63
47
  homepage:
64
48
  licenses: []
65
-
66
49
  post_install_message:
67
50
  rdoc_options: []
68
-
69
- require_paths:
51
+ require_paths:
70
52
  - lib
71
- required_ruby_version: !ruby/object:Gem::Requirement
53
+ required_ruby_version: !ruby/object:Gem::Requirement
72
54
  none: false
73
- requirements:
74
- - - ">="
75
- - !ruby/object:Gem::Version
76
- version: "0"
77
- required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
60
  none: false
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: "0"
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
83
65
  requirements: []
84
-
85
66
  rubyforge_project:
86
- rubygems_version: 1.7.1
67
+ rubygems_version: 1.8.6
87
68
  signing_key:
88
69
  specification_version: 3
89
70
  summary: Easy integration of rails apps with sproutcore.
90
71
  test_files: []
91
-
@@ -1,19 +0,0 @@
1
- require 'sproutcore'
2
- require 'sproutcore/rack/service'
3
- require 'sproutcore/models/project'
4
-
5
- module Bulk
6
- class Sproutcore
7
- def sproutcore
8
- @sproutcore ||= begin
9
- project = SC::Project.load Rails.application.paths["app/sproutcore"].first, :parent => SC.builtin_project
10
- SC::Rack::Service.new(project)
11
- end
12
- end
13
-
14
- def call(env)
15
- env["PATH_INFO"] = "/" if env["PATH_INFO"].blank?
16
- sproutcore.call(env)
17
- end
18
- end
19
- end