Class: Preference

Inherits:
Sequel::Model
  • Object
show all
Includes:
ASModel
Defined in:
backend/app/model/preference.rb

Constant Summary

Constants included from JSONModel

JSONModel::REFERENCE_KEY_REGEX

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ASModel

all_models, included, update_publish_flag, update_suppressed_flag

Methods included from JSONModel

JSONModel, #JSONModel, add_error_handler, all, allow_unmapped_enum_value, backend_url, check_valid_refs, client_mode?, custom_validations, destroy_model, enum_default_value, enum_values, handle_error, load_schema, #models, models, parse_jsonmodel_ref, parse_reference, repository, repository_for, schema_src, set_publish_flags!, set_repository, strict_mode, strict_mode?, validate_schema, with_repository

Class Method Details

.create_from_json(json, opts = {}) ⇒ Object



144
145
146
# File 'backend/app/model/preference.rb', line 144

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



87
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
# File 'backend/app/model/preference.rb', line 87

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

.defaultsObject



77
78
79
80
81
82
83
84
# File 'backend/app/model/preference.rb', line 77

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_defaultsObject



53
54
55
56
57
# File 'backend/app/model/preference.rb', line 53

def self.global_defaults
  RequestContext.open(:repo_id => Repository.global_repo_id) do
    self.parsed_defaults_for(:user_id => nil)
  end
end

.initObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'backend/app/model/preference.rb', line 7

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



47
48
49
50
# File 'backend/app/model/preference.rb', line 47

def self.parsed_defaults_for(filter)
  pref = self[filter.merge(:repo_id => RequestContext.get(:repo_id))]
  pref ? pref.parsed_defaults : {}
end

.repo_defaultsObject



72
73
74
# File 'backend/app/model/preference.rb', line 72

def self.repo_defaults
  self.user_global_defaults.merge(self.parsed_defaults_for(:user_id => nil))
end

.sequel_to_jsonmodel(objs, opts = {}) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'backend/app/model/preference.rb', line 133

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_defaultsObject



60
61
62
63
64
65
66
67
68
69
# File 'backend/app/model/preference.rb', line 60

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_saveObject



35
36
37
38
39
# File 'backend/app/model/preference.rb', line 35

def after_save
  Notifications.notify("REFRESH_PREFERENCES")

  reset_initial_values
end

#before_saveObject



29
30
31
32
# File 'backend/app/model/preference.rb', line 29

def before_save
  super
  self.user_uniq = self.user_id || 'GLOBAL_USER'
end

#parsed_defaultsObject



42
43
44
# File 'backend/app/model/preference.rb', line 42

def parsed_defaults
  ASUtils.json_parse(self.defaults)
end

#update_from_json(json, opts = {}, apply_nested_records = true) ⇒ Object



149
150
151
152
# File 'backend/app/model/preference.rb', line 149

def update_from_json(json, opts = {}, apply_nested_records = true)
  super(json, opts.merge('defaults' => JSON(json.defaults)),
        apply_nested_records)
end