Created
December 29, 2014 15:11
-
-
Save soeffing/78ce88f33d6538edb942 to your computer and use it in GitHub Desktop.
Drying up controllers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Api | |
module V2 | |
class AdsController < ApiController | |
respond_to :json | |
before_filter :return_attributes | |
before_filter :check_for_file_urls, only: [:update, :create] | |
def create | |
@object = ::Ad.new(params[:ad]) | |
if @object.save | |
render json: JsonResponseBuilder.new(@object, @return_attributes, @api_query).render | |
else | |
render 'api/v2/shared/errors.json.jbuilder' | |
end | |
end | |
def show | |
@object = ::Ad.find(params[:id]) | |
render json: JsonResponseBuilder.new(@object, @return_attributes, @api_query).render | |
rescue ActiveRecord::RecordNotFound | |
render 'api/v2/shared/not_found.json.jbuilder' | |
end | |
def index | |
if @api_query.filter && @api_query.order | |
@collection = ::Ad.where(@api_query.filter).order(@api_query.order) | |
elsif @api_query.filter | |
@collection = ::Ad.where(@api_query.filter) | |
elsif @api_query.order | |
@collection = ::Ad.order(@api_query.order) | |
else | |
@collection = ::Ad.all | |
end | |
# the paginate method is from the api-pagination gem and paginates the collection | |
# and return a @pagination_meta hash | |
paginate json: @collection | |
render json: JsonResponseBuilder.new(@collection, @return_attributes, @api_query, @pagination_meta).render_index | |
end | |
def update | |
@object = ::Ad.find(params[:id]) | |
if @object.update_attributes(params[:ad]) | |
render json: JsonResponseBuilder.new(@object, @return_attributes, @api_query).render | |
else | |
render 'api/v2/shared/errors.json.jbuilder' | |
end | |
end | |
def destroy | |
@object = ::Ad.find(params[:id]).destroy | |
render json: JsonResponseBuilder.new(@object, @return_attributes, @api_query).render | |
rescue ActiveRecord::RecordNotFound | |
render 'api/v2/shared/not_found.json.jbuilder' | |
end | |
# This method lays down the attributes a show & index action returns | |
def return_attributes | |
@return_attributes = [ :id, :contest_id, :user_id, :resource_id, :resource_type, :title, | |
:description, :created_at, :updated_at, :score, :views, | |
:state, :permalink, :delta, :muted, :hp_featured, :user_featured, | |
:display, :facebook_upload, :approved, :clearance_video_author, | |
:clearance_video_hire, :clearance_video_licensed, :clearance_audio_author, | |
:clearance_audio_hire, :clearance_audio_licensed, :clearance_stills_author, | |
:clearance_stills_hire, :clearance_stills_licensed, | |
:clearance_audio_not_applicable, :clearance_stills_not_applicable, | |
:clearance_talent_not_applicable, :clearance_talent_release, | |
:clearance_meets_special_age_requirements, :comments_count, :average_rating, | |
:votes_count, :recommended | |
] | |
add_computable_attributes | |
end | |
end | |
end | |
end | |
module Api | |
module V2 | |
class AdRatingsController < ApiController | |
respond_to :json | |
before_filter :return_attributes | |
def create | |
@object = AdRating.new(params[:ad_rating]) | |
if @object.save | |
render json: JsonResponseBuilder.new(@object, @return_attributes, @api_query).render | |
else | |
render 'api/v2/shared/errors.json.jbuilder' | |
end | |
end | |
def show | |
@object = AdRating.find(params[:id]) | |
render json: JsonResponseBuilder.new(@object, @return_attributes, @api_query).render | |
rescue ActiveRecord::RecordNotFound | |
render 'api/v2/shared/not_found.json.jbuilder' | |
end | |
def index | |
if @api_query.filter && @api_query.order | |
@collection = AdRating.where(@api_query.filter).order(@api_query.order) | |
elsif @api_query.filter | |
@collection = AdRating.where(@api_query.filter) | |
elsif @api_query.order | |
@collection = AdRating.order(@api_query.order) | |
else | |
@collection = AdRating.all | |
end | |
# the paginate method is from the api-pagination gem and paginates the collection | |
# and return a @pagination_meta hash | |
paginate json: @collection | |
render json: JsonResponseBuilder.new(@collection, @return_attributes, @api_query, @pagination_meta).render_index | |
end | |
def update | |
@object = AdRating.find(params[:id]) | |
if @object.update_attributes(params[:ad_rating]) | |
render json: JsonResponseBuilder.new(@object, @return_attributes, @api_query).render | |
else | |
render 'api/v2/shared/errors.json.jbuilder' | |
end | |
end | |
def destroy | |
@object = AdRating.find(params[:id]).destroy | |
render json: JsonResponseBuilder.new(@object, @return_attributes, @api_query).render | |
end | |
# This method lays down the attributes a show & index action returns | |
def return_attributes | |
@return_attributes = [ :id, :user_id, :ad_id, :value ] | |
add_computable_attributes | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment