Skip to content

Commit f9094e9

Browse files
committed
Merge branch 'develop' into 'master'
Develop See merge request gtt/redmine_supply!4
2 parents 0c3cc62 + a572d89 commit f9094e9

22 files changed

+386
-10
lines changed

app/controllers/supply_items_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def autocomplete
1515
end
1616

1717
def index
18-
@supply_items = SupplyItem.where project_id: @project.id
18+
@supply_items = SupplyItem.order(name: :asc).where project_id: @project.id
1919
end
2020

2121
def edit
@@ -59,7 +59,7 @@ def destroy
5959
private
6060

6161
def supply_item_params
62-
params[:supply_item].permit :name, :description, :unit
62+
params[:supply_item].permit :name, :description, :unit, :stock
6363
end
6464

6565
def find_supply_item

app/models/issue_supply_item.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,26 @@ class IssueSupplyItem < ActiveRecord::Base
55
validates :supply_item, presence: true
66
validates :quantity, presence: true, numericality: { only_integer: true,
77
greater_than: 0 }
8+
9+
after_destroy ->(isi){
10+
RedmineSupply::RecordIssueSupplyItemChange.(isi.issue,
11+
isi.supply_item,
12+
isi.quantity)
13+
}
14+
15+
after_create ->(isi){
16+
RedmineSupply::RecordIssueSupplyItemChange.(isi.issue,
17+
isi.supply_item,
18+
-1 * isi.quantity)
19+
}
20+
21+
after_update ->(isi){
22+
if isi.quantity_was != isi.quantity
23+
RedmineSupply::RecordIssueSupplyItemChange.(isi.issue,
24+
isi.supply_item,
25+
isi.quantity_was - isi.quantity)
26+
end
27+
}
28+
29+
830
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class IssueSupplyItemChange < SupplyItemJournal
2+
belongs_to :issue
3+
4+
validates :issue, presence: true
5+
6+
def activity_title
7+
I18n.t :text_issue_supply_item_changed, id: issue.id, name: supply_item.name, change: change_text
8+
end
9+
10+
def activity_url
11+
{
12+
controller: 'issues',
13+
action: 'show',
14+
id: issue.to_param
15+
}
16+
end
17+
18+
def change_text
19+
# we want to display the change on the issue, thus we have to inverse the item
20+
# stock change
21+
change = -1 * self.change
22+
if change > 0
23+
"+" + supply_item.stock_text(change)
24+
elsif change < 0
25+
supply_item.stock_text change
26+
else
27+
I18n.t :text_supply_item_nochange
28+
end
29+
end
30+
end

app/models/supply_item.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
# frozen_string_literal: true
2+
13
class SupplyItem < ActiveRecord::Base
24
belongs_to :project
35
has_many :issue_supply_items
46
has_many :issues, through: :issue_supply_items
57

8+
has_many :journals, class_name: 'SupplyItemJournal', dependent: :delete_all
9+
610
validates :project, presence: true
711
validates :name, presence: true,
812
uniqueness: { case_sensitive: false,
913
scope: :project_id }
14+
validates :stock, presence: true,
15+
numericality: { only_integer: true }
1016

1117
scope :sorted, ->{ order name: :asc}
1218

@@ -23,6 +29,10 @@ class SupplyItem < ActiveRecord::Base
2329
end
2430
}
2531

32+
def stock_text(amount = self.stock)
33+
"#{amount} #{unit_name}"
34+
end
35+
2636
def unit_name
2737
I18n.t :"label_supply_item_unit_#{unit}"
2838
end

app/models/supply_item_creation.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class SupplyItemCreation < SupplyItemJournal
2+
3+
def activity_title
4+
I18n.t :text_supply_item_created, name: supply_item.name, stock: supply_item.stock_text
5+
end
6+
end

app/models/supply_item_journal.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class SupplyItemJournal < ActiveRecord::Base
2+
3+
belongs_to :supply_item
4+
belongs_to :user
5+
alias author user
6+
7+
delegate :project, to: :supply_item
8+
9+
validates :type, presence: true
10+
validates :supply_item, presence: true
11+
12+
acts_as_event(
13+
title: :activity_title,
14+
description: '',
15+
datetime: :created_at,
16+
url: :activity_url
17+
)
18+
19+
acts_as_activity_provider(
20+
scope: eager_load(supply_item: :project),
21+
author_key: 'supply_item_journals.user_id',
22+
timestamp: 'supply_item_journals.created_at',
23+
permission: :view_supply_items
24+
)
25+
26+
27+
# implement in subclasses
28+
def activity_title
29+
to_s
30+
end
31+
32+
def activity_url
33+
{
34+
controller: 'supply_items',
35+
action: 'index',
36+
project_id: supply_item.project.to_param
37+
}
38+
end
39+
40+
def change
41+
new_stock - old_stock if new_stock && old_stock
42+
end
43+
44+
end

app/models/supply_item_update.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
#
3+
class SupplyItemUpdate < SupplyItemJournal
4+
5+
def activity_title
6+
I18n.t :text_supply_item_updated, name: supply_item.name, change: change_text
7+
end
8+
9+
def change_text
10+
if change > 0
11+
"+" + supply_item.stock_text(change)
12+
elsif change < 0
13+
supply_item.stock_text change
14+
else
15+
I18n.t :text_supply_item_nochange
16+
end
17+
end
18+
19+
end

app/views/supply_items/_form.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<div class="box tabular">
44
<p><%= f.text_field :name, required: true, size: 25 %></p>
5+
<p><%= f.text_field :stock, required: true, size: 25, label: l(:field_supply_item_stock) %></p>
56
<p><%= f.select :unit, SupplyItem.units.keys.map{|u|[t(:"label_supply_item_unit_#{u}"), u]}, label: l(:field_supply_item_unit) %></p>
67
<p><%= f.text_area :description, label: l(:field_supply_item_description), rows: 10 %></p>
78
</div>

app/views/supply_items/_list.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<thead>
44
<tr>
55
<th><%= l :label_supply_item_name %></th>
6+
<th><%= l :label_supply_item_stock %></th>
67
<th><%= l :label_supply_item_description %></th>
78
<th></th>
89
</tr>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<tr>
22
<td><%= link_to supply_item.name, edit_project_supply_item_path(supply_item.project, supply_item) %></td>
3+
<td><%= supply_item.stock_text %></td>
34
<td class="text"><div><%= textilizable supply_item.description %></div></td>
45
<td><%= delete_link(project_supply_item_path(supply_item.project, supply_item)) %>
56
</tr>

0 commit comments

Comments
 (0)