Class: Toast::Association
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
Attributes inherited from Resource
#base_uri, #payload_content_type, #prefered_media_type
Instance Method Summary collapse
- #delete ⇒ Object
- #get ⇒ Object
-
#initialize(model, id, subresource_name, params, format, config, assoc_model, assoc_config_in, assoc_config_out) ⇒ Association
constructor
A new instance of Association.
- #link(link_path_info) ⇒ Object
- #post(payload, media_type) ⇒ Object
- #put(payload, media_type) ⇒ Object
- #unlink(link_path_info) ⇒ Object
Methods inherited from Resource
#apply, build, get_class_by_resource_name
Constructor Details
#initialize(model, id, subresource_name, params, format, config, assoc_model, assoc_config_in, assoc_config_out) ⇒ Association
Returns a new instance of Association.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/toast/association.rb', line 6 def initialize model, id, subresource_name, params, format, config, assoc_model, assoc_config_in, assoc_config_out unless config.exposed_associations.include? subresource_name raise ResourceNotFound end @model = model @record = model.find(id) rescue raise(ResourceNotFound) @assoc = subresource_name @format = format @is_collection = [:has_many, :has_and_belongs_to_many].include? @model.reflect_on_association(@assoc.to_sym).macro @config = config @associate_model = assoc_model @associate_config_in = assoc_config_in @associate_config_out = assoc_config_out @params = params end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
4 5 6 |
# File 'lib/toast/association.rb', line 4 def model @model end |
Instance Method Details
#delete ⇒ Object
143 144 145 |
# File 'lib/toast/association.rb', line 143 def delete raise MethodNotAllowed end |
#get ⇒ Object
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 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/toast/association.rb', line 24 def get unless @record.class.reflect_on_all_associations.detect{|a| a.name.to_s == @assoc} raise "Toast Error: Association '#{@assoc}' not found in model '#{@record.class}'" end reflection = @model.reflect_on_association(@assoc.to_sym) if reflection.collection? if(@config.pass_params_to.include?(@assoc) and reflection.[:extend] and reflection.[:extend].detect{|e| e.method_defined? :find_by_params} ) result, pagination_info = paginate_query( @config, @assoc, @record.send(@assoc).find_by_params(@params), @params) else result, pagination_info = paginate_query( @config, @assoc, @record.send(@assoc), @params) end raise ResourceNotFound if result.nil? { :json => result.map{|r| r.represent( @associate_config_out.in_collection.exposed_attributes, @associate_config_out.in_collection.exposed_associations, @base_uri, @associate_config_out.media_type ) }, :status => :ok, :content_type => @associate_config_out.in_collection.media_type, :pagination_info => pagination_info } else result = if(@config.pass_params_to.include?(@assoc) and reflection.[:extend] and reflectin.[:extend].detect{|e| e.method_defined? :find_by_params} ) @record.send(@assoc).find_by_params(@params) else @record.send(@assoc) end raise ResourceNotFound if result.nil? { :json => result.represent( @associate_config_out.exposed_attributes, @associate_config_out.exposed_associations, @base_uri, @associate_config_out.media_type), :status => :ok, :content_type => @associate_config_out.media_type } end rescue ActiveRecord::RecordNotFound raise ResourceNotFound end |
#link(link_path_info) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/toast/association.rb', line 147 def link link_path_info slash, link_resource_name, link_id = link_path_info.split('/') link_model = Resource.get_class_by_resource_name(link_resource_name) link_record = link_model.find(link_id) reflection = @model.reflect_on_association(@assoc.to_sym) if(reflection.collection? ) # has_many, hbtm if( @config.pass_params_to.include?(@assoc) and reflection.[:extend] and reflection.[:extend].detect{|e| e.method_defined? :<<}) @record.send(@assoc).<<(link_record,@params) else @record.send(@assoc) << link_record end else # has_one, belongs_to @record.send(@assoc+"=",link_record) @record.save end { :nothing => true, :status => :no_content } rescue ActiveRecord::AssociationTypeMismatch raise Toast::ResourceNotAcceptable rescue ActiveRecord::RecordNotFound raise Toast::ResourceNotFound rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, ActiveRecord::RecordNotUnique => e # model validation failed (join model used for linking) raise PayloadInvalid.new(e.) end |
#post(payload, media_type) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/toast/association.rb', line 94 def post payload, media_type raise MethodNotAllowed unless @config.writables.include? @assoc if media_type != @associate_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 payload.delete_if {|key,value| !@associate_config_in.writables.include?(key) or @associate_config_in.exposed_associations.include?(key) } begin reflection = @model.reflect_on_association(@assoc.to_sym) if(@config.pass_params_to.include?(@assoc) and reflection.[:extend] and reflection.[:extend].detect{|e| e.method_defined? :create!}) record = @record.send(@assoc).create! payload, @params else record = @record.send(@assoc).create! payload end { :json => record.represent( @associate_config_out.exposed_attributes, @associate_config_out.exposed_associations, @base_uri, @associate_config_out.media_type), :location => self.base_uri + record.uri_path, :status => :created, :content_type => @associate_config_out.media_type } rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, ActiveRecord::RecordNotUnique => e # model validation failed raise PayloadInvalid.new(e.) end end |
#put(payload, media_type) ⇒ Object
90 91 92 |
# File 'lib/toast/association.rb', line 90 def put payload, media_type raise MethodNotAllowed end |
#unlink(link_path_info) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/toast/association.rb', line 184 def unlink link_path_info slash, link_resource_name, link_id = link_path_info.split('/') link_model = Resource.get_class_by_resource_name(link_resource_name) link_record = link_model.find(link_id) reflection = @model.reflect_on_association(@assoc.to_sym) if @model.reflect_on_association(@assoc.to_sym).collection? # has_many, hbtm if( @config.pass_params_to.include?(@assoc) and reflection.[:extend] and reflection.[:extend].detect{|e| e.method_defined? :delete}) @record.send(@assoc).delete(link_record,@params) else @record.send(@assoc).delete(link_record) unless link_record.nil? end else # has_one, belongs_to if @record.send(@assoc) == link_record @record.send(@assoc+"=",nil) @record.save! end end { :nothing => true, :status => :no_content } rescue ActiveRecord::RecordNotFound raise ResourceNotFound end |