app/controllers/api/resources_controller/base.rb in rails-add_ons-0.2.0 vs app/controllers/api/resources_controller/base.rb in rails-add_ons-0.3.0
- old
+ new
@@ -110,13 +110,13 @@
def add_conditions_from_query(scope)
request.query_parameters.each do |field, condition|
case field
when 'limit'
- scope = scope.limit(condition)
+ scope = scope.limit(condition.to_i)
when 'offset'
- scope = scope.offset(condition)
+ scope = scope.offset(condition.to_i)
when 'order'
scope = scope.order(condition)
when 'includes'
scope = scope.includes(condition.to_sym)
else
@@ -188,11 +188,13 @@
end
private
def load_count
- @count = resource_class.count
+ base_scope = resource_class
+ scope = add_conditions_from_query(base_scope)
+ @count = scope.count
end
end
module DestroyAllAction
extend ActiveSupport::Concern
@@ -239,16 +241,79 @@
def delete_collection
@count = resource_class.delete_all
end
end
+
+ module FirstAction
+ extend ActiveSupport::Concern
+
+ included do
+ if respond_to?(:before_action)
+ before_action :load_first, only: [:first]
+ else
+ before_filter :load_first, only: [:first]
+ end
+ end
+
+ def first
+ respond_to do |format|
+ if @resource.nil?
+ format.json { render json: nil }
+ else
+ format.json { render json: [serialize_resource(@resource)] }
+ end
+ end
+ end
+
+ private
+
+ def load_first
+ base_scope = resource_class
+ scope = add_conditions_from_query(base_scope)
+ @resource = scope.first
+ end
+ end
+
+ module LastAction
+ extend ActiveSupport::Concern
+
+ included do
+ if respond_to?(:before_action)
+ before_action :load_last, only: [:last]
+ else
+ before_filter :load_last, only: [:last]
+ end
+ end
+
+ def last
+ respond_to do |format|
+ if @resource.nil?
+ format.json { render json: nil }
+ else
+ format.json { render json: [serialize_resource(@resource)] }
+ end
+ end
+ end
+
+ private
+
+ def load_last
+ base_scope = resource_class
+ scope = add_conditions_from_query(base_scope)
+ @resource = scope.last
+ end
+ end
+
include RestActions
include Resources
include RestResourceUrls
include Serialization
include CountAction
include DestroyAllAction
include DeleteAllAction
+ include FirstAction
+ include LastAction
include ApiControllerConcerns::ExceptionHandling
end
end
end