settings_to_make_issue_id_comment_fields_mandatory_v2.patch

Marius BALTEANU, 2016-12-11 02:21

Download (5.05 KB)

View differences:

app/helpers/settings_helper.rb
25 25
            {:name => 'api', :partial => 'settings/api', :label => :label_api},
26 26
            {:name => 'projects', :partial => 'settings/projects', :label => :label_project_plural},
27 27
            {:name => 'issues', :partial => 'settings/issues', :label => :label_issue_tracking},
28
            {:name => 'timelog', :partial => 'settings/timelog', :label => :label_time_tracking},
28 29
            {:name => 'attachments', :partial => 'settings/attachments', :label => :label_attachment_plural},
29 30
            {:name => 'notifications', :partial => 'settings/notifications', :label => :field_mail_notification},
30 31
            {:name => 'mail_handler', :partial => 'settings/mail_handler', :label => :label_incoming_emails},
app/models/time_entry.rb
38 38
                            :scope => joins(:project).preload(:project)
39 39

  
40 40
  validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
41
  validates_presence_of :issue_id, :if => lambda { Setting.timelog_required_fields.include?('issue_id') }
42
  validates_presence_of :comments, :if => lambda { Setting.timelog_required_fields.include?('comments') }
41 43
  validates_numericality_of :hours, :allow_nil => true, :message => :invalid
42 44
  validates_length_of :comments, :maximum => 1024, :allow_nil => true
43 45
  validates :spent_on, :date => true
app/views/settings/_timelog.html.erb
1
<%= form_tag({:action => 'edit', :tab => 'timelog'}) do %>
2

  
3
<div class="box tabular settings">
4
<p><%= setting_multiselect(:timelog_required_fields,
5
        [[l(:field_issue), 'issue_id'], [l(:field_comments), 'comments'] ]) %></p>
6

  
7
</div>
8

  
9
<%= submit_tag l(:button_save) %>
10
<% end %>
app/views/timelog/_form.html.erb
12 12
    <% end %>
13 13
  <% end %>
14 14
  <p>
15
    <%= f.text_field :issue_id, :size => 6 %>
15
    <%= f.text_field :issue_id, :size => 6, :required => Setting.timelog_required_fields.include?('issue_id') %>
16 16
    <span id="time_entry_issue">
17 17
      <%=  link_to_issue(@time_entry.issue) if @time_entry.issue.try(:visible?) %>
18 18
    </span>
19 19
  </p>
20 20
  <p><%= f.date_field :spent_on, :size => 10, :required => true %><%= calendar_for('time_entry_spent_on') %></p>
21 21
  <p><%= f.hours_field :hours, :size => 6, :required => true %></p>
22
  <p><%= f.text_field :comments, :size => 100, :maxlength => 1024 %></p>
22
  <p><%= f.text_field :comments, :size => 100, :maxlength => 1024, :required => Setting.timelog_required_fields.include?('comments') %></p>
23 23
  <p><%= f.select :activity_id, activity_collection_for_select_options(@time_entry), :required => true %></p>
24 24
  <% @time_entry.custom_field_values.each do |value| %>
25 25
    <p><%= custom_field_tag_with_label :time_entry, value %></p>
config/locales/en.yml
448 448
  setting_attachment_extensions_allowed: Allowed extensions
449 449
  setting_attachment_extensions_denied: Disallowed extensions
450 450
  setting_new_item_menu_tab: Project menu tab for creating new objects
451
  setting_timelog_required_fields: Required fields for time logs
451 452

  
452 453
  permission_add_project: Create project
453 454
  permission_add_subprojects: Create subprojects
config/settings.yml
279 279
  - '7'
280 280
new_item_menu_tab:
281 281
  default: 2
282
timelog_required_fields:
283
  serialized: true
284
  default: ''
test/unit/time_entry_test.rb
172 172
                          :activity => activity)
173 173
    assert_equal project.id, te.project.id
174 174
  end
175

  
176
  def test_create_with_required_issue_id_and_comment_should_be_validated
177
    with_settings :timelog_required_fields => ['issue_id' , 'comments'] do
178
      entry = TimeEntry.new(:project => Project.find(1), :spent_on => Date.today, :user => User.find(1), :activity => TimeEntryActivity.first, :hours => 1)
179

  
180
      assert !entry.save
181
      assert_equal ["Comment cannot be blank", "Issue cannot be blank"], entry.errors.full_messages.sort
182
    end
183
  end
175 184
end