Class: LocationsController

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

Constant Summary

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

Instance Method Summary (collapse)

Methods inherited from ApplicationController

can_access?, permission_mappings, set_access_control

Instance Method Details

- (Object) batch

[View source]

105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'frontend/app/controllers/locations_controller.rb', line 105

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.inject({}) { |c, (k,v)| c[k] = v if LOCATION_STICKY_PARAMS.include?(k); c }
    @location_batch = JSONModel(:location_batch).new(location_params)
  end
end

- (Object) batch_create

[View source]

121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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
# File 'frontend/app/controllers/locations_controller.rb', line 121

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.kind_of?(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

- (Object) create

[View source]

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'frontend/app/controllers/locations_controller.rb', line 41

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

- (Object) defaults

[View source]

75
76
77
78
79
80
81
82
83
# File 'frontend/app/controllers/locations_controller.rb', line 75

def defaults
  defaults = DefaultValues.get 'location'

  values = defaults ? defaults.form_values : {}

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

  render "defaults"
end

- (Object) delete

[View source]

186
187
188
189
190
191
192
193
194
195
196
197
# File 'frontend/app/controllers/locations_controller.rb', line 186

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

- (Object) edit

[View source]

37
38
39
# File 'frontend/app/controllers/locations_controller.rb', line 37

def edit
  get_location
end

- (Object) get_location

[View source]

15
16
17
# File 'frontend/app/controllers/locations_controller.rb', line 15

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

- (Object) index

[View source]

10
11
12
# File 'frontend/app/controllers/locations_controller.rb', line 10

def index
  @search_data = Search.for_type(session[:repo_id], "location", params_for_backend_search.merge({"facet[]" => SearchResultData.LOCATION_FACETS}))
end

- (Object) new

[View source]

23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'frontend/app/controllers/locations_controller.rb', line 23

def new
  location_params = params.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

- (Object) show

[View source]

19
20
21
# File 'frontend/app/controllers/locations_controller.rb', line 19

def show
  get_location
end

- (Object) update

[View source]

64
65
66
67
68
69
70
71
72
73
# File 'frontend/app/controllers/locations_controller.rb', line 64

def update
  handle_crud(:instance => :location,
              :model => JSONModel(:location),
              :obj => JSONModel(:location).find(params[:id]),
              :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

- (Object) update_defaults

[View source]

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'frontend/app/controllers/locations_controller.rb', line 85

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