From 8e9b65b01bbd9d44d4077d94f2dc4ac375cf8e85 Mon Sep 17 00:00:00 2001 From: jessib Date: Mon, 18 Nov 2013 15:44:54 -0800 Subject: Start of service level code, which will be tweaked * stores desired & effective service level * whenever desired level is changed, effective level will be updated * allows user to set their desired service level * allow admin to update desired & effective service level --- users/app/controllers/users_controller.rb | 6 ++++++ users/app/models/unauthenticated_user.rb | 2 ++ users/app/models/user.rb | 12 ++++++++++++ users/app/views/users/_edit.html.haml | 18 ++++++++++++++++++ 4 files changed, 38 insertions(+) (limited to 'users') diff --git a/users/app/controllers/users_controller.rb b/users/app/controllers/users_controller.rb index 3cbb6dc..8b4715c 100644 --- a/users/app/controllers/users_controller.rb +++ b/users/app/controllers/users_controller.rb @@ -34,6 +34,12 @@ class UsersController < UsersBaseController def edit end + ## added so updating service level works, but not sure we will actually want this. also not sure that this is place to prevent user from updating own effective service level, but here as placeholder: + def update + @user.update_attributes(params[:user]) unless (!admin? and params[:user][:effective_service_level]) + respond_with @user + end + def deactivate @user.enabled = false @user.save diff --git a/users/app/models/unauthenticated_user.rb b/users/app/models/unauthenticated_user.rb index 99a6874..0fc17d2 100644 --- a/users/app/models/unauthenticated_user.rb +++ b/users/app/models/unauthenticated_user.rb @@ -1,4 +1,6 @@ # The nil object for the user class class UnauthenticatedUser < Object + # will probably want something here to return service level as APP_CONFIG[:service_levels][0] but not sure how will be accessing. + end diff --git a/users/app/models/user.rb b/users/app/models/user.rb index a14fcb5..35212a1 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -9,6 +9,12 @@ class User < CouchRest::Model::Base property :enabled, TrueClass, :default => true + # these will be null by default. should we set to APP_CONFIG[:default_service_level] by default, or have code assume that until these get set?: + property :desired_service_level, Integer, :accessible => true + property :effective_service_level, Integer, :accessible => true + + before_save :update_effective_service_level + validates :login, :password_salt, :password_verifier, :presence => true @@ -116,4 +122,10 @@ class User < CouchRest::Model::Base def serverside? true end + + def update_effective_service_level + if self.desired_service_level_changed? + self.effective_service_level = self.desired_service_level + end + end end diff --git a/users/app/views/users/_edit.html.haml b/users/app/views/users/_edit.html.haml index b86172e..d5a0ff1 100644 --- a/users/app/views/users/_edit.html.haml +++ b/users/app/views/users/_edit.html.haml @@ -37,6 +37,24 @@ .controls = f.submit t(:save), :class => 'btn', :data => {"loading-text" => "Saving..."} + +-# TODO: probably won't want here, but here for now. Also, we will need way to ensure payment if they pick a non-free plan. +-# +-# SERVICE LEVEL +-# +- form_options = {:html => {:class => user_form_class('form-horizontal'), :id => 'update_service_level', :data => {token: session[:token]}}, :validate => true} += simple_form_for @user, form_options do |f| + %legend= t(:service_level) + - if @user != current_user + = t(:desired_service_level) + = f.select :desired_service_level, [[APP_CONFIG[:service_levels][1][:description], 1],[APP_CONFIG[:service_levels][2][:description], 2]], :selected => @user.desired_service_level || APP_CONFIG[:default_service_level] + - if @user != current_user + %p + = t(:effective_service_level) + = f.select :effective_service_level, [[APP_CONFIG[:service_levels][1][:description], 1],[APP_CONFIG[:service_levels][2][:description], 2]], :selected => @user.effective_service_level || APP_CONFIG[:default_service_level] + .control-group + .controls + = f.submit t(:save), :class => 'btn', :data => {"loading-text" => "Saving..."} -# -# DESTROY ACCOUNT -# -- cgit v1.2.3 From 35761333404e3cc2c93bca23036d0fd8e47fd10b Mon Sep 17 00:00:00 2001 From: jessib Date: Tue, 3 Dec 2013 12:17:51 -0800 Subject: Add ServiceLevel class to wrap config and give accessors. Has some hacky parts, but seems like okay generic start for now. --- users/app/models/service_level.rb | 31 +++++++++++++++++++++++++++++++ users/app/models/user.rb | 22 +++++++++++++++++----- users/app/views/users/_edit.html.haml | 6 ++++-- 3 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 users/app/models/service_level.rb (limited to 'users') diff --git a/users/app/models/service_level.rb b/users/app/models/service_level.rb new file mode 100644 index 0000000..ac5244f --- /dev/null +++ b/users/app/models/service_level.rb @@ -0,0 +1,31 @@ +class ServiceLevel + + def initialize(attributes = {}) + @level = attributes[:level] || APP_CONFIG[:default_service_level] + end + + def level + @level + end + + def name + APP_CONFIG[:service_levels][@level][:name] + end + + def cert_prefix + APP_CONFIG[:service_levels][@level][:cert_prefix] + end + + def description + APP_CONFIG[:service_levels][@level][:description] + end + + def cost + APP_CONFIG[:service_levels][@level][:cost] + end + + def quota + APP_CONFIG[:service_levels][@level][:quota] + end + +end diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 35212a1..621ff4e 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -9,9 +9,9 @@ class User < CouchRest::Model::Base property :enabled, TrueClass, :default => true - # these will be null by default. should we set to APP_CONFIG[:default_service_level] by default, or have code assume that until these get set?: - property :desired_service_level, Integer, :accessible => true - property :effective_service_level, Integer, :accessible => true + # these will be null by default but we shouldn't ever pull them directly, but only via the methods that will return the full ServiceLevel + property :desired_service_level_code, Integer, :accessible => true + property :effective_service_level_code, Integer, :accessible => true before_save :update_effective_service_level @@ -100,6 +100,16 @@ class User < CouchRest::Model::Base @identity = Identity.for(self) end + def desired_service_level + code = self.desired_service_level_code || APP_CONFIG[:default_service_level] + ServiceLevel.new({level: code}) + end + + def effective_service_level + code = self.effective_service_level_code || self.desired_service_level.level + ServiceLevel.new({level: code}) + end + protected ## @@ -124,8 +134,10 @@ class User < CouchRest::Model::Base end def update_effective_service_level - if self.desired_service_level_changed? - self.effective_service_level = self.desired_service_level + # TODO: Is this always the case? Might there be a situation where the admin has set the effective service level and we don't want it changed to match the desired one? + if self.desired_service_level_code_changed? + self.effective_service_level_code = self.desired_service_level_code end end + end diff --git a/users/app/views/users/_edit.html.haml b/users/app/views/users/_edit.html.haml index d5a0ff1..d2c2d95 100644 --- a/users/app/views/users/_edit.html.haml +++ b/users/app/views/users/_edit.html.haml @@ -47,11 +47,13 @@ %legend= t(:service_level) - if @user != current_user = t(:desired_service_level) - = f.select :desired_service_level, [[APP_CONFIG[:service_levels][1][:description], 1],[APP_CONFIG[:service_levels][2][:description], 2]], :selected => @user.desired_service_level || APP_CONFIG[:default_service_level] + - sl1 = ServiceLevel.new({level: 1}) #ugly but okay for now + - sl2 = ServiceLevel.new({level: 2}) + = f.select :desired_service_level_code, [[sl1.description, sl1.level],[sl2.description, sl2.level]], :selected => @user.desired_service_level.level - if @user != current_user %p = t(:effective_service_level) - = f.select :effective_service_level, [[APP_CONFIG[:service_levels][1][:description], 1],[APP_CONFIG[:service_levels][2][:description], 2]], :selected => @user.effective_service_level || APP_CONFIG[:default_service_level] + = f.select :effective_service_level_code, [[sl1.description, sl1.level],[sl2.description, sl2.level]], :selected => @user.effective_service_level.level .control-group .controls = f.submit t(:save), :class => 'btn', :data => {"loading-text" => "Saving..."} -- cgit v1.2.3 From 7d4a9658c29cad526cfe5c952f71109e8eb304e7 Mon Sep 17 00:00:00 2001 From: jessib Date: Tue, 3 Dec 2013 15:07:11 -0800 Subject: Some simplification of code. --- users/app/models/service_level.rb | 26 +++++++------------------- users/app/models/user.rb | 6 +++--- users/app/views/users/_edit.html.haml | 6 ++---- 3 files changed, 12 insertions(+), 26 deletions(-) (limited to 'users') diff --git a/users/app/models/service_level.rb b/users/app/models/service_level.rb index ac5244f..299aaf1 100644 --- a/users/app/models/service_level.rb +++ b/users/app/models/service_level.rb @@ -1,31 +1,19 @@ class ServiceLevel def initialize(attributes = {}) - @level = attributes[:level] || APP_CONFIG[:default_service_level] + @id = attributes[:id] || APP_CONFIG[:default_service_level] end - def level - @level + def self.authenticated_select_options + APP_CONFIG[:service_levels].map { |id,config_hash| [config_hash[:description], id] if config_hash[:name] != 'anonymous'}.compact end - def name - APP_CONFIG[:service_levels][@level][:name] + def id + @id end - def cert_prefix - APP_CONFIG[:service_levels][@level][:cert_prefix] - end - - def description - APP_CONFIG[:service_levels][@level][:description] - end - - def cost - APP_CONFIG[:service_levels][@level][:cost] - end - - def quota - APP_CONFIG[:service_levels][@level][:quota] + def config_hash + APP_CONFIG[:service_levels][@id] end end diff --git a/users/app/models/user.rb b/users/app/models/user.rb index 621ff4e..720f5a9 100644 --- a/users/app/models/user.rb +++ b/users/app/models/user.rb @@ -102,12 +102,12 @@ class User < CouchRest::Model::Base def desired_service_level code = self.desired_service_level_code || APP_CONFIG[:default_service_level] - ServiceLevel.new({level: code}) + ServiceLevel.new({id: code}) end def effective_service_level - code = self.effective_service_level_code || self.desired_service_level.level - ServiceLevel.new({level: code}) + code = self.effective_service_level_code || self.desired_service_level.id + ServiceLevel.new({id: code}) end protected diff --git a/users/app/views/users/_edit.html.haml b/users/app/views/users/_edit.html.haml index d2c2d95..897c54b 100644 --- a/users/app/views/users/_edit.html.haml +++ b/users/app/views/users/_edit.html.haml @@ -47,13 +47,11 @@ %legend= t(:service_level) - if @user != current_user = t(:desired_service_level) - - sl1 = ServiceLevel.new({level: 1}) #ugly but okay for now - - sl2 = ServiceLevel.new({level: 2}) - = f.select :desired_service_level_code, [[sl1.description, sl1.level],[sl2.description, sl2.level]], :selected => @user.desired_service_level.level + = f.select :desired_service_level_code, ServiceLevel.authenticated_select_options, :selected => @user.desired_service_level.id - if @user != current_user %p = t(:effective_service_level) - = f.select :effective_service_level_code, [[sl1.description, sl1.level],[sl2.description, sl2.level]], :selected => @user.effective_service_level.level + = f.select :effective_service_level_code, ServiceLevel.authenticated_select_options, :selected => @user.effective_service_level.id .control-group .controls = f.submit t(:save), :class => 'btn', :data => {"loading-text" => "Saving..."} -- cgit v1.2.3 From 6ebf095bc553345a3e0b8c48cadc3e1440c59ca5 Mon Sep 17 00:00:00 2001 From: jessib Date: Thu, 5 Dec 2013 11:11:39 -0800 Subject: We won't want service levels in production mode, and have it so this initial service level code won't break anything if it isn't set in the config. --- users/app/views/users/_edit.html.haml | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) (limited to 'users') diff --git a/users/app/views/users/_edit.html.haml b/users/app/views/users/_edit.html.haml index 897c54b..0b36d6e 100644 --- a/users/app/views/users/_edit.html.haml +++ b/users/app/views/users/_edit.html.haml @@ -42,19 +42,20 @@ -# -# SERVICE LEVEL -# -- form_options = {:html => {:class => user_form_class('form-horizontal'), :id => 'update_service_level', :data => {token: session[:token]}}, :validate => true} -= simple_form_for @user, form_options do |f| - %legend= t(:service_level) - - if @user != current_user - = t(:desired_service_level) - = f.select :desired_service_level_code, ServiceLevel.authenticated_select_options, :selected => @user.desired_service_level.id - - if @user != current_user - %p - = t(:effective_service_level) - = f.select :effective_service_level_code, ServiceLevel.authenticated_select_options, :selected => @user.effective_service_level.id - .control-group - .controls - = f.submit t(:save), :class => 'btn', :data => {"loading-text" => "Saving..."} +- if APP_CONFIG[:service_levels] + - form_options = {:html => {:class => user_form_class('form-horizontal'), :id => 'update_service_level', :data => {token: session[:token]}}, :validate => true} + = simple_form_for @user, form_options do |f| + %legend= t(:service_level) + - if @user != current_user + = t(:desired_service_level) + = f.select :desired_service_level_code, ServiceLevel.authenticated_select_options, :selected => @user.desired_service_level.id + - if @user != current_user + %p + = t(:effective_service_level) + = f.select :effective_service_level_code, ServiceLevel.authenticated_select_options, :selected => @user.effective_service_level.id + .control-group + .controls + = f.submit t(:save), :class => 'btn', :data => {"loading-text" => "Saving..."} -# -# DESTROY ACCOUNT -# -- cgit v1.2.3