spree_shipping_matrix 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/.gitignore +14 -0
  2. data/.rspec +3 -0
  3. data/.tailor +29 -0
  4. data/.travis.yml +20 -0
  5. data/Gemfile +7 -0
  6. data/LICENSE +22 -0
  7. data/README.md +56 -0
  8. data/Rakefile +28 -0
  9. data/Versionfile +1 -0
  10. data/app/assets/javascripts/spree/backend/spree_shipping_matrix.js +42 -0
  11. data/app/assets/javascripts/spree/frontend/spree_shipping_matrix.js +2 -0
  12. data/app/assets/stylesheets/spree/backend/spree_shipping_matrix.css +3 -0
  13. data/app/assets/stylesheets/spree/frontend/spree_shipping_matrix.css +4 -0
  14. data/app/controllers/spree/admin/shipping_matrices_controller.rb +13 -0
  15. data/app/helpers/spree/admin/base_helper_decorator.rb +9 -0
  16. data/app/helpers/spree/admin/form_builder_decorator.rb +9 -0
  17. data/app/models/spree/shipping_matrix.rb +18 -0
  18. data/app/models/spree/shipping_matrix_calculator.rb +40 -0
  19. data/app/models/spree/shipping_matrix_rule.rb +39 -0
  20. data/app/overrides/spree/admin/shared/_configuration_menu/add_shipping_matrix_link.html.erb.deface +2 -0
  21. data/app/views/spree/admin/shipping_matrices/_form.html.erb +18 -0
  22. data/app/views/spree/admin/shipping_matrices/edit.html.erb +25 -0
  23. data/app/views/spree/admin/shipping_matrices/index.html.erb +63 -0
  24. data/app/views/spree/admin/shipping_matrices/new.html.erb +11 -0
  25. data/app/views/spree/admin/shipping_matrices/new.js.erb +6 -0
  26. data/app/views/spree/admin/shipping_matrix_rules/_form.html.erb +33 -0
  27. data/bin/rails +7 -0
  28. data/config/locales/en.yml +13 -0
  29. data/config/routes.rb +5 -0
  30. data/db/migrate/20140826110342_create_spree_shipping_matrices.rb +8 -0
  31. data/db/migrate/20140826112807_create_spree_shipping_matrix_rules.rb +10 -0
  32. data/lib/generators/spree_shipping_matrix/install/install_generator.rb +27 -0
  33. data/lib/spree_shipping_matrix/engine.rb +25 -0
  34. data/lib/spree_shipping_matrix/version.rb +3 -0
  35. data/lib/spree_shipping_matrix.rb +2 -0
  36. data/spec/factories/shipping_matrix_factory.rb +17 -0
  37. data/spec/factories/shipping_matrix_rule_factory.rb +8 -0
  38. data/spec/features/shipping_matrix_calculator_spec.rb +34 -0
  39. data/spec/features/shipping_matrix_checkout_spec.rb +92 -0
  40. data/spec/features/shipping_matrix_spec.rb +86 -0
  41. data/spec/spec_helper.rb +91 -0
  42. data/spec/support/user_steps.rb +22 -0
  43. data/spec/unit/models/shipping_matrix_calculator_spec.rb +81 -0
  44. data/spec/unit/models/shipping_matrix_rule_spec.rb +35 -0
  45. data/spec/unit/models/shipping_matrix_spec.rb +39 -0
  46. data/spree_shipping_matrix.gemspec +42 -0
  47. metadata +298 -0
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ \#*
2
+ *~
3
+ .#*
4
+ .DS_Store
5
+ .idea
6
+ .project
7
+ .sass-cache
8
+ coverage
9
+ Gemfile.lock
10
+ tmp
11
+ nbproject
12
+ pkg
13
+ *.swp
14
+ spec/dummy
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format documentation
data/.tailor ADDED
@@ -0,0 +1,29 @@
1
+ Tailor.config do |config|
2
+ config.formatters "text"
3
+ config.file_set '{app,spec/{factories,unit}}/**/*.rb' do |style|
4
+ style.allow_camel_case_methods false, level: :error
5
+ style.allow_conditional_parentheses false, level: :warn
6
+ style.allow_hard_tabs false, level: :error
7
+ style.allow_screaming_snake_case_classes false, level: :error
8
+ style.allow_trailing_line_spaces false, level: :error
9
+ style.allow_unnecessary_interpolation false, level: :warn
10
+ style.allow_unnecessary_double_quotes false, level: :warn
11
+ style.allow_invalid_ruby false, level: :warn
12
+ style.indentation_spaces 2, level: :off
13
+ style.max_code_lines_in_class 300, level: :error
14
+ style.max_code_lines_in_method 30, level: :error
15
+ style.max_line_length 130, level: :error
16
+ style.spaces_after_comma 1, level: :error
17
+ style.spaces_after_conditional 1, level: :error
18
+ style.spaces_after_lbrace 1, level: :error
19
+ style.spaces_after_lbracket 0, level: :error
20
+ style.spaces_after_lparen 0, level: :error
21
+ style.spaces_before_comma 0, level: :error
22
+ style.spaces_before_lbrace 1, level: :error
23
+ style.spaces_before_rbrace 1, level: :error
24
+ style.spaces_before_rbracket 0, level: :error
25
+ style.spaces_before_rparen 0, level: :error
26
+ style.spaces_in_empty_braces 0, level: :error
27
+ style.trailing_newlines 1, level: :error
28
+ end
29
+ end
data/.travis.yml ADDED
@@ -0,0 +1,20 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ before_install:
7
+ - "export DISPLAY=:99.0"
8
+ - "export CODECLIMATE_REPO_TOKEN=f965fda6d493fe7c6d3e3358a9ea39f2b6bad21bf837fc2c2e41a2aaa3bf4cd2 "
9
+ - "sh -e /etc/init.d/xvfb start"
10
+ before_script:
11
+ - "bundle exec rake test_app"
12
+ script:
13
+ - "bundle exec rake"
14
+ notifications:
15
+ email:
16
+ - luke@madetech.co.uk
17
+ - seb@madetech.co.uk
18
+ branches:
19
+ only:
20
+ - master
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'spree', github: 'spree/spree', branch: 'master'
4
+ # Provides basic authentication functionality for testing parts of your engine
5
+ gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: 'master'
6
+
7
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Made Tech Ltd
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # SpreeShippingMatrix
2
+
3
+ Advanced shipping calculator based on rules matrix for Spree.
4
+
5
+ [![Code Climate](https://codeclimate.com/github/madebymade/spree_shipping_matrix/badges/gpa.svg)](https://codeclimate.com/github/madebymade/spree_shipping_matrix)
6
+ [![Build Status](https://travis-ci.org/madebymade/spree_shipping_matrix.svg?branch=master)](https://travis-ci.org/madebymade/spree_shipping_matrix)
7
+ [![Brakeman](http://rails-brakeman.com/madebymade/spree_shipping_matrix.png)](http://rails-brakeman.com/madebymade/spree_shipping_matrix)
8
+ [![Test Coverage](https://codeclimate.com/github/madebymade/spree_shipping_matrix/badges/coverage.svg)](https://codeclimate.com/github/madebymade/spree_shipping_matrix)
9
+
10
+ ## Installation
11
+
12
+ Add spree_shipping_matrix to your Gemfile:
13
+
14
+ ```ruby
15
+ gem 'spree_shipping_matrix'
16
+ ```
17
+
18
+ Bundle your dependencies and run the installation generator:
19
+
20
+ ```shell
21
+ bundle
22
+ bundle exec rails g spree_shipping_matrix:install
23
+ ```
24
+
25
+ ## Testing
26
+
27
+ First bundle your dependencies, then run `rake`. `rake` will default to building the dummy app if it does not exist, then it will run specs. The dummy app can be regenerated by using `rake test_app`.
28
+
29
+ ```shell
30
+ bundle
31
+ bundle exec rake
32
+ ```
33
+
34
+ When testing your applications integration with this extension you may use it's factories.
35
+ Simply add this require statement to your spec_helper:
36
+
37
+ ```ruby
38
+ require 'spree_shipping_matrix/factories'
39
+ ```
40
+
41
+ ## Credits
42
+
43
+ [![made](https://s3-eu-west-1.amazonaws.com/made-assets/googleapps/google-apps.png)][made]
44
+
45
+ Developed and maintained by [Made Tech][made]. Key contributions:
46
+
47
+ * [Seb Ashton](https://github.com/SebAshton)
48
+ * [Luke Morton](https://github.com/DrPheltRight)
49
+
50
+ ## License
51
+
52
+ Copyright © 2014 Made Tech Ltd. It is free software, and may be
53
+ redistributed under the terms specified in the [MIT-LICENSE][license] file.
54
+
55
+ [made]: http://www.madetech.co.uk?ref=github&repo=spree_shipping_matrix
56
+ [license]: https://github.com/madebymade/cf-deploy/blob/master/LICENSE
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ require 'spree/testing_support/extension_rake'
6
+
7
+ RSpec::Core::RakeTask.new
8
+
9
+ task :default do
10
+ if Dir["spec/dummy"].empty?
11
+ Rake::Task[:test_app].invoke
12
+ Dir.chdir("../../")
13
+ end
14
+ Rake::Task[:tailor].invoke
15
+ Rake::Task[:spec].invoke
16
+ end
17
+
18
+ desc 'Generates a dummy app for testing'
19
+ task :test_app do
20
+ ENV['LIB_NAME'] = 'spree_shipping_matrix'
21
+ Rake::Task['extension:test_app'].invoke
22
+ end
23
+
24
+ begin
25
+ require 'tailor/rake_task'
26
+ Tailor::RakeTask.new
27
+ rescue LoadError
28
+ end
data/Versionfile ADDED
@@ -0,0 +1 @@
1
+ '2.4.x' => { :branch => 'master' }
@@ -0,0 +1,42 @@
1
+ //= require spree/backend
2
+
3
+ $(document).on('click', '.js-remove-row', function () {
4
+ var $removeLink = $(this);
5
+ var $row = $removeLink.closest('.row');
6
+ var destroyName = $removeLink.data('row-object-name') + '[_destroy]';
7
+ $row.replaceWith(
8
+ '<input type="hidden" name="' + destroyName + '" value="1" />');
9
+ return false;
10
+ });
11
+
12
+ $(document).on('click', '.js-duplicate-last-row', function () {
13
+ var $duplicateLink = $(this);
14
+ var $row = $duplicateLink.prev();
15
+ var reRowId = /\[([0-9]+)\]/;
16
+ var rowId = parseInt(reRowId.exec($row.find('[name]:first').attr('name'))[1], 10);
17
+ var newRowId = rowId + 1;
18
+ var $duplicateRow = $row.clone();
19
+
20
+ $duplicateRow.find('[for]').each(function () {
21
+ var labelFor = $(this).attr('for');
22
+ $(this).attr('for', labelFor.replace('_' + rowId + '_', '_' + newRowId + '_'));
23
+ });
24
+
25
+ $duplicateRow.find('[name]').each(function () {
26
+ var name = $(this).attr('name');
27
+ var id = $(this).attr('id');
28
+ $(this).attr('id', id.replace('_' + rowId + '_', '_' + newRowId + '_'));
29
+ $(this).attr('name', name.replace(reRowId, '[' + newRowId + ']'));
30
+ });
31
+
32
+ $duplicateRow.find('.select2-container').remove();
33
+
34
+ $duplicateLink.before($duplicateRow);
35
+
36
+ $('select.select2').select2({
37
+ allowClear: true,
38
+ dropdownAutoWidth: true
39
+ });
40
+
41
+ return false;
42
+ });
@@ -0,0 +1,2 @@
1
+ // Placeholder manifest file.
2
+ // the installer will append this file to the app vendored assets here: vendor/assets/javascripts/spree/frontend/all.js'
@@ -0,0 +1,3 @@
1
+ /**
2
+ *= require spree/backend
3
+ */
@@ -0,0 +1,4 @@
1
+ /*
2
+ Placeholder manifest file.
3
+ the installer will append this file to the app vendored assets here: 'vendor/assets/stylesheets/spree/frontend/all.css'
4
+ */
@@ -0,0 +1,13 @@
1
+ module Spree
2
+ module Admin
3
+ class ShippingMatricesController < ResourceController
4
+ def new
5
+ @shipping_matrix.rules.build
6
+ end
7
+
8
+ def edit
9
+ @shipping_matrix.rules.build
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,9 @@
1
+ Spree::Admin::BaseHelper.module_eval do
2
+ def preference_field_for(form, field, options)
3
+ if options[:type] == :matrix_id
4
+ form.matrix_field(field, preference_field_options(options))
5
+ else
6
+ super
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ ActionView::Helpers::FormBuilder.class_eval do
2
+ def matrix_field(method, options)
3
+ collection_select(method,
4
+ Spree::ShippingMatrix.all,
5
+ :id, :name,
6
+ options,
7
+ class: 'select2 fullwidth')
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ module Spree
2
+ class ShippingMatrix < ActiveRecord::Base
3
+ has_many :rules, ->{ order('min_line_item_total DESC') },
4
+ class_name: Spree::ShippingMatrixRule,
5
+ inverse_of: :matrix
6
+
7
+ accepts_nested_attributes_for :rules, allow_destroy: true,
8
+ reject_if: :rule_blank?
9
+
10
+ validates :name, presence: true
11
+
12
+ private
13
+
14
+ def rule_blank?(attributes)
15
+ attributes['min_line_item_total'].blank? || attributes['amount'].blank?
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,40 @@
1
+ module Spree
2
+ class ShippingMatrixCalculator < Spree::ShippingCalculator
3
+ preference :matrix_id, :matrix_id
4
+
5
+ def self.description
6
+ 'Shipping Matrix Calculator'
7
+ end
8
+
9
+ def compute_package(package)
10
+ matched_amount(user: package.contents.first.inventory_unit.order.user,
11
+ line_item_total: total(package.contents))
12
+ end
13
+
14
+ private
15
+
16
+ def matched_amount(info)
17
+ matched = matched_rule(info)
18
+
19
+ if matched.nil?
20
+ 0
21
+ else
22
+ matched.amount
23
+ end
24
+ end
25
+
26
+ def matched_rule(info)
27
+ matched = matrix.rules.find { |rule| rule.matches?(info) }
28
+
29
+ if matched.nil?
30
+ matrix.rules.last
31
+ else
32
+ matched
33
+ end
34
+ end
35
+
36
+ def matrix
37
+ @matrix ||= Spree::ShippingMatrix.find(preferred_matrix_id)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,39 @@
1
+ module Spree
2
+ class ShippingMatrixRule < ActiveRecord::Base
3
+ belongs_to :matrix, class_name: Spree::ShippingMatrix,
4
+ foreign_key: 'shipping_matrix_id'
5
+
6
+ belongs_to :role, class_name: Spree::Role
7
+
8
+ validates :matrix, presence: true
9
+ validates :role, presence: true
10
+
11
+ validates :min_line_item_total, presence: true,
12
+ numericality: true
13
+
14
+ validates :amount, presence: true,
15
+ numericality: true
16
+
17
+ def matches?(info)
18
+ matches_role?(info) && matches_line_item_total?(info)
19
+ end
20
+
21
+ private
22
+
23
+ def matches_role?(info)
24
+ if info[:user].nil?
25
+ role.name == 'entry'
26
+ else
27
+ matches_user_role?(info)
28
+ end
29
+ end
30
+
31
+ def matches_user_role?(info)
32
+ info[:user].spree_roles.include?(role)
33
+ end
34
+
35
+ def matches_line_item_total?(info)
36
+ info[:line_item_total] > min_line_item_total
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,2 @@
1
+ <!-- insert_bottom '[data-hook="admin_configurations_sidebar_menu"]' -->
2
+ <%= configurations_sidebar_menu_item Spree.t(:title, scope: :shipping_matrix), spree.admin_shipping_matrices_path %>
@@ -0,0 +1,18 @@
1
+ <%= f.field_container :name, class: ['row'] do %>
2
+ <%= f.label :name, Spree.t(:name, scope: :shipping_matrix) %>
3
+ <span class="required">*</span><br />
4
+ <%= f.text_field :name, class: ['fullwidth'] %>
5
+ <%= f.error_message_on :name %>
6
+ <% end %>
7
+
8
+ <div class="js-shipping-matrix-rules">
9
+ <%= f.fields_for :rules do |f| %>
10
+ <%= render partial: 'spree/admin/shipping_matrix_rules/form',
11
+ locals: { f: f } %>
12
+ <% end %>
13
+
14
+ <%= link_to Spree.t(:add_rule, scope: :shipping_matrix),
15
+ '#new-rule',
16
+ class: 'js-duplicate-last-row' %>
17
+ </div>
18
+
@@ -0,0 +1,25 @@
1
+ <%= render partial: 'spree/admin/shared/configuration_menu' %>
2
+
3
+ <% content_for :page_title do %>
4
+ <%= Spree.t(:editting, scope: :shipping_matrix) %>
5
+ <span class="green">"<%= @shipping_matrix.name %>"</span>
6
+ <% end %>
7
+
8
+ <% content_for :page_actions do %>
9
+ <li>
10
+ <%= button_link_to Spree.t(:back, scope: :shipping_matrix),
11
+ spree.admin_shipping_matrices_path,
12
+ icon: 'arrow-left' %>
13
+ </li>
14
+ <% end %>
15
+
16
+ <%= render partial: 'spree/shared/error_messages',
17
+ locals: { target: @shipping_matrix } %>
18
+
19
+ <%= form_for [:admin, @shipping_matrix] do |f| %>
20
+ <fieldset>
21
+ <legend align="center"><%= Spree.t(:editting, scope: :shipping_matrix) %></legend>
22
+ <%= render :partial => 'form', :locals => { :f => f } %>
23
+ <%= render :partial => 'spree/admin/shared/edit_resource_links' %>
24
+ </fieldset>
25
+ <% end %>
@@ -0,0 +1,63 @@
1
+ <%= render :partial => 'spree/admin/shared/configuration_menu' %>
2
+
3
+ <% content_for :page_title do %>
4
+ <%= Spree.t(:title, scope: :shipping_matrix) %>
5
+ <% end %>
6
+
7
+ <% content_for :page_actions do %>
8
+ <li id="new_er_link">
9
+ <%= button_link_to Spree.t(:new, scope: :shipping_matrix),
10
+ new_admin_shipping_matrix_url,
11
+ remote: true,
12
+ icon: 'plus' %>
13
+ </li>
14
+ <% end %>
15
+
16
+ <div class="js-new-shipping-matrix"></div>
17
+
18
+ <% if @shipping_matrices.any? %>
19
+ <table
20
+ class="index sortable"
21
+ id="list_shipping_matrices"
22
+ data-hook
23
+ data-sortable-link="<%= update_positions_admin_option_types_url %>"
24
+ >
25
+ <colgroup>
26
+ <col style="width: 35%">
27
+ <col style="width: 25%">
28
+ <col style="width: 25%">
29
+ <col style="width: 15%">
30
+ </colgroup>
31
+ <thead>
32
+ <tr data-hook="option_header">
33
+ <th><%= Spree.t(:name, scope: :shipping_matrix) %></th>
34
+ <th class="actions"></th>
35
+ </tr>
36
+ </thead>
37
+ <tbody>
38
+ <% @shipping_matrices.each do |shipping_matrix| %>
39
+ <tr
40
+ class="spree_shipping_matrix <%= cycle('odd', 'even')%>"
41
+ id="<%= spree_dom_id shipping_matrix %>"
42
+ data-hook="shipping_matrix_row"
43
+ >
44
+ <td class="align-center"><%= shipping_matrix.name %></td>
45
+ <td class="actions">
46
+ <%= link_to_edit(shipping_matrix,
47
+ class: 'admin_edit_shipping_matrix',
48
+ no_text: true) %>
49
+ <%= link_to_delete(shipping_matrix, no_text: true) %>
50
+ </td>
51
+ </tr>
52
+ <% end %>
53
+ </tbody>
54
+ </table>
55
+ <% else %>
56
+ <div class="alpha twelve columns no-objects-found">
57
+ <%= Spree.t(:no_resource_found, resource: Spree.t(:title, scope: :shipping_matrix)) %>,
58
+
59
+ <%= link_to Spree.t(:add_one),
60
+ spree.new_admin_shipping_matrix_path,
61
+ remote: true %>!
62
+ </div>
63
+ <% end %>
@@ -0,0 +1,11 @@
1
+ <%= render partial: 'spree/shared/error_messages',
2
+ locals: { target: @shipping_matrix } %>
3
+
4
+ <%= form_for [:admin, @shipping_matrix] do |f| %>
5
+ <fieldset>
6
+ <legend align="center"><%= Spree.t(:new, scope: :shipping_matrix) %></legend>
7
+ <%= render partial: 'form', locals: { f: f } %>
8
+
9
+ <%= render partial: 'spree/admin/shared/new_resource_links' %>
10
+ </fieldset>
11
+ <% end %>
@@ -0,0 +1,6 @@
1
+ $('.js-new-shipping-matrix').html('<%= escape_javascript(render template: "spree/admin/shipping_matrices/new", formats: [:html], handlers: [:erb]) %>');
2
+ $('a[href$="<%= spree.new_admin_shipping_matrix_path %>"]').parent().hide();
3
+ $('select.select2').select2({
4
+ allowClear: true,
5
+ dropdownAutoWidth: true
6
+ });
@@ -0,0 +1,33 @@
1
+ <div class="row">
2
+ <div class="alpha four columns">
3
+ <%= f.label(:role) %>
4
+ <%= f.collection_select(:role_id,
5
+ Spree::Role.all,
6
+ :id, :name, {},
7
+ {class: 'select2 fullwidth'}) %>
8
+ </div>
9
+
10
+ <div class="four columns">
11
+ <%= f.label(:min_line_item_total) %>
12
+ <%= f.number_field(:min_line_item_total,
13
+ value: number_with_precision(f.object.min_line_item_total,
14
+ precision: 2)) %>
15
+ </div>
16
+
17
+ <div class="omega four columns">
18
+ <%= f.label(:amount) %>
19
+ <%= f.number_field(:amount,
20
+ value: number_with_precision(f.object.amount,
21
+ precision: 2)) %>
22
+
23
+ <% if f.object.persisted? %>
24
+ <a
25
+ href="#remove-<%= f.object.id %>"
26
+ class="js-remove-row"
27
+ data-row-object-name="<%= f.object_name %>"
28
+ >
29
+ <%= Spree.t(:remove) %>
30
+ </a>
31
+ <% end %>
32
+ </div>
33
+ </div>
data/bin/rails ADDED
@@ -0,0 +1,7 @@
1
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
2
+
3
+ ENGINE_ROOT = File.expand_path('../..', __FILE__)
4
+ ENGINE_PATH = File.expand_path('../../lib/spree_shipping_matrix/engine', __FILE__)
5
+
6
+ require 'rails/all'
7
+ require 'rails/engine/commands'
@@ -0,0 +1,13 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ spree:
6
+ shipping_matrix:
7
+ new: 'New Shipping Matrix'
8
+ name: 'Name'
9
+ title: 'Shipping Matrices'
10
+ editting: 'Edit Shipping Matrix'
11
+ back: 'Back to Shipping Matrices'
12
+ add_rule: 'Add rule'
13
+ matrix_id: 'Shipping Matrix'
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ Spree::Core::Engine.routes.draw do
2
+ namespace :admin do
3
+ resources :shipping_matrices
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ class CreateSpreeShippingMatrices < ActiveRecord::Migration
2
+ def change
3
+ create_table :spree_shipping_matrices do |t|
4
+ t.string :name
5
+ t.timestamps
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,10 @@
1
+ class CreateSpreeShippingMatrixRules < ActiveRecord::Migration
2
+ def change
3
+ create_table :spree_shipping_matrix_rules do |t|
4
+ t.references :shipping_matrix, index: true
5
+ t.references :role, index: true
6
+ t.decimal :min_line_item_total, precision: 5, scale: 2, null: false
7
+ t.decimal :amount, precision: 5, scale: 2, null: false
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,27 @@
1
+ module SpreeShippingMatrix
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ class_option :auto_run_migrations, :type => :boolean, :default => false
5
+
6
+ def add_javascripts
7
+ append_file 'vendor/assets/javascripts/spree/backend/all.js', "//= require spree/backend/spree_shipping_matrix\n"
8
+ end
9
+
10
+ def add_stylesheets
11
+ end
12
+
13
+ def add_migrations
14
+ run 'bundle exec rake railties:install:migrations FROM=spree_shipping_matrix'
15
+ end
16
+
17
+ def run_migrations
18
+ run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask 'Would you like to run the migrations now? [Y/n]')
19
+ if run_migrations
20
+ run 'bundle exec rake db:migrate'
21
+ else
22
+ puts 'Skipping rake db:migrate, don\'t forget to run it!'
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,25 @@
1
+ module SpreeShippingMatrix
2
+ class Engine < Rails::Engine
3
+ require 'spree/core'
4
+ isolate_namespace Spree
5
+ engine_name 'spree_shipping_matrix'
6
+
7
+ # use rspec for tests
8
+ config.generators do |g|
9
+ g.test_framework :rspec
10
+ end
11
+
12
+
13
+ def self.activate
14
+ Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c|
15
+ Rails.configuration.cache_classes ? require(c) : load(c)
16
+ end
17
+ end
18
+
19
+ config.to_prepare &method(:activate).to_proc
20
+
21
+ config.after_initialize do
22
+ config.spree.calculators.shipping_methods << Spree::ShippingMatrixCalculator
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module SpreeShippingMatrix
2
+ VERSION = '0.1.0'
3
+ end