object_helpers.rb 7.16 KB
Newer Older
1
module ObjectHelpers
jplang's avatar
jplang committed
2 3 4 5
  def User.generate!(attributes={})
    @generated_user_login ||= 'user0'
    @generated_user_login.succ!
    user = User.new(attributes)
jplang's avatar
jplang committed
6
    user.login = @generated_user_login.dup if user.login.blank?
jplang's avatar
jplang committed
7 8 9 10 11 12 13 14
    user.mail = "#{@generated_user_login}@example.com" if user.mail.blank?
    user.firstname = "Bob" if user.firstname.blank?
    user.lastname = "Doe" if user.lastname.blank?
    yield user if block_given?
    user.save!
    user
  end

15 16
  def User.add_to_project(user, project, roles=nil)
    roles = Role.find(1) if roles.nil?
jplang's avatar
jplang committed
17
    roles = [roles] if roles.is_a?(Role)
jplang's avatar
jplang committed
18 19 20
    Member.create!(:principal => user, :project => project, :roles => roles)
  end

jplang's avatar
jplang committed
21 22 23 24
  def Group.generate!(attributes={})
    @generated_group_name ||= 'Group 0'
    @generated_group_name.succ!
    group = Group.new(attributes)
jplang's avatar
jplang committed
25
    group.name = @generated_group_name.dup if group.name.blank?
jplang's avatar
jplang committed
26 27 28 29 30 31 32 33 34
    yield group if block_given?
    group.save!
    group
  end

  def Project.generate!(attributes={})
    @generated_project_identifier ||= 'project-0000'
    @generated_project_identifier.succ!
    project = Project.new(attributes)
jplang's avatar
jplang committed
35 36
    project.name = @generated_project_identifier.dup if project.name.blank?
    project.identifier = @generated_project_identifier.dup if project.identifier.blank?
jplang's avatar
jplang committed
37 38 39 40 41
    yield project if block_given?
    project.save!
    project
  end

42
  def Project.generate_with_parent!(parent, attributes={})
43 44 45 46
    project = Project.generate!(attributes) do |p|
      p.parent = parent
    end
    parent.reload if parent
47 48 49
    project
  end

jplang's avatar
jplang committed
50 51 52 53 54 55 56 57 58 59
  def IssueStatus.generate!(attributes={})
    @generated_status_name ||= 'Status 0'
    @generated_status_name.succ!
    status = IssueStatus.new(attributes)
    status.name = @generated_status_name.dup if status.name.blank?
    yield status if block_given?
    status.save!
    status
  end

jplang's avatar
jplang committed
60 61 62 63
  def Tracker.generate!(attributes={})
    @generated_tracker_name ||= 'Tracker 0'
    @generated_tracker_name.succ!
    tracker = Tracker.new(attributes)
jplang's avatar
jplang committed
64
    tracker.name = @generated_tracker_name.dup if tracker.name.blank?
jplang's avatar
jplang committed
65
    tracker.default_status ||= IssueStatus.order('position').first || IssueStatus.generate!
jplang's avatar
jplang committed
66 67 68 69 70 71 72 73 74
    yield tracker if block_given?
    tracker.save!
    tracker
  end

  def Role.generate!(attributes={})
    @generated_role_name ||= 'Role 0'
    @generated_role_name.succ!
    role = Role.new(attributes)
jplang's avatar
jplang committed
75
    role.name = @generated_role_name.dup if role.name.blank?
jplang's avatar
jplang committed
76 77 78 79 80
    yield role if block_given?
    role.save!
    role
  end

81 82
  # Generates an unsaved Issue
  def Issue.generate(attributes={})
jplang's avatar
jplang committed
83
    issue = Issue.new(attributes)
84 85
    issue.project ||= Project.find(1)
    issue.tracker ||= issue.project.trackers.first
jplang's avatar
jplang committed
86 87 88
    issue.subject = 'Generated' if issue.subject.blank?
    issue.author ||= User.find(2)
    yield issue if block_given?
89 90 91 92 93 94
    issue
  end

  # Generates a saved Issue
  def Issue.generate!(attributes={}, &block)
    issue = Issue.generate(attributes, &block)
jplang's avatar
jplang committed
95 96 97 98
    issue.save!
    issue
  end

99 100 101 102 103 104
  # Generates an issue with 2 children and a grandchild
  def Issue.generate_with_descendants!(attributes={})
    issue = Issue.generate!(attributes)
    child = Issue.generate!(:project => issue.project, :subject => 'Child1', :parent_issue_id => issue.id)
    Issue.generate!(:project => issue.project, :subject => 'Child2', :parent_issue_id => issue.id)
    Issue.generate!(:project => issue.project, :subject => 'Child11', :parent_issue_id => child.id)
105 106 107
    issue.reload
  end

108 109 110 111 112 113
  def Issue.generate_with_child!(attributes={})
    issue = Issue.generate!(attributes)
    Issue.generate!(:parent_issue_id => issue.id)
    issue.reload
  end

jplang's avatar
jplang committed
114 115 116 117 118 119 120 121 122
  def Journal.generate!(attributes={})
    journal = Journal.new(attributes)
    journal.user ||= User.first
    journal.journalized ||= Issue.first
    yield journal if block_given?
    journal.save!
    journal
  end

jplang's avatar
jplang committed
123 124 125 126
  def Version.generate!(attributes={})
    @generated_version_name ||= 'Version 0'
    @generated_version_name.succ!
    version = Version.new(attributes)
jplang's avatar
jplang committed
127
    version.name = @generated_version_name.dup if version.name.blank?
128
    version.project ||= Project.find(1)
jplang's avatar
jplang committed
129 130 131 132 133
    yield version if block_given?
    version.save!
    version
  end

134 135 136
  def TimeEntry.generate!(attributes={})
    entry = TimeEntry.new(attributes)
    entry.user ||= User.find(2)
137
    entry.issue ||= Issue.find(1) unless entry.project
138 139 140
    entry.project ||= entry.issue.project
    entry.activity ||= TimeEntryActivity.first
    entry.spent_on ||= Date.today
141
    entry.hours ||= 1.0
142 143 144 145
    entry.save!
    entry
  end

jplang's avatar
jplang committed
146 147 148 149
  def AuthSource.generate!(attributes={})
    @generated_auth_source_name ||= 'Auth 0'
    @generated_auth_source_name.succ!
    source = AuthSource.new(attributes)
jplang's avatar
jplang committed
150
    source.name = @generated_auth_source_name.dup if source.name.blank?
jplang's avatar
jplang committed
151 152 153
    yield source if block_given?
    source.save!
    source
154
  end
jplang's avatar
jplang committed
155 156 157 158 159

  def Board.generate!(attributes={})
    @generated_board_name ||= 'Forum 0'
    @generated_board_name.succ!
    board = Board.new(attributes)
jplang's avatar
jplang committed
160 161
    board.name = @generated_board_name.dup if board.name.blank?
    board.description = @generated_board_name.dup if board.description.blank?
jplang's avatar
jplang committed
162 163 164 165
    yield board if block_given?
    board.save!
    board
  end
166 167 168 169 170 171 172 173

  def Attachment.generate!(attributes={})
    @generated_filename ||= 'testfile0'
    @generated_filename.succ!
    attributes = attributes.dup
    attachment = Attachment.new(attributes)
    attachment.container ||= Issue.find(1)
    attachment.author ||= User.find(2)
jplang's avatar
jplang committed
174
    attachment.filename = @generated_filename.dup if attachment.filename.blank?
175 176 177
    attachment.save!
    attachment
  end
178 179 180 181 182 183 184 185 186 187 188

  def CustomField.generate!(attributes={})
    @generated_custom_field_name ||= 'Custom field 0'
    @generated_custom_field_name.succ!
    field = new(attributes)
    field.name = @generated_custom_field_name.dup if field.name.blank?
    field.field_format = 'string' if field.field_format.blank?
    yield field if block_given?
    field.save!
    field
  end
189 190 191 192 193 194 195 196 197 198 199 200

  def Changeset.generate!(attributes={})
    @generated_changeset_rev ||= '123456'
    @generated_changeset_rev.succ!
    changeset = new(attributes)
    changeset.repository ||= Project.find(1).repository
    changeset.revision ||= @generated_changeset_rev
    changeset.committed_on ||= Time.now
    yield changeset if block_given?
    changeset.save!
    changeset
  end
201 202 203 204 205 206 207 208

  def Query.generate!(attributes={})
    query = new(attributes)
    query.name = "Generated query" if query.name.blank?
    query.user ||= User.find(1)
    query.save!
    query
  end
209
end
210

jplang's avatar
jplang committed
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
module TrackerObjectHelpers
  def generate_transitions!(*args)
    options = args.last.is_a?(Hash) ? args.pop : {}
    if args.size == 1
      args << args.first
    end
    if options[:clear]
      WorkflowTransition.where(:tracker_id => id).delete_all
    end
    args.each_cons(2) do |old_status_id, new_status_id|
      WorkflowTransition.create!(
        :tracker => self,
        :role_id => (options[:role_id] || 1),
        :old_status_id => old_status_id,
        :new_status_id => new_status_id
      )
    end
  end
end
Tracker.send :include, TrackerObjectHelpers

232 233 234 235 236 237 238 239 240 241 242
module IssueObjectHelpers
  def close!
    self.status = IssueStatus.where(:is_closed => true).first
    save!
  end

  def generate_child!(attributes={})
    Issue.generate!(attributes.merge(:parent_issue_id => self.id))
  end
end
Issue.send :include, IssueObjectHelpers