Class: LocationsController

Inherits:
ApplicationController show all
Includes:
ExportHelper
Defined in:
frontend/app/controllers/locations_controller.rb

Constant Summary collapse

LOCATION_STICKY_PARAMS =
["building", "floor", "room", "area" ]

Constants included from Searchable

Searchable::ABSTRACT

Instance Method Summary collapse

Methods included from ExportHelper

#csv_response, #xml_response

Methods inherited from ApplicationController

#archivesspace, can_access?, permission_mappings, set_access_control

Methods included from JsonHelper

#merge_notes, #process_json_notes

Methods included from Searchable

#default_search_opts, #get_filter_years, #handle_results, #html_notes, #process_results, #process_search_results, #repo_context, #search_terms, #set_up_advanced_search, #set_up_and_run_search, #set_up_search, #strip_facet_fields

Methods included from HandleFaceting

#fetch_only_facets, #get_pretty_facet_value, #strip_facets

Methods included from ManipulateNode

#inheritance, #process_mixed_content, #strip_mixed_content

Instance Method Details

#batchObject



131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'frontend/app/controllers/locations_controller.rb', line 131

def batch
  @is_batch_update = false
  @action = "create" # we use this for some label in the view..

  if request.post? # if it's a post, we're starting an update
    @is_batch_update = true
    @action = "update" # we use this for some label in the view..
    @location_batch = JSONModel(:location_batch_update).new(params)._always_valid!
  else # we're just creatinga new batch from scratch
    location_params = params.permit(LOCATION_STICKY_PARAMS.map(&:to_sym)).to_h.inject({}) { |c, (k, v)| c[k] = v if LOCATION_STICKY_PARAMS.include?(k); c }
    @location_batch = JSONModel(:location_batch).new(location_params)
  end
end

#batch_createObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'frontend/app/controllers/locations_controller.rb', line 146

def batch_create
  begin
    if params[:location_batch][:record_uris] && params[:location_batch][:record_uris].length > 0
      batch = cleanup_params_for_schema(params[:location_batch], JSONModel(:location_batch_update).schema)
      @location_batch = JSONModel(:location_batch_update).from_hash(batch, false)._always_valid!

      uri = "#{JSONModel::HTTP.backend_url}/locations/batch_update"
      response = JSONModel::HTTP.post_json(URI(uri), batch.to_json)

      batch_response = ASUtils.json_parse(response.body)
    else

      batch = cleanup_params_for_schema(params[:location_batch], JSONModel(:location_batch).schema)

      @location_batch = JSONModel(:location_batch).from_hash(batch, false)

      uri = "#{JSONModel::HTTP.backend_url}/locations/batch"
      if params["dry_run"]
        uri += "?dry_run=true"
      end
      response = JSONModel::HTTP.post_json(URI(uri), batch.to_json)

      batch_response = ASUtils.json_parse(response.body)
    end

    if batch_response.is_a?(Hash) and batch_response.has_key?("error")
      if params["dry_run"]
        return render_aspace_partial :partial => "shared/quick_messages", :locals => {:exceptions => batch_response, :jsonmodel => "location_batch"}
      else
        @exceptions = {:errors => batch_response["error"]}

        return render :action => :batch
      end
    end

    if params["dry_run"]
      render_aspace_partial :partial => "locations/batch_preview", :locals => {:locations => batch_response}
    else

      # we want 'created' or 'updated' messages displayed
      if @location_batch.jsonmodel_type == "location_batch_update"
        flash[:success] = I18n.t("location_batch._frontend.messages.updated", :number_created => batch_response.length)
      else
        flash[:success] = I18n.t("location_batch._frontend.messages.created", :number_created => batch_response.length)
      end

      if params.has_key?(:plus_one)
        sticky_params = { :controller => :locations, :action => :batch}
        @location_batch.to_hash.each_pair do |k, v|
          sticky_params[k] = v if LOCATION_STICKY_PARAMS.include?(k)
        end

        return redirect_to sticky_params
      end
      redirect_to :action => :index
    end
  rescue JSONModel::ValidationException => e
    @exceptions = @location_batch._exceptions

    return render :action => :batch
  end
end

#check_for_temporary_location(obj) ⇒ Object

temporary_location is unusual in that when set it’s a disabled field so does not get submitted in params, so add it back if available otherwise it gets nilled



94
95
96
97
98
99
100
# File 'frontend/app/controllers/locations_controller.rb', line 94

def check_for_temporary_location(obj)
  if params[:location][:temporary_question]
    # don't use .blank? here -- "" is a valid selection (and will unset correctly)
    temporary = !params[:location][:temporary].nil? ? params[:location][:temporary] : obj["temporary"]
    params[:location][:temporary] = temporary
  end
end

#createObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'frontend/app/controllers/locations_controller.rb', line 56

