diff options
| author | Miquel Sabaté Solà <msabate@suse.com> | 2024-03-13 18:03:43 +0100 |
|---|---|---|
| committer | Miquel Sabaté Solà <msabate@suse.com> | 2024-03-13 18:03:43 +0100 |
| commit | a7791635aa98f012d35f97c3c5288494bbb28e38 (patch) | |
| tree | bad987e6b4d10ad2f7cd02e40658d464907595ee /app/controllers/searches_controller.rb | |
| download | operum-a7791635aa98f012d35f97c3c5288494bbb28e38.tar.gz operum-a7791635aa98f012d35f97c3c5288494bbb28e38.zip | |
Initial commit
Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
Diffstat (limited to 'app/controllers/searches_controller.rb')
| -rw-r--r-- | app/controllers/searches_controller.rb | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/app/controllers/searches_controller.rb b/app/controllers/searches_controller.rb new file mode 100644 index 0000000..752ccec --- /dev/null +++ b/app/controllers/searches_controller.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +class SearchesController < ApplicationController + before_action :set_search, only: %i[show edit update destroy] + + def index + current_user.update!(last_search_id: nil) + @search = Search.new + end + + def show + current_user.update!(last_search_id: @search.id) + end + + def new + @search = Search.new + end + + def edit; end + + def create + @search = Search.new(search_params) + @search.user_id = @current_user.id + + if @search.save + redirect_to @search + else + render :new, status: :unprocessable_entity + end + end + + def update + if @search.update(search_params) + redirect_to @search, status: :see_other + else + render :edit, status: :unprocessable_entity + end + end + + def destroy + @search.destroy! + redirect_to searches_url, status: :see_other + end + + def search + @search = Search.new + @search.body = params.permit(:body).fetch(:body, '') + render 'search', partial: true, locals: { items: @search.results } + end + + protected + + def set_search + @search = Search.find(params[:id]) + end + + def search_params + params.require(:search).permit(:name, :body, :shared) + end +end |
