toast 0.5.0 → 0.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,6 @@
1
1
  module Toast
2
2
  class Association < Resource
3
-
3
+
4
4
  attr_reader :model
5
5
 
6
6
  def initialize model, id, subresource_name, format
@@ -16,13 +16,13 @@ module Toast
16
16
 
17
17
  @associate_model = Resource.get_class_by_resource_name subresource_name
18
18
  @associate_model.uri_base = @model.uri_base
19
-
19
+
20
20
  end
21
21
 
22
22
  def get
23
23
  result = @record.send(@assoc)
24
24
 
25
- if result.is_a? Array
25
+ if result.is_a? Array
26
26
  {
27
27
  :json => result.map{|r|
28
28
  r.exposed_attributes(:in_collection => true).
@@ -41,7 +41,7 @@ module Toast
41
41
  end
42
42
 
43
43
  def put payload
44
- raise MethodNotAllowed
44
+ raise MethodNotAllowed
45
45
  end
46
46
 
47
47
  def post payload
@@ -51,7 +51,7 @@ module Toast
51
51
  raise UnsupportedMediaType
52
52
  end
53
53
 
54
- begin
54
+ begin
55
55
  payload = ActiveSupport::JSON.decode(payload)
56
56
  rescue
57
57
  raise PayloadFormatError
@@ -59,7 +59,7 @@ module Toast
59
59
 
60
60
  unless payload.is_a? Hash
61
61
  raise PayloadFormatError
62
- end
62
+ end
63
63
 
64
64
 
65
65
  # silently ignore all exposed readable, but not writable fields
@@ -67,16 +67,16 @@ module Toast
67
67
  payload.delete(rof)
68
68
  end
69
69
 
70
-
70
+
71
71
  begin
72
72
  record = @record.send(@assoc).create! payload
73
-
73
+
74
74
  {
75
75
  :json => record.exposed_attributes.merge( uri_fields(record) ),
76
76
  :location => self.base_uri + record.uri_fullpath,
77
77
  :status => :created
78
78
  }
79
-
79
+
80
80
  rescue ActiveRecord::RecordInvalid => e
81
81
  # model validation failed
82
82
  raise PayloadInvalid.new(e.message)
@@ -1,12 +1,12 @@
1
1
  module Toast
2
- class RootCollection < Resource
3
-
2
+ class Collection < Resource
3
+
4
4
  attr_reader :model
5
-
5
+
6
6
  def initialize model, subresource_name, params
7
-
7
+
8
8
  subresource_name ||= "all"
9
-
9
+
10
10
  unless model.toast_config.collections.include? subresource_name
11
11
  raise ResourceNotFound
12
12
  end
@@ -18,8 +18,8 @@ module Toast
18
18
  end
19
19
 
20
20
  def get
21
-
22
- records = if @model.toast_config.pass_params_to.include?(@collection)
21
+
22
+ records = if @model.toast_config.pass_params_to.include?(@collection)
23
23
  @model.send(@collection, @params)
24
24
  else
25
25
  @model.send(@collection)
@@ -29,9 +29,9 @@ module Toast
29
29
  when "html"
30
30
  {
31
31
  :template => "resources/#{model.to_s.pluralize.underscore}",
32
- :locals => { model.to_s.pluralize.underscore.to_sym => records }
32
+ :locals => { model.to_s.pluralize.underscore.to_sym => records }
33
33
  }
34
- when "json"
34
+ when "json"
35
35
  {
36
36
  :json => records.map{|r|
37
37
  r.exposed_attributes(:in_collection => true).
@@ -59,7 +59,7 @@ module Toast
59
59
  raise UnsupportedMediaType
60
60
  end
61
61
 
62
- begin
62
+ begin
63
63
  payload = ActiveSupport::JSON.decode(payload)
64
64
  rescue
65
65
  raise PayloadFormatError
@@ -72,16 +72,16 @@ module Toast
72
72
  (@model.toast_config.readables - @model.toast_config.writables + ["uri"]).each do |rof|
73
73
  payload.delete(rof)
74
74
  end
75
-
75
+
76
76
  begin
77
- record = @model.create! payload
77
+ record = @model.create! payload
78
78
 
79
79
  {
80
80
  :json => record.exposed_attributes.merge( uri_fields(record) ),
81
81
  :location => self.base_uri + record.uri_fullpath,
82
82
  :status => :created
83
83
  }
84
-
84
+
85
85
  rescue ActiveRecord::RecordInvalid => e
86
86
  # model validation failed
87
87
  raise PayloadInvalid.new(e.message)
@@ -1,6 +1,6 @@
1
1
  require 'toast/active_record_extensions.rb'
2
2
  require 'toast/resource.rb'
3
- require 'toast/root_collection'
3
+ require 'toast/collection'
4
4
  require 'toast/association'
5
5
  require 'toast/record'
6
6
  require 'toast/single'
@@ -1,10 +1,10 @@
1
1
  module Toast
2
2
  class Record < Resource
3
3
 
4
- attr_reader :model
5
-
4
+ attr_reader :model
5
+
6
6
  def initialize model, id, format
7
- @model = model
7
+ @model = model
8
8
  @record = model.find(id) rescue raise(ResourceNotFound.new)
9
9
  @format = format
10
10
  end
@@ -16,18 +16,18 @@ module Toast
16
16
  # get, put, delete, post return a Hash that can be used as
17
17
  # argument for ActionController#render
18
18
 
19
- def put payload
20
-
19
+ def put payload
20
+
21
21
  if self.media_type != @model.toast_config.media_type
22
22
  raise UnsupportedMediaType
23
23
  end
24
24
 
25
- begin
25
+ begin
26
26
  payload = ActiveSupport::JSON.decode(payload)
27
27
  rescue
28
28
  raise PayloadFormatError
29
29
  end
30
-
30
+
31
31
  unless payload.is_a? Hash
32
32
  raise PayloadFormatError
33
33
  end
@@ -37,14 +37,14 @@ module Toast
37
37
  payload.delete(rof)
38
38
  end
39
39
 
40
- # set the virtual attributes
40
+ # set the virtual attributes
41
41
  (payload.keys.to_set - @record.attribute_names.to_set).each do |vattr|
42
- @record.send("#{vattr}=", payload.delete(vattr))
43
- end
44
-
45
- # mass-update for the rest
42
+ @record.send("#{vattr}=", payload.delete(vattr))
43
+ end
44
+
45
+ # mass-update for the rest
46
46
  @record.update_attributes payload
47
- {
47
+ {
48
48
  :json => @record.exposed_attributes.merge( uri_fields(@record) ),
49
49
  :status => :ok,
50
50
  :location => self.base_uri + @record.uri_fullpath
@@ -58,12 +58,12 @@ module Toast
58
58
  :template => "resources/#{model.to_s.underscore}",
59
59
  :locals => { model.to_s.underscore.to_sym => @record } # full record, view should filter
60
60
  }
61
- when "json"
61
+ when "json"
62
62
  {
63
- :json => @record.exposed_attributes.merge( uri_fields(@record) ),
63
+ :json => @record.exposed_attributes.merge( uri_fields(@record) ),
64
64
  :status => :ok
65
65
  }
66
- else
66
+ else
67
67
  raise ResourceNotFound
68
68
  end
69
69
  end
@@ -77,6 +77,6 @@ module Toast
77
77
  :status => :ok
78
78
  }
79
79
  end
80
-
80
+
81
81
  end
82
82
  end
@@ -7,7 +7,7 @@ module Toast
7
7
  class UnsupportedMediaType < Exception; end
8
8
 
9
9
  # Represents a resource. There are following resource types as sub classes:
10
- # Record, RootCollection, Association, Single
10
+ # Record, Collection, Association, Single
11
11
  class Resource
12
12
 
13
13
  attr_accessor :media_type, :base_uri
@@ -30,7 +30,7 @@ module Toast
30
30
  rsc = if id.nil? and model.toast_config.singles.include?(subresource_name)
31
31
  Toast::Single.new(model, subresource_name, params.clone)
32
32
  elsif id.nil?
33
- Toast::RootCollection.new(model, subresource_name, params.clone)
33
+ Toast::Collection.new(model, subresource_name, params.clone)
34
34
  elsif subresource_name.nil?
35
35
  Toast::Record.new(model, id, format)
36
36
  elsif model.toast_config.exposed_associations.include? subresource_name
@@ -40,7 +40,7 @@ module Toast
40
40
  end
41
41
 
42
42
  rsc.media_type = request.media_type
43
- rsc.base_uri = request.base_url
43
+ rsc.base_uri = request.base_url + request.script_name
44
44
 
45
45
  rsc
46
46
  rescue NameError
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 5
8
- - 0
9
- version: 0.5.0
8
+ - 2
9
+ version: 0.5.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Robert Annies
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2012-03-20 00:00:00 +01:00
17
+ date: 2012-03-21 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -48,11 +48,11 @@ files:
48
48
  - lib/toast.rb
49
49
  - lib/toast/active_record_extensions.rb
50
50
  - lib/toast/association.rb
51
+ - lib/toast/collection.rb
51
52
  - lib/toast/config_dsl.rb
52
53
  - lib/toast/engine.rb
53
54
  - lib/toast/record.rb
54
55
  - lib/toast/resource.rb
55
- - lib/toast/root_collection.rb
56
56
  - lib/toast/single.rb
57
57
  - README.md
58
58
  has_rdoc: true