From e493e3e1994d8166e7d053bc9b875776374f971d Mon Sep 17 00:00:00 2001 From: Braulio Bhavamitra Date: Sat, 14 May 2016 10:42:59 -0300 Subject: [PATCH] Move core extensions out of noosfero scope --- lib/extensions.rb | 5 +++++ lib/extensions/active_record/calculations.rb | 10 ++++++++++ lib/extensions/active_record/reflection.rb | 20 ++++++++++++++++++++ lib/extensions/integer.rb | 39 +++++++++++++++++++++++++++++++++++++++ lib/extensions/string.rb | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/noosfero/core_ext.rb | 5 ----- lib/noosfero/core_ext/active_record/calculations.rb | 10 ---------- lib/noosfero/core_ext/active_record/reflection.rb | 20 -------------------- lib/noosfero/core_ext/integer.rb | 39 --------------------------------------- lib/noosfero/core_ext/string.rb | 93 --------------------------------------------------------------------------------------------- 10 files changed, 167 insertions(+), 167 deletions(-) create mode 100644 lib/extensions.rb create mode 100644 lib/extensions/active_record/calculations.rb create mode 100644 lib/extensions/active_record/reflection.rb create mode 100644 lib/extensions/integer.rb create mode 100644 lib/extensions/string.rb delete mode 100644 lib/noosfero/core_ext.rb delete mode 100644 lib/noosfero/core_ext/active_record/calculations.rb delete mode 100644 lib/noosfero/core_ext/active_record/reflection.rb delete mode 100644 lib/noosfero/core_ext/integer.rb delete mode 100644 lib/noosfero/core_ext/string.rb diff --git a/lib/extensions.rb b/lib/extensions.rb new file mode 100644 index 0000000..fe5737a --- /dev/null +++ b/lib/extensions.rb @@ -0,0 +1,5 @@ +require_relative 'extensions/string' +require_relative 'extensions/integer' +require_relative 'extensions/active_record/calculations' +require_relative 'extensions/active_record/reflection' + diff --git a/lib/extensions/active_record/calculations.rb b/lib/extensions/active_record/calculations.rb new file mode 100644 index 0000000..cdaf434 --- /dev/null +++ b/lib/extensions/active_record/calculations.rb @@ -0,0 +1,10 @@ +ActiveRecord::Calculations.class_eval do + def count_with_distinct column_name=self.primary_key + if column_name + distinct.count_without_distinct column_name + else + count_without_distinct + end + end + alias_method_chain :count, :distinct +end diff --git a/lib/extensions/active_record/reflection.rb b/lib/extensions/active_record/reflection.rb new file mode 100644 index 0000000..dd9a113 --- /dev/null +++ b/lib/extensions/active_record/reflection.rb @@ -0,0 +1,20 @@ + +# on STI classes tike Article and Profile, plugins' extensions +# on associations should be reflected on descendants +module ActiveRecord + module Reflection + + class << self + + def add_reflection_with_descendants(ar, name, reflection) + self.add_reflection_without_descendants ar, name, reflection + ar.descendants.each do |k| + k._reflections.merge!(name.to_s => reflection) + end if ar.base_class == ar + end + + alias_method_chain :add_reflection, :descendants + + end + end +end diff --git a/lib/extensions/integer.rb b/lib/extensions/integer.rb new file mode 100644 index 0000000..d5e1d70 --- /dev/null +++ b/lib/extensions/integer.rb @@ -0,0 +1,39 @@ +class Integer + def to_humanreadable + value = self + if value < 1023 + return "%i bytes" % value + end + value /= 1024 + + if value < 1023 + return "%1.1f KB" % value + end + value /= 1024 + + if value < 1023 + return "%1.1f MB" % value + end + value /= 1024 + + if value < 1023 + return "%1.1f GB" % value + end + value /= 1024 + + if value < 1023 + return "%1.1f TB" % value + end + value /= 1024 + + if value < 1023 + return "%1.1f PB" % value + end + value /= 1024 + + if value < 1023 + return "%1.1f EB" % value + end + value /= 1024 + end +end diff --git a/lib/extensions/string.rb b/lib/extensions/string.rb new file mode 100644 index 0000000..3f3a870 --- /dev/null +++ b/lib/extensions/string.rb @@ -0,0 +1,93 @@ +# encoding: utf-8 + +class String + + TRANSLITERATIONS = { + [ 'Á', 'À', 'À', 'Â', 'Ã', 'Ä', 'Å' ] => 'A', + [ 'á', 'à', 'à', 'â', 'ã', 'ä', 'å' ,'ª' ] => 'a', + [ 'É', 'È', 'Ê', 'Ë' ] => 'E', + [ 'é', 'è', 'ê', 'ë' ] => 'e', + [ 'Í', 'Ì', 'Î', 'Ï' ] => 'I', + [ 'í', 'ì', 'î', 'ï' ] => 'i', + [ 'Ó', 'Ò', 'Ô', 'Ö', 'Õ', 'º' ] => 'O', + [ 'ó', 'ò', 'ô', 'ö', 'õ', 'º' ] => 'o', + [ 'Ú', 'Ù', 'Û', 'Ü' ] => 'U', + [ 'ú', 'ù', 'û', 'ü' ] => 'u', + [ 'ß' ] => 'ss', + [ 'Ç' ] => 'C', + [ 'ç' ] => 'c', + [ 'Ñ' ] => 'N', + [ 'ñ' ] => 'n', + [ 'Ÿ' ] => 'Y', + [ 'ÿ' ] => 'y', +# Cyrillic alphabet transliteration + [ 'а', 'А' ] => 'a', + [ 'б', 'Б' ] => 'b', + [ 'в', 'В' ] => 'v', + [ 'г', 'Г' ] => 'g', + [ 'д', 'Д' ] => 'd', + [ 'е', 'Е' ] => 'e', + [ 'ё', 'Ё' ] => 'yo', + [ 'ж', 'Ж' ] => 'zh', + [ 'з', 'З' ] => 'z', + [ 'и', 'И' ] => 'i', + [ 'й', 'Й' ] => 'y', + [ 'к', 'К' ] => 'k', + [ 'л', 'Л' ] => 'l', + [ 'м', 'М' ] => 'm', + [ 'н', 'Н' ] => 'n', + [ 'о', 'О' ] => 'o', + [ 'п', 'П' ] => 'p', + [ 'р', 'Р' ] => 'r', + [ 'с', 'С' ] => 's', + [ 'т', 'Т' ] => 't', + [ 'у', 'У' ] => 'u', + [ 'ф', 'Ф' ] => 'f', + [ 'х', 'Х' ] => 'h', + [ 'ц', 'Ц' ] => 'ts', + [ 'ч', 'Ч' ] => 'ch', + [ 'ш', 'Ш' ] => 'sh', + [ 'щ', 'Щ' ] => 'sch', + [ 'э', 'Э' ] => 'e', + [ 'ю', 'Ю' ] => 'yu', + [ 'я', 'Я' ] => 'ya', + [ 'ы', 'Ы' ] => 'i', + [ 'ь', 'Ь' ] => '', + [ 'ъ', 'Ъ' ] => '', +# Ukrainian lovely letters + [ 'і', 'І' ] => 'i', + [ 'ї', 'Ї' ] => 'yi', + [ 'є', 'Є' ] => 'ye', + [ 'ґ', 'Ґ' ] => 'g', + } + + # transliterate a string (assumed to contain UTF-8 data) + # into ASCII by replacing non-ascii characters to their + # ASCII. + # + # The transliteration is, of course, lossy, and its performance is poor. + # Don't abuse this method. + def transliterate + + new = self.dup + TRANSLITERATIONS.each { |from,to| + from.each { |seq| + new.gsub!(seq, to) + } + } + new + end + + def to_slug + transliterate.downcase.gsub(/[^[[:word:]]~\s:;+=_."'`-]/, '').gsub(/[\s:;+=_"'`-]+/, '-').gsub(/-$/, '').gsub(/^-/, '').to_s + end + + def to_css_class + underscore.dasherize.gsub('/','_') + end + + def fix_i18n + self.sub('{fn} ', '') + end + +end diff --git a/lib/noosfero/core_ext.rb b/lib/noosfero/core_ext.rb deleted file mode 100644 index f9470c0..0000000 --- a/lib/noosfero/core_ext.rb +++ /dev/null @@ -1,5 +0,0 @@ -require 'noosfero/core_ext/string' -require 'noosfero/core_ext/integer' -require 'noosfero/core_ext/active_record/calculations' -require 'noosfero/core_ext/active_record/reflection' - diff --git a/lib/noosfero/core_ext/active_record/calculations.rb b/lib/noosfero/core_ext/active_record/calculations.rb deleted file mode 100644 index cdaf434..0000000 --- a/lib/noosfero/core_ext/active_record/calculations.rb +++ /dev/null @@ -1,10 +0,0 @@ -ActiveRecord::Calculations.class_eval do - def count_with_distinct column_name=self.primary_key - if column_name - distinct.count_without_distinct column_name - else - count_without_distinct - end - end - alias_method_chain :count, :distinct -end diff --git a/lib/noosfero/core_ext/active_record/reflection.rb b/lib/noosfero/core_ext/active_record/reflection.rb deleted file mode 100644 index dd9a113..0000000 --- a/lib/noosfero/core_ext/active_record/reflection.rb +++ /dev/null @@ -1,20 +0,0 @@ - -# on STI classes tike Article and Profile, plugins' extensions -# on associations should be reflected on descendants -module ActiveRecord - module Reflection - - class << self - - def add_reflection_with_descendants(ar, name, reflection) - self.add_reflection_without_descendants ar, name, reflection - ar.descendants.each do |k| - k._reflections.merge!(name.to_s => reflection) - end if ar.base_class == ar - end - - alias_method_chain :add_reflection, :descendants - - end - end -end diff --git a/lib/noosfero/core_ext/integer.rb b/lib/noosfero/core_ext/integer.rb deleted file mode 100644 index d5e1d70..0000000 --- a/lib/noosfero/core_ext/integer.rb +++ /dev/null @@ -1,39 +0,0 @@ -class Integer - def to_humanreadable - value = self - if value < 1023 - return "%i bytes" % value - end - value /= 1024 - - if value < 1023 - return "%1.1f KB" % value - end - value /= 1024 - - if value < 1023 - return "%1.1f MB" % value - end - value /= 1024 - - if value < 1023 - return "%1.1f GB" % value - end - value /= 1024 - - if value < 1023 - return "%1.1f TB" % value - end - value /= 1024 - - if value < 1023 - return "%1.1f PB" % value - end - value /= 1024 - - if value < 1023 - return "%1.1f EB" % value - end - value /= 1024 - end -end diff --git a/lib/noosfero/core_ext/string.rb b/lib/noosfero/core_ext/string.rb deleted file mode 100644 index 3f3a870..0000000 --- a/lib/noosfero/core_ext/string.rb +++ /dev/null @@ -1,93 +0,0 @@ -# encoding: utf-8 - -class String - - TRANSLITERATIONS = { - [ 'Á', 'À', 'À', 'Â', 'Ã', 'Ä', 'Å' ] => 'A', - [ 'á', 'à', 'à', 'â', 'ã', 'ä', 'å' ,'ª' ] => 'a', - [ 'É', 'È', 'Ê', 'Ë' ] => 'E', - [ 'é', 'è', 'ê', 'ë' ] => 'e', - [ 'Í', 'Ì', 'Î', 'Ï' ] => 'I', - [ 'í', 'ì', 'î', 'ï' ] => 'i', - [ 'Ó', 'Ò', 'Ô', 'Ö', 'Õ', 'º' ] => 'O', - [ 'ó', 'ò', 'ô', 'ö', 'õ', 'º' ] => 'o', - [ 'Ú', 'Ù', 'Û', 'Ü' ] => 'U', - [ 'ú', 'ù', 'û', 'ü' ] => 'u', - [ 'ß' ] => 'ss', - [ 'Ç' ] => 'C', - [ 'ç' ] => 'c', - [ 'Ñ' ] => 'N', - [ 'ñ' ] => 'n', - [ 'Ÿ' ] => 'Y', - [ 'ÿ' ] => 'y', -# Cyrillic alphabet transliteration - [ 'а', 'А' ] => 'a', - [ 'б', 'Б' ] => 'b', - [ 'в', 'В' ] => 'v', - [ 'г', 'Г' ] => 'g', - [ 'д', 'Д' ] => 'd', - [ 'е', 'Е' ] => 'e', - [ 'ё', 'Ё' ] => 'yo', - [ 'ж', 'Ж' ] => 'zh', - [ 'з', 'З' ] => 'z', - [ 'и', 'И' ] => 'i', - [ 'й', 'Й' ] => 'y', - [ 'к', 'К' ] => 'k', - [ 'л', 'Л' ] => 'l', - [ 'м', 'М' ] => 'm', - [ 'н', 'Н' ] => 'n', - [ 'о', 'О' ] => 'o', - [ 'п', 'П' ] => 'p', - [ 'р', 'Р' ] => 'r', - [ 'с', 'С' ] => 's', - [ 'т', 'Т' ] => 't', - [ 'у', 'У' ] => 'u', - [ 'ф', 'Ф' ] => 'f', - [ 'х', 'Х' ] => 'h', - [ 'ц', 'Ц' ] => 'ts', - [ 'ч', 'Ч' ] => 'ch', - [ 'ш', 'Ш' ] => 'sh', - [ 'щ', 'Щ' ] => 'sch', - [ 'э', 'Э' ] => 'e', - [ 'ю', 'Ю' ] => 'yu', - [ 'я', 'Я' ] => 'ya', - [ 'ы', 'Ы' ] => 'i', - [ 'ь', 'Ь' ] => '', - [ 'ъ', 'Ъ' ] => '', -# Ukrainian lovely letters - [ 'і', 'І' ] => 'i', - [ 'ї', 'Ї' ] => 'yi', - [ 'є', 'Є' ] => 'ye', - [ 'ґ', 'Ґ' ] => 'g', - } - - # transliterate a string (assumed to contain UTF-8 data) - # into ASCII by replacing non-ascii characters to their - # ASCII. - # - # The transliteration is, of course, lossy, and its performance is poor. - # Don't abuse this method. - def transliterate - - new = self.dup - TRANSLITERATIONS.each { |from,to| - from.each { |seq| - new.gsub!(seq, to) - } - } - new - end - - def to_slug - transliterate.downcase.gsub(/[^[[:word:]]~\s:;+=_."'`-]/, '').gsub(/[\s:;+=_"'`-]+/, '-').gsub(/-$/, '').gsub(/^-/, '').to_s - end - - def to_css_class - underscore.dasherize.gsub('/','_') - end - - def fix_i18n - self.sub('{fn} ', '') - end - -end -- libgit2 0.21.2