diff --git a/app/controllers/registrations_controller.rb b/app/controllers/registrations_controller.rb
index c377ae7..0a2c03b 100644
--- a/app/controllers/registrations_controller.rb
+++ b/app/controllers/registrations_controller.rb
@@ -2,7 +2,8 @@
class RegistrationsController < Devise::RegistrationsController
def edit
- @user.create_default_preferences if @user.prefs == {}
+ @user = current_user
+ @prefs = @user.get_preferences(:email)
@languages = Language.sorted_by_abbreviation
@orgs = Org.where(parent_id: nil).order("name")
@other_organisations = Org.where(parent_id: nil, is_other: true).pluck(:id)
@@ -72,6 +73,7 @@
def update
if user_signed_in? then
+ @prefs = @user.get_preferences(:email)
@orgs = Org.where(parent_id: nil).order("name")
@default_org = current_user.org
@other_organisations = Org.where(parent_id: nil, is_other: true).pluck(:id)
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 6adb808..1915dc5 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -58,28 +58,36 @@
end
end
- def update_preferences
- @user = User.find(params[:user_id])
+ def update_email_preferences
prefs = params[:prefs]
- authorize @user, :update?
- # Set all preferences to false
- @user.prefs.each do |key, value|
- value.each_key do |k|
- @user.prefs[key][k] = false
- end
+ authorize current_user, :update?
+ pref = current_user.pref
+ # does user not have prefs?
+ if pref.blank?
+ pref = Pref.new
+ pref.settings = {}
+ pref.user = current_user
end
+ pref.settings[:email] = booleanize_hash(prefs)
+ pref.save
- # Sets the preferences the user wants to true
- if prefs
- prefs.each_key do |key|
- prefs[key].each_key do |k|
- @user.prefs[key.to_sym][k.to_sym] = true
- end
- end
- end
@tab = params[:tab]
- @user.save
redirect_to edit_user_registration_path(tab: @tab), notice: success_message(_('preferences'), _('saved'))
end
+ private
+
+ ##
+ # html forms return our boolean values as strings, this converts them to true/false
+ def booleanize_hash(node)
+ #leaf: convert to boolean and return
+ #hash: iterate over leaves
+ unless node.is_a?(Hash)
+ return node == "true"
+ end
+ node.each do |key, value|
+ node[key] = booleanize_hash(value)
+ end
+ end
+
end
diff --git a/app/models/pref.rb b/app/models/pref.rb
new file mode 100644
index 0000000..02e1a59
--- /dev/null
+++ b/app/models/pref.rb
@@ -0,0 +1,19 @@
+class Pref < ActiveRecord::Base
+ ##
+ # Serialize prefs to JSON
+ # The settings object only stores deviations from the default
+ serialize :settings, JSON
+
+ ##
+ # Associations
+ belongs_to :user
+
+ ##
+ # Returns the hash generated from default preferences
+ #
+ # @return [JSON] preferences hash
+ def self.default_settings
+ return Rails.configuration.branding[:preferences]
+ end
+
+end
\ No newline at end of file
diff --git a/app/models/user.rb b/app/models/user.rb
index 08f6317..678c8dd 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -1,5 +1,4 @@
class User < ActiveRecord::Base
- include GlobalHelpers
##
# Devise
@@ -20,6 +19,7 @@
has_and_belongs_to_many :perms, join_table: :users_perms
belongs_to :language
belongs_to :org
+ has_one :pref
has_many :answers
has_many :notes
has_many :exported_plans
@@ -42,24 +42,8 @@
has_many :user_identifiers
has_many :identifier_schemes, through: :user_identifiers
- ##
- # Possibly needed for active_admin
- # -relies on protected_attributes gem as syntax depricated in rails 4.2
- #accepts_nested_attributes_for :roles
- #attr_accessible :password_confirmation, :encrypted_password, :remember_me,
- # :id, :email, :firstname, :last_login,:login_count, :orcid_id,
- # :password, :shibboleth_id, :user_status_id, :surname,
- # :user_type_id, :org_id, :skip_invitation, :other_organisation,
- # :accept_terms, :role_ids, :dmponline3, :api_token,
- # :organisation, :language, :language_id, :org, :perms,
- # :confirmed_at, :org_id
-
validates :email, email: true, allow_nil: true, uniqueness: {message: _("must be unique")}
-
- validates :prefs, presence: true
- before_validation :create_default_preferences
-
##
# Scopes
default_scope { includes(:org, :perms, :plans) }
@@ -223,17 +207,6 @@
end
##
- # checks what type the user's organisation is
- #
- # @return [String] the organisation type
-=begin
- def org_type
- org_type = org.organisation_type
- return org_type
- end
-=end
-
- ##
# removes the api_token from the user
# modifies the user model
def remove_token!
@@ -273,53 +246,22 @@
end
##
+ # Return the user's preferences for a given base key
+ #
+ # @return [JSON] with symbols as keys
+ def get_preferences(key)
+ if self.pref.present? && self.pref.settings[key.to_s].present?
+ return self.pref.settings[key.to_s].deep_symbolize_keys
+ else
+ return Pref.default_settings[key]
+ end
+ end
+
+ ##
# Override devise_invitable email title
# --------------------------------------------------------------
def deliver_invitation(options = {})
super(options.merge(subject: _('A Data Management Plan in %{application_name} has been shared with you') % {application_name: Rails.configuration.branding[:application][:name]}))
end
-
- ##
- # User Notification Preferences
- def create_default_preferences
- # Set the default preferences for a new user or if existing user if null
- if self.id.nil? || self.prefs == {}
- self.prefs = self.class.create_default_preferences
- end
- end
-
- def self.create_default_preferences
- default_prefs = {
- users: {
- new_comment: true,
- admin_privileges: true,
- added_as_coowner: true
- },
- owners_and_coowners: {
- visibility_changed: true
- },
- admins: {
- template_published: true,
- template_unpublished: true,
- feedback_requested: true
- }
- }
- end
-
-# TODO: Remove this, its never called.
- # this generates a reset password link for a given user
- # which can then be sent to them with the appropriate host
- # prepended.
-=begin
- def reset_password_link
- raw, enc = Devise.token_generator.generate(self.class, :reset_password_token)
- self.reset_password_token = enc
- self.reset_password_sent_at = Time.now.utc
- save(validate: false)
-
- edit_user_password_path + '?reset_password_token=' + raw
- end
-=end
-
end
diff --git a/app/views/users/_notification_preferences.html.erb b/app/views/users/_notification_preferences.html.erb
index 819240d..ef6e11a 100644
--- a/app/views/users/_notification_preferences.html.erb
+++ b/app/views/users/_notification_preferences.html.erb
@@ -5,43 +5,49 @@