Skip to content

Commit

Permalink
Merge branch 'master' into uk/1316-ui-grid-reports
Browse files Browse the repository at this point in the history
  • Loading branch information
jeronimo committed May 3, 2017
2 parents 20b5aca + d91c3d1 commit c60182c
Show file tree
Hide file tree
Showing 32 changed files with 252 additions and 49 deletions.
5 changes: 2 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -646,9 +646,8 @@ GEM
whenever (0.9.2)
activesupport (>= 2.3.4)
chronic (>= 0.6.3)
wicked_pdf (0.11.0)
rails
wkhtmltopdf-binary (0.9.9.3)
wicked_pdf (1.1.0)
wkhtmltopdf-binary (0.12.3.1)
xml-simple (1.1.4)
xpath (2.0.0)
nokogiri (~> 1.3)
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ The site is configured to use
startup time while Rails loads. See the Zeus github page for
usage instructions.

Once [npm dependencies are
installed](https://github.com/openfoodfoundation/openfoodnetwork/wiki/Karma), AngularJS tests can be run with:

./script/karma run

If you want karma to automatically rerun the tests on file modification, use:

./script/karma start

## Credits

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ angular.module('admin.orderCycles')
$scope.StatusMessage = StatusMessage

$scope.$watch 'order_cycle_form.$dirty', (newValue) ->
StatusMessage.display 'notice', 'You have unsaved changes' if newValue
StatusMessage.display 'notice', t("admin.unsaved_changes") if newValue

$scope.$watch 'order_cycle_form.$valid', (isValid) ->
StatusMessage.setValidation(isValid)

$scope.loaded = ->
Enterprise.loaded && EnterpriseFee.loaded && OrderCycle.loaded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ angular.module('admin.orderCycles')
$scope.StatusMessage = StatusMessage

$scope.$watch 'order_cycle_form.$dirty', (newValue) ->
StatusMessage.display 'notice', 'You have unsaved changes' if newValue
StatusMessage.display 'notice', t("admin.unsaved_changes") if newValue

$scope.$watch 'order_cycle_form.$valid', (isValid) ->
StatusMessage.setValidation(isValid)

$scope.loaded = ->
Enterprise.loaded && EnterpriseFee.loaded && OrderCycle.loaded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ angular.module('admin.orderCycles').controller "AdminSimpleCreateOrderCycleCtrl"
$scope.enterprise_fees = EnterpriseFee.index(coordinator_id: ocInstance.coordinator_id)

$scope.$watch 'order_cycle_form.$dirty', (newValue) ->
StatusMessage.display 'notice', 'You have unsaved changes' if newValue
StatusMessage.display 'notice', t("admin.unsaved_changes") if newValue

$scope.$watch 'order_cycle_form.$valid', (isValid) ->
StatusMessage.setValidation(isValid)

$scope.init = (enterprises) ->
enterprise = enterprises[Object.keys(enterprises)[0]]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ angular.module('admin.orderCycles').controller "AdminSimpleEditOrderCycleCtrl",
$scope.init()

$scope.$watch 'order_cycle_form.$dirty', (newValue) ->
StatusMessage.display 'notice', 'You have unsaved changes' if newValue
StatusMessage.display 'notice', t("admin.unsaved_changes") if newValue

$scope.$watch 'order_cycle_form.$valid', (isValid) ->
StatusMessage.setValidation(isValid)

$scope.loaded = ->
Enterprise.loaded && EnterpriseFee.loaded && OrderCycle.loaded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ angular.module("admin.utils").factory "StatusMessage", ($timeout) ->
text: ""
style: {}

invalidMessage: ""

setValidation: (isValid) ->
if isValid
StatusMessage.invalidMessage = ''
else
StatusMessage.invalidMessage = t("admin.form_invalid")

active: ->
@statusMessage.text != ''

Expand Down
5 changes: 4 additions & 1 deletion app/assets/javascripts/templates/admin/save_bar.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#save-bar.animate-show{ ng: { show: 'dirty || persist || StatusMessage.active()' } }
.container
.eight.columns.alpha
%h5#status-message{ ng: { style: 'StatusMessage.statusMessage.style' } }
%h5#status-message{ ng: { show: "StatusMessage.invalidMessage == ''", style: 'StatusMessage.statusMessage.style' } }
{{ StatusMessage.statusMessage.text || " " }}
%h5#status-message{ ng: { show: "StatusMessage.invalidMessage !== ''" }, style: 'color: #da5354' }
{{ StatusMessage.invalidMessage || " " }}
.eight.columns.omega.text-right{ ng: { transclude: true } }
5 changes: 5 additions & 0 deletions app/controllers/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ class BaseController < ApplicationController
private

def set_order_cycles
unless @distributor.ready_for_checkout?
@order_cycles = OrderCycle.where('false')
return
end

@order_cycles = OrderCycle.with_distributor(@distributor).active
.order(@distributor.preferred_shopfront_order_cycle_order)

Expand Down
9 changes: 3 additions & 6 deletions app/helpers/checkout_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@ def display_line_item_tax_rates(line_item)
end

def display_adjustment_tax_rates(adjustment)
tax_rate = (adjustment.included_tax / (adjustment.amount - adjustment.included_tax)).round(2)
if tax_rate == 0 || tax_rate.infinite?
""
else
number_to_percentage(tax_rate * 100, :precision => 1)
end
tax_rates = adjustment.tax_rates
return "" if adjustment.amount == adjustment.included_tax
tax_rates.map { |tr| number_to_percentage(tr.amount * 100, :precision => 1) }.join(", ")
end

def display_adjustment_amount(adjustment)
Expand Down
24 changes: 22 additions & 2 deletions app/models/spree/adjustment_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,30 @@ def has_tax?
included_tax > 0
end

def display_included_tax
Spree::Money.new(included_tax, { :currency => currency })
def tax_rates
case originator
when Spree::TaxRate
[originator]
when EnterpriseFee
case source
when Spree::LineItem
tax_category = originator.inherits_tax_category? ? source.product.tax_category : originator.tax_category
return tax_category ? tax_category.tax_rates.match(source.order) : []
when Spree::Order
return originator.tax_category ? originator.tax_category.tax_rates.match(source) : []
end
else
[find_closest_tax_rate_from_included_tax]
end
end

def find_closest_tax_rate_from_included_tax
approximation = (included_tax / (amount - included_tax))
return nil if approximation.infinite? or approximation.zero?
Spree::TaxRate.order("ABS(amount - #{approximation})").first
end


def self.without_callbacks
skip_callback :save, :after, :update_adjustable
skip_callback :destroy, :after, :update_adjustable
Expand Down
12 changes: 6 additions & 6 deletions app/models/spree/order_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ def tax_adjustments

def tax_adjustment_totals
tax_adjustments.each_with_object(Hash.new) do |adjustment, hash|
if adjustment.originator_type == "Spree::TaxRate"
tax_rate = adjustment.originator.amount
else
tax_rate = (adjustment.included_tax / (adjustment.amount - adjustment.included_tax)).round(2)
end
hash.update({tax_rate => adjustment.included_tax}) { |_tax_rate, amount1, amount2| amount1 + amount2 }
tax_rates = adjustment.tax_rates
tax_rates_hash = Hash[tax_rates.collect do |tax_rate|
tax_amount = tax_rates.one? ? adjustment.included_tax : tax_rate.compute_tax(adjustment.amount)
[tax_rate.amount, tax_amount]
end]
hash.update(tax_rates_hash) { |_tax_rate, amount1, amount2| amount1 + amount2 }
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/order_cycles/_exchange_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
%td.tags.panel-toggle.text-center{ name: "tags", ng: { if: 'enterprises[exchange.enterprise_id].managed || order_cycle.viewing_as_coordinator' } }
{{ exchange.tags.length }}
%td.collection-details
= text_field_tag 'order_cycle_outgoing_exchange_{{ $index }}_pickup_time', '', 'id' => 'order_cycle_outgoing_exchange_{{ $index }}_pickup_time', 'placeholder' => t('.pickup_time_placeholder'), 'ng-model' => 'exchange.pickup_time', 'ng-disabled' => '!enterprises[exchange.enterprise_id].managed && !order_cycle.viewing_as_coordinator', 'maxlength' => 35
= text_field_tag 'order_cycle_outgoing_exchange_{{ $index }}_pickup_time', '', 'id' => 'order_cycle_outgoing_exchange_{{ $index }}_pickup_time', 'required' => 'required', 'placeholder' => t('.pickup_time_placeholder'), 'ng-model' => 'exchange.pickup_time', 'ng-disabled' => '!enterprises[exchange.enterprise_id].managed && !order_cycle.viewing_as_coordinator', 'maxlength' => 35
%span.icon-question-sign{'ofn-with-tip' => t('admin.order_cycles.edit.pickup_time_tip')}
%br/
= text_field_tag 'order_cycle_outgoing_exchange_{{ $index }}_pickup_instructions', '', 'id' => 'order_cycle_outgoing_exchange_{{ $index }}_pickup_instructions', 'placeholder' => t('.pickup_instructions_placeholder'), 'ng-model' => 'exchange.pickup_instructions', 'ng-disabled' => '!enterprises[exchange.enterprise_id].managed && !order_cycle.viewing_as_coordinator'
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/order_cycles/_simple_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
.alpha.two.columns
= label_tag t('.ready_for')
.six.columns
= text_field_tag 'order_cycle_outgoing_exchange_0_pickup_time', '', 'id' => 'order_cycle_outgoing_exchange_0_pickup_time', 'placeholder' => t('.ready_for_placeholder'), 'ng-model' => 'outgoing_exchange.pickup_time', 'size' => 30, 'maxlength' => 35
= text_field_tag 'order_cycle_outgoing_exchange_0_pickup_time', '', 'id' => 'order_cycle_outgoing_exchange_0_pickup_time', 'required' => 'required', 'placeholder' => t('.ready_for_placeholder'), 'ng-model' => 'outgoing_exchange.pickup_time', 'size' => 30, 'maxlength' => 35
%span.icon-question-sign{'ofn-with-tip' => t('admin.order_cycles.edit.pickup_time_tip')}
.two.columns
= label_tag t('.customer_instructions')
Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/order_cycles/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
= form_for [main_app, :admin, @order_cycle], :url => '', :html => {:class => 'ng order_cycle', 'ng-app' => 'admin.orderCycles', 'ng-controller' => ng_controller, name: 'order_cycle_form'} do |f|

%save-bar{ dirty: "order_cycle_form.$dirty", persist: "true" }
%input.red{ type: "button", value: t(:update), ng: { click: "submit($event, null)", disabled: "!order_cycle_form.$dirty" } }
%input.red{ type: "button", value: t('.update_and_close'), ng: { click: "submit($event, '#{main_app.admin_order_cycles_path}')", disabled: "!order_cycle_form.$dirty" } }
%input.red{ type: "button", value: t(:update), ng: { click: "submit($event, null)", disabled: "!order_cycle_form.$dirty || order_cycle_form.$invalid" } }
%input.red{ type: "button", value: t('.update_and_close'), ng: { click: "submit($event, '#{main_app.admin_order_cycles_path}')", disabled: "!order_cycle_form.$dirty || order_cycle_form.$invalid" } }
%input{ type: "button", ng: { value: "order_cycle_form.$dirty ? 'Cancel' : 'Close'", click: "cancel('#{main_app.admin_order_cycles_path}')" } }

- if order_cycles_simple_form
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/order_cycles/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
= form_for [main_app, :admin, @order_cycle], :url => '', :html => {:class => 'ng order_cycle', 'ng-app' => 'admin.orderCycles', 'ng-controller' => ng_controller, name: 'order_cycle_form'} do |f|

%save-bar{ dirty: "order_cycle_form.$dirty", persist: "true" }
%input.red{ type: "button", value: t(:create), ng: { click: "submit($event, '#{main_app.admin_order_cycles_path}')", disabled: "!order_cycle_form.$dirty" } }
%input.red{ type: "button", value: t(:create), ng: { click: "submit($event, '#{main_app.admin_order_cycles_path}')", disabled: "!order_cycle_form.$dirty || order_cycle_form.$invalid" } }
%input{ type: "button", ng: { value: "order_cycle_form.$dirty ? 'Cancel' : 'Close'", click: "cancel('#{main_app.admin_order_cycles_path}')" } }

- if order_cycles_simple_form
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= t('.request_sent_text') %>

<%= t('.link_text') %>

<%= @edit_password_reset_url %>

<%= t('.issue_text') %>
1 change: 1 addition & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

# Tests assume English text on the site.
config.i18n.default_locale = "en"
I18n.locale = config.i18n.locale = config.i18n.default_locale

# Use SQL instead of Active Record's schema dumper when creating the test database.
# This is necessary if your schema can't be completely dumped by the schema dumper,
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en-GB.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ en-GB:
clear_all: Clear All
start_date: "Start Date"
end_date: "End Date"
unsaved_changes: "You have unsaved changes"
form_invalid: "Form contains missing or invalid fields"
columns: Columns
actions: Actions
viewing: "Viewing: %{current_view_name}"
Expand Down
12 changes: 12 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ en:
clear_all: Clear All
start_date: "Start Date"
end_date: "End Date"
unsaved_changes: "You have unsaved changes"
form_invalid: "Form contains missing or invalid fields"

columns: Columns
actions: Actions
Expand Down Expand Up @@ -1595,3 +1597,13 @@ Please follow the instructions there to make your enterprise visible on the Open
orders:
invoice:
tax_invoice: "TAX INVOICE: "
user_mailer:
reset_password_instructions:
request_sent_text: |
A request to reset your password has been made.
If you did not make this request, simply ignore this email.
link_text: >
If you did make this request just click the link below:
issue_text: |
If the above URL does not work try copying and pasting it into your browser.
If you continue to have problems please feel free to contact us.
12 changes: 1 addition & 11 deletions lib/open_food_network/enterprise_fee_applicator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,11 @@ def base_adjustment_label
end

def adjustment_tax(adjustable, adjustment)
tax_rates = rates_for(adjustable)
tax_rates = adjustment.tax_rates

tax_rates.select(&:included_in_price).sum do |rate|
rate.compute_tax adjustment.amount
end
end

def rates_for(adjustable)
case adjustable
when Spree::LineItem
tax_category = enterprise_fee.inherits_tax_category? ? adjustable.product.tax_category : enterprise_fee.tax_category
return tax_category ? tax_category.tax_rates.match(adjustable.order) : []
when Spree::Order
return enterprise_fee.tax_category ? enterprise_fee.tax_category.tax_rates.match(adjustable) : []
end
end
end
end
2 changes: 1 addition & 1 deletion lib/open_food_network/enterprise_injection_data.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module OpenFoodNetwork
class EnterpriseInjectionData
def active_distributors
@active_distributors ||= Enterprise.distributors_with_active_order_cycles
@active_distributors ||= Enterprise.distributors_with_active_order_cycles.ready_for_checkout
end

def earliest_closing_times
Expand Down
15 changes: 12 additions & 3 deletions lib/tasks/karma.rake
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
namespace :karma do
desc 'Debug on browser'
task :debug => :environment do
task :debug => :environment do |task|
continue_only_in_test_env task
with_tmp_config :start, '--browsers=Chrome --debug'
end

desc 'Continous run'
task :start => :environment do
task :start => :environment do |task|
continue_only_in_test_env task
with_tmp_config :start
end

desc 'Single run of tests'
task :run => :environment do
task :run => :environment do |task|
continue_only_in_test_env task
with_tmp_config :start, "--single-run"
end

private

def continue_only_in_test_env task
if Rails.env != 'test'
raise "Task must be called in test environment:\n bundle exec rake #{task.name} RAILS_ENV=test"
end
end

def with_tmp_config(command, args = nil)
Tempfile.open('karma_unit.js', Rails.root.join('tmp') ) do |f|
f.write unit_js(application_spec_files << i18n_file)
Expand Down
2 changes: 1 addition & 1 deletion script/ci/run_js_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ echo "--- Bundling"
bundle install

echo "--- Running tests"
bundle exec rake karma:run
./script/karma run
13 changes: 13 additions & 0 deletions script/karma
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/sh

task="$1"

if [ "$task" = "run" ] || [ "$task" = "start" ]; then
exec bundle exec rake "karma:$task" RAILS_ENV=test
else
echo "Usage:"
echo " $0 run # to run the tests once"
echo " $0 start # to run the tests on every file modification"
exit 1
fi

4 changes: 3 additions & 1 deletion spec/controllers/shop_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
require 'spec_helper'

describe ShopController do
let(:distributor) { create(:distributor_enterprise) }
let!(:pm) { create(:payment_method) }
let!(:sm) { create(:shipping_method) }
let(:distributor) { create(:distributor_enterprise, payment_methods: [pm], shipping_methods: [sm]) }

it "redirects to the home page if no distributor is selected" do
spree_get :show
Expand Down
2 changes: 1 addition & 1 deletion spec/controllers/shops_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
let!(:invisible_distributor) { create(:distributor_enterprise, visible: false) }

before do
Enterprise.stub(:distributors_with_active_order_cycles) { [distributor] }
Enterprise.stub_chain("distributors_with_active_order_cycles.ready_for_checkout") { [distributor] }
end

# Exclusion from actual rendered view handled in features/consumer/home
Expand Down
7 changes: 7 additions & 0 deletions spec/features/admin/order_cycles_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,8 @@
fill_in 'order_cycle_outgoing_exchange_0_pickup_instructions', with: 'New instructions 0'
fill_in 'order_cycle_outgoing_exchange_1_pickup_time', with: 'New time 1'
fill_in 'order_cycle_outgoing_exchange_1_pickup_instructions', with: 'New instructions 1'
fill_in 'order_cycle_outgoing_exchange_2_pickup_time', with: 'New time 2'
fill_in 'order_cycle_outgoing_exchange_2_pickup_instructions', with: 'New instructions 2'

page.find("table.exchanges tr.distributor-#{distributor.id} td.tags").click
within ".exchange-tags" do
Expand Down Expand Up @@ -616,6 +618,11 @@
select 'Permitted distributor', from: 'new_distributor_id'
click_button 'Add distributor'

fill_in 'order_cycle_outgoing_exchange_0_pickup_time', with: 'pickup time'
fill_in 'order_cycle_outgoing_exchange_0_pickup_instructions', with: 'pickup instructions'
fill_in 'order_cycle_outgoing_exchange_1_pickup_time', with: 'pickup time 2'
fill_in 'order_cycle_outgoing_exchange_1_pickup_instructions', with: 'pickup instructions'

# Should only have suppliers / distributors listed which the user is managing or
# has E2E permission to add products to order cycles
page.should_not have_select 'new_supplier_id', with_options: [supplier_unmanaged.name]
Expand Down
Loading

0 comments on commit c60182c

Please sign in to comment.