Class: CollectionsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/collections_controller.rb

Overview

Copyright 2011-2013 innoQ Deutschland GmbH

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Instance Method Summary collapse

Instance Method Details

#createObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/controllers/collections_controller.rb', line 99

def create
  authorize! :create, Iqvoc::Collection.base_class

  @collection = Iqvoc::Collection.base_class.new(concept_params)

  @collection.lock_by_user(current_user.id)

  if @collection.save
    flash[:success] = I18n.t('txt.controllers.collections.save.success')
    redirect_to collection_path(published: 0, id: @collection.origin)
  else
    flash.now[:error] = I18n.t('txt.controllers.collections.save.error')
    render :new
  end
end

#destroyObject



144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/controllers/collections_controller.rb', line 144

def destroy
  @collection = Iqvoc::Collection.base_class.by_origin(params[:id]).last!
  authorize! :destroy, @collection

  if @collection.destroy
    flash[:success] = I18n.t('txt.controllers.collections.destroy.success')
    redirect_to collections_path
  else
    flash.now[:error] = I18n.t('txt.controllers.collections.destroy.error')
    render action: :show
  end
end

#editObject



115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/controllers/collections_controller.rb', line 115

def edit
  @collection = Iqvoc::Collection.base_class.by_origin(params[:id]).last!
  authorize! :update, @collection

  # When in single query mode, AR handles ALL includes to be loaded by that
  # one query. We don't want that! So let's do it manually :-)
  Iqvoc::Collection.base_class.preload(@collection, [
      :pref_labels,
      { members: { target: [:pref_labels] + Iqvoc::Concept.base_class.default_includes } }])

  build_note_relations
end

#indexObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/collections_controller.rb', line 18

def index
  authorize! :read, Iqvoc::Collection.base_class

  respond_to do |format|
    format.html do
      @top_collections = Iqvoc::Collection.base_class.with_pref_labels.published
      @top_collections = if params[:root].present?
        @top_collections.by_parent_id(params[:root])
      else
        @top_collections.tops
      end

      @top_collections.to_a.sort! { |a, b| a.pref_label.to_s <=> b.pref_label.to_s }

      Iqvoc::Collection.base_class.preload(@top_collections, { members: :target })
    end
    format.json do # For the widget and treeview
      response = if params[:root].present?
        collections = Iqvoc::Collection.base_class
                                       .with_pref_labels
                                       .published
                                       .by_parent_id(params[:root])
                                       .sort_by { |c| c.pref_label.to_s }

        collections.map do |collection|
          res = {
            id: collection.id,
            url: collection_path(id: collection, format: :html),
            name: CGI.escapeHTML(collection.pref_label.to_s),
            load_on_demand: collection.subcollections.any?
          }
          res[:additionalText] = " (#{collection.additional_info})" if collection.additional_info

          res
        end
      else
        collections = Iqvoc::Collection.base_class
                                       .with_pref_labels
                                       .published
                                       .merge(Label::Base.by_query_value("#{params[:query]}%"))
                                       .sort_by { |c| c.pref_label.to_s }
                                       .map { |c| collection_widget_data(c) }
      end
      render json: response
    end
  end
end

#newObject



92
93
94
95
96
97
# File 'app/controllers/collections_controller.rb', line 92

def new
  authorize! :create, Iqvoc::Collection.base_class

  @collection = Iqvoc::Collection.base_class.new
  build_note_relations
end

#showObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/controllers/collections_controller.rb', line 66

def show
  published = params[:published] == '1' || !params[:published]
  scope = Iqvoc::Collection.base_class.by_origin(params[:id])

  if published
    scope = scope.published
    @new_collection_version = Iqvoc::Collection.base_class.by_origin(params[:id]).unpublished.last
  else
    scope = scope.unpublished
  end

  @collection = scope.last!
  authorize! :read, @collection

  # When in single query mode, AR handles ALL includes to be loaded by that
  # one query. We don't want that! So let's do it manually :-)
  Iqvoc::Collection.base_class.preload(@collection,
    [:pref_labels,
      { members: { target: [:pref_labels] + Iqvoc::Collection.base_class.default_includes } }])

  respond_to do |format|
    format.html { published ? render('show_published') : render('show_unpublished') }
    format.any(:rdf, :ttl, :nt)
  end
end

#updateObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/controllers/collections_controller.rb', line 128

def update
  @collection = Iqvoc::Collection.base_class.by_origin(params[:id]).last!
  authorize! :update, @collection

  # set to_review to false if someone edits a concepts
  concept_params["to_review"] = "false"

  if @collection.update(concept_params)
    flash[:success] = I18n.t('txt.controllers.collections.save.success')
    redirect_to collection_path(@collection, published: 0)
  else
    flash.now[:error] = I18n.t('txt.controllers.collections.save.error')
    render :edit
  end
end