diff options
Diffstat (limited to 'app/models')
| -rw-r--r-- | app/models/concerns/mentionable.rb | 42 | ||||
| -rw-r--r-- | app/models/key.rb | 1 | ||||
| -rw-r--r-- | app/models/note.rb | 2 | ||||
| -rw-r--r-- | app/models/repository.rb | 34 | ||||
| -rw-r--r-- | app/models/user.rb | 14 |
5 files changed, 78 insertions, 15 deletions
diff --git a/app/models/concerns/mentionable.rb b/app/models/concerns/mentionable.rb index 56849f28ff0..5b0ae411642 100644 --- a/app/models/concerns/mentionable.rb +++ b/app/models/concerns/mentionable.rb @@ -79,22 +79,36 @@ module Mentionable end end - # If the mentionable_text field is about to change, locate any *added* references and create cross references for - # them. Invoke from an observer's #before_save implementation. - def notice_added_references(p = project, a = author) - ch = changed_attributes - original, mentionable_changed = "", false - self.class.mentionable_attrs.each do |attr| - if ch[attr] - original << ch[attr] - mentionable_changed = true - end - end + # When a mentionable field is changed, creates cross-reference notes that + # don't already exist + def create_new_cross_references!(p = project, a = author) + changes = detect_mentionable_changes + + return if changes.empty? - # Only proceed if the saved changes actually include a chance to an attr_mentionable field. - return unless mentionable_changed + original_text = changes.collect { |_, vals| vals.first }.join(' ') - preexisting = references(p, self.author, original) + preexisting = references(p, self.author, original_text) create_cross_references!(p, a, preexisting) end + + private + + # Returns a Hash of changed mentionable fields + # + # Preference is given to the `changes` Hash, but falls back to + # `previous_changes` if it's empty (i.e., the changes have already been + # persisted). + # + # See ActiveModel::Dirty. + # + # Returns a Hash. + def detect_mentionable_changes + source = (changes.present? ? changes : previous_changes).dup + + mentionable = self.class.mentionable_attrs + + # Only include changed fields that are mentionable + source.select { |key, val| mentionable.include?(key) } + end end diff --git a/app/models/key.rb b/app/models/key.rb index bbc28678177..2dcae059c0e 100644 --- a/app/models/key.rb +++ b/app/models/key.rb @@ -24,6 +24,7 @@ class Key < ActiveRecord::Base validates :title, presence: true, length: { within: 0..255 } validates :key, presence: true, length: { within: 0..5000 }, format: { with: /\A(ssh|ecdsa)-.*\Z/ }, uniqueness: true + validates :key, format: { without: /\n|\r/, message: 'should be a single line' } validates :fingerprint, uniqueness: true, presence: { message: 'cannot be generated' } delegate :name, :email, to: :user, prefix: true diff --git a/app/models/note.rb b/app/models/note.rb index 68b9d433ae0..62567f471dc 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -356,7 +356,7 @@ class Note < ActiveRecord::Base end def set_references - notice_added_references(project, author) + create_new_cross_references!(project, author) end def editable? diff --git a/app/models/repository.rb b/app/models/repository.rb index c767d1051d1..6262b5c4c92 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -431,6 +431,40 @@ class Repository end end + def search_files(query, ref) + offset = 2 + args = %W(git grep -i -n --before-context #{offset} --after-context #{offset} #{query} #{ref || root_ref}) + Gitlab::Popen.popen(args, path_to_repo).first.scrub.split(/^--$/) + end + + def parse_search_result(result) + ref = nil + filename = nil + startline = 0 + + lines = result.lines + lines.each_with_index do |line, index| + if line =~ /^.*:.*:\d+:/ + ref, filename, startline = line.split(':') + startline = startline.to_i - index + break + end + end + + data = lines.map do |line| + line.sub(ref, '').sub(filename, '').sub(/^:-\d+-/, '').sub(/^::\d+:/, '') + end + + data = data.join("") + + OpenStruct.new( + filename: filename, + ref: ref, + startline: startline, + data: data + ) + end + private def cache diff --git a/app/models/user.rb b/app/models/user.rb index dc84f5141d8..fb330ff7185 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -177,6 +177,10 @@ class User < ActiveRecord::Base # Note: When adding an option, it MUST go on the end of the array. enum dashboard: [:projects, :stars] + # User's Project preference + # Note: When adding an option, it MUST go on the end of the array. + enum project_view: [:readme, :activity] + alias_attribute :private_token, :authentication_token delegate :path, to: :namespace, allow_nil: true, prefix: true @@ -322,6 +326,16 @@ class User < ActiveRecord::Base @reset_token end + def disable_two_factor! + update_attributes( + two_factor_enabled: false, + encrypted_otp_secret: nil, + encrypted_otp_secret_iv: nil, + encrypted_otp_secret_salt: nil, + otp_backup_codes: nil + ) + end + def namespace_uniq namespace_name = self.username existing_namespace = Namespace.by_path(namespace_name) |
