Class: Preference
- Inherits:
-
Sequel::Model
- Object
- Sequel::Model
- Preference
- Defined in:
- backend/app/model/preference.rb
Class Method Summary collapse
-
.create_from_json(json, opts = {}) ⇒ Object
-
.current_preferences(repo_id = RequestContext.get(:repo_id)) ⇒ Object
-
.defaults ⇒ Object
-
.global_defaults ⇒ Object
-
.init ⇒ Object
-
.parsed_defaults_for(filter) ⇒ Object
-
.repo_defaults ⇒ Object
-
.sequel_to_jsonmodel(objs, opts = {}) ⇒ Object
-
.user_global_defaults ⇒ Object
Instance Method Summary collapse
-
#after_save ⇒ Object
-
#before_save ⇒ Object
-
#parsed_defaults ⇒ Object
-
#update_from_json(json, opts = {}, apply_nested_records = true) ⇒ Object
Class Method Details
.create_from_json(json, opts = {}) ⇒ Object
145 146 147 |
# File 'backend/app/model/preference.rb', line 145 def self.create_from_json(json, opts = {}) super(json, opts.merge('defaults' => JSON(json.defaults || {}))) end |
.current_preferences(repo_id = RequestContext.get(:repo_id)) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'backend/app/model/preference.rb', line 88 def self.current_preferences(repo_id = RequestContext.get(:repo_id)) return {} unless RequestContext.get(:current_username) user_id = User[:username => RequestContext.get(:current_username)].id filter = {:repo_id => repo_id, :user_uniq => [user_id.to_s, 'GLOBAL_USER']} json_prefs = {'defaults' => {}} prefs = {} defaults = {} if repo_id != Repository.global_repo_id self.filter(filter).each do |pref| if pref.user_uniq == 'GLOBAL_USER' json_prefs['repo'] = self.to_jsonmodel(pref) prefs[:repo] = pref else json_prefs['user_repo'] = self.to_jsonmodel(pref) prefs[:user_repo] = pref end end end RequestContext.in_global_repo do filter = {:repo_id => Repository.global_repo_id, :user_uniq => [user_id.to_s, 'GLOBAL_USER']} self.filter(filter).each do |pref| if pref.user_uniq == 'GLOBAL_USER' json_prefs['global'] = self.to_jsonmodel(pref) prefs[:global] = pref else json_prefs['user_global'] = self.to_jsonmodel(pref) prefs[:user_global] = pref end end end [:global, :user_global, :repo, :user_repo].each do |k| if prefs[k] json_prefs['defaults'].merge!(prefs[k].parsed_defaults) json_prefs["defaults_#{k}"] = json_prefs['defaults'].clone end end json_prefs['defaults'].delete('jsonmodel_type') json_prefs end |
.defaults ⇒ Object
78 79 80 81 82 83 84 85 |
# File 'backend/app/model/preference.rb', line 78 def self.defaults if RequestContext.get(:current_username) user_defs = self.parsed_defaults_for(:user_id => User[:username => RequestContext.get(:current_username)].id) self.repo_defaults.merge(user_defs) else self.repo_defaults end end |
.global_defaults ⇒ Object
54 55 56 57 58 |
# File 'backend/app/model/preference.rb', line 54 def self.global_defaults RequestContext.open(:repo_id => Repository.global_repo_id) do self.parsed_defaults_for(:user_id => nil) end end |
.init ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'backend/app/model/preference.rb', line 8 def self.init defaults = PreferenceConfig.defaults RequestContext.in_global_repo do filter = {:repo_id => Repository.global_repo_id, :user_id => nil} if self.filter(filter).count == 0 Log.info("Creating system preferences") Preference.create_from_json(JSONModel(:preference).from_hash({ :user_id => nil, :defaults => defaults }), :repo_id => Repository.global_repo_id) else Log.info("Updating system preferences") pref = self.filter(filter).first pref.update_from_json(JSONModel(:preference).from_hash({:defaults => defaults}), :lock_version => pref.lock_version) end end end |
.parsed_defaults_for(filter) ⇒ Object
48 49 50 51 |
# File 'backend/app/model/preference.rb', line 48 def self.parsed_defaults_for(filter) pref = self[filter.merge(:repo_id => RequestContext.get(:repo_id))] pref ? pref.parsed_defaults : {} end |
.repo_defaults ⇒ Object
73 74 75 |
# File 'backend/app/model/preference.rb', line 73 def self.repo_defaults self.user_global_defaults.merge(self.parsed_defaults_for(:user_id => nil)) end |
.sequel_to_jsonmodel(objs, opts = {}) ⇒ Object
134 135 136 137 138 139 140 141 142 |
# File 'backend/app/model/preference.rb', line 134 def self.sequel_to_jsonmodel(objs, opts = {}) jsons = super jsons.zip(objs).each do |json, obj| json['defaults'] = JSONModel(:defaults).from_json(obj.defaults) end jsons end |
.user_global_defaults ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'backend/app/model/preference.rb', line 61 def self.user_global_defaults RequestContext.open(:repo_id => Repository.global_repo_id) do if RequestContext.get(:current_username) user_defs = self.parsed_defaults_for(:user_id => User[:username => RequestContext.get(:current_username)].id) self.global_defaults.merge(user_defs) else self.global_defaults end end end |
Instance Method Details
#after_save ⇒ Object
36 37 38 39 40 |
# File 'backend/app/model/preference.rb', line 36 def after_save Notifications.notify("REFRESH_PREFERENCES") reset_initial_values end |
#before_save ⇒ Object
30 31 32 33 |
# File 'backend/app/model/preference.rb', line 30 def before_save super self.user_uniq = self.user_id || 'GLOBAL_USER' end |
#parsed_defaults ⇒ Object
43 44 45 |
# File 'backend/app/model/preference.rb', line 43 def parsed_defaults ASUtils.json_parse(self.defaults) end |
#update_from_json(json, opts = {}, apply_nested_records = true) ⇒ Object
150 151 152 153 |
# File 'backend/app/model/preference.rb', line 150 def update_from_json(json, opts = {}, apply_nested_records = true) super(json, opts.merge('defaults' => JSON(json.defaults)), apply_nested_records) end |