def create
  handle_crud(:instance => :location,
              :model => JSONModel(:location),
              :on_invalid => ->() {
                return render_aspace_partial :partial => "locations/new" if inline?
                return render :action => :new
              },
              :on_valid => ->(id) {
                return render :json => @location.to_hash if inline?

                flash[:success] = I18n.t("location._frontend.messages.created")
                if params.has_key?(:plus_one)
                  sticky_params = { :controller => :locations, :action => :new}
                  @location.to_hash.each_pair do |k, v|
                    sticky_params[k] = v if LOCATION_STICKY_PARAMS.include?(k)
                  end

                  return redirect_to sticky_params
                end
                redirect_to :controller => :locations, :action => :edit, :id => id
              })
end

#current_recordObject



30
31
32
# File 'frontend/app/controllers/locations_controller.rb', line 30

def current_record
  @location
end

#defaultsObject



102
103
104
105
106
107
108
109
110
# File 'frontend/app/controllers/locations_controller.rb', line 102

def defaults
  defaults = DefaultValues.get 'location'

  values = defaults ? defaults.form_values : {}

  @location = JSONModel(:location).new(values)._always_valid!

  render "defaults"
end

#deleteObject



210
211
212
213
214
215
216
217
218
219
220
221
# File 'frontend/app/controllers/locations_controller.rb', line 210

def delete
  location = JSONModel(:location).find(params[:id])
  begin
    location.delete
  rescue ConflictException => e
    flash[:error] = location.translate_exception_message(e.conflicts)
    return redirect_to(:controller => :locations, :action => :show, :id => location.id)
  end

  flash[:success] = I18n.t("location._frontend.messages.deleted", JSONModelI18nWrapper.new(:location => location))
  redirect_to(:controller => :locations, :action => :index, :deleted_uri => location.uri)
end

#editObject



52
53
54
# File 'frontend/app/controllers/locations_controller.rb', line 52

def edit
  get_location
end

#get_locationObject



26
27
28
# File 'frontend/app/controllers/locations_controller.rb', line 26

def get_location
  @location = JSONModel(:location).find(params[:id], find_opts)
end

#indexObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'frontend/app/controllers/locations_controller.rb', line 11

def index
  respond_to do |format|
    format.html {
      @search_data = Search.for_type(session[:repo_id], "location", params_for_backend_search.merge({"facet[]" => SearchResultData.LOCATION_FACETS, "blank_facet_query_fields" => ["owner_repo_display_string_u_ssort"]}))
    }
    format.csv {
      search_params = params_for_backend_search.merge({"facet[]" => SearchResultData.LOCATION_FACETS, "blank_facet_query_fields" => ["owner_repo_display_string_u_ssort"]})
      search_params["type[]"] = "location"
      uri = "/repositories/#{session[:repo_id]}/search"
      csv_response( uri, Search.build_filters(search_params), "#{I18n.t('location._plural').downcase}." )
    }
  end
end

#newObject



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'frontend/app/controllers/locations_controller.rb', line 38

def new
  location_params = params.permit(LOCATION_STICKY_PARAMS.map(&:to_sym)).to_h.inject({}) { |c, (k, v)| c[k] = v if LOCATION_STICKY_PARAMS.include?(k); c }
  @location = JSONModel(:location).new(location_params)._always_valid!

  if user_prefs['default_values']
    defaults = DefaultValues.get 'location'

    @location.update(defaults.values) if defaults
  end


  render_aspace_partial :partial => "locations/new" if inline?
end

#showObject



34
35
36
# File 'frontend/app/controllers/locations_controller.rb', line 34

def show
  get_location
end

#updateObject



79
80
81
82
83
84
85
86
87
88
89
90
# File 'frontend/app/controllers/locations_controller.rb', line 79

def update
  obj = JSONModel(:location).find(params[:id])
  check_for_temporary_location(obj)
  handle_crud(:instance => :location,
              :model => JSONModel(:location),
              :obj => obj,
              :on_invalid => ->() { return render :action => :edit },
              :on_valid => ->(id) {
                flash[:success] = I18n.t("location._frontend.messages.updated")
                redirect_to :controller => :locations, :action => :edit, :id => id
              })
end

#update_defaultsObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'frontend/app/controllers/locations_controller.rb', line 112

def update_defaults
  begin
    DefaultValues.from_hash({
                              "record_type" => "location",
                              "lock_version" => params[:location].delete('lock_version'),
                              "defaults" => cleanup_params_for_schema(
                                                                      params[:location],
                                                                      JSONModel(:location).schema)
                            }).save

    flash[:success] = I18n.t("default_values.messages.defaults_updated")
    redirect_to :controller => :locations, :action => :defaults
  rescue Exception => e
    flash[:error] = e.message
    redirect_to :controller => :locations, :action => :defaults
  end
end