Class: PreferencesController

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

Instance Method Summary (collapse)

Methods inherited from ApplicationController

can_access?, permission_mappings, set_access_control

Instance Method Details

- (Object) edit



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'frontend/app/controllers/preferences_controller.rb', line 5

def edit
  scope = params['global'] ? 'global' : 'repo'
  user_prefix = params['repo'] ? '' : 'user_'
  @current_prefs, global_repo_id = current_preferences
  @defaults = @current_prefs['defaults']
  level = "#{user_prefix}#{scope}"
  @inherited_defaults = @current_prefs["defaults_global"]
  ['user_global', 'repo', 'user_repo'].each do |lev|
    break if lev == level
    @inherited_defaults = @current_prefs["defaults_#{lev}"] if @current_prefs["defaults_#{lev}"]
  end
  opts = {}
  if params['global']
    opts[:repo_id] = global_repo_id
  end

  if @current_prefs["#{user_prefix}#{scope}"]
    pref = JSONModel(:preference).from_hash(@current_prefs["#{user_prefix}#{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

- (Object) update



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

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 => ->(){
                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