diff --git a/app/assets/javascripts/protected_branches_access_select.js.coffee b/app/assets/javascripts/protected_branches_access_select.js.coffee index 6df11146ba9..2c29513ae61 100644 --- a/app/assets/javascripts/protected_branches_access_select.js.coffee +++ b/app/assets/javascripts/protected_branches_access_select.js.coffee @@ -3,7 +3,7 @@ class @ProtectedBranchesAccessSelect @container.find(".allowed-to-merge").each (i, element) => fieldName = $(element).data('field-name') $(element).glDropdown - data: [{id: 'developers', text: 'Developers + Masters'}, {id: 'masters', text: 'Masters'}] + data: gon.merge_access_levels selectable: true fieldName: fieldName clicked: _.partial(@onSelect, element) @@ -11,9 +11,7 @@ class @ProtectedBranchesAccessSelect @container.find(".allowed-to-push").each (i, element) => fieldName = $(element).data('field-name') $(element).glDropdown - data: [{id: 'no_one', text: 'No one'}, - {id: 'developers', text: 'Developers + Masters'}, - {id: 'masters', text: 'Masters'}] + data: gon.push_access_levels selectable: true fieldName: fieldName clicked: _.partial(@onSelect, element) diff --git a/app/controllers/projects/protected_branches_controller.rb b/app/controllers/projects/protected_branches_controller.rb index 126358bfe77..ddf1824ccb9 100644 --- a/app/controllers/projects/protected_branches_controller.rb +++ b/app/controllers/projects/protected_branches_controller.rb @@ -9,7 +9,9 @@ class Projects::ProtectedBranchesController < Projects::ApplicationController def index @protected_branch = @project.protected_branches.new - gon.push({ open_branches: @project.open_branches.map { |br| { text: br.name, id: br.name, title: br.name } } }) + gon.push({ open_branches: @project.open_branches.map { |br| { text: br.name, id: br.name, title: br.name } }, + push_access_levels: ProtectedBranch::PushAccessLevel.human_access_levels.map { |id, text| { id: id, text: text } }, + merge_access_levels: ProtectedBranch::MergeAccessLevel.human_access_levels.map { |id, text| { id: id, text: text } } }) end def create diff --git a/app/models/protected_branch/merge_access_level.rb b/app/models/protected_branch/merge_access_level.rb index 3d2a6971702..d536f816317 100644 --- a/app/models/protected_branch/merge_access_level.rb +++ b/app/models/protected_branch/merge_access_level.rb @@ -4,6 +4,13 @@ class ProtectedBranch::MergeAccessLevel < ActiveRecord::Base enum access_level: [:masters, :developers] + def self.human_access_levels + { + "masters" => "Masters", + "developers" => "Developers + Masters" + }.with_indifferent_access + end + def check_access(user) if masters? user.can?(:push_code, project) if project.team.master?(user) @@ -11,4 +18,8 @@ class ProtectedBranch::MergeAccessLevel < ActiveRecord::Base user.can?(:push_code, project) if project.team.master?(user) || project.team.developer?(user) end end + + def humanize + self.class.human_access_levels[self.access_level] + end end diff --git a/app/models/protected_branch/push_access_level.rb b/app/models/protected_branch/push_access_level.rb index d446c1a03f0..bb46b39b714 100644 --- a/app/models/protected_branch/push_access_level.rb +++ b/app/models/protected_branch/push_access_level.rb @@ -4,6 +4,14 @@ class ProtectedBranch::PushAccessLevel < ActiveRecord::Base enum access_level: [:masters, :developers, :no_one] + def self.human_access_levels + { + "masters" => "Masters", + "developers" => "Developers + Masters", + "no_one" => "No one" + }.with_indifferent_access + end + def check_access(user) if masters? user.can?(:push_code, project) if project.team.master?(user) @@ -13,4 +21,8 @@ class ProtectedBranch::PushAccessLevel < ActiveRecord::Base false end end + + def humanize + self.class.human_access_levels[self.access_level] + end end diff --git a/app/views/projects/protected_branches/_protected_branch.html.haml b/app/views/projects/protected_branches/_protected_branch.html.haml index 89d606d9e20..e27dea8145d 100644 --- a/app/views/projects/protected_branches/_protected_branch.html.haml +++ b/app/views/projects/protected_branches/_protected_branch.html.haml @@ -16,12 +16,12 @@ (branch was removed from repository) %td = hidden_field_tag "allowed_to_merge_#{protected_branch.id}", protected_branch.merge_access_level.access_level - = dropdown_tag(protected_branch.merge_access_level.access_level.humanize, + = dropdown_tag(protected_branch.merge_access_level.humanize, options: { title: "Allowed To Merge", toggle_class: 'allowed-to-merge', dropdown_class: 'dropdown-menu-selectable merge', data: { field_name: "allowed_to_merge_#{protected_branch.id}", url: url, id: protected_branch.id, type: "allowed_to_merge" }}) %td = hidden_field_tag "allowed_to_push_#{protected_branch.id}", protected_branch.push_access_level.access_level - = dropdown_tag(protected_branch.push_access_level.access_level.humanize, + = dropdown_tag(protected_branch.push_access_level.humanize, options: { title: "Allowed To Push", toggle_class: 'allowed-to-push', dropdown_class: 'dropdown-menu-selectable push', data: { field_name: "allowed_to_push_#{protected_branch.id}", url: url, id: protected_branch.id, type: "allowed_to_push" }}) - if can_admin_project diff --git a/spec/features/protected_branches_spec.rb b/spec/features/protected_branches_spec.rb index 087e3677169..553d1c70461 100644 --- a/spec/features/protected_branches_spec.rb +++ b/spec/features/protected_branches_spec.rb @@ -83,11 +83,7 @@ feature 'Projected Branches', feature: true, js: true do end describe "access control" do - [ - ['developers', 'Developers + Masters'], - ['masters', 'Masters'], - ['no_one', 'No one'] - ].each do |access_type_id, access_type_name| + ProtectedBranch::PushAccessLevel.human_access_levels.each do |(access_type_id, access_type_name)| it "allows creating protected branches that #{access_type_name} can push to" do visit namespace_project_protected_branches_path(project.namespace, project) set_protected_branch_name('master') @@ -120,10 +116,7 @@ feature 'Projected Branches', feature: true, js: true do end end - [ - ['developers', 'Developers + Masters'], - ['masters', 'Masters'] - ].each do |access_type_id, access_type_name| + ProtectedBranch::MergeAccessLevel.human_access_levels.each do |(access_type_id, access_type_name)| it "allows creating protected branches that #{access_type_name} can merge to" do visit namespace_project_protected_branches_path(project.namespace, project) set_protected_branch_name('master')