abstract finding of object

This commit is contained in:
Tyrel Souza 2014-04-25 11:32:58 -04:00
parent 051bbc9485
commit ac02238d62
3 changed files with 14 additions and 12 deletions

View File

@ -5,6 +5,18 @@ class ApplicationController < ActionController::Base
before_action :authenticate_user_from_token!, except: [:publish]
before_action :authenticate_user!, except: [:publish] #unless Rails.env.test?
def set_by_options(name, field)
what = params[field]
model = name.to_s.classify.constantize
if params[name]
object = model.find_by(field => params[name][field]) || not_found
else
object = model.find_by(field=>what) || not_found
end
end
private

View File

@ -58,12 +58,7 @@ class CampaignsController < ApplicationController
private
def set_campaign
listingcode = params[:listingcode]
if params[:campaign]
@campaign = Campaign.find_by_listingcode(params[:campaign][:listingcode]) || not_found
else
@campaign = Campaign.find_by_listingcode(listingcode) || not_found
end
@campaign = set_by_options(:campaign, :listingcode)
end
def campaign_params

View File

@ -17,12 +17,7 @@ class VideolistingsController < ApplicationController
private
# Use callbacks to share common setup or constraints between actions.
def set_videolisting
videocode = params[:videocode]
if params[:videolisting]
@videolisting = Videolisting.find_by_videocode(params[:videolisting][:videocode]) || not_found
else
@videolisting = Videolisting.find_by_videocode(videocode) || not_found
end
@videolisting = set_by_options(:videolisting, :videocode)
end
# Only allow a trusted parameter "white list" through.