Skip to content
Snippets Groups Projects
Commit a9f1b5a2 authored by Mauricio Giacomini Girardello's avatar Mauricio Giacomini Girardello
Browse files

move resource_model method for ResourceModel concern to improve code reuse

parent 0db5f434
No related branches found
No related tags found
No related merge requests found
module ResourceModel
extend ActiveSupport::Concern
protected
def resource_model
resource, id = request.path.split('/')[2, 3]
resource_model = resource.singularize.classify.constantize
resource_model.find(id)
end
end
class V1::ActivitiesController < ApplicationController class V1::ActivitiesController < ApplicationController
before_action :authenticate_user!, only: [:my_activities] include ::ResourceModel
before_action :authenticate_user!
# GET v1/activities # GET v1/activities
# GET v1/activities.json # GET v1/activities.json
...@@ -15,12 +16,6 @@ class V1::ActivitiesController < ApplicationController ...@@ -15,12 +16,6 @@ class V1::ActivitiesController < ApplicationController
private private
def resource_model
resource, id = request.path.split('/')[2, 3]
resource_model = resource.singularize.classify.constantize
resource_model.find(id)
end
def activities(object) def activities(object)
condition = '(owner_type = :type AND owner_id = :id) OR (recipient_type = :type AND recipient_id = :id)' condition = '(owner_type = :type AND owner_id = :id) OR (recipient_type = :type AND recipient_id = :id)'
PublicActivity::Activity.order('created_at DESC').where(condition, {type: object.class.to_s, id: object.id}).all PublicActivity::Activity.order('created_at DESC').where(condition, {type: object.class.to_s, id: object.id}).all
......
class V1::VersionsController < ApplicationController class V1::VersionsController < ApplicationController
include ::ResourceModel
# GET v1/learning_objects/1/versions # GET v1/learning_objects/1/versions
# GET v1/learning_objects/1/versions.json # GET v1/learning_objects/1/versions.json
...@@ -13,6 +14,7 @@ class V1::VersionsController < ApplicationController ...@@ -13,6 +14,7 @@ class V1::VersionsController < ApplicationController
render json: resource render json: resource
end end
# POST /learning_objects/1/versions/234/checkout
def checkout def checkout
resource = PaperTrail::Version.find(params[:id]).reify resource = PaperTrail::Version.find(params[:id]).reify
if resource.save if resource.save
...@@ -22,12 +24,4 @@ class V1::VersionsController < ApplicationController ...@@ -22,12 +24,4 @@ class V1::VersionsController < ApplicationController
end end
end end
private
def resource_model
resource, id = request.path.split('/')[2, 3]
resource_model = resource.singularize.classify.constantize
resource_model.find(id)
end
end end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment