Permalink
Browse files

Refactored calculators to support configuration through admin interface.

  • Loading branch information...
1 parent 795990d commit dfcb54465e4cefde4279938edab1979a57d2d817 @schof schof committed Jul 16, 2009
Showing with 260 additions and 156 deletions.
  1. +0 −1 .gitignore
  2. +2 −0 app/controllers/admin/calculators_controller.rb
  3. +32 −17 app/controllers/admin/shipping_methods_controller.rb
  4. +10 −1 app/helpers/admin/base_helper.rb
  5. +2 −0 app/helpers/admin/calculators_helper.rb
  6. +17 −0 app/models/calculator.rb
  7. +2 −0 app/models/shipping_calculator.rb
  8. +13 −3 app/models/shipping_method.rb
  9. +10 −2 app/views/admin/shipping_methods/_form.html.erb
  10. +1 −1 app/views/admin/shipping_methods/edit.html.erb
  11. +1 −1 app/views/admin/shipping_methods/index.html.erb
  12. +3 −4 app/views/layouts/admin.html.erb
  13. +5 −2 config/locales/en-US.yml
  14. +2 −1 config/routes.rb
  15. +22 −0 db/migrate/20090715172345_create_calculators.rb
  16. +12 −0 db/sample/calculators.yml
  17. +15 −0 db/sample/preferences.yml
  18. +3 −10 db/sample/shipping_methods.yml
  19. +0 −16 db/sample/zone_members.yml
  20. +0 −6 db/sample/zones.yml
  21. +4 −0 generate
  22. +13 −0 public/javascripts/calculator.js
  23. +4 −0 public/stylesheets/admin/admin.css
  24. +0 −59 spec/models/shipping_method_spec.rb
  25. +11 −1 test/factories.rb
  26. +7 −0 test/fixtures/calculators.yml
  27. +8 −0 test/functional/calculators_controller_test.rb
  28. +7 −1 test/test_helper.rb
  29. +6 −0 test/unit/calculator_test.rb
  30. +4 −0 test/unit/helpers/calculators_helper_test.rb
  31. +37 −0 test/unit/shppping_method_test.rb
  32. +7 −0 vendor/extensions/flat_rate_shipping/app/models/flat_rate_shipping_calculator.rb
  33. +0 −1 vendor/extensions/flat_rate_shipping/flat_rate_shipping_extension.rb
  34. +0 −7 vendor/extensions/flat_rate_shipping/lib/flat_rate_shipping_configuration.rb
  35. +0 −22 vendor/extensions/flat_rate_shipping/lib/spree/flat_rate_shipping/config.rb
