spree_multi_currency 1.0.4 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.coveralls.yml +0 -0
- data/.gitignore +1 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +21 -3
- data/README.markdown +8 -0
- data/Rakefile +4 -2
- data/app/assets/javascripts/admin/spree_multi_currency.js +0 -0
- data/app/assets/javascripts/store/spree_multi_currency.js +0 -0
- data/app/assets/stylesheets/admin/spree_multi_currency.css +0 -0
- data/app/assets/stylesheets/store/spree_multi_currency.css +0 -0
- data/app/controllers/spree/admin/currencies_controller.rb +3 -1
- data/app/controllers/spree/admin/currency_converters_controller.rb +2 -0
- data/app/controllers/spree/base_controller_decorator.rb +5 -2
- data/app/controllers/spree/currency_controller.rb +7 -2
- data/app/helpers/base_helper_decorator.rb +8 -0
- data/app/helpers/number_helper_decorator.rb +19 -17
- data/app/models/spree/adjustment_decorator.rb +2 -0
- data/app/models/spree/controller_helper_order.rb +11 -0
- data/app/models/spree/currency.rb +82 -38
- data/app/models/spree/currency_converter.rb +2 -0
- data/app/models/spree/line_item_decorator.rb +19 -6
- data/app/models/spree/money_decorator.rb +15 -0
- data/app/models/spree/order_decorator.rb +63 -55
- data/app/models/spree/product_decorator.rb +33 -0
- data/app/models/spree/stock/estimator_decorator.rb +18 -0
- data/app/models/spree/variant_decorator.rb +93 -16
- data/app/overrides/add_currencies_admin_configurations_menu.rb +8 -7
- data/app/overrides/add_currency_selection.rb +15 -0
- data/app/views/spree/admin/currencies/new.html.erb +4 -4
- data/app/views/spree/admin/currency_converters/edit.html.erb +4 -4
- data/app/views/spree/admin/currency_converters/new.html.erb +3 -3
- data/config/routes.rb +2 -0
- data/db/migrate/20101109134351_create_currencies.rb +2 -0
- data/db/migrate/20101109134453_create_currency_converters.rb +2 -0
- data/db/seeds.rb +2 -0
- data/features/step_definitions/product.rb +2 -0
- data/features/support/env.rb +2 -0
- data/features/support/paths.rb +2 -0
- data/lib/generators/spree_multi_currency/install/install_generator.rb +27 -15
- data/lib/spree_multi_currency.rb +13 -9
- data/lib/spree_multi_currency/engine.rb +6 -0
- data/lib/tasks/spree_multi_currency.rake +57 -38
- data/spec/changing_currency_spec.rb +44 -15
- data/spec/controllers/spree/currency_controller_spec.rb +16 -0
- data/spec/features/buy_spec.rb +103 -0
- data/spec/helpers/number_helper_spec.rb +21 -0
- data/spec/models/spree/currency_spec.rb +32 -0
- data/spec/models/spree/variant_spec.rb +32 -0
- data/spec/spec_helper.rb +50 -10
- data/spree_multi_currency.gemspec +2 -4
- metadata +29 -34
- data.tar.gz.sig +0 -3
- data/Versionfile +0 -9
- metadata.gz.sig +0 -1
data/.coveralls.yml
ADDED
File without changes
|
data/.gitignore
CHANGED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
CHANGED
@@ -1,11 +1,29 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
3
|
gem 'sqlite3'
|
4
4
|
|
5
5
|
group :test do
|
6
|
-
gem 'rspec-rails', '
|
6
|
+
gem 'rspec-rails', '>= 2.6.1'
|
7
7
|
gem 'factory_girl'
|
8
8
|
gem 'faker'
|
9
|
+
gem 'therubyracer'
|
10
|
+
gem 'execjs'
|
11
|
+
gem 'rspec'
|
12
|
+
gem 'capybara'
|
13
|
+
gem 'capybara-webkit'
|
14
|
+
gem 'factory_girl'
|
15
|
+
gem 'ffaker'
|
16
|
+
gem 'simplecov'
|
17
|
+
gem 'database_cleaner','1.0.1'
|
18
|
+
gem 'i18n-spec'
|
19
|
+
gem 'launchy'
|
20
|
+
gem 'coveralls', require: false
|
21
|
+
gem 'sass'
|
22
|
+
gem 'coffee-script'
|
9
23
|
end
|
10
24
|
|
11
|
-
|
25
|
+
gem 'spree', github: 'spree/spree', branch: '2-0-stable'
|
26
|
+
# provides basic authentication functionality for testing parts of your engine
|
27
|
+
gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: '2-0-stable'
|
28
|
+
|
29
|
+
gemspec
|
data/README.markdown
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
[![Coverage Status](https://coveralls.io/repos/pronix/spree-multi-currency/badge.png)](https://coveralls.io/r/pronix/spree-multi-currency)
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/pronix/spree-multi-currency.png)](https://travis-ci.org/pronix/spree-multi-currency)
|
4
|
+
|
1
5
|
# Spree Multi-Currency
|
2
6
|
|
3
7
|
Support different currency and recalculate price from one to another
|
@@ -180,6 +184,10 @@ Settings
|
|
180
184
|
|
181
185
|
For tests
|
182
186
|
_________________________
|
187
|
+
For start tests require exec:
|
188
|
+
`bundle exec rake test_app && bundle exec rake spec`
|
189
|
+
|
190
|
+
|
183
191
|
|
184
192
|
extention require store in ./spree
|
185
193
|
in Rakefile defined
|
data/Rakefile
CHANGED
@@ -1,9 +1,11 @@
|
|
1
|
+
# Encoding: utf-8
|
2
|
+
require 'rubygems'
|
1
3
|
require 'rake'
|
2
4
|
require 'rake/testtask'
|
3
5
|
require 'rake/packagetask'
|
4
6
|
require 'rubygems/package_task'
|
5
7
|
require 'rspec/core/rake_task'
|
6
|
-
require 'spree/
|
8
|
+
require 'spree/testing_support/common_rake'
|
7
9
|
|
8
10
|
RSpec::Core::RakeTask.new
|
9
11
|
|
@@ -25,4 +27,4 @@ desc "Generates a dummy app for testing"
|
|
25
27
|
task :test_app do
|
26
28
|
ENV['LIB_NAME'] = 'spree_multi_currency'
|
27
29
|
Rake::Task['common:test_app'].invoke
|
28
|
-
end
|
30
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -1,11 +1,14 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
Spree::BaseController.class_eval do
|
2
4
|
before_filter :set_currency
|
3
5
|
|
4
6
|
private
|
5
7
|
|
6
8
|
def set_currency
|
7
|
-
if session[:currency_id].present?
|
8
|
-
|
9
|
+
if session[:currency_id].present?
|
10
|
+
@currency = Spree::Currency.find_by_char_code(session[:currency_id])
|
11
|
+
Spree::Currency.current!(@currency) if @currency
|
9
12
|
end
|
10
13
|
end
|
11
14
|
|
@@ -1,9 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
module Spree
|
2
4
|
class CurrencyController < Spree::BaseController
|
3
5
|
|
4
6
|
def set
|
5
|
-
|
6
|
-
|
7
|
+
char_code = params[:id].to_s.upcase
|
8
|
+
@currency = Spree::Currency.find_by_char_code(char_code)
|
9
|
+
if @currency
|
10
|
+
session[:currency_id] = char_code.to_sym
|
7
11
|
Spree::Currency.current!(@currency)
|
8
12
|
flash.notice = t(:currency_changed)
|
9
13
|
else
|
@@ -12,5 +16,6 @@ module Spree
|
|
12
16
|
|
13
17
|
redirect_back_or_default(root_path)
|
14
18
|
end
|
19
|
+
|
15
20
|
end
|
16
21
|
end
|
@@ -1,3 +1,9 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Encoding: utf-8
|
4
|
+
#
|
5
|
+
# redefined default method
|
6
|
+
# http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html
|
1
7
|
module ActionView
|
2
8
|
module Helpers
|
3
9
|
module NumberHelper
|
@@ -5,11 +11,17 @@ module ActionView
|
|
5
11
|
def number_to_currency(number, options = {})
|
6
12
|
return nil if number.nil?
|
7
13
|
options.symbolize_keys!
|
8
|
-
|
9
|
-
|
10
|
-
|
14
|
+
currency_char = Spree::Currency.current.try(:char_code) ||
|
15
|
+
I18n.default_locale
|
16
|
+
options[:locale] = "currency_#{ currency_char }"
|
17
|
+
defaults = I18n.translate('number.format',
|
18
|
+
locale: options[:locale],
|
19
|
+
default: {})
|
20
|
+
currency = I18n.translate('number.currency.format',
|
21
|
+
locale: options[:locale],
|
22
|
+
default: {})
|
11
23
|
defaults = DEFAULT_CURRENCY_VALUES.merge(defaults).merge!(currency)
|
12
|
-
defaults[:negative_format] =
|
24
|
+
defaults[:negative_format] = '-%n %u'
|
13
25
|
options = defaults.merge!(options)
|
14
26
|
|
15
27
|
unit = options.delete(:unit)
|
@@ -17,24 +29,14 @@ module ActionView
|
|
17
29
|
|
18
30
|
if number.to_f < 0
|
19
31
|
format = options.delete(:negative_format)
|
20
|
-
number = number.
|
32
|
+
number = number.abs
|
21
33
|
end
|
22
34
|
|
23
|
-
|
24
|
-
|
25
|
-
format.gsub(/%n/, value).gsub(/%u/, unit).html_safe
|
26
|
-
rescue InvalidNumberError => e
|
27
|
-
if options[:raise]
|
28
|
-
raise
|
29
|
-
else
|
30
|
-
formatted_number = format.gsub(/%n/, e.number).gsub(/%u/, unit)
|
31
|
-
e.number.to_s.html_safe? ? formatted_number.html_safe : formatted_number
|
32
|
-
end
|
33
|
-
end
|
35
|
+
value = number_with_precision(number, options.merge(raise: true))
|
36
|
+
format.gsub(/%n/, value).gsub(/%u/, unit).html_safe
|
34
37
|
|
35
38
|
end
|
36
39
|
|
37
|
-
|
38
40
|
end
|
39
41
|
end
|
40
42
|
end
|
@@ -1,24 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require 'money'
|
2
|
-
class OriginMoney < Money
|
3
|
-
end
|
4
4
|
|
5
5
|
module Spree
|
6
6
|
class Currency < ActiveRecord::Base
|
7
7
|
|
8
8
|
has_many :currency_converters do
|
9
9
|
def get_rate(date)
|
10
|
-
last(:
|
10
|
+
last(conditions: ["date_req <= ?", date])
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
14
|
-
default_scope :
|
15
|
-
scope :locale, lambda{|str| where("locale like ?", "%#{str}%")}
|
14
|
+
default_scope order: 'spree_currencies.locale'
|
15
|
+
scope :locale, lambda { |str| where("locale like ?", "%#{str}%") }
|
16
16
|
after_save :reset_basic_currency
|
17
17
|
|
18
18
|
attr_accessible :basic, :locale, :char_code, :num_code, :name
|
19
19
|
|
20
|
+
# FIXME must be transaction
|
20
21
|
def basic!
|
21
|
-
self.class.update_all(:
|
22
|
+
self.class.update_all(basic: false) && update_attribute(:basic, true)
|
22
23
|
end
|
23
24
|
|
24
25
|
def locale=(locales)
|
@@ -26,40 +27,89 @@ module Spree
|
|
26
27
|
end
|
27
28
|
|
28
29
|
def locale(need_split = true)
|
29
|
-
|
30
|
+
locale_var = read_attribute(:locale).to_s
|
31
|
+
if need_split
|
32
|
+
locale_var.split(',')
|
33
|
+
else
|
34
|
+
locale_var
|
35
|
+
end
|
30
36
|
end
|
31
37
|
|
32
38
|
# We can only have one main currency.
|
33
39
|
# Therefore we reset all other currencies but the current if it's the main.
|
34
40
|
def reset_basic_currency
|
35
|
-
|
41
|
+
if basic?
|
42
|
+
self.class.where("id != ?", id).update_all(basic: false)
|
43
|
+
end
|
36
44
|
end
|
37
45
|
|
38
46
|
class << self
|
39
47
|
|
48
|
+
# return array of all char_codes
|
49
|
+
def all_currencies
|
50
|
+
all.map(&:char_code)
|
51
|
+
end
|
52
|
+
|
40
53
|
# Get the current locale
|
41
|
-
def current(
|
54
|
+
def current(current_locale = nil)
|
42
55
|
@current ||= locale(current_locale || I18n.locale).first
|
56
|
+
if @current
|
57
|
+
return @current
|
58
|
+
else
|
59
|
+
mess = 'Require load and set default currency'
|
60
|
+
mess << '<br/>'
|
61
|
+
mess << 'Locale field is factor for determine current currency'
|
62
|
+
raise mess
|
63
|
+
end
|
43
64
|
end
|
44
65
|
|
45
|
-
def current!(current_locale = nil
|
46
|
-
|
66
|
+
def current!(current_locale = nil)
|
67
|
+
if current_locale.is_a?(Spree::Currency)
|
68
|
+
@current = current_locale
|
69
|
+
else
|
70
|
+
@current = locale(current_locale || I18n.locale).first
|
71
|
+
end
|
47
72
|
end
|
48
73
|
|
49
|
-
def load_rate(options= {})
|
74
|
+
def load_rate(options = {})
|
50
75
|
current(options[:locale] || I18n.locale)
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
76
|
+
load_rate_from(@current.char_code)
|
77
|
+
end
|
78
|
+
|
79
|
+
# load rate for currency(char_code) to basic
|
80
|
+
def load_rate_from(from_char_code)
|
81
|
+
from_cur = Spree::Currency.find_by_char_code(from_char_code)
|
82
|
+
basic = Spree::Currency.basic
|
83
|
+
rate = from_cur.currency_converters.get_rate(Time.now)
|
84
|
+
if rate
|
85
|
+
add_rate(basic.char_code,
|
86
|
+
from_cur.char_code,
|
87
|
+
rate.nominal/rate.value.to_f)
|
88
|
+
add_rate(from_cur.char_code,
|
89
|
+
basic.char_code,
|
90
|
+
rate.value.to_f)
|
55
91
|
end
|
56
92
|
end
|
57
93
|
|
58
94
|
# Exchanges money between two currencies.
|
59
95
|
# E.g. with these args: 150, DKK, GBP returns 16.93
|
60
96
|
def convert(value, from, to)
|
61
|
-
|
62
|
-
|
97
|
+
begin
|
98
|
+
from_money = ::Money.new(value.to_f * 10000, from)
|
99
|
+
res = from_money.exchange_to(to)
|
100
|
+
res = (res.to_f / 100).round(2)
|
101
|
+
rescue => e
|
102
|
+
load_rate_from(from)
|
103
|
+
begin
|
104
|
+
currency_config = Spree::Config.currency
|
105
|
+
res = from_money.exchange_to(currency_config)
|
106
|
+
load_rate_from(to)
|
107
|
+
res = ::Money.new(res, currency_config).exchange_to(to)
|
108
|
+
res = res.to_f
|
109
|
+
rescue => e
|
110
|
+
raise "Require load actual currency \t\n #{e}"
|
111
|
+
end
|
112
|
+
end
|
63
113
|
res
|
64
114
|
end
|
65
115
|
|
@@ -70,47 +120,41 @@ module Spree
|
|
70
120
|
load_rate(options)
|
71
121
|
convert(value, @basic.char_code, @current.char_code)
|
72
122
|
rescue => ex
|
73
|
-
|
123
|
+
error_logger(ex)
|
74
124
|
value
|
75
125
|
end
|
76
126
|
|
77
|
-
# Converts the currency value of the current locale to the basic currency
|
127
|
+
# Converts the currency value of the current locale to the basic currency
|
78
128
|
# In the parameters you can specify the locale you wish to convert FROM.
|
79
129
|
# Usage: Currency.conversion_from_current(100, :locale => "da")
|
80
|
-
def conversion_from_current(value, options={})
|
130
|
+
def conversion_from_current(value, options = {})
|
81
131
|
load_rate(options)
|
82
|
-
|
83
|
-
# Replace commas with dots as decimal mark for those languages that use this.
|
84
|
-
|
85
|
-
# 2,000.00 => 2000.00
|
86
|
-
value.gsub!(",","") if (value =~ /\,[0-9]+\./)
|
87
|
-
|
88
|
-
# 2.000,00 => 2000.00
|
89
|
-
value.gsub!(".","").gsub!(",",".") if (value =~ /\.[0-9]+\,/)
|
90
|
-
|
91
|
-
# 2000,00 => 2000.00
|
92
|
-
value.gsub!(",","") if (value =~ /\.[0-9]+\,/)
|
93
|
-
|
94
|
-
convert(value, @current.char_code, @basic.char_code)
|
132
|
+
convert(parse_price(value), @current.char_code, @basic.char_code)
|
95
133
|
rescue => ex
|
96
|
-
|
134
|
+
error_logger(ex)
|
97
135
|
value
|
98
136
|
end
|
99
137
|
|
138
|
+
# write to error log
|
139
|
+
# ex - Exception from 'rescue => e'
|
140
|
+
def error_logger(ex)
|
141
|
+
mes = " [ Currency ] :#{ex.inspect} \n #{ex.backtrace.join('\n ')}"
|
142
|
+
Rails.logger.error mes
|
143
|
+
end
|
100
144
|
|
101
145
|
# Retrieves the main currency.
|
102
146
|
def basic
|
103
|
-
@basic ||= where(:
|
147
|
+
@basic ||= where(basic: true).first
|
104
148
|
end
|
105
149
|
|
106
|
-
def get(num_code, options ={
|
150
|
+
def get(num_code, options = {})
|
107
151
|
find_by_num_code(num_code) || create(options)
|
108
152
|
end
|
109
153
|
|
110
154
|
private
|
111
155
|
|
112
156
|
def add_rate(from, to, rate)
|
113
|
-
|
157
|
+
::Money.add_rate(from, to, rate.to_f)
|
114
158
|
end
|
115
159
|
|
116
160
|
end
|