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.
Files changed (55) hide show
  1. data/.coveralls.yml +0 -0
  2. data/.gitignore +1 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +5 -0
  5. data/Gemfile +21 -3
  6. data/README.markdown +8 -0
  7. data/Rakefile +4 -2
  8. data/app/assets/javascripts/admin/spree_multi_currency.js +0 -0
  9. data/app/assets/javascripts/store/spree_multi_currency.js +0 -0
  10. data/app/assets/stylesheets/admin/spree_multi_currency.css +0 -0
  11. data/app/assets/stylesheets/store/spree_multi_currency.css +0 -0
  12. data/app/controllers/spree/admin/currencies_controller.rb +3 -1
  13. data/app/controllers/spree/admin/currency_converters_controller.rb +2 -0
  14. data/app/controllers/spree/base_controller_decorator.rb +5 -2
  15. data/app/controllers/spree/currency_controller.rb +7 -2
  16. data/app/helpers/base_helper_decorator.rb +8 -0
  17. data/app/helpers/number_helper_decorator.rb +19 -17
  18. data/app/models/spree/adjustment_decorator.rb +2 -0
  19. data/app/models/spree/controller_helper_order.rb +11 -0
  20. data/app/models/spree/currency.rb +82 -38
  21. data/app/models/spree/currency_converter.rb +2 -0
  22. data/app/models/spree/line_item_decorator.rb +19 -6
  23. data/app/models/spree/money_decorator.rb +15 -0
  24. data/app/models/spree/order_decorator.rb +63 -55
  25. data/app/models/spree/product_decorator.rb +33 -0
  26. data/app/models/spree/stock/estimator_decorator.rb +18 -0
  27. data/app/models/spree/variant_decorator.rb +93 -16
  28. data/app/overrides/add_currencies_admin_configurations_menu.rb +8 -7
  29. data/app/overrides/add_currency_selection.rb +15 -0
  30. data/app/views/spree/admin/currencies/new.html.erb +4 -4
  31. data/app/views/spree/admin/currency_converters/edit.html.erb +4 -4
  32. data/app/views/spree/admin/currency_converters/new.html.erb +3 -3
  33. data/config/routes.rb +2 -0
  34. data/db/migrate/20101109134351_create_currencies.rb +2 -0
  35. data/db/migrate/20101109134453_create_currency_converters.rb +2 -0
  36. data/db/seeds.rb +2 -0
  37. data/features/step_definitions/product.rb +2 -0
  38. data/features/support/env.rb +2 -0
  39. data/features/support/paths.rb +2 -0
  40. data/lib/generators/spree_multi_currency/install/install_generator.rb +27 -15
  41. data/lib/spree_multi_currency.rb +13 -9
  42. data/lib/spree_multi_currency/engine.rb +6 -0
  43. data/lib/tasks/spree_multi_currency.rake +57 -38
  44. data/spec/changing_currency_spec.rb +44 -15
  45. data/spec/controllers/spree/currency_controller_spec.rb +16 -0
  46. data/spec/features/buy_spec.rb +103 -0
  47. data/spec/helpers/number_helper_spec.rb +21 -0
  48. data/spec/models/spree/currency_spec.rb +32 -0
  49. data/spec/models/spree/variant_spec.rb +32 -0
  50. data/spec/spec_helper.rb +50 -10
  51. data/spree_multi_currency.gemspec +2 -4
  52. metadata +29 -34
  53. data.tar.gz.sig +0 -3
  54. data/Versionfile +0 -9
  55. metadata.gz.sig +0 -1
data/.coveralls.yml ADDED
File without changes
data/.gitignore CHANGED
@@ -3,3 +3,4 @@
3
3
  Gemfile.lock
