summaryrefslogtreecommitdiff
path: root/app/models/protectable_dropdown.rb
diff options
context:
space:
mode:
authorJames Edwards-Jones <jedwardsjones@gitlab.com>2017-04-03 17:10:58 +0100
committerJames Edwards-Jones <jedwardsjones@gitlab.com>2017-04-03 17:19:53 +0100
commitb8c7bef5c092152ea85d1840e587cfc04293e1d7 (patch)
tree51338a1599fa24d4e42c4eb7b6c02ac91555a73c /app/models/protectable_dropdown.rb
parent65f3d5062f081d8f8ebf727a3408650d90ec9711 (diff)
downloadgitlab-ce-b8c7bef5c092152ea85d1840e587cfc04293e1d7.tar.gz
Extracted ProtectableDropdown to clean up Project#open_branches
Makes it clear this is only used in dropdowns, instead of cluttering up Project class. Since we only care about branch names, it is also possible to refactor out a lot of the set/reject logic. A benchmark on Array/Set subtraction favoured using Arrays. This was with 5000 ‘branches’ and 2000 ‘protections’ to ensure a similar comparison to the commit which introduced using Set for intersection. Comparison: array subtraction: 485.8 i/s set subtraction: 128.7 i/s - 3.78x slower
Diffstat (limited to 'app/models/protectable_dropdown.rb')
-rw-r--r--app/models/protectable_dropdown.rb26
1 files changed, 26 insertions, 0 deletions
diff --git a/app/models/protectable_dropdown.rb b/app/models/protectable_dropdown.rb
new file mode 100644
index 00000000000..c9b2b213cd2
--- /dev/null
+++ b/app/models/protectable_dropdown.rb
@@ -0,0 +1,26 @@
+class ProtectableDropdown
+ def initialize(project, ref_type)
+ @project = project
+ @ref_type = ref_type
+ end
+
+ # Tags/branches which are yet to be individually protected
+ def protectable_ref_names
+ non_wildcard_protections = protections.reject(&:wildcard?)
+ refs.map(&:name) - non_wildcard_protections.map(&:name)
+ end
+
+ def hash
+ protectable_ref_names.map { |ref_name| { text: ref_name, id: ref_name, title: ref_name } }
+ end
+
+ private
+
+ def refs
+ @project.repository.public_send(@ref_type)
+ end
+
+ def protections
+ @project.public_send("protected_#{@ref_type}")
+ end
+end