Class: Toast::Record

Inherits:
Resource show all
Defined in:
lib/toast/record.rb

Instance Attribute Summary collapse

Attributes inherited from Resource

#base_uri, #payload_content_type, #prefered_media_type

Instance Method Summary collapse

Methods inherited from Resource

#apply, build, get_class_by_resource_name

Constructor Details

#initialize(model, id, format, params, config_in, config_out) ⇒ Record

Returns a new instance of Record.



6
7
8
9
10
11
12
13
# File 'lib/toast/record.rb', line 6

def initialize model, id, format, params, config_in, config_out
  @model = model
  @record = model.find(id) rescue raise(ResourceNotFound.new)
  @format = format
  @config_in = config_in
  @config_out = config_out
  @params = params
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



4
5
6
# File 'lib/toast/record.rb', line 4

def model
  @model
end

Instance Method Details

#deleteObject

Raises:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/toast/record.rb', line 98

def delete
  raise MethodNotAllowed unless @config_out.deletable?

  if @record.destroy
    {
      :nothing => true,
      :status => :no_content
    }
  else
    {
      :nothing => true,
      :status => :conflict
    }
  end

end

#getObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/toast/record.rb', line 77

def get
  case @format
  when "html", "xml"
    {
      :template => "resources/#{model.to_s.underscore}",
      :locals => { model.to_s.underscore.to_sym => @record } # full record, view should filter
    }
  when "json"
    {
      :json => @record.represent( @config_out.exposed_attributes,
                                  @config_out.exposed_associations,
                                  @base_uri,
                                  @config_out.media_type),
      :status => :ok,
      :content_type => @config_out.media_type
    }
  else
    raise ResourceNotFound
  end
end

Raises:



115
116
117
# File 'lib/toast/record.rb', line 115

def link l
  raise MethodNotAllowed
end

#post(payload, media_type) ⇒ Object

Raises:



15
16
17
# File 'lib/toast/record.rb', line 15

def post payload, media_type
  raise MethodNotAllowed
end

#put(payload, media_type) ⇒ Object

get, put, delete, post return a Hash that can be used as argument for ActionController#render



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/toast/record.rb', line 22

def put payload, media_type

  if media_type != @config_in.media_type
    raise UnsupportedMediaType
  end

  begin
    payload = ActiveSupport::JSON.decode(payload)
  rescue
    raise PayloadFormatError
  end

  unless payload.is_a? Hash
    raise PayloadFormatError
  end

  # ignore all not explicitly writable attribute
  payload.delete_if {|key,value|
    !@config_in.writables.include?(key) or
    @config_in.exposed_associations.include?(key)
  }

  # set the virtual attributes
  (@config_in.writables - @record.attribute_names -  @config_in.exposed_associations).each do |vattr|

    unless (@record.respond_to?("#{vattr}=") && @record.method("#{vattr}=").arity == 1)
      raise "Toast Error: Connot find setter '#{@record.class}##{vattr}='"
    end
    # FIXME: omitted virtual attributes are set to nil, omiited non-virtual attributes are ignored
    @record.send("#{vattr}=", payload.delete(vattr))
  end

  # mass-update for the rest
  if @config_in.pass_params_to.include? 'self'
    # parameter passing
    @record.update_attributes! payload, @params
  else
    @record.update_attributes! payload
  end

  {
    :json => @record.represent( @config_out.exposed_attributes,
                                @config_out.exposed_associations,
                                @base_uri,
                                @config_out.media_type ),
    :status => :ok,
    :location => self.base_uri + @record.uri_path,
    :content_type => @config_out.media_type
  }

rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, ActiveRecord::RecordNotUnique => e
  # model validation failed
  raise PayloadInvalid.new(e.message)
end

Raises:



119
120
121
# File 'lib/toast/record.rb', line 119

def unlink l
  raise MethodNotAllowed
end