diff --git a/app/controllers/roles_controller.rb b/app/controllers/roles_controller.rb index d9d539a..35c95d6 100644 --- a/app/controllers/roles_controller.rb +++ b/app/controllers/roles_controller.rb @@ -5,7 +5,8 @@ def create @role = Role.new(role_params) authorize @role - @role.access_level = params[:role][:access_level].to_i + access_level = params[:role][:access_level].to_i + set_access_level(access_level) if params[:user].present? message = _('User added to project') user = User.find_by(email: params[:user]) @@ -31,7 +32,8 @@ def update @role = Role.find(params[:id]) authorize @role - @role.access_level = params[:role][:access_level].to_i + access_level = params[:role][:access_level].to_i + set_access_level(access_level) if @role.update_attributes(role_params) flash[:notice] = _('Sharing details successfully updated.') UserMailer.permissions_change_notification(@role).deliver @@ -56,6 +58,19 @@ private def role_params - params.require(:role).permit(:plan_id, :access_level) + params.require(:role).permit(:plan_id) end + + def set_access_level(access_level) + if access_level >= 1 + @role.commenter = true + end + if access_level >= 2 + @role.editor = true + end + if access_level >= 3 + @role.administrator = true + end + end + end \ No newline at end of file diff --git a/app/models/role.rb b/app/models/role.rb index cf49c87..cee961b 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -1,8 +1,6 @@ class Role < ActiveRecord::Base include FlagShihTzu - before_validation :check_access_level - ## # Associations belongs_to :user @@ -14,6 +12,7 @@ has_flags 1 => :creator, 2 => :administrator, 3 => :editor, + 4 => :commenter, column: 'access' validates :user, :plan, :access, presence: true @@ -24,41 +23,17 @@ # 3 if the user is an administrator # 2 if the user is an editor # 1 if the user can only read + # used to facilliatte formtastic # # @return [Integer] def access_level - if self.administrator? then + if self.administrator? return 3 - elsif self.editor? then + elsif self.editor? return 2 - else + elsif self.commenter? return 1 end end - ## - # define a new access level for the current project group - # if >=3, the user is a project administrator - # if >=2, the user is an editor - # - # @param new_access_level [Integer] the access level to give the user - def access_level=(new_access_level) - new_access_level = new_access_level.to_i - if new_access_level >= 3 then - self.administrator = true - else - self.administrator = false - end - if new_access_level >= 2 then - self.editor = true - else - self.editor = false - end - self.creator = true unless self.administrator? || self.editor? - end - - # Ensures that the access attribute is set (will default to creator - see logic in access_level=) - def check_access_level - self.access_level = self.access_level - end end diff --git a/app/views/plans/share.html.erb b/app/views/plans/share.html.erb index 6852681..2b9c5df 100644 --- a/app/views/plans/share.html.erb +++ b/app/views/plans/share.html.erb @@ -33,7 +33,7 @@ <%= _('Owner')%> <% else %> <%= form_for role, :url => {:controller => :roles, :action => :update, :id => role.id }, :html=>{:method=>:put} do |f| %> - <%= f.select :access_level, {_('Co-owner') => 3, _('Edit') => 2, _('Read only') => 1}, {}, {:id => "#{role.id}-can-edit", :class => "toggle-existing-user-access has-tooltip", 'data-toggle' => "tooltip", 'title' => _('Editors can contribute to plans. Co-owners have additional rights to edit plan details and control access.') } %> + <%= f.select :access_level, {_('Co-owner') => 3, _('Editor') => 2, _('Read only') => 1}, {}, {:id => "#{role.id}-can-edit", :class => "toggle-existing-user-access has-tooltip", 'data-toggle' => "tooltip", 'title' => _('Editors can contribute to plans. Co-owners have additional rights to edit plan details and control access.') } %> <% end %> <% end %> @@ -62,7 +62,7 @@ <%= user.email_field :email, :for => :user, :name => "user", :label => false, placeholder: _('Email') %> <% end %>

<%= _('Permissions')%>:

- <%= f.select :access_level, [[_('Co-owner'), 3], [ _('Edit') , 2], [ _('Read only'), 1]], {}, {:class => 'has-tooltip', 'data-toggle' => "tooltip", 'title' => _('Editors can contribute to plans. Co-owners have additional rights to edit plan details and control access.') } %> + <%= f.select :access_level, [[_('Co-owner'), 3], [ _('Editor') , 2], [ _('Read only'), 1]], {}, {:class => 'has-tooltip', 'data-toggle' => "tooltip", 'title' => _('Editors can contribute to plans. Co-owners have additional rights to edit plan details and control access.') } %> <% end %> <%= f.actions do %> <%= f.action :submit, :label => _('Add collaborator'), :button_html => { :class => "btn btn-primary" } %> diff --git a/app/views/user_mailer/permissions_change_notification.html.erb b/app/views/user_mailer/permissions_change_notification.html.erb index 7cb8c10..f44b1fd 100644 --- a/app/views/user_mailer/permissions_change_notification.html.erb +++ b/app/views/user_mailer/permissions_change_notification.html.erb @@ -4,9 +4,10 @@ access_level = "read-only" if @role.editor? access_level = "editor" -elsif @role.administrator? +end +if @role.administrator? access_level = "co-owner" end %> -

<%= ('Your permissions relating to') %>"<%= link_to @role.plan.title, url_for(action: 'show', controller: 'plan', id: @role.plan.id, locale: I18n.default_locale) %>"<%= _(' have changed. You now have') %><%= access_level %> <%= _('access.') %>

+

<%= ('Your permissions relating to') %>"<%= link_to @role.plan.title, url_for(action: 'show', controller: 'plans', id: @role.plan.id, locale: I18n.default_locale) %>"<%= _(' have changed. You now have') %><%= access_level %> <%= _('access.') %>