Class: PreferencesController

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

Constant Summary

Constants included from Searchable

Searchable::ABSTRACT

Instance Method Summary collapse

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

#editObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'frontend/app/controllers/preferences_controller.rb', line 5

def edit
  opts, user_scope = setup_defaults

  if @current_prefs[user_scope]
    pref = JSONModel(:preference).from_hash(@current_prefs[user_scope])
  else
    pref = JSONModel(:preference).new({
                                        :defaults => {},
                                        :user_id => params['repo'] ? nil : JSONModel(:user).id_for(session[:user_uri])
                                      })
    pref.save(opts)
  end

  if params['id'] == pref.id.to_s
    @preference = pref
  else
    redirect_to(:controller => :preferences,
                :action => :edit,
                :id => pref.id,
                :global => params['global'],
                :repo => params['repo'])
  end
end

#resetObject



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

def reset
  redirect_params = {
    :controller => :preferences,
    :action => :edit,
    :id => 0,
    :global => params['global'],
    :repo => params['repo']
  }

  begin
    _, global_repo_id = current_preferences
    opts = {}
    opts[:repo_id] = global_repo_id if params['global']
    preference = JSONModel(:preference).find(params[:id], opts)
    preference.update({:defaults => {}})
    preference.save(opts)

    flash[:success] = I18n.t("preference._frontend.messages.reset")
    redirect_to(redirect_params)
  rescue Exception => e
    flash[:error] = I18n.t("preference._frontend.messages.reset_error", :exception => e)
    redirect_to(redirect_params)
    return
  end
end

#updateObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'frontend/app/controllers/preferences_controller.rb', line 30

def update
  prefs, global_repo_id = current_preferences
  opts = {}
  opts[:repo_id] = global_repo_id if params['global']
  handle_crud(:instance => :preference,
              :model => JSONModel(:preference),
              :obj => JSONModel(:preference).find(params['id'], opts),
              :find_opts => opts,
              :save_opts => opts,
              :replace => false,
              :on_invalid => ->() {
                setup_defaults
                return render action: "edit"
              },
              :on_valid => ->(id) {
                flash[:success] = I18n.t("preference._frontend.messages.updated",
                                         JSONModelI18nWrapper.new(:preference => @preference))
                redirect_to(:controller => :preferences,
                            :action => :edit,
                            :id => id,
                            :global => params['global'],
                            :repo => params['repo'])
              })
end