Module: ContextHelp::Base

Defined in:
lib/context_help/base.rb

Class Method Summary collapse

Class Method Details

.configObject



31
32
33
# File 'lib/context_help/base.rb', line 31

def self.config
  @config
end

.context_help(&block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/context_help/base.rb', line 43

def self.context_help(&block)
  help = ''
  @help_items.each do |item|
    item[:calculated_path] = self.help_path_for(item, false) if !item[:calculated_path]
    if block_given?
      help += yield(item)
    elsif !item[:skip] and item[:calculated_path]
      help += self.html_help(item)
    end
  end
  help.strip
end

.flush_itemsObject



34
35
36
# File 'lib/context_help/base.rb', line 34

def self.flush_items
  @help_items = []
end

.get_option(name, options) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/context_help/base.rb', line 135

def self.get_option(name, options)
  return @config[name] if options[:calculated_path].nil?
  value = I18n.t(options[:calculated_path]+'.'+name.to_s, :default => {})
  if value.is_a?(Hash)
    @config[name]
  else
    value
  end
end

.help_for(options) ⇒ Object



38
39
40
41
42
# File 'lib/context_help/base.rb', line 38

def self.help_for(options)
  help_options = options[:context_help]
  self.help_path_for(help_options)
  self.inline_help(help_options)
end

.help_itemsObject



28
29
30
# File 'lib/context_help/base.rb', line 28

def self.help_items
  @help_items
end

.help_path_for(options, register = true) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/context_help/base.rb', line 65

def self.help_path_for(options,register=true)
  path = options[:path]
  return path if path.is_a?(String)

  ruta = nil
  if !options[:skip]
    if path[:model] and @config[:exclude_models].index(path[:model].to_sym).nil?
      ruta = 'context_help.models.'+Helpers.model_name(path[:model])
      options[:pre_level_class] = @config[:level_classes][:model]
      
      if (path[:attribute])
        ruta = ruta + '.attributes.'+ path[:attribute].to_s.underscore
        options[:pre_level_class] = @config[:level_classes][:model_attribute]
      end  
    elsif path[:custom]
      ruta = 'context_help.custom.'+path[:custom]
      options[:pre_level_class] = @config[:level_classes][:custom]
    elsif path[:tag] and @config[:exclude_tags].index(path[:tag].to_sym).nil?
      ruta = 'context_help.html.'+path[:tag].to_s.downcase
      ruta = ruta + '.' + path[:tag_options][:id].to_s if path[:tag_options][:id]
      
      if @config[:level_classes][:html].include?(path[:tag])
        options[:pre_level_class] = @config[:level_classes][:html][path[:tag]]
      else
        options[:pre_level_class] = @config[:level_classes][:html][:default]
      end
    end
  end
  if ruta              
    options[:calculated_path] = ruta
    self.register_item(options) if register
    return options[:calculated_path]
  end
  nil
end

.html_help(options) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/context_help/base.rb', line 100

def self.html_help(options)
  if options[:help_builder].is_a?(Proc) 
    options[:help_builder].call(options)
  elsif options[:calculated_path]
    title = Helpers.get_title(options)
    text = Helpers.get_text(options)
    return '' if (title.nil? || text.nil?) and !(Rails.env.development? and @config[:show_missing])

    html = "<#{options[:title_tag]} id=\"#{options[:item_id]}\" class=\"#{options[:title_class]} #{options[:level_class]}\">#{title}</#{options[:title_tag]}>
    <#{options[:text_tag]} class=\"#{options[:text_class]} #{options[:level_class]}\">#{text}</#{options[:text_tag]}>"
    html += self.link_to_object(options)
    html
  end
end

.inline_help(options) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/context_help/base.rb', line 55

def self.inline_help(options)
  return '' if options[:skip] or !options[:calculated_path]
  if options[:inline_help_builder].is_a?(Proc) 
    options[:inline_help_builder].call(options)
  elsif options[:show_inline]
    self.html_help(options)
  else
    ''
  end
end


114
115
116
117
118
119
120
121
122
123
124
# File 'lib/context_help/base.rb', line 114

def self.link_to_help(options)  
  if options[:link_to_help_builder].is_a?(Proc)
    options[:link_to_help_builder].call(options)
  elsif options[:link_to_help] and options[:calculated_path]
    title = Helpers.get_title(options)
    return '' if title.nil?
    "<a href=\"##{options[:item_id]}\" id=\"#{options[:item_id]}_object\" class=\"context_help_link_to_help\">help</a>"
  else
    ''
  end
end


125
126
127
128
129
130
131
132
133
# File 'lib/context_help/base.rb', line 125

def self.link_to_object(options)  
  if options[:link_to_object_builder].is_a?(Proc)
    options[:link_to_object_builder].call(options)
  elsif options[:link_to_object]
    "<a href=\"#link_to_help_#{options[:item_id]}\">help</a>"
  else
    ''
  end
end

.register_item(options) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/context_help/base.rb', line 134

def self.register_item(options)
  def self.get_option(name, options)
    return @config[name] if options[:calculated_path].nil?
    value = I18n.t(options[:calculated_path]+'.'+name.to_s, :default => {})
    if value.is_a?(Hash)
      @config[name]
    else
      value
    end
  end
  found = false
  last = nil
  @help_items.each do |item|
    found = (item[:calculated_path] == options[:calculated_path])
    last = item
    break if found
  end
  if found
    options.update(last)
  else
    options[:show_inline] ||= get_option(:show_inline, options)
    options[:title_tag] ||= get_option(:title_tag, options)
    options[:title_class] ||= get_option(:title_class, options)
    options[:text_tag] ||= get_option(:text_tag, options)
    options[:text_class] ||= get_option(:text_class, options)
    options[:link_to_object] ||= get_option(:link_to_object, options)
    options[:link_to_help] ||= get_option(:link_to_help, options)
    options[:level_class] ||= I18n.t(options[:calculated_path]+'.level_class', :default => (options[:pre_level_class] || 'help_level_1'))
    options[:link_to_help_builder] ||= @config[:link_to_help_builder]
    options[:link_to_object_builder] ||= @config[:link_to_object_builder]
    options[:inline_help_builder] ||= @config[:inline_help_builder]
    options[:help_builder] ||= @config[:help_builder]
    
    options[:item_id] = "context_help_item_#{(@help_items.length+1).to_s}"
    @help_items << options 
  end
end