Sha256: 3746ee8ee95a1c8b96e1744de7c2d559d63d15be67c716699268f4b36d6b77fa
Contents?: true
Size: 1.96 KB
Versions: 1
Compression:
Stored size: 1.96 KB
Contents
module MakeRestful::Loaders private def load_resource @resource = self.class.resource_class @resource.params = params.except(:action, :controller, :id) if @resource.respond_to?(:params=) end def load_collection @collection = if self.class.pagination && !params.has_key?(:all) paginated_collection = @resource.paginate page: params[:page], per_page: (params[:per_page] || self.class.pagination[:per_page] || 25) { :"#{self.class.resource_name}" => collection, pagination: { current_page: paginated_collection.current_page, per_page: paginated_collection.per_page, total_entries: paginated_collection.total_entries, total_pages: paginated_collection.total_pages } } else @resource.all end if @collection.empty? @response = { error: 'no results found' } @status = :not_found end eval("@#{self.class.resource_name} = @collection") end def load_instance(opts={}) finder = opts[:finder] || self.class.finder finder_value = params[finder] if finder && @resource.respond_to?(:find) @instance = @resource.where(finder.to_sym => finder_value).first eval "@#{self.class.instance_name} = @instance" end unless @instance @response = { error: "Could not find record with #{finder}: #{finder_value}" } @status = :not_found render_formats(load_template: false) and return end end def verify_method! return if allow_method? params[:action] @response = { error: "Method not allowed"} @status = :method_not_allowed render end alias :verify_action! :verify_method! def method_missing(m, *args, &block) if /load_instance_by_(?<finder>[a-z_]+)/ =~ m.to_s load_instance(finder: finder.to_sym) else super end end def respond_to_missing?(method_name, include_private = false) true if /load_instance_by_(?<finder>[a-z_]+)/ =~ method_name.to_s end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
make_restful-0.1.2 | lib/make_restful/loaders.rb |