Module: Tuttle::ApplicationHelper

Defined in:
app/helpers/tuttle/application_helper.rb

Constant Summary collapse

BUNDLER_GEM_PATHS_REGEX =
%r{(#{Bundler.rubygems.gem_dir}|#{File.realpath(Bundler.rubygems.gem_dir)})+(/bundler)*/gems}

Instance Method Summary collapse

Instance Method Details

#display_path(path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/helpers/tuttle/application_helper.rb', line 39

def display_path(path)
  return if path.blank?
  display_location =
    if path.start_with?(Rails.root.to_s)
      path.sub(Rails.root.to_s, "$RAILS_ROOT")
    elsif File.realpath(path).start_with?(File.realpath(Bundler.rubygems.gem_dir))
      File.realpath(path).sub(BUNDLER_GEM_PATHS_REGEX, "$GEMS")
    else
      path
    end
  expanded_path = File.expand_path(path)
  (:span, display_location, :class => 'tuttle-path', :data => { :initial => path, :expanded => expanded_path })
end

#display_source_locaction(path, line) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'app/helpers/tuttle/application_helper.rb', line 53

def display_source_locaction(path, line)
  return (:span, "Unknown", :class => 'tuttle-path') if path.nil?

  display_location = file_location(path)
  expanded_path = File.expand_path(path)
  (:span, :class => 'tuttle-path', :data => { :initial => path, :expanded => expanded_path }) do
    "#{display_location}##{line}"
  end
end

#file_location(path) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/tuttle/application_helper.rb', line 63

def file_location(path)
  if path.start_with?(Rails.root.to_s)
    path.gsub(Rails.root.to_s, "$RAILS_ROOT")
  elsif path.start_with?(RbConfig::CONFIG['rubylibdir'])
    path.gsub(RbConfig::CONFIG['rubylibdir'], "$RUBY_LIB_DIR")
  elsif path == "<internal:prelude>"
    path
  elsif File.realpath(path).start_with?(File.realpath(Bundler.rubygems.gem_dir))
    File.realpath(path).gsub(BUNDLER_GEM_PATHS_REGEX, "$GEMS")
  else
    path
  end
end


31
32
33
# File 'app/helpers/tuttle/application_helper.rb', line 31

def link_to_rails_guide(file, title)
  (:a, title, :href => rails_guides_versioned_url(file + '.html'))
end

#main_app_root_pathObject



23
24
25
# File 'app/helpers/tuttle/application_helper.rb', line 23

def main_app_root_path
  main_app.respond_to?(:root_path) ? main_app.root_path : '/'
end

#main_app_root_urlObject



27
28
29
# File 'app/helpers/tuttle/application_helper.rb', line 27

def main_app_root_url
  main_app.respond_to?(:root_url) ? main_app.root_url : '/'
end

#rails_guides_versioned_url(path) ⇒ Object



35
36
37
# File 'app/helpers/tuttle/application_helper.rb', line 35

def rails_guides_versioned_url(path)
  "http://guides.rubyonrails.org/v#{Tuttle::VersionDetector.rails_major_minor}/#{path}"
end

#true_label(is_true, true_label = 'true') ⇒ Object

Display label if condition is true



13
14
15
# File 'app/helpers/tuttle/application_helper.rb', line 13

def true_label(is_true, true_label = 'true')
  (:span, true_label, class: %w[label label-success]) if is_true
end

#truth_label(is_true, true_label = 'true', false_label = 'false') ⇒ Object

Display true/false label with text overrides



7
8
9
10
# File 'app/helpers/tuttle/application_helper.rb', line 7

def truth_label(is_true, true_label = 'true', false_label = 'false')
  (:span, is_true ? true_label : false_label,
              class: ['label', is_true ? 'label-success' : 'label-danger'])
end

#tuttle_redacted(enumarator) ⇒ Object



17
18
19
20
21
# File 'app/helpers/tuttle/application_helper.rb', line 17

def tuttle_redacted(enumarator)
  enumarator.collect do |key, value|
    yield key, redact_by_key(key, value)
  end
end

#value_inspect(val, hide_nil: false) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/helpers/tuttle/application_helper.rb', line 77

def value_inspect(val, hide_nil: false)
  case val
  when NilClass
    (:code, val.inspect) unless hide_nil
  when String
    (:code, val.inspect)
  when Symbol
    (:code, val.inspect)
  else
    val.inspect
  end
end