lib/ab_admin/utils.rb in ab_admin-0.3.6 vs lib/ab_admin/utils.rb in ab_admin-0.4.0

- old
+ new

@@ -3,12 +3,12 @@ autoload :CSVBuilder, 'ab_admin/utils/csv_builder' autoload :EvalHelpers, 'ab_admin/utils/eval_helpers' autoload :Logger, 'ab_admin/utils/logger' autoload :Mysql, 'ab_admin/utils/mysql' - def load_files!(path = 'lib/utils') - Dir[Rails.root.join("#{path}/**/*.rb")].each do |path| + def load_files!(base_path = 'lib/utils') + Dir[Rails.root.join("#{base_path}/**/*.rb")].each do |path| require_dependency path end end def bm(message = 'Benchmarking', options = {}) @@ -43,33 +43,34 @@ text = raw_text.to_s.gsub(/&quot;|&laquo;|&raquo;|&#x27;/, '\'').gsub(/&nbsp;/, ' ').gsub(/&mdash;/, '-').no_html.squish text.truncate(size, separator: ' ') end def l_path(locale=nil) - return '' unless locale + locale ||= I18n.locale locale == I18n.default_locale ? '' : "/#{locale}" end alias :locale_path :l_path def rss_text(raw_html) Rack::Utils.escape_html(raw_html.no_html.squish) end - #AbAdmin.normalize_html('<!--das asd as as as--><script>asd</script><div>dasd</div><p>asdasd</p>') - #=> "<p>dasd</p><p>asdasd</p>" + # html like: '<!-- html comment --><script>script content</script><div>div content</div><p>p content</p>' + # normalized to: "<p>div content</p><p>p content</p>" def normalize_html(raw_html) @@sanitizer ||= Sanitizer.new @@sanitizer.normalize_html(raw_html) end def url_helpers Rails.application.routes.url_helpers end def full_url(path) - host = Rails.application.config.action_mailer.default_url_options[:host] || 'www.example.com' + return path if path =~ %r{^(http|//)} + host = AbAdmin.base_url || Rails.application.config.action_mailer.default_url_options[:host] || 'www.example.com' "http://#{host}#{path}" end def js_date_data { @@ -99,16 +100,17 @@ def display_name_method_for(resource) @@display_name_methods_cache[resource.class.name] ||= AbAdmin.display_name_methods.find { |method| resource.respond_to? method } end def display_name(resource) + return unless resource resource.send(display_name_method_for(resource)) end def safe_display_name(resource) return unless display_name_method_for(resource) - resource.send(display_name_method_for(resource)) + display_name(resource) end def pretty_data(object) case object when String, Integer, BigDecimal, Float @@ -124,7 +126,14 @@ else AbAdmin.safe_display_name(object) || object end end + def test_env? + Rails.env.test? || Rails.env.cucumber? + end + + def friendly_token(n=10) + SecureRandom.base64(16).tr('+/=', 'xyz').first(n) + end end end \ No newline at end of file