Commit e493e3e1994d8166e7d053bc9b875776374f971d

Authored by Braulio Bhavamitra
1 parent 5391262c

Move core extensions out of noosfero scope

lib/extensions.rb 0 → 100644
@@ -0,0 +1,5 @@ @@ -0,0 +1,5 @@
  1 +require_relative 'extensions/string'
  2 +require_relative 'extensions/integer'
  3 +require_relative 'extensions/active_record/calculations'
  4 +require_relative 'extensions/active_record/reflection'
  5 +
lib/extensions/active_record/calculations.rb 0 → 100644
@@ -0,0 +1,10 @@ @@ -0,0 +1,10 @@
  1 +ActiveRecord::Calculations.class_eval do
  2 + def count_with_distinct column_name=self.primary_key
  3 + if column_name
  4 + distinct.count_without_distinct column_name
  5 + else
  6 + count_without_distinct
  7 + end
  8 + end
  9 + alias_method_chain :count, :distinct
  10 +end
lib/extensions/active_record/reflection.rb 0 → 100644
@@ -0,0 +1,20 @@ @@ -0,0 +1,20 @@
  1 +
  2 +# on STI classes tike Article and Profile, plugins' extensions
  3 +# on associations should be reflected on descendants
  4 +module ActiveRecord
  5 + module Reflection
  6 +
  7 + class << self
  8 +
  9 + def add_reflection_with_descendants(ar, name, reflection)
  10 + self.add_reflection_without_descendants ar, name, reflection
  11 + ar.descendants.each do |k|
  12 + k._reflections.merge!(name.to_s => reflection)
  13 + end if ar.base_class == ar
  14 + end
  15 +
  16 + alias_method_chain :add_reflection, :descendants
  17 +
  18 + end
  19 + end
  20 +end
lib/extensions/integer.rb 0 → 100644
@@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
  1 +class Integer
  2 + def to_humanreadable
  3 + value = self
  4 + if value < 1023
  5 + return "%i bytes" % value
  6 + end
  7 + value /= 1024
  8 +
  9 + if value < 1023
  10 + return "%1.1f KB" % value
  11 + end
  12 + value /= 1024
  13 +
  14 + if value < 1023
  15 + return "%1.1f MB" % value
  16 + end
  17 + value /= 1024
  18 +
  19 + if value < 1023
  20 + return "%1.1f GB" % value
  21 + end
  22 + value /= 1024
  23 +
  24 + if value < 1023
  25 + return "%1.1f TB" % value
  26 + end
  27 + value /= 1024
  28 +
  29 + if value < 1023
  30 + return "%1.1f PB" % value
  31 + end
  32 + value /= 1024
  33 +
  34 + if value < 1023
  35 + return "%1.1f EB" % value
  36 + end
  37 + value /= 1024
  38 + end
  39 +end
lib/extensions/string.rb 0 → 100644
@@ -0,0 +1,93 @@ @@ -0,0 +1,93 @@
  1 +# encoding: utf-8
  2 +
  3 +class String
  4 +
  5 + TRANSLITERATIONS = {
  6 + [ 'Á', 'À', 'À', 'Â', 'Ã', 'Ä', 'Å' ] => 'A',
  7 + [ 'á', 'à', 'à', 'â', 'ã', 'ä', 'å' ,'ª' ] => 'a',
  8 + [ 'É', 'È', 'Ê', 'Ë' ] => 'E',
  9 + [ 'é', 'è', 'ê', 'ë' ] => 'e',
  10 + [ 'Í', 'Ì', 'Î', 'Ï' ] => 'I',
  11 + [ 'í', 'ì', 'î', 'ï' ] => 'i',
  12 + [ 'Ó', 'Ò', 'Ô', 'Ö', 'Õ', 'º' ] => 'O',
  13 + [ 'ó', 'ò', 'ô', 'ö', 'õ', 'º' ] => 'o',
  14 + [ 'Ú', 'Ù', 'Û', 'Ü' ] => 'U',
  15 + [ 'ú', 'ù', 'û', 'ü' ] => 'u',
  16 + [ 'ß' ] => 'ss',
  17 + [ 'Ç' ] => 'C',
  18 + [ 'ç' ] => 'c',
  19 + [ 'Ñ' ] => 'N',
  20 + [ 'ñ' ] => 'n',
  21 + [ 'Ÿ' ] => 'Y',
  22 + [ 'ÿ' ] => 'y',
  23 +# Cyrillic alphabet transliteration
  24 + [ 'а', 'А' ] => 'a',
  25 + [ 'б', 'Б' ] => 'b',
  26 + [ 'в', 'В' ] => 'v',
  27 + [ 'г', 'Г' ] => 'g',
  28 + [ 'д', 'Д' ] => 'd',
  29 + [ 'е', 'Е' ] => 'e',
  30 + [ 'ё', 'Ё' ] => 'yo',
  31 + [ 'ж', 'Ж' ] => 'zh',
  32 + [ 'з', 'З' ] => 'z',
  33 + [ 'и', 'И' ] => 'i',
  34 + [ 'й', 'Й' ] => 'y',
  35 + [ 'к', 'К' ] => 'k',
  36 + [ 'л', 'Л' ] => 'l',
  37 + [ 'м', 'М' ] => 'm',
  38 + [ 'н', 'Н' ] => 'n',
  39 + [ 'о', 'О' ] => 'o',
  40 + [ 'п', 'П' ] => 'p',
  41 + [ 'р', 'Р' ] => 'r',
  42 + [ 'с', 'С' ] => 's',
  43 + [ 'т', 'Т' ] => 't',
  44 + [ 'у', 'У' ] => 'u',
  45 + [ 'ф', 'Ф' ] => 'f',
  46 + [ 'х', 'Х' ] => 'h',
  47 + [ 'ц', 'Ц' ] => 'ts',
  48 + [ 'ч', 'Ч' ] => 'ch',
  49 + [ 'ш', 'Ш' ] => 'sh',
  50 + [ 'щ', 'Щ' ] => 'sch',
  51 + [ 'э', 'Э' ] => 'e',
  52 + [ 'ю', 'Ю' ] => 'yu',
  53 + [ 'я', 'Я' ] => 'ya',
  54 + [ 'ы', 'Ы' ] => 'i',
  55 + [ 'ь', 'Ь' ] => '',
  56 + [ 'ъ', 'Ъ' ] => '',
  57 +# Ukrainian lovely letters
  58 + [ 'і', 'І' ] => 'i',
  59 + [ 'ї', 'Ї' ] => 'yi',
  60 + [ 'є', 'Є' ] => 'ye',
  61 + [ 'ґ', 'Ґ' ] => 'g',
  62 + }
  63 +
  64 + # transliterate a string (assumed to contain UTF-8 data)
  65 + # into ASCII by replacing non-ascii characters to their
  66 + # ASCII.
  67 + #
  68 + # The transliteration is, of course, lossy, and its performance is poor.
  69 + # Don't abuse this method.
  70 + def transliterate
  71 +
  72 + new = self.dup
  73 + TRANSLITERATIONS.each { |from,to|
  74 + from.each { |seq|
  75 + new.gsub!(seq, to)
  76 + }
  77 + }
  78 + new
  79 + end
  80 +
  81 + def to_slug
  82 + transliterate.downcase.gsub(/[^[[:word:]]~\s:;+=_."'`-]/, '').gsub(/[\s:;+=_"'`-]+/, '-').gsub(/-$/, '').gsub(/^-/, '').to_s
  83 + end
  84 +
  85 + def to_css_class
  86 + underscore.dasherize.gsub('/','_')
  87 + end
  88 +
  89 + def fix_i18n
  90 + self.sub('{fn} ', '')
  91 + end
  92 +
  93 +end
lib/noosfero/core_ext.rb
@@ -1,5 +0,0 @@ @@ -1,5 +0,0 @@
1 -require 'noosfero/core_ext/string'  
2 -require 'noosfero/core_ext/integer'  
3 -require 'noosfero/core_ext/active_record/calculations'  
4 -require 'noosfero/core_ext/active_record/reflection'  
5 -  
lib/noosfero/core_ext/active_record/calculations.rb
@@ -1,10 +0,0 @@ @@ -1,10 +0,0 @@
1 -ActiveRecord::Calculations.class_eval do  
2 - def count_with_distinct column_name=self.primary_key  
3 - if column_name  
4 - distinct.count_without_distinct column_name  
5 - else  
6 - count_without_distinct  
7 - end  
8 - end  
9 - alias_method_chain :count, :distinct  
10 -end  
lib/noosfero/core_ext/active_record/reflection.rb
@@ -1,20 +0,0 @@ @@ -1,20 +0,0 @@
1 -  
2 -# on STI classes tike Article and Profile, plugins' extensions  
3 -# on associations should be reflected on descendants  
4 -module ActiveRecord  
5 - module Reflection  
6 -  
7 - class << self  
8 -  
9 - def add_reflection_with_descendants(ar, name, reflection)  
10 - self.add_reflection_without_descendants ar, name, reflection  
11 - ar.descendants.each do |k|  
12 - k._reflections.merge!(name.to_s => reflection)  
13 - end if ar.base_class == ar  
14 - end  
15 -  
16 - alias_method_chain :add_reflection, :descendants  
17 -  
18 - end  
19 - end  
20 -end  
lib/noosfero/core_ext/integer.rb
@@ -1,39 +0,0 @@ @@ -1,39 +0,0 @@
1 -class Integer  
2 - def to_humanreadable  
3 - value = self  
4 - if value < 1023  
5 - return "%i bytes" % value  
6 - end  
7 - value /= 1024  
8 -  
9 - if value < 1023  
10 - return "%1.1f KB" % value  
11 - end  
12 - value /= 1024  
13 -  
14 - if value < 1023  
15 - return "%1.1f MB" % value  
16 - end  
17 - value /= 1024  
18 -  
19 - if value < 1023  
20 - return "%1.1f GB" % value  
21 - end  
22 - value /= 1024  
23 -  
24 - if value < 1023  
25 - return "%1.1f TB" % value  
26 - end  
27 - value /= 1024  
28 -  
29 - if value < 1023  
30 - return "%1.1f PB" % value  
31 - end  
32 - value /= 1024  
33 -  
34 - if value < 1023  
35 - return "%1.1f EB" % value  
36 - end  
37 - value /= 1024  
38 - end  
39 -end  
lib/noosfero/core_ext/string.rb
@@ -1,93 +0,0 @@ @@ -1,93 +0,0 @@
1 -# encoding: utf-8  
2 -  
3 -class String  
4 -  
5 - TRANSLITERATIONS = {  
6 - [ 'Á', 'À', 'À', 'Â', 'Ã', 'Ä', 'Å' ] => 'A',  
7 - [ 'á', 'à', 'à', 'â', 'ã', 'ä', 'å' ,'ª' ] => 'a',  
8 - [ 'É', 'È', 'Ê', 'Ë' ] => 'E',  
9 - [ 'é', 'è', 'ê', 'ë' ] => 'e',  
10 - [ 'Í', 'Ì', 'Î', 'Ï' ] => 'I',  
11 - [ 'í', 'ì', 'î', 'ï' ] => 'i',  
12 - [ 'Ó', 'Ò', 'Ô', 'Ö', 'Õ', 'º' ] => 'O',  
13 - [ 'ó', 'ò', 'ô', 'ö', 'õ', 'º' ] => 'o',  
14 - [ 'Ú', 'Ù', 'Û', 'Ü' ] => 'U',  
15 - [ 'ú', 'ù', 'û', 'ü' ] => 'u',  
16 - [ 'ß' ] => 'ss',  
17 - [ 'Ç' ] => 'C',  
18 - [ 'ç' ] => 'c',  
19 - [ 'Ñ' ] => 'N',  
20 - [ 'ñ' ] => 'n',  
21 - [ 'Ÿ' ] => 'Y',  
22 - [ 'ÿ' ] => 'y',  
23 -# Cyrillic alphabet transliteration  
24 - [ 'а', 'А' ] => 'a',  
25 - [ 'б', 'Б' ] => 'b',  
26 - [ 'в', 'В' ] => 'v',  
27 - [ 'г', 'Г' ] => 'g',  
28 - [ 'д', 'Д' ] => 'd',  
29 - [ 'е', 'Е' ] => 'e',  
30 - [ 'ё', 'Ё' ] => 'yo',  
31 - [ 'ж', 'Ж' ] => 'zh',  
32 - [ 'з', 'З' ] => 'z',  
33 - [ 'и', 'И' ] => 'i',  
34 - [ 'й', 'Й' ] => 'y',  
35 - [ 'к', 'К' ] => 'k',  
36 - [ 'л', 'Л' ] => 'l',  
37 - [ 'м', 'М' ] => 'm',  
38 - [ 'н', 'Н' ] => 'n',  
39 - [ 'о', 'О' ] => 'o',  
40 - [ 'п', 'П' ] => 'p',  
41 - [ 'р', 'Р' ] => 'r',  
42 - [ 'с', 'С' ] => 's',  
43 - [ 'т', 'Т' ] => 't',  
44 - [ 'у', 'У' ] => 'u',  
45 - [ 'ф', 'Ф' ] => 'f',  
46 - [ 'х', 'Х' ] => 'h',  
47 - [ 'ц', 'Ц' ] => 'ts',  
48 - [ 'ч', 'Ч' ] => 'ch',  
49 - [ 'ш', 'Ш' ] => 'sh',  
50 - [ 'щ', 'Щ' ] => 'sch',  
51 - [ 'э', 'Э' ] => 'e',  
52 - [ 'ю', 'Ю' ] => 'yu',  
53 - [ 'я', 'Я' ] => 'ya',  
54 - [ 'ы', 'Ы' ] => 'i',  
55 - [ 'ь', 'Ь' ] => '',  
56 - [ 'ъ', 'Ъ' ] => '',  
57 -# Ukrainian lovely letters  
58 - [ 'і', 'І' ] => 'i',  
59 - [ 'ї', 'Ї' ] => 'yi',  
60 - [ 'є', 'Є' ] => 'ye',  
61 - [ 'ґ', 'Ґ' ] => 'g',  
62 - }  
63 -  
64 - # transliterate a string (assumed to contain UTF-8 data)  
65 - # into ASCII by replacing non-ascii characters to their  
66 - # ASCII.  
67 - #  
68 - # The transliteration is, of course, lossy, and its performance is poor.  
69 - # Don't abuse this method.  
70 - def transliterate  
71 -  
72 - new = self.dup  
73 - TRANSLITERATIONS.each { |from,to|  
74 - from.each { |seq|  
75 - new.gsub!(seq, to)  
76 - }  
77 - }  
78 - new  
79 - end  
80 -  
81 - def to_slug  
82 - transliterate.downcase.gsub(/[^[[:word:]]~\s:;+=_."'`-]/, '').gsub(/[\s:;+=_"'`-]+/, '-').gsub(/-$/, '').gsub(/^-/, '').to_s  
83 - end  
84 -  
85 - def to_css_class  
86 - underscore.dasherize.gsub('/','_')  
87 - end  
88 -  
89 - def fix_i18n  
90 - self.sub('{fn} ', '')  
91 - end  
92 -  
93 -end