You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pgfqe6ch8/app/controllers/libraries_controller.rb

61 lines
1.5 KiB

class LibrariesController < ApplicationController
before_filter :require_login
def index
libraries = Library.where(nil)
libraries =
if params[:filter] == 'mine'
libraries.where(user_id: current_user.id).order('created_at desc')
else
libraries.where(status: :published).order('visited_count desc')
end
search = params[:search].to_s.strip
libraries = libraries.where('title LIKE ?', "%#{search}%") if search.present?
@libraries = paginateHelper libraries.includes(user: :user_extensions)
end
def show
@library = Library.find(params[:id])
@library.increment_visited_count!
end
def new
@library = current_user.libraries.new
end
def create
@library = current_user.libraries.new
Libraries::SaveService.new(@library, current_user, params).call
redirect_to library_path(id: @library.id)
rescue ActiveRecord::RecordInvalid => _
render 'new'
end
def edit
@library = current_library
end
def update
@library = current_library
Libraries::SaveService.new(@library, current_user, params).call
redirect_to library_path(id: @library.id)
rescue ActiveRecord::RecordInvalid => _
render 'edit'
end
def publish
Libraries::SubmitService.new(current_library).call
render json: { status: 0 }
rescue Libraries::SubmitService::Error => ex
render json: { status: 0, message: ex.message }
end
private
def current_library
@_current_library ||= current_user.libraries.find(params[:id])
end
end