Commit 4c76fa8e1c19134334546331f2844e0bcab6ce67
Committed by
Rodrigo Souto
1 parent
2a82af5f
Exists in
master
and in
29 other branches
Makes price details to work with "other costs"
closes ActionItem2519
Showing
8 changed files
with
36 additions
and
19 deletions
Show diff stats
app/models/price_detail.rb
@@ -4,11 +4,11 @@ class PriceDetail < ActiveRecord::Base | @@ -4,11 +4,11 @@ class PriceDetail < ActiveRecord::Base | ||
4 | validates_presence_of :product_id | 4 | validates_presence_of :product_id |
5 | 5 | ||
6 | belongs_to :production_cost | 6 | belongs_to :production_cost |
7 | - validates_presence_of :production_cost | 7 | + # Do not validates_presence_of production_cost. We may have undefined other costs. |
8 | validates_uniqueness_of :production_cost_id, :scope => :product_id | 8 | validates_uniqueness_of :production_cost_id, :scope => :product_id |
9 | 9 | ||
10 | def name | 10 | def name |
11 | - production_cost.name | 11 | + production_cost.nil? ? _('other costs') : production_cost.name |
12 | end | 12 | end |
13 | 13 | ||
14 | def price | 14 | def price |
app/models/product.rb
@@ -191,11 +191,13 @@ class Product < ActiveRecord::Base | @@ -191,11 +191,13 @@ class Product < ActiveRecord::Base | ||
191 | (price - total_production_cost.to_f).zero? | 191 | (price - total_production_cost.to_f).zero? |
192 | end | 192 | end |
193 | 193 | ||
194 | - def update_price_details(price_details) | ||
195 | - self.price_details.destroy_all | ||
196 | - price_details.each do |price_detail| | ||
197 | - self.price_details.create(price_detail) | 194 | + def update_price_details(new_price_details) |
195 | + price_details.destroy_all | ||
196 | + new_price_details.each do |detail| | ||
197 | + price_details.create(detail) | ||
198 | end | 198 | end |
199 | + reload # to remove temporary duplicated price_details | ||
200 | + price_details | ||
199 | end | 201 | end |
200 | 202 | ||
201 | def price_description_percentage | 203 | def price_description_percentage |
app/views/manage_products/_display_price_details.rhtml
@@ -10,7 +10,7 @@ | @@ -10,7 +10,7 @@ | ||
10 | </li> | 10 | </li> |
11 | <% @product.price_details.each do |price_detail| %> | 11 | <% @product.price_details.each do |price_detail| %> |
12 | <li> | 12 | <li> |
13 | - <div class='price-detail-name'><%= "%s:" % price_detail.production_cost.name %></div> | 13 | + <div class='price-detail-name'><%= "%s:" % price_detail.name %></div> |
14 | <div class='price-detail-price'><%= float_to_currency(price_detail.price) %></div> | 14 | <div class='price-detail-price'><%= float_to_currency(price_detail.price) %></div> |
15 | </li> | 15 | </li> |
16 | <% end %> | 16 | <% end %> |
app/views/manage_products/_edit_price_details.rhtml
1 | <% price_details.each do |price_detail| %> | 1 | <% price_details.each do |price_detail| %> |
2 | <tr id='<%= "price-detail-#{price_detail.id}" %>'> | 2 | <tr id='<%= "price-detail-#{price_detail.id}" %>'> |
3 | <td><%= select_production_cost(@product, price_detail.production_cost_id) %></td> | 3 | <td><%= select_production_cost(@product, price_detail.production_cost_id) %></td> |
4 | - <td><%= labelled_form_field(environment.currency_unit, text_field_tag('price_details[][price]', price_detail.formatted_value(:price), :class => 'numbers-only price-details-price')) %></td> | 4 | + <td><%= labelled_form_field(environment.currency_unit, text_field_tag('price_details[][price]', price_detail.formatted_value(:price), :class => 'numbers-only price-details-price', :size => 6)) %></td> |
5 | <td> | 5 | <td> |
6 | <%= link_to_remote(_('Remove'), | 6 | <%= link_to_remote(_('Remove'), |
7 | :update => "price-detail-#{price_detail.id}", | 7 | :update => "price-detail-#{price_detail.id}", |
app/views/manage_products/_manage_product_details.rhtml
@@ -34,7 +34,7 @@ | @@ -34,7 +34,7 @@ | ||
34 | <table id='new-cost-fields'> | 34 | <table id='new-cost-fields'> |
35 | <tr> | 35 | <tr> |
36 | <td><%= select_production_cost(@product) %></td> | 36 | <td><%= select_production_cost(@product) %></td> |
37 | - <td><%= labelled_form_field(environment.currency_unit, text_field_tag('price_details[][price]', nil, :class => 'numbers-only price-details-price')) %></td> | 37 | + <td><%= labelled_form_field(environment.currency_unit, text_field_tag('price_details[][price]', nil, :class => 'numbers-only price-details-price', :size => 6)) %></td> |
38 | <td><%= link_to(_('Cancel'), '#', {:class => 'cancel-new-cost'}) %></td> | 38 | <td><%= link_to(_('Cancel'), '#', {:class => 'cancel-new-cost'}) %></td> |
39 | </tr> | 39 | </tr> |
40 | </table> | 40 | </table> |
public/javascripts/manage-products.js
@@ -23,13 +23,24 @@ | @@ -23,13 +23,24 @@ | ||
23 | return false; | 23 | return false; |
24 | }); | 24 | }); |
25 | 25 | ||
26 | - $("#manage-product-details-form").live('submit', function(data) { | 26 | + $('#manage-product-details-form').live('submit', function(data) { |
27 | var form = this; | 27 | var form = this; |
28 | $(form).find('.loading-area').addClass('small-loading'); | 28 | $(form).find('.loading-area').addClass('small-loading'); |
29 | $(form).css('cursor', 'progress'); | 29 | $(form).css('cursor', 'progress'); |
30 | - $.post(form.action, $(form).serialize(), function(data) { | ||
31 | - $("#display-manage-price-details").html(data); | ||
32 | - $("#manage-product-details-button").show(); | 30 | + var request = $.ajax(form.action, { |
31 | + type: 'POST', | ||
32 | + dataType: 'html', | ||
33 | + data: $(form).serialize() | ||
34 | + }); | ||
35 | + request.done(function(data, textStatus, jqXHR) { | ||
36 | + $('#display-manage-price-details').html(data); | ||
37 | + $('#manage-product-details-button').show(); | ||
38 | + }); | ||
39 | + request.fail(function(jqXHR, textStatus, errorThrown) { | ||
40 | + log.error('manage_product_details', 'Request failed', errorThrown); | ||
41 | + alert('manage_product_details\nRequest failed: '+ errorThrown + | ||
42 | + '\n\nPlease, contact the site administrator.'); | ||
43 | + $('#display-manage-price-details .loading-area').hide(); | ||
33 | }); | 44 | }); |
34 | if ($('#progressbar-icon').hasClass('ui-icon-check')) { | 45 | if ($('#progressbar-icon').hasClass('ui-icon-check')) { |
35 | display_notice($('#progressbar-icon').attr('data-price-described-notice')); | 46 | display_notice($('#progressbar-icon').attr('data-price-described-notice')); |
public/stylesheets/application.css
@@ -3003,6 +3003,10 @@ div#activation_enterprise label, div#activation_enterprise input, div#activation | @@ -3003,6 +3003,10 @@ div#activation_enterprise label, div#activation_enterprise input, div#activation | ||
3003 | display: inline-block; | 3003 | display: inline-block; |
3004 | } | 3004 | } |
3005 | 3005 | ||
3006 | +#manage-product-details-form .formfieldline { | ||
3007 | + white-space: nowrap; | ||
3008 | +} | ||
3009 | + | ||
3006 | #manage-product-details-form .formlabel, | 3010 | #manage-product-details-form .formlabel, |
3007 | #manage-product-details-form .formfield { | 3011 | #manage-product-details-form .formfield { |
3008 | display: inline-block; | 3012 | display: inline-block; |
test/unit/price_detail_test.rb
@@ -50,14 +50,14 @@ class PriceDetailTest < ActiveSupport::TestCase | @@ -50,14 +50,14 @@ class PriceDetailTest < ActiveSupport::TestCase | ||
50 | assert_equal cost, PriceDetail.find(detail.id).production_cost | 50 | assert_equal cost, PriceDetail.find(detail.id).production_cost |
51 | end | 51 | end |
52 | 52 | ||
53 | - should 'production cost be mandatory' do | ||
54 | - p = PriceDetail.new | ||
55 | - p.valid? | ||
56 | - | ||
57 | - assert p.errors.invalid?(:production_cost) | 53 | + should 'production cost not be mandatory' do |
54 | + product = fast_create(Product) | ||
55 | + price = PriceDetail.new :product=>product | ||
56 | + price.valid? | ||
57 | + assert price.errors.empty? | ||
58 | end | 58 | end |
59 | 59 | ||
60 | - should 'th production cost be unique on scope of product' do | 60 | + should 'the production cost be unique on scope of product' do |
61 | product = fast_create(Product) | 61 | product = fast_create(Product) |
62 | cost = fast_create(ProductionCost, :owner_id => Environment.default.id, :owner_type => 'environment') | 62 | cost = fast_create(ProductionCost, :owner_id => Environment.default.id, :owner_type => 'environment') |
63 | 63 |