Commit 2249f490670d7bb1276a8823cc639d0ebe402e75

Authored by Aurélio A. Heckert
1 parent 1b3f0ea9

Makes price details to work with "other costs"

closes ActionItem2519
app/models/price_detail.rb
... ... @@ -4,11 +4,11 @@ class PriceDetail < ActiveRecord::Base
4 4 validates_presence_of :product_id
5 5  
6 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 8 validates_uniqueness_of :production_cost_id, :scope => :product_id
9 9  
10 10 def name
11   - production_cost.name
  11 + production_cost.nil? ? _('other costs') : production_cost.name
12 12 end
13 13  
14 14 def price
... ...
app/models/product.rb
... ... @@ -191,11 +191,13 @@ class Product < ActiveRecord::Base
191 191 (price - total_production_cost.to_f).zero?
192 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 198 end
  199 + reload # to remove temporary duplicated price_details
  200 + price_details
199 201 end
200 202  
201 203 def price_description_percentage
... ...
app/views/manage_products/_display_price_details.rhtml
... ... @@ -10,7 +10,7 @@
10 10 </li>
11 11 <% @product.price_details.each do |price_detail| %>
12 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 14 <div class='price-detail-price'><%= float_to_currency(price_detail.price) %></div>
15 15 </li>
16 16 <% end %>
... ...
app/views/manage_products/_edit_price_details.rhtml
1 1 <% price_details.each do |price_detail| %>
2 2 <tr id='<%= "price-detail-#{price_detail.id}" %>'>
3 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 5 <td>
6 6 <%= link_to_remote(_('Remove'),
7 7 :update => "price-detail-#{price_detail.id}",
... ...
app/views/manage_products/_manage_product_details.rhtml
... ... @@ -34,7 +34,7 @@
34 34 <table id='new-cost-fields'>
35 35 <tr>
36 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 38 <td><%= link_to(_('Cancel'), '#', {:class => 'cancel-new-cost'}) %></td>
39 39 </tr>
40 40 </table>
... ...
public/javascripts/manage-products.js
... ... @@ -23,13 +23,24 @@
23 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 27 var form = this;
28 28 $(form).find('.loading-area').addClass('small-loading');
29 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 45 if ($('#progressbar-icon').hasClass('ui-icon-check')) {
35 46 display_notice($('#progressbar-icon').attr('data-price-described-notice'));
... ...
public/stylesheets/application.css
... ... @@ -2964,6 +2964,10 @@ div#activation_enterprise label, div#activation_enterprise input, div#activation
2964 2964 display: inline-block;
2965 2965 }
2966 2966  
  2967 +#manage-product-details-form .formfieldline {
  2968 + white-space: nowrap;
  2969 +}
  2970 +
2967 2971 #manage-product-details-form .formlabel,
2968 2972 #manage-product-details-form .formfield {
2969 2973 display: inline-block;
... ...
test/unit/price_detail_test.rb
... ... @@ -50,14 +50,14 @@ class PriceDetailTest &lt; ActiveSupport::TestCase
50 50 assert_equal cost, PriceDetail.find(detail.id).production_cost
51 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 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 61 product = fast_create(Product)
62 62 cost = fast_create(ProductionCost, :owner_id => Environment.default.id, :owner_type => 'environment')
63 63  
... ...