manage bookmarks, bugfixes
[smr.git] / gui / app / controllers / objects / bookmark_controller.rb
blobbc6e1b43fcb9234dcdef04d6d00a18bb2674a224
2 # This file is part of SMR.
4 # SMR is free software: you can redistribute it and/or modify it under the
5 # terms of the GNU General Public License as published by the Free Software
6 # Foundation, either version 3 of the License, or (at your option) any later
7 # version.
9 # SMR is distributed in the hope that it will be useful, but WITHOUT ANY
10 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License along with
14 # SMR.  If not, see <http://www.gnu.org/licenses/>.
18 # handles CRUD on Bookmark records
19 class Objects::BookmarkController < ObjectsController
21     ##
22     # List all Bookmark records for editing.
23     def index
24         super
25         session[:objects_bookmark_query] = params[:q] if params[:q]
27         @query = Bookmark.ransack(session[:objects_bookmark_query])
28         @bookmarks, @total_pages = smr_paginate(
29             @page=smr_page,
30             @query.result.to_a
31         )
32     end
34     ##
35     # defines empty Bookmark to #create a new one
36     def new
37         @securities = smr_securities_list
38         @organizations = smr_organizations_list(:drop_universe=>false)
39         @portfolios = smr_portfolios_list(:show_all=>true)
40         @figure_vars = FigureVar.for_user(current_user.id)
42         @bookmark = Bookmark.new(:id_user=>current_user.id)
43     end
45     ##
46     # retrieve Bookmark for editing
47     def edit
48         self.new
49         @bookmark = Bookmark.for_user(current_user).where(:id=>params[:id]).first
50         render :new
51     end
53     ##
54     # handles creates and updates
55     def create
56         n = Array.new
58         if params[:bookmark][:id].to_i > 0 then
59             bookmark = Bookmark.for_user(current_user).where(:id=>params[:bookmark][:id]).first
60             bookmark.update(bookmark_params)
61         else bookmark = Bookmark.create(bookmark_params.merge({:id_user=>current_user.id})) end
63         bookmark.id_user = params[:mark_bookmark_private] == '1' ? current_user.id : 0
64         bookmark.save
65         n << bookmark.errors.full_messages.uniq
67         redirect_to objects_bookmark_index_path, :notice=>n.compact.flatten.join(', ')
68     end
70     ##
71     # delete Bookmark
72     def destroy
73         b = Bookmark.find(params[:id])
74         begin b.destroy
75         rescue ActiveRecord::DeleteRestrictionError => errors
76             flash[:alert] = errors.to_s
77         end
79         redirect_to :action=>:index
80     end
82  protected
84     ##
85     # internal helper defining parameters acceptable for Bookmark create/update
86     def bookmark_params
87         params.require(:bookmark).permit(
88           :name, :url, :rank,
89           :id_security, :id_organization, :id_portfolio, :id_figure_var
90         )
91     end
93 end