Skip to content

Instantly share code, notes, and snippets.

@ka8725
Created September 4, 2012 10:53
Show Gist options
  • Select an option

  • Save ka8725/3620081 to your computer and use it in GitHub Desktop.

Select an option

Save ka8725/3620081 to your computer and use it in GitHub Desktop.
class ContentItemsController < ApplicationController
before_filter :init_resource, :authorize_content_item
def index
@popular_content_items = ContentItem.search_with_filter(params).
sx_active.sx_ordered("votes_count")
if params[:category_id].blank?
if signed_in?
@friends_content_items = ContentItem.search_with_filter(params).
sx_active.sx_by_friends(current_user).sx_ordered(params[:order])
@recommended_content_items = ContentItem.
search_with_filter(params.merge({ category_id: current_user.preferred_genre_ids })).
sx_active.sx_ordered(params[:order])
end
else
if params[:category_id] == Category::RECOMMENDED_PARAM_TYPE
@content_items = ContentItem.
search_with_filter(params.merge({ category_id: current_user.preferred_genre_ids })).
sx_active.sx_ordered(params[:order])
@category = params[:category_id]
else
@content_items = ContentItem.search_with_filter(params).sx_active.sx_ordered(params[:order])
@category = Category.find(params[:category_id])
end
end
end
def show
@content_item.update_preferred_genres_for(current_user)
if @content_item.class.uploaders.present?
gon.media_file_path = @content_item.content_file.url
end
end
def multiple_new
gon.file_uploader = { create_path: content_items_path(method: :post, format: :js),
server_error: t('uploader.server_error'),
file_template: (render_to_string partial: 'uploader/file_template'),
token: form_authenticity_token }
end
def new
end
def create
status = @content_item.save
respond_to do |format|
format.html do
if status
redirect_to user_path(current_user), notice: I18n.t('content_items.create.success')
else
render action: 'new'
end
end
format.js do
if status
controls = render_to_string partial: 'uploader/file_controls', locals: { item: @content_item }
else
controls = nil
end
render json: { success: status,
itemId: @content_item.id,
errors: @content_item.errors.full_messages,
controls: controls }
end
end
end
def edit
end
def update
update_result = @content_item.update_attributes(params[:content_item])
respond_to do |format|
format.html do
if update_result
redirect_to user_path(current_user), notice: I18n.t('content_items.update.success')
else
render action: 'edit'
end
end
format.js { render json: @content_item }
end
end
def destroy
if params[:deletion_reason]
@content_item.mark_as_deleted(current_user.id, params[:deletion_reason])
else
@content_item.destroy
end
respond_to do |format|
format.html do
redirect_to params[:return_path].blank? ? user_path(@content_item.user) : params[:return_path],
notice: I18n.t('content_items.destroy.success')
end
format.js { render json: { id: @content_item.id } }
end
end
private
def init_resource
if params[:id]
@content_item = ContentItem.active.find params[:id]
elsif action_name.in? ['new', 'create', 'multiple_new']
@content_item = ContentItem.init_item params[:content_item], params[:file], params[:qqfile]
@content_item.user = current_user if @content_item
end
end
def authorize_content_item
item_for_authorize = @content_item ? @content_item.to_content_item : ContentItem
authorize! action_name.to_sym, item_for_authorize
authorize! :for_sale, item_for_authorize if params[:content_item] && params[:content_item][:sale].present?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment