test_redmine_vz 0.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/Gemfile +4 -0
  4. data/README.md +71 -0
  5. data/Rakefile +20 -0
  6. data/config/currency_iso.json +2532 -0
  7. data/doc/CHANGELOG +55 -0
  8. data/doc/LICENSE.txt +339 -0
  9. data/lib/redmine_crm.rb +67 -0
  10. data/lib/redmine_crm/currency.rb +439 -0
  11. data/lib/redmine_crm/currency/formatting.rb +227 -0
  12. data/lib/redmine_crm/currency/heuristics.rb +151 -0
  13. data/lib/redmine_crm/currency/loader.rb +24 -0
  14. data/lib/redmine_crm/helpers/tags_helper.rb +15 -0
  15. data/lib/redmine_crm/helpers/vote_helper.rb +38 -0
  16. data/lib/redmine_crm/liquid/drops/issues_drop.rb +61 -0
  17. data/lib/redmine_crm/liquid/drops/news_drop.rb +45 -0
  18. data/lib/redmine_crm/liquid/drops/projects_drop.rb +78 -0
  19. data/lib/redmine_crm/liquid/drops/users_drop.rb +59 -0
  20. data/lib/redmine_crm/liquid/filters.rb +85 -0
  21. data/lib/redmine_crm/money_helper.rb +67 -0
  22. data/lib/redmine_crm/rcrm_acts_as_taggable.rb +342 -0
  23. data/lib/redmine_crm/rcrm_acts_as_viewed.rb +287 -0
  24. data/lib/redmine_crm/rcrm_acts_as_votable.rb +79 -0
  25. data/lib/redmine_crm/rcrm_acts_as_voter.rb +27 -0
  26. data/lib/redmine_crm/tag.rb +81 -0
  27. data/lib/redmine_crm/tag_list.rb +112 -0
  28. data/lib/redmine_crm/tagging.rb +20 -0
  29. data/lib/redmine_crm/version.rb +3 -0
  30. data/lib/redmine_crm/votable.rb +334 -0
  31. data/lib/redmine_crm/vote.rb +30 -0
  32. data/lib/redmine_crm/voter.rb +136 -0
  33. data/redmine_crm.gemspec +22 -0
  34. data/test/acts_as_taggable_test.rb +384 -0
  35. data/test/currency_test.rb +292 -0
  36. data/test/database.yml +17 -0
  37. data/test/fixtures/issue.rb +14 -0
  38. data/test/fixtures/issues.yml +12 -0
  39. data/test/fixtures/taggings.yml +32 -0
  40. data/test/fixtures/tags.yml +11 -0
  41. data/test/fixtures/user.rb +7 -0
  42. data/test/fixtures/users.yml +5 -0
  43. data/test/fixtures/votable_caches.yml +2 -0
  44. data/test/fixtures/votables.yml +4 -0
  45. data/test/fixtures/vote_classes.rb +54 -0
  46. data/test/fixtures/voters.yml +6 -0
  47. data/test/liquid_test.rb +80 -0
  48. data/test/money_helper_test.rb +12 -0
  49. data/test/schema.rb +100 -0
  50. data/test/tag_test.rb +63 -0
  51. data/test/tagging_test.rb +14 -0
  52. data/test/tags_helper_test.rb +29 -0
  53. data/test/test_helper.rb +118 -0
  54. data/test/viewed_test.rb +45 -0
  55. data/test/votable_model_test.rb +478 -0
  56. data/test/votable_test.rb +17 -0
  57. data/test/vote_helper_test.rb +28 -0
  58. data/test/voter_model_test.rb +296 -0
  59. metadata +141 -0
@@ -0,0 +1,439 @@
1
+ require "json"
2
+ require "redmine_crm/currency/loader"
3
+ require "redmine_crm/currency/heuristics"
4
+ require "redmine_crm/currency/formatting"
5
+
6
+ module RedmineCrm
7
+ # Represents a specific currency unit.
8
+ #
9
+ # @see http://en.wikipedia.org/wiki/Currency
10
+ # @see http://iso4217.net/
11
+ class Currency
12
+ include Comparable
13
+ extend Enumerable
14
+ extend RedmineCrm::Currency::Loader
15
+ extend RedmineCrm::Currency::Heuristics
16
+ extend RedmineCrm::Currency::Formatting
17
+
18
+
19
+
20
+ # Thrown when a Currency has been registered without all the attributes
21
+ # which are required for the current action.
22
+ class MissingAttributeError < StandardError
23
+ def initialize(method, currency, attribute)
24
+ super(
25
+ "Can't call Currency.#{method} - currency '#{currency}' is missing "\
26
+ "the attribute '#{attribute}'"
27
+ )
28
+ end
29
+ end
30
+
31
+ # Thrown when an unknown currency is requested.
32
+ class UnknownCurrency < ArgumentError; end
33
+
34
+ class << self
35
+
36
+ # Lookup a currency with given +id+ an returns a +Currency+ instance on
37
+ # success, +nil+ otherwise.
38
+ #
39
+ # @param [String, Symbol, #to_s] id Used to look into +table+ and
40
+ # retrieve the applicable attributes.
41
+ #
42
+ # @return [Money::Currency]
43
+ #
44
+ # @example
45
+ # Money::Currency.find(:eur) #=> #<Money::Currency id: eur ...>
46
+ # Money::Currency.find(:foo) #=> nil
47
+ def find(id)
48
+ return nil if id == ""
49
+ id = id.to_s.downcase.to_sym
50
+ new(id)
51
+ rescue UnknownCurrency
52
+ nil
53
+ end
54
+
55
+ # Lookup a currency with given +num+ as an ISO 4217 numeric and returns an
56
+ # +Currency+ instance on success, +nil+ otherwise.
57
+ #
58
+ # @param [#to_s] num used to look into +table+ in +iso_numeric+ and find
59
+ # the right currency id.
60
+ #
61
+ # @return [Money::Currency]
62
+ #
63
+ # @example
64
+ # Money::Currency.find_by_iso_numeric(978) #=> #<Money::Currency id: eur ...>
65
+ # Money::Currency.find_by_iso_numeric('001') #=> nil
66
+ def find_by_iso_numeric(num)
67
+ num = num.to_s
68
+ id, _ = self.table.find{|key, currency| currency[:iso_numeric] == num}
69
+ new(id)
70
+ rescue UnknownCurrency
71
+ nil
72
+ end
73
+
74
+ # Wraps the object in a +Currency+ unless it's already a +Currency+
75
+ # object.
76
+ #
77
+ # @param [Object] object The object to attempt and wrap as a +Currency+
78
+ # object.
79
+ #
80
+ # @return [Money::Currency]
81
+ #
82
+ # @example
83
+ # c1 = Money::Currency.new(:usd)
84
+ # Money::Currency.wrap(nil) #=> nil
85
+ # Money::Currency.wrap(c1) #=> #<Money::Currency id: usd ...>
86
+ # Money::Currency.wrap("usd") #=> #<Money::Currency id: usd ...>
87
+ def wrap(object)
88
+ if object.nil?
89
+ nil
90
+ elsif object.is_a?(Currency)
91
+ object
92
+ else
93
+ Currency.new(object)
94
+ end
95
+ end
96
+
97
+ # List of known currencies.
98
+ #
99
+ # == monetary unit
100
+ # The standard unit of value of a currency, as the dollar in the United States or the peso in Mexico.
101
+ # http://www.answers.com/topic/monetary-unit
102
+ # == fractional monetary unit, subunit
103
+ # A monetary unit that is valued at a fraction (usually one hundredth) of the basic monetary unit
104
+ # http://www.answers.com/topic/fractional-monetary-unit-subunit
105
+ #
106
+ # See http://en.wikipedia.org/wiki/List_of_circulating_currencies and
107
+ # http://search.cpan.org/~tnguyen/Locale-Currency-Format-1.28/Format.pm
108
+ def table
109
+ @table ||= load_currencies
110
+ end
111
+
112
+ # List the currencies imported and registered
113
+ # @return [Array]
114
+ #
115
+ # @example
116
+ # Money::Currency.iso_codes()
117
+ # [#<Currency ..USD>, 'CAD', 'EUR']...
118
+ def all
119
+ table.keys.map do |curr|
120
+ c = Currency.new(curr)
121
+ if c.priority.nil?
122
+ raise MissingAttributeError.new(:all, c.id, :priority)
123
+ end
124
+ c
125
+ end.sort_by(&:priority)
126
+ end
127
+
128
+ # We need a string-based validator before creating an unbounded number of
129
+ # symbols.
130
+ # http://www.randomhacks.net/articles/2007/01/20/13-ways-of-looking-at-a-ruby-symbol#11
131
+ # https://github.com/RubyMoney/money/issues/132
132
+ #
133
+ # @return [Set]
134
+ def stringified_keys
135
+ @stringified_keys ||= stringify_keys
136
+ end
137
+
138
+ # Register a new currency
139
+ #
140
+ # @param curr [Hash] information about the currency
141
+ # @option priority [Numeric] a numerical value you can use to sort/group
142
+ # the currency list
143
+ # @option iso_code [String] the international 3-letter code as defined
144
+ # by the ISO 4217 standard
145
+ # @option iso_numeric [Integer] the international 3-digit code as
146
+ # defined by the ISO 4217 standard
147
+ # @option name [String] the currency name
148
+ # @option symbol [String] the currency symbol (UTF-8 encoded)
149
+ # @option subunit [String] the name of the fractional monetary unit
150
+ # @option subunit_to_unit [Numeric] the proportion between the unit and
151
+ # the subunit
152
+ # @option separator [String] character between the whole and fraction
153
+ # amounts
154
+ # @option delimiter [String] character between each thousands place
155
+ def register(curr)
156
+ key = curr.fetch(:iso_code).downcase.to_sym
157
+ table if !@table
158
+ @table[key] = curr
159
+ @stringified_keys = stringify_keys
160
+ end
161
+
162
+
163
+ # Unregister a currency.
164
+ #
165
+ # @param [Object] curr A Hash with the key `:iso_code`, or the ISO code
166
+ # as a String or Symbol.
167
+ #
168
+ # @return [Boolean] true if the currency previously existed, false
169
+ # if it didn't.
170
+ def unregister(curr)
171
+ if curr.is_a?(Hash)
172
+ key = curr.fetch(:iso_code).to_s.downcase.to_sym
173
+ else
174
+ key = curr.to_s.downcase.to_sym
175
+ end
176
+ existed = @table.delete(key)
177
+ @stringified_keys = stringify_keys
178
+ existed ? true : false
179
+ end
180
+
181
+
182
+ def each
183
+ all.each { |c| yield(c) }
184
+ end
185
+
186
+ private
187
+
188
+ def stringify_keys
189
+ table.keys.each_with_object(Set.new) { |k, set| set.add(k.to_s.downcase) }
190
+ end
191
+ end
192
+
193
+ # @!attribute [r] id
194
+ # @return [Symbol] The symbol used to identify the currency, usually THE
195
+ # lowercase +iso_code+ attribute.
196
+ # @!attribute [r] priority
197
+ # @return [Integer] A numerical value you can use to sort/group the
198
+ # currency list.
199
+ # @!attribute [r] iso_code
200
+ # @return [String] The international 3-letter code as defined by the ISO
201
+ # 4217 standard.
202
+ # @!attribute [r] iso_numeric
203
+ # @return [String] The international 3-numeric code as defined by the ISO
204
+ # 4217 standard.
205
+ # @!attribute [r] name
206
+ # @return [String] The currency name.
207
+ # @!attribute [r] symbol
208
+ # @return [String] The currency symbol (UTF-8 encoded).
209
+ # @!attribute [r] disambiguate_symbol
210
+ # @return [String] Alternative currency used if symbol is ambiguous
211
+ # @!attribute [r] html_entity
212
+ # @return [String] The html entity for the currency symbol
213
+ # @!attribute [r] subunit
214
+ # @return [String] The name of the fractional monetary unit.
215
+ # @!attribute [r] subunit_to_unit
216
+ # @return [Integer] The proportion between the unit and the subunit
217
+ # @!attribute [r] decimal_mark
218
+ # @return [String] The decimal mark, or character used to separate the
219
+ # whole unit from the subunit.
220
+ # @!attribute [r] The
221
+ # @return [String] character used to separate thousands grouping of the
222
+ # whole unit.
223
+ # @!attribute [r] symbol_first
224
+ # @return [Boolean] Should the currency symbol precede the amount, or
225
+ # should it come after?
226
+ # @!attribute [r] smallest_denomination
227
+ # @return [Integer] Smallest amount of cash possible (in the subunit of
228
+ # this currency)
229
+
230
+ attr_reader :id, :priority, :iso_code, :iso_numeric, :name, :symbol,
231
+ :disambiguate_symbol, :html_entity, :subunit, :subunit_to_unit, :decimal_mark,
232
+ :thousands_separator, :symbol_first, :smallest_denomination
233
+
234
+ alias_method :separator, :decimal_mark
235
+ alias_method :delimiter, :thousands_separator
236
+ alias_method :eql?, :==
237
+
238
+ # Create a new +Currency+ object.
239
+ #
240
+ # @param [String, Symbol, #to_s] id Used to look into +table+ and retrieve
241
+ # the applicable attributes.
242
+ #
243
+ # @return [Money::Currency]
244
+ #
245
+ # @example
246
+ # Money::Currency.new(:usd) #=> #<Money::Currency id: usd ...>
247
+ def initialize(id)
248
+ id = id.to_s.downcase
249
+ unless self.class.stringified_keys.include?(id)
250
+ raise UnknownCurrency, "Unknown currency '#{id}'"
251
+ end
252
+ @id = id.to_sym
253
+ initialize_data!
254
+ end
255
+
256
+ # Compares +self+ with +other_currency+ against the value of +priority+
257
+ # attribute.
258
+ #
259
+ # @param [Money::Currency] other_currency The currency to compare to.
260
+ #
261
+ # @return [-1,0,1] -1 if less than, 0 is equal to, 1 if greater than
262
+ #
263
+ # @example
264
+ # c1 = Money::Currency.new(:usd)
265
+ # c2 = Money::Currency.new(:jpy)
266
+ # c1 <=> c2 #=> 1
267
+ # c2 <=> c1 #=> -1
268
+ # c1 <=> c1 #=> 0
269
+ def <=>(other_currency)
270
+ # <=> returns nil when one of the values is nil
271
+ comparison = (self.priority <=> other_currency.priority || 0) rescue 0
272
+
273
+ if comparison == 0
274
+ self.id.to_s <=> other_currency.id.to_s
275
+ else
276
+ comparison
277
+ end
278
+ end
279
+
280
+ # Compares +self+ with +other_currency+ and returns +true+ if the are the
281
+ # same or if their +id+ attributes match.
282
+ #
283
+ # @param [Money::Currency] other_currency The currency to compare to.
284
+ #
285
+ # @return [Boolean]
286
+ #
287
+ # @example
288
+ # c1 = Money::Currency.new(:usd)
289
+ # c2 = Money::Currency.new(:jpy)
290
+ # c1 == c1 #=> true
291
+ # c1 == c2 #=> false
292
+ def ==(other_currency)
293
+ self.equal?(other_currency) || compare_ids(other_currency)
294
+ end
295
+
296
+ def compare_ids(other_currency)
297
+ other_currency_id = if other_currency.is_a?(Currency)
298
+ other_currency.id.to_s.downcase
299
+ else
300
+ other_currency.to_s.downcase
301
+ end
302
+ self.id.to_s.downcase == other_currency_id
303
+ end
304
+
305
+ private :compare_ids
306
+
307
+ # Returns a Fixnum hash value based on the +id+ attribute in order to use
308
+ # functions like & (intersection), group_by, etc.
309
+ #
310
+ # @return [Fixnum]
311
+ #
312
+ # @example
313
+ # Money::Currency.new(:usd).hash #=> 428936
314
+ def hash
315
+ id.hash
316
+ end
317
+
318
+ # Returns a human readable representation.
319
+ #
320
+ # @return [String]
321
+ #
322
+ # @example
323
+ # Money::Currency.new(:usd) #=> #<Currency id: usd ...>
324
+ def inspect
325
+ "#<#{self.class.name} id: #{id}, priority: #{priority}, symbol_first: #{symbol_first}, thousands_separator: #{thousands_separator}, html_entity: #{html_entity}, decimal_mark: #{decimal_mark}, name: #{name}, symbol: #{symbol}, subunit_to_unit: #{subunit_to_unit}, exponent: #{exponent}, iso_code: #{iso_code}, iso_numeric: #{iso_numeric}, subunit: #{subunit}, smallest_denomination: #{smallest_denomination}>"
326
+ end
327
+
328
+ # Returns a string representation corresponding to the upcase +id+
329
+ # attribute.
330
+ #
331
+ # --
332
+ # DEV: id.to_s.upcase corresponds to iso_code but don't use ISO_CODE for consistency.
333
+ #
334
+ # @return [String]
335
+ #
336
+ # @example
337
+ # Money::Currency.new(:usd).to_s #=> "USD"
338
+ # Money::Currency.new(:eur).to_s #=> "EUR"
339
+ def to_s
340
+ id.to_s.upcase
341
+ end
342
+
343
+ # Returns a string representation corresponding to the upcase +id+
344
+ # attribute. Useful in cases where only implicit conversions are made.
345
+ #
346
+ # @return [String]
347
+ #
348
+ # @example
349
+ # Money::Currency.new(:usd).to_str #=> "USD"
350
+ # Money::Currency.new(:eur).to_str #=> "EUR"
351
+ def to_str
352
+ id.to_s.upcase
353
+ end
354
+
355
+ # Returns a symbol representation corresponding to the upcase +id+
356
+ # attribute.
357
+ #
358
+ # @return [Symbol]
359
+ #
360
+ # @example
361
+ # Money::Currency.new(:usd).to_sym #=> :USD
362
+ # Money::Currency.new(:eur).to_sym #=> :EUR
363
+ def to_sym
364
+ id.to_s.upcase.to_sym
365
+ end
366
+
367
+ # Conversation to +self+.
368
+ #
369
+ # @return [self]
370
+ def to_currency
371
+ self
372
+ end
373
+
374
+ # Returns currency symbol or iso code for currencies with no symbol.
375
+ #
376
+ # @return [String]
377
+ def code
378
+ symbol || iso_code
379
+ end
380
+
381
+ def symbol_first?
382
+ !!@symbol_first
383
+ end
384
+
385
+ # Returns the number of digits after the decimal separator.
386
+ #
387
+ # @return [Float]
388
+ def exponent
389
+ Math.log10(@subunit_to_unit) if @subunit_to_unit
390
+ end
391
+
392
+ # Cache decimal places for subunit_to_unit values. Common ones pre-cached.
393
+ def self.decimal_places_cache
394
+ @decimal_places_cache ||= {1 => 0, 10 => 1, 100 => 2, 1000 => 3}
395
+ end
396
+
397
+ # The number of decimal places needed.
398
+ #
399
+ # @return [Integer]
400
+ def decimal_places
401
+ cache[subunit_to_unit] ||= calculate_decimal_places(subunit_to_unit)
402
+ end
403
+
404
+ private
405
+
406
+ def cache
407
+ self.class.decimal_places_cache
408
+ end
409
+
410
+ # If we need to figure out how many decimal places we need we
411
+ # use repeated integer division.
412
+ def calculate_decimal_places(num)
413
+ i = 1
414
+ while num >= 10
415
+ num /= 10
416
+ i += 1 if num >= 10
417
+ end
418
+ i
419
+ end
420
+
421
+ def initialize_data!
422
+ data = self.class.table[@id]
423
+ @alternate_symbols = data[:alternate_symbols]
424
+ @decimal_mark = data[:decimal_mark]
425
+ @disambiguate_symbol = data[:disambiguate_symbol]
426
+ @html_entity = data[:html_entity]
427
+ @iso_code = data[:iso_code]
428
+ @iso_numeric = data[:iso_numeric]
429
+ @name = data[:name]
430
+ @priority = data[:priority]
431
+ @smallest_denomination = data[:smallest_denomination]
432
+ @subunit = data[:subunit]
433
+ @subunit_to_unit = data[:subunit_to_unit]
434
+ @symbol = data[:symbol]
435
+ @symbol_first = data[:symbol_first]
436
+ @thousands_separator = data[:thousands_separator]
437
+ end
438
+ end
439
+ end
@@ -0,0 +1,227 @@
1
+ # encoding: UTF-8
2
+ module RedmineCrm
3
+ class Currency
4
+ module Formatting
5
+ def self.included(base)
6
+ [
7
+ [:thousands_separator, :delimiter, ","],
8
+ [:decimal_mark, :separator, "."]
9
+ ].each do |method, name, character|
10
+ define_i18n_method(method, name, character)
11
+ end
12
+ end
13
+
14
+ def self.define_i18n_method(method, name, character)
15
+ define_method(method) do
16
+ if self.class.use_i18n
17
+ begin
18
+ I18n.t name, :scope => "number.currency.format", :raise => true
19
+ rescue I18n::MissingTranslationData
20
+ I18n.t name, :scope =>"number.format", :default => (currency.send(method) || character)
21
+ end
22
+ else
23
+ currency.send(method) || character
24
+ end
25
+ end
26
+ alias_method name, method
27
+ end
28
+
29
+
30
+ def format(value, currency, *rules)
31
+ # support for old format parameters
32
+ rules = normalize_formatting_rules(rules)
33
+ if currency
34
+ rules = self.localize_formatting_rules(rules, currency)
35
+ rules = self.translate_formatting_rules(rules, currency.code) if rules[:translate]
36
+ rules[:decimal_mark] = currency.decimal_mark if rules[:decimal_mark].nil?
37
+ rules[:decimal_places] = currency.decimal_places
38
+ rules[:subunit_to_unit] = currency.subunit_to_unit
39
+ rules[:thousands_separator] = currency.thousands_separator if rules[:thousands_separator].nil?
40
+ end
41
+ rules = Currency.default_formatting_rules.merge(rules){|key, v1, v2| v2.nil? ? v1 : v2}
42
+
43
+ # if fractional == 0
44
+ if rules[:display_free].respond_to?(:to_str)
45
+ return rules[:display_free]
46
+ elsif rules[:display_free]
47
+ return "free"
48
+ end
49
+ # end
50
+
51
+ symbol_value = currency.try(:symbol) || ""
52
+
53
+ formatted = value.abs.to_s
54
+
55
+ # if rules[:rounded_infinite_precision]
56
+ if currency
57
+ formatted.gsub!(/#{rules[:decimal_mark]}/, '.') unless '.' == rules[:decimal_mark]
58
+ formatted = ((BigDecimal(formatted) * currency.subunit_to_unit).round / BigDecimal(currency.subunit_to_unit.to_s)).to_s("F")
59
+ formatted.gsub!(/\..*/) do |decimal_part|
60
+ decimal_part << '0' while decimal_part.length < (currency.decimal_places + 1)
61
+ decimal_part
62
+ end
63
+ formatted.gsub!(/\./, rules[:decimal_mark]) unless '.' == rules[:decimal_mark]
64
+ end
65
+
66
+ sign = value < 0 ? '-' : ''
67
+
68
+ if rules[:no_cents] || (rules[:no_cents_if_whole] && cents % currency.subunit_to_unit == 0)
69
+ formatted = "#{formatted.to_i}"
70
+ end
71
+
72
+ # thousands_separator_value = currency.thousands_separator
73
+ # Determine thousands_separator
74
+ if rules.has_key?(:thousands_separator)
75
+ thousands_separator_value = rules[:thousands_separator] || ''
76
+ end
77
+ decimal_mark = rules[:decimal_mark]
78
+ # Apply thousands_separator
79
+ formatted.gsub!(regexp_format(formatted, rules, decimal_mark, symbol_value),
80
+ "\\1#{thousands_separator_value}")
81
+
82
+ symbol_position = symbol_position_from(rules, currency) if currency
83
+
84
+ if rules[:sign_positive] == true && (value >= 0)
85
+ sign = '+'
86
+ end
87
+
88
+ if rules[:sign_before_symbol] == true
89
+ sign_before = sign
90
+ sign = ''
91
+ end
92
+
93
+ if symbol_value && !symbol_value.empty?
94
+ symbol_value = "<span class=\"currency_symbol\">#{symbol_value}</span>" if rules[:html_wrap_symbol]
95
+ formatted = if symbol_position == :before
96
+ symbol_space = rules[:symbol_before_without_space] === false ? " " : ""
97
+ "#{sign_before}#{symbol_value}#{symbol_space}#{sign}#{formatted}"
98
+ else
99
+ symbol_space = rules[:symbol_after_without_space] ? "" : " "
100
+ "#{sign_before}#{sign}#{formatted}#{symbol_space}#{symbol_value}"
101
+ end
102
+ else
103
+ formatted="#{sign_before}#{sign}#{formatted}"
104
+ end
105
+
106
+ # apply_decimal_mark_from_rules(formatted, rules)
107
+
108
+ if rules[:with_currency]
109
+ formatted << " "
110
+ formatted << '<span class="currency">' if rules[:html]
111
+ formatted << currency.to_s
112
+ formatted << '</span>' if rules[:html]
113
+ end
114
+ formatted
115
+ end
116
+
117
+ def default_formatting_rules
118
+ {
119
+ :decimal_mark =>".",
120
+ :thousands_separator => ",",
121
+ :subunit_to_unit => 100
122
+ }
123
+ end
124
+
125
+ def regexp_format(formatted, rules, decimal_mark, symbol_value)
126
+ regexp_decimal = Regexp.escape(decimal_mark)
127
+ if rules[:south_asian_number_formatting]
128
+ /(\d+?)(?=(\d\d)+(\d)(?:\.))/
129
+ else
130
+ # Symbols may contain decimal marks (E.g "դր.")
131
+ if formatted.sub(symbol_value.to_s, "") =~ /#{regexp_decimal}/
132
+ /(\d)(?=(?:\d{3})+(?:#{regexp_decimal}))/
133
+ else
134
+ /(\d)(?=(?:\d{3})+(?:[^\d]{1}|$))/
135
+ end
136
+ end
137
+ end
138
+
139
+ def translate_formatting_rules(rules, iso_code)
140
+ begin
141
+ rules[:symbol] = I18n.t iso_code, :scope => "number.currency.symbol", :raise => true
142
+ rescue I18n::MissingTranslationData
143
+ # Do nothing
144
+ end
145
+ rules
146
+ end
147
+
148
+ def localize_formatting_rules(rules, currency)
149
+ if currency.iso_code == "JPY" && I18n.locale == :ja
150
+ rules[:symbol] = "円" unless rules[:symbol] == false
151
+ rules[:symbol_position] = :after
152
+ rules[:symbol_after_without_space] = true
153
+ elsif currency.iso_code == "CHF"
154
+ rules[:symbol_before_without_space] = false
155
+ end
156
+ rules
157
+ end
158
+
159
+ def symbol_value_from(rules)
160
+ if rules.has_key?(:symbol)
161
+ if rules[:symbol] === true
162
+ symbol
163
+ elsif rules[:symbol]
164
+ rules[:symbol]
165
+ else
166
+ ""
167
+ end
168
+ elsif rules[:html]
169
+ currency.html_entity == '' ? currency.symbol : currency.html_entity
170
+ elsif rules[:disambiguate] and currency.disambiguate_symbol
171
+ currency.disambiguate_symbol
172
+ else
173
+ symbol
174
+ end
175
+ end
176
+
177
+ def symbol_position_from(rules, currency)
178
+ if rules.has_key?(:symbol_position)
179
+ if [:before, :after].include?(rules[:symbol_position])
180
+ return rules[:symbol_position]
181
+ else
182
+ raise ArgumentError, ":symbol_position must be ':before' or ':after'"
183
+ end
184
+ elsif currency.symbol_first?
185
+ :before
186
+ else
187
+ :after
188
+ end
189
+ end
190
+
191
+ private
192
+
193
+ # Cleans up formatting rules.
194
+ #
195
+ # @param [Hash] rules
196
+ #
197
+ # @return [Hash]
198
+ def normalize_formatting_rules(rules)
199
+ if rules.size == 0
200
+ rules = {}
201
+ elsif rules.size == 1
202
+ rules = rules.pop
203
+ rules = { rules => true } if rules.is_a?(Symbol)
204
+ end
205
+ rules[:decimal_mark] = rules[:separator] || rules[:decimal_mark]
206
+ rules[:thousands_separator] = rules[:delimiter] || rules[:thousands_separator]
207
+ rules
208
+ end
209
+
210
+ # Applies decimal mark from rules to formatted
211
+ #
212
+ # @param [String] formatted
213
+ # @param [Hash] rules
214
+ def apply_decimal_mark_from_rules(formatted, rules)
215
+ if rules.has_key?(:decimal_mark) && rules[:decimal_mark]
216
+ # && rules[:decimal_mark] != decimal_mark
217
+
218
+ regexp_decimal = Regexp.escape(rules[:decimal_mark])
219
+ formatted.sub!(/(.*)(#{regexp_decimal})(.*)\Z/,
220
+ "\\1#{rules[:decimal_mark]}\\3")
221
+ end
222
+ end
223
+
224
+ end
225
+
226
+ end
227
+ end