Class: Toast::Collection
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, subresource_name, params, config_in, config_out) ⇒ Collection
constructor
A new instance of Collection.
- #link(l) ⇒ Object
- #post(payload, media_type) ⇒ Object
- #put ⇒ Object
- #unlink(l) ⇒ Object
Methods inherited from Resource
#apply, build, get_class_by_resource_name
Constructor Details
#initialize(model, subresource_name, params, config_in, config_out) ⇒ Collection
Returns a new instance of Collection.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/toast/collection.rb', line 6 def initialize model, subresource_name, params, config_in, config_out subresource_name ||= "all" unless config_out.collections.include? subresource_name if subresource_name == "all" ToastController.logger.debug "\n\tToast Debug: you may want to declare 'collections :all' in model '#{model}' to enable delivery of the collection '/#{model.to_s.underscore.pluralize}'\n" end raise ResourceNotFound end @model = model @collection = subresource_name @params = params @format = params[:format] @config_out = config_out @config_in = config_in end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
4 5 6 |
# File 'lib/toast/collection.rb', line 4 def model @model end |
Instance Method Details
#delete ⇒ Object
126 127 128 |
# File 'lib/toast/collection.rb', line 126 def delete raise MethodNotAllowed end |
#get ⇒ Object
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 |
# File 'lib/toast/collection.rb', line 25 def get unless @model.respond_to?(@collection) raise "Toast Error: Cannot find class method '#{@collection}' of model '#{@model}', which is configured in 'acts_as_resource > collections'." end # FIXME: This is a lot of hallooballoo to check if the #send # will be successful, but if it's not checked the error # message is not helpful to find the error. if @config_out.pass_params_to.include?(@collection) if @model.method(@collection).arity**2 != 1 raise "Toast Error: Class method '#{@collection}' of model '#{@model}' must accept one parameter, as configured by 'acts_as_resource > pass_params_to'." end # fetch results #binding.pry if $halt records, pagination_info = paginate_query( @config_out, @collection, @model.send(@collection, @params), @params ) else if @model.method(@collection).arity > 0 raise "Toast Error: Class method '#{@collection}' of model '#{@model}' must not accept any parameter, as configured by 'acts_as_resource'" end records, pagination_info = paginate_query( @config_out, @collection, @model.send(@collection=='all'? 'scoped': @collection), # #scoped ?: #all would trigger query too early @params ) end case @format when "html" { :template => "resources/#{@model.to_s.pluralize.underscore}", :locals => { @model.to_s.pluralize.underscore.to_sym => records, :pagination_info => pagination_info } } when "json" { :json => records.map{|r| r.represent( @config_out.in_collection.exposed_attributes, @config_out.in_collection.exposed_associations, self.base_uri, @config_out.media_type) }, :status => :ok, :content_type => @config_out.in_collection.media_type, :pagination_info => pagination_info } else raise ResourceNotFound end end |
#link(l) ⇒ Object
130 131 132 |
# File 'lib/toast/collection.rb', line 130 def link l raise MethodNotAllowed end |
#post(payload, media_type) ⇒ Object
84 85 86 87 88 89 90 91 92 93 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 |
# File 'lib/toast/collection.rb', line 84 def post payload, media_type raise MethodNotAllowed unless @config_in.postable? 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 payload.delete_if {|key,value| !@config_in.writables.include?(key) or @config_in.exposed_associations.include?(key) } begin record = @model.create! payload { :json => record.represent( @config_out.exposed_attributes, @config_out.exposed_associations, self.base_uri, @config_out.media_type), :location => self.base_uri + record.uri_path, :status => :created, :content_type => @config_out.media_type } rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, ActiveRecord::RecordNotUnique => e # model validation failed raise PayloadInvalid.new(e.) end end |
#unlink(l) ⇒ Object
134 135 136 |
# File 'lib/toast/collection.rb', line 134 def unlink l raise MethodNotAllowed end |