manage bookmarks, bugfixes
[smr.git] / gui / app / controllers / objects / organization_controller.rb
blobdf0beba207a436080f725627482e06ea15e865ec
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 Organization records
19 class Objects::OrganizationController < ObjectsController
20     def index
21         super
22         session[:objects_organization_query] = params[:q] if params[:q]
24         @query = Organization.ransack(session[:objects_organization_query])
25         @organizations, @total_pages = smr_paginate(
26             @page = smr_page,
27             @query.result.to_a
28         )
29     end
31     ##
32     # defines empty Organization to create() a new one
33     def new
34         @organization = Organization.new
35         render :edit
36     end
38     ##
39     # edit existing Organization
40     def edit
41         @organization = Organization.where(:id=>params[:id]).first
42     end
44     ##
45     # handles creates and updates
46     def create
47         if params[:organization] and params[:organization][:id].to_i > 0 then
48             o = Organization.where(:id=>params[:organization][:id]).first
49         else o = Organization.new(organization_params) end
51         # hard-wired so noone spoofes it in here
52         # FIXME: how to handle properly? given 0 _or_ current user id
53 #        fv.id_user = current_user.id
55         if params[:organization][:id].to_i > 0 then
56             o.update(organization_params)
57         else o.save end
59         redirect_to objects_organization_index_path, :notice=>o.errors.full_messages.uniq.join(', ')
60     end
62  protected
64     ##
65     # internal helper defining parameters acceptable for Organization create/update
66     def organization_params
67         params.require(:organization).permit(
68           :id, :name, :contact_email, :description,
69           :Bookmark_attributes=> [ :id, :name, :url, :rank ]
70         )
71     end
72 end