aboutsummaryrefslogtreecommitdiff
path: root/app/controllers
diff options
context:
space:
mode:
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/application_controller.rb22
-rw-r--r--app/controllers/comments_controller.rb68
-rw-r--r--app/controllers/concerns/.keep0
-rw-r--r--app/controllers/concerns/taggable.rb17
-rw-r--r--app/controllers/exports_controller.rb38
-rw-r--r--app/controllers/licenses_controller.rb7
-rw-r--r--app/controllers/searches_controller.rb60
-rw-r--r--app/controllers/sessions_controller.rb27
-rw-r--r--app/controllers/shared_searches_controller.rb13
-rw-r--r--app/controllers/tags_controller.rb39
-rw-r--r--app/controllers/things_controller.rb69
11 files changed, 360 insertions, 0 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
new file mode 100644
index 0000000..2a63b66
--- /dev/null
+++ b/app/controllers/application_controller.rb
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+class ApplicationController < ActionController::Base
+ before_action :user_authenticated!
+ before_action :set_searches
+
+ helper_method :current_user
+
+ def current_user
+ @current_user ||= User.find(session[:user_id]) if session[:user_id]
+ end
+
+ def user_authenticated!
+ return if current_user
+
+ redirect_to new_sessions_path
+ end
+
+ def set_searches
+ @searches = Search.all
+ end
+end
diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb
new file mode 100644
index 0000000..d362460
--- /dev/null
+++ b/app/controllers/comments_controller.rb
@@ -0,0 +1,68 @@
+# frozen_string_literal: true
+
+class CommentsController < ApplicationController
+ include Taggable
+
+ before_action :set_thing
+ before_action :set_comment, only: %i[update destroy]
+
+ def create
+ ps = comment_params
+ from = ps.delete(:from)
+ tag_ids = ps.delete(:tag_ids)
+
+ ok = ActiveRecord::Base.transaction do
+ comment = @thing.comments.create(ps)
+ next unless comment
+
+ update_tags!(object: comment, ids: tag_ids)
+ end
+
+ flash[:alert] = t('comments.bad-create') unless ok
+ handle_from_param!(from:)
+ end
+
+ def update
+ ps = comment_params
+ from = ps.delete(:from)
+ tag_ids = ps.delete(:tag_ids)
+
+ ok = ActiveRecord::Base.transaction do
+ next unless @comment.update(ps)
+
+ update_tags!(object: @comment, ids: tag_ids)
+ end
+
+ flash[:alert] = t('comments.bad-update') unless ok
+ handle_from_param!(from:)
+ end
+
+ def destroy
+ comment = Comment.find(params[:id])
+ comment.destroy
+
+ handle_from_param!(from: params[:from])
+ end
+
+ protected
+
+ def comment_params
+ params.required(:comment).permit(:content, :from, tag_ids: [])
+ end
+
+ def set_thing
+ @thing = Thing.find(params[:thing_id])
+ end
+
+ def set_comment
+ @comment = Comment.find(params[:id])
+ end
+
+ def handle_from_param!(from:)
+ if from == 'things/edit'
+ redirect_to edit_thing_path(@thing)
+ else
+ redirect_to @thing
+ end
+ end
+end
diff --git a/app/controllers/concerns/.keep b/app/controllers/concerns/.keep
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/app/controllers/concerns/.keep
diff --git a/app/controllers/concerns/taggable.rb b/app/controllers/concerns/taggable.rb
new file mode 100644
index 0000000..a9bda12
--- /dev/null
+++ b/app/controllers/concerns/taggable.rb
@@ -0,0 +1,17 @@
+# frozen_string_literal: true
+
+# This module provides methods for controllers that have to deal with tag
+# references (i.e. comments and things). This is closely tied to `TagReference`
+# and its polymorphic `:taggable` column.
+module Taggable
+ extend ActiveSupport::Concern
+
+ # Update tag references for the given `object` with tags identified by the
+ # `ids` array.
+ def update_tags!(object:, ids:)
+ object.tag_references.destroy_all
+ return true unless ids
+
+ ids.each { |id| TagReference.create!(tag_id: id, taggable: object) }
+ end
+end
diff --git a/app/controllers/exports_controller.rb b/app/controllers/exports_controller.rb
new file mode 100644
index 0000000..4303ea6
--- /dev/null
+++ b/app/controllers/exports_controller.rb
@@ -0,0 +1,38 @@
+# frozen_string_literal: true
+
+class ExportsController < ApplicationController
+ before_action :set_search
+ before_action :set_results, only: %i[show]
+
+ def show
+ respond_to do |format|
+ format.csv { set_file_name!(ext: 'csv') }
+ format.uoc { set_file_name!(ext: 'tex') }
+ end
+ end
+
+ def new; end
+
+ protected
+
+ # Sets the file name for the attachment according to the @search's name, and
+ # it adds the given string `ext` as the extension.
+ def set_file_name!(ext:)
+ response.headers['Content-Disposition'] = "attachment; filename=\"#{@search.name}.#{ext}\""
+ end
+
+ # Set @search. If there was no `:search_id` parameter, then we assume that the
+ # ID = 0, which is the fake search we use for the base 'Home' (i.e. get
+ # everything).
+ def set_search
+ @search = if params[:search_id].to_i.zero?
+ Search.new(id: 0, name: 'Home')
+ else
+ Search.find(params[:search_id])
+ end
+ end
+
+ def set_results
+ @results = @search.results.fetch(:things, [])
+ end
+end
diff --git a/app/controllers/licenses_controller.rb b/app/controllers/licenses_controller.rb
new file mode 100644
index 0000000..03b6087
--- /dev/null
+++ b/app/controllers/licenses_controller.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+class LicensesController < ApplicationController
+ skip_before_action :user_authenticated!, only: %i[show]
+
+ def show; end
+end
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
diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb
new file mode 100644
index 0000000..b79e3dd
--- /dev/null
+++ b/app/controllers/sessions_controller.rb
@@ -0,0 +1,27 @@
+# frozen_string_literal: true
+
+# SessionsController holds the logic for logging in and out users.
+class SessionsController < ApplicationController
+ skip_before_action :user_authenticated!, only: %i[new create]
+ skip_before_action :set_searches, only: %i[new create]
+
+ # We only need to render its template.
+ def new; end
+
+ # Create a session for the current user.
+ def create
+ user = User.find_by(username: params[:username])
+ if user&.authenticate(params[:password])
+ session[:user_id] = user.id
+ redirect_to root_url
+ else
+ redirect_to root_url, alert: t('sessions.wrong-credentials')
+ end
+ end
+
+ # Destroy the current session in order to log out.
+ def destroy
+ session[:user_id] = nil
+ redirect_to root_url
+ end
+end
diff --git a/app/controllers/shared_searches_controller.rb b/app/controllers/shared_searches_controller.rb
new file mode 100644
index 0000000..3208e7c
--- /dev/null
+++ b/app/controllers/shared_searches_controller.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+class SharedSearchesController < ApplicationController
+ skip_before_action :user_authenticated!, only: %i[show]
+
+ def index
+ @searches = Search.where(shared: true)
+ end
+
+ def show
+ @search = Search.find(params[:search_id])
+ end
+end
diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb
new file mode 100644
index 0000000..d967185
--- /dev/null
+++ b/app/controllers/tags_controller.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+class TagsController < ApplicationController
+ before_action :set_tag, only: %i[destroy]
+
+ def index
+ @tags = Tag.all
+ end
+
+ def new
+ @tag = Tag.new
+ end
+
+ def create
+ @tag = Tag.new(tag_params)
+
+ if @tag.save
+ redirect_to tags_url, notice: t('tags.create-success')
+ else
+ render :new, status: :unprocessable_entity
+ end
+ end
+
+ def destroy
+ @tag.destroy!
+
+ redirect_to tags_url, notice: t('tags.destroy-success')
+ end
+
+ private
+
+ def set_tag
+ @tag = Tag.find(params[:id])
+ end
+
+ def tag_params
+ params.require(:tag).permit(:name)
+ end
+end
diff --git a/app/controllers/things_controller.rb b/app/controllers/things_controller.rb
new file mode 100644
index 0000000..450b409
--- /dev/null
+++ b/app/controllers/things_controller.rb
@@ -0,0 +1,69 @@
+# frozen_string_literal: true
+
+class ThingsController < ApplicationController
+ include Taggable
+
+ before_action :set_thing, only: %i[edit show update destroy]
+
+ def show; end
+
+ def new
+ @thing = Thing.new
+ end
+
+ def edit; end
+
+ def create
+ ps = thing_params
+ tag_ids = ps.delete(:tag_ids)
+ @thing = Thing.new(ps)
+ @thing.user_id = @current_user.id
+
+ updated = ActiveRecord::Base.transaction do
+ next unless @thing.save
+
+ update_tags!(object: @thing, ids: tag_ids)
+ end
+
+ if updated
+ redirect_to thing_url(@thing.reload), notice: t('things.create-success')
+ else
+ render :new, status: :unprocessable_entity
+ end
+ end
+
+ def update
+ ps = thing_params
+ tag_ids = ps.delete(:tag_ids)
+
+ updated = ActiveRecord::Base.transaction do
+ next unless @thing.update(ps)
+
+ update_tags!(object: @thing, ids: tag_ids)
+ end
+
+ if updated
+ redirect_to edit_thing_url(@thing), notice: t('things.update-success')
+ else
+ render :edit, status: :unprocessable_entity
+ end
+ end
+
+ def destroy
+ @thing.destroy!
+
+ redirect_to root_url, notice: t('things.destroy-success')
+ end
+
+ private
+
+ def set_thing
+ @thing = Thing.find(params[:id])
+ end
+
+ def thing_params
+ params.require(:thing).permit(:target, :title, :publisher, :address, :year, :url, :access,
+ :authors, :location, :insideof, :pages, :rate, :status, :kind,
+ :bought_at, :editors, :note, :where_is_it, tag_ids: [])
+ end
+end