PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/rails_plugin/tests/mingle_stubs.rb

https://github.com/ThoughtWorksStudios/mingle_hg_plugin
Ruby | 108 lines | 80 code | 19 blank | 9 comment | 6 complexity | 0a66b23e7aac9ebc6868c4120ad55286 MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0, JSON
  1. # Copyright 2010 ThoughtWorks, Inc. Licensed under the Apache License, Version 2.0.
  2. # mixed into all repository configuration models. allows us to treat repository path
  3. # changes as separate delete and create steps, allowing for cleanup, while treating
  4. # user and password updates as simple updates. (perhaps, we should treat all changes
  5. # as 'new' repositories, but... initial repository caching is so expensive...)
  6. module RepositoryModelHelper
  7. def self.included(base)
  8. base.extend ClassMethods
  9. end
  10. module ClassMethods
  11. def create_or_update(project_id, id, options={})
  12. options = options.symbolize_keys
  13. options.delete(:project_id)
  14. options.delete(:id)
  15. if !id.blank? && config = self.find_by_project_id_and_id(project_id, id)
  16. if config.username != options[:username].to_s.strip
  17. config.password = nil
  18. end
  19. if config.repository_location_changed?(options.dup)
  20. config.mark_for_deletion
  21. create_or_update(project_id, nil, config.clone_repository_options.merge(options))
  22. else
  23. config.update_attributes(options)
  24. config
  25. end
  26. else
  27. existing = find_project(project_id).repository_configuration
  28. if !existing
  29. options.merge! :project_id => project_id
  30. self.create(options)
  31. else
  32. errored_config = self.new(options)
  33. errored_config.errors.add_to_base("Could not create the new repository configuration because a repository configuration already exists.")
  34. errored_config
  35. end
  36. end
  37. end
  38. def find_project(project_id)
  39. Project.find(project_id)
  40. end
  41. end
  42. #overwrite this method if behaviour is different
  43. def mark_for_deletion
  44. update_attribute :marked_for_deletion, true
  45. end
  46. end
  47. # Unfortunately, Mingle is dependent upon a Repository::NoSuchRevisionError
  48. # defined in its code base...
  49. class Repository
  50. class NoSuchRevisionError < StandardError
  51. end
  52. end
  53. # stub Mingle's Project class
  54. class Project
  55. @@instances = {}
  56. class << self
  57. def register_instance_for_find(project)
  58. @@instances[project.id] = project
  59. end
  60. def clear_find_registry
  61. @@instances = {}
  62. end
  63. def find(*args)
  64. @@last_requested = args.first
  65. @@instances[@@last_requested]
  66. end
  67. def current
  68. find(@@last_requested)
  69. end
  70. end
  71. def encrypt(text)
  72. "ENCRYPTED" + text
  73. end
  74. def decrypt(text)
  75. text.gsub(/\AENCRYPTED/, "")
  76. end
  77. def id
  78. 3487
  79. end
  80. def new_record?
  81. false
  82. end
  83. def identifier
  84. "test_project"
  85. end
  86. def repository_configuration
  87. nil
  88. end
  89. end