4
4
  pkg/*
5
5
  spec/dummy
6
+ coverage
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --backtrace
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ script: bundle exec rake test_app ; bundle exec rspec
5
+
data/Gemfile CHANGED
@@ -1,11 +1,29 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gem 'sqlite3'
4
4
 
5
5
  group :test do
6
- gem 'rspec-rails', '= 2.6.1'
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
- gemspec
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/core/testing_support/common_rake'
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
@@ -1,6 +1,8 @@
1
+ # encoding: utf-8
2
+
1
3
  module Spree
2
4
  class Admin::CurrenciesController < Admin::ResourceController
3
- before_filter :set_currency, :only => [:edit, :update]
5
+ before_filter :set_currency, only: [:edit, :update]
4
6
 
5
7
  private
6
8
 
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module Spree
2
4
  class Admin::CurrencyConvertersController < Admin::ResourceController
3
5
  end
@@ -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? && (@currency = Spree::Currency.find_by_char_code(session[:currency_id]))
8
- Spree::Currency.current!(@currency)
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
- if @currency = Spree::Currency.find_by_char_code(params[:id].to_s.upcase)
6
- session[:currency_id] = params[:id].to_s.upcase.to_sym
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
@@ -0,0 +1,8 @@
1
+ Spree::BaseHelper.class_eval do
2
+
3
+ # redefine spree/core/app/helpers/spree/base_helper.rb
4
+ def display_price(product_or_variant)
5
+ product_or_variant.price_in(Spree::Currency.current).money.to_html
6
+ end
7
+ end
8
+
@@ -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
- options[:locale] = "currency_#{ Spree::Currency.current.try(:char_code) || I18n.default_locale }"
9
- defaults = I18n.translate('number.format', :locale => options[:locale], :default => {})
10
- currency = I18n.translate('number.currency.format', :locale => options[:locale], :default => {})
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] = "-" + options[:format] if options[: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.respond_to?("abs") ? number.abs : number.sub(/^-/, '')
32
+ number = number.abs
21
33
  end
22
34
 
23
- begin
24
- value = number_with_precision(number, options.merge(:raise => true))
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,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  Spree::Adjustment.class_eval do
2
4
  extend Spree::MultiCurrency
3
5
  multi_currency :amount
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ Spree::Core::ControllerHelpers::Order.class_eval do
4
+
5
+ # redefined spree/core/lib/spree/core/controller_helpers/order.rb
6
+ def current_currency
7
+ Spree::Currency.current.char_code
8
+ end
9
+
10
+ end
11
+
@@ -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(:conditions => ["date_req <= ?", date])
10
+ last(conditions: ["date_req <= ?", date])
11
11
  end
12
12
  end
13
13
 
14
- default_scope :order => "spree_currencies.locale"
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(:basic => false) && update_attribute(:basic, true)
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
- need_split ? read_attribute(:locale).to_s.split(',') : read_attribute(:locale).to_s
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
- self.class.where("id != ?", self.id).update_all(:basic => false) if self.basic?
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( current_locale = nil )
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
- @current = current_locale.is_a?(Spree::Currency) ? current_locale : locale(current_locale||I18n.locale).first
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
- basic
52
- if @rate = @current.currency_converters.get_rate(options[:date] || Time.now)
53
- add_rate(@basic.char_code, @current.char_code, @rate.nominal/@rate.value.to_f)
54
- add_rate(@current.char_code, @basic.char_code, @rate.value.to_f)
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
- res = ( OriginMoney.new(value.to_f * 10000, from).exchange_to(to).to_f / 100).round(2)
62
- #Rails.logger.info "#{value} #{from} == #{res} #{to}"
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
- Rails.logger.error " [ Currency ] :#{ex.inspect} \n #{ex.backtrace.join('\n ')}"
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
- Rails.logger.error " [ Currency ] :#{ex.inspect} \n #{e.backtrace.join('\n ')}"
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(:basic => true).first
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
- OriginMoney.add_rate(from, to, rate.to_f )
157
+ ::Money.add_rate(from, to, rate.to_f)
114
158
  end
115
159
 
116
160
  end