View
@@ -39,7 +39,6 @@ spree_dev
spree_test
tmp
vendor/rails
-vendor/extensions
public/images/reviews_ratings.png
public/javascripts/jquery.lightbox.js
public/javascripts/jquery.product_reviews_ratings.*
@@ -0,0 +1,2 @@
+class Admin::CalculatorsController < Admin::BaseController
+end
@@ -1,19 +1,34 @@
+
+
class Admin::ShippingMethodsController < Admin::BaseController
- resource_controller
- before_filter :load_data
- layout 'admin'
-
- update.response do |wants|
- wants.html { redirect_to collection_url }
- end
-
-
- create.response do |wants|
- wants.html { redirect_to collection_url }
- end
-
- private
- def load_data
- @available_zones = Zone.find :all, :order => :name
- end
+ resource_controller
+ before_filter :load_data
+ layout 'admin'
+
+ update.before do
+ calc_type = params[:shipping_method][:calculator_type].constantize
+ next if calc_type == @shipping_method.calculator.class
+ @shipping_method.calculator = calc_type.new
+ @shipping_method.save
+ end
+
+ update.response do |wants|
+ wants.html { redirect_to collection_url }
+ end
+
+ create.response do |wants|
+ wants.html { redirect_to collection_url }
+ end
+
+ private
+ def build_object
+ @object ||= end_of_association_chain.send parent? ? :build : :new, object_params
+ @object.calculator = params[:shipping_method][:calculator_type].constantize.new if params[:shipping_method]
+ end
+
+ def load_data
+ @available_zones = Zone.find :all, :order => :name
+ # TODO - remove hard coded
+ @shipping_calculators = [FlatRateShippingCalculator]
+ end
end
@@ -162,5 +162,14 @@ def remove_nested(fields)
out << fields.hidden_field(:_delete) unless fields.object.new_record?
out << (link_to icon("delete"), "#", :class => "remove")
out
- end
+ end
+
+ def preference_fields(calculator, form)
+ field_html = ""
+ calculator.preferences.keys.each do |key|
+ field_html += (form.label("preferred_#{key}", "#{t(key)}: "))
+ field_html += (form.text_field("preferred_#{key}"))
+ end
+ field_html
+ end
end
@@ -0,0 +1,2 @@
+module Admin::CalculatorsHelper
+end
@@ -0,0 +1,17 @@
+class Calculator < ActiveRecord::Base
+ belongs_to :calculable, :polymorphic => true
+ validates_presence_of :calculable_id
+
+ def self.shipping
+ all.select { |c| c.is_a? ShippingCalculator }
+ end
+
+ def available?(order)
+ true
+ end
+
+ def to_s
+ self.class.to_s.titleize
+ end
+
+end
@@ -0,0 +1,2 @@
+class ShippingCalculator < Calculator
+end
@@ -1,13 +1,23 @@
class ShippingMethod < ActiveRecord::Base
belongs_to :zone
-
+ has_one :calculator, :as => :calculable, :dependent => :destroy
+
+ accepts_nested_attributes_for :calculator
+
+ def calculator_type
+ calculator.class.to_s if calculator
+ end
+
+ def calculator_type=(calculator_type)
+ # does nothing - just here to satisfy the form
+ end
+
def calculate_shipping(shipment)
return 0 unless zone.include?(shipment.address)
- return shipping_calculator.constantize.new.send(:calculate_shipping, shipment)
+ return calculator.calculate_shipping(shipment)
end
def available?(order)
- calculator = shipping_calculator.constantize.new
return true unless calculator.respond_to?(:available?)
calculator.available?(order)
end
@@ -9,6 +9,14 @@
</tr>
<tr>
<td><%=t("shipping_calculator")%>:</td>
- <td><%= f.text_field :shipping_calculator, {"style" => "width:200px"} %></td>
+ <td><%= f.collection_select(:calculator_type, @shipping_calculators, :to_s, :to_s, {}, {:id=> "calc-type", "style" => "width:200px"}) %></td>
</tr>
-</table>
+</table>
+
+<h2><%= t('settings') %></h2>
+<div class="calculator-settings">
+ <% f.fields_for :calculator do |calculator_form| %>
+ <%= preference_fields(@shipping_method.calculator, calculator_form) %>
+ <% end %>
+</div>
+<div id="calculator-settings-warning"><%= t('calculator_settings_warning')%></div>
@@ -1,6 +1,6 @@
<h1><%= t("editing_shipping_method") %></h1>
<%= error_messages_for :shipping_rate %>
-<% form_for(:shipping_method, :url => object_url, :html => { :method => :put }) do |f| %>
+<% form_for(@shipping_method, :url => object_url, :html => { :method => :put }) do |f| %>
<%= render :partial => "form", :locals => { :f => f } %>
<p>
<%=submit_tag t("update")%>
@@ -21,7 +21,7 @@
<tr id="<%= dom_id shipping_method %>">
<td width="100px"><%= shipping_method.name %></td>
<td width="100px"><%= shipping_method.zone.name %></td>
- <td width="100px"><%= shipping_method.shipping_calculator %></td>
+ <td width="100px"><%= shipping_method.calculator.to_s %></td>
<td>
<%= link_to_edit shipping_method %> &nbsp;
<%= link_to_delete shipping_method %>
@@ -8,15 +8,14 @@
<%= stylesheet_link_tag 'admin/admin-reset', 'admin/grids', 'admin/admin-typography', 'admin/admin-forms', 'admin/admin-tables', 'admin/admin', :cache => 'admin-all' %>
<%= javascript_include_tag :defaults %> <!-- TODO: Remove. See Issue #320 -->
- <%= javascript_include_tag 'jquery-1.3.2.min', 'spree', 'nested-attribute', 'zone', :cache => 'jquery-and-extras' %>
- <%= javascript_tag "var AUTH_TOKEN = #{form_authenticity_token.inspect};" if protect_against_forgery? %>
+ <%= javascript_include_tag 'jquery-1.3.2.min', 'spree', 'nested-attribute', 'zone', 'calculator', :cache => 'jquery-and-extras' %>
+ <%= javascript_tag "var AUTH_TOKEN = #{form_authenticity_token.inspect};" if protect_against_forgery? %>
+ <%= javascript_include_tag 'jquery.alerts/jquery.alerts.js' %>
- <%= javascript_include_tag 'jquery.alerts/jquery.alerts.js' %>
<%= stylesheet_link_tag '/javascripts/jquery.alerts/jquery.alerts.css' %>
<%= stylesheet_link_tag '/javascripts/jquery.alerts/jquery.alerts.spree.css' %>
<%= javascript_tag "jQuery.alerts.dialogClass = 'spree';" %>
-
<%= yield :head %>
</head>
<body class='yui-skin-sam admin'>
@@ -270,7 +270,8 @@ en-US:
extensions: Extensions
filename: Filename
final_confirmation: "Final Confirmation"
- first_name: "First Name"
+ first_name: "First Name"
+ flat_rate_amount: Amount
forgot_password: "Forgot Password"
gateway: Gateway
gateway_configuration: "Gateway configuration"
@@ -452,7 +453,9 @@ en-US:
send_copy_of_orders_mails_to: Send Copy of Order Mails To
send_mails_as: Send Mails As
send_order_mails_as: Send Order Mails As
- server_error: "The server returned an error"
+ server_error: "The server returned an error"
+ settings: Settings
+ calculator_settings_warning: "If you are changing the calculator type, you must save first before you can edit the calculator settings"
ship: ship
ship_address: "Ship Address"
shipment: Shipment
View
@@ -87,7 +87,8 @@
admin.resources :shipping_methods
admin.resources :shipping_categories
admin.resources :tax_rates
- admin.resource :tax_settings
+ admin.resource :tax_settings
+ admin.resources :calculators
end
@@ -0,0 +1,22 @@
+class CreateCalculators < ActiveRecord::Migration
+ def self.up
+ create_table :calculators do |t|
+ t.string :type
+ t.references :calculable, :polymorphic => true, :null => false
+ t.timestamps
+ end
+ change_table :shipping_methods do |t|
+ t.remove :shipping_calculator
+ end
+ ShippingMethod.all.each do |shipping_method|
+ FlatRateShippingCalculator.create(:calculable => shipping_method)
+ end
+ end
+
+ def self.down
+ drop_table :calculators
+ change_table :shipping_methods do |t|
+ t.string :shipping_calculator
+ end
+ end
+end
@@ -0,0 +1,12 @@
+ups_ground:
+ calculable: ups_ground
+ calculable_type: ShippingMethod
+ type: FlatRateShippingCalculator
+ups_two_day:
+ calculable: ups_two_day
+ calculable_type: ShippingMethod
+ type: FlatRateShippingCalculator
+ups_one_day:
+ calculable: ups_one_day
+ calculable_type: ShippingMethod
+ type: FlatRateShippingCalculator
@@ -0,0 +1,15 @@
+flat_rate_ground:
+ attribute: flat_rate_amount
+ owner: ups_ground
+ owner_type: Calculator
+ value: 5
+flat_rate_two_day:
+ attribute: flat_rate_amount
+ owner: ups_two_day
+ owner_type: Calculator
+ value: 10
+flat_rate_one_day:
+ attribute: flat_rate_amount
+ owner: ups_one_day
+ owner_type: Calculator
+ value: 15
@@ -1,16 +1,9 @@
ups_ground:
name: UPS Ground
- zone: available_shipping
- shipping_calculator: Spree::FlatRateShipping::Calculator
+ zone_id: 2
ups_two_day:
name: UPS Two Day
- zone: available_shipping
- shipping_calculator: Spree::FlatRateShipping::Calculator
+ zone_id: 2
ups_one_day:
name: UPS One Day
- zone: available_shipping
- shipping_calculator: Spree::FlatRateShipping::Calculator
-one_option:
- name: Only one option
- zone: one_ship_option
- shipping_calculator: Spree::FlatRateShipping::Calculator
+ zone_id: 2
@@ -1,16 +0,0 @@
-usa_member:
- zone: available_shipping
- zoneable_id: 214
- zoneable_type: Country
-canada_member:
- zone: available_shipping
- zoneable_id: 35
- zoneable_type: Country
-uk_one_ship_opt:
- zone: one_ship_option
- zoneable_id: 213
- zoneable_type: Country
-uae_one_ship_opt:
- zone: one_ship_option
- zoneable_id: 212 # UAE, just cos it's close to US/UK in the list
- zoneable_type: Country
View
@@ -1,6 +0,0 @@
-available_shipping:
- name: North America
- description: USA + Canada
-one_ship_option:
- name: Zone with one shipping option
- description: Zone with one shipping option
View
@@ -0,0 +1,4 @@
+Script started on Wed Jul 15 13:22:29 2009
+script: resource: No such file or directory
+
+Script done on Wed Jul 15 13:22:29 2009
@@ -0,0 +1,13 @@
+$j(function() {
+ var original_calc_type = $j('#calc-type').attr('value');
+ $j('div#calculator-settings-warning').hide();
+ $j('#calc-type').change(function() {
+ if ($j('#calc-type').attr('value') == original_calc_type) {
+ $j('div.calculator-settings').show();
+ $j('div#calculator-settings-warning').hide();
+ } else {
+ $j('div.calculator-settings').hide();
+ $j('div#calculator-settings-warning').show();
+ }
+ });
+})
@@ -424,6 +424,10 @@ span.current_page {
border: 1px solid navy;
background-color: #2e6ab1;
color: #FFF;
+}
+
+div#calculator-settings-warning {
+ color: #f00;
}
#progress{
display:none;
Oops, something went wrong.

0 comments on commit dfcb544

Please sign in to comment.