Module: ContextHelp::Helpers

Defined in:
lib/context_help/base.rb

Class Method Summary collapse

Class Method Details

.clean(options) ⇒ Object



253
254
255
256
257
# File 'lib/context_help/base.rb', line 253

def self.clean(options)
  options.delete(:context_help)
  options.delete('context_help')
  options
end

.get_doc_string(path) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/context_help/base.rb', line 212

def self.get_doc_string(path)
  plugin_prefixes = ['']
  plugin_prefixes += Rails.plugins.select{|p| p.is_a?(Engines::Plugin) }.map{ |p| "#{p.name}." }
  
  text = ''
  found = false
  
  plugin_prefixes.each do |prefix|
    begin
      item = I18n.translate prefix + path, :raise => true
      if item.start_with?('t.')
        item = I18n.t item[2, item.length]
      elsif item.start_with?('I18n.')
        item = I18n.t item[5, item.length]
      end

      text = text + ' ' + item
      found = true
    rescue
    end
  end
  found ? text : nil
end

.get_text(options, missing = true) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/context_help/base.rb', line 200

def self.get_text(options,missing=true)
  text = options[:text] || get_doc_string(options[:calculated_path]+'.text')
  if text.nil?
    if missing and Rails.env.development? and ContextHelp::Base.config[:show_missing]
      I18n.t(options[:calculated_path]+'.text')
    else
      nil
    end
  else
    text
  end
end

.get_title(options) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/context_help/base.rb', line 179

def self.get_title(options)
  title = options[:title] || get_doc_string(options[:calculated_path]+'.title')
  if title.nil?
    title = nil
    if options[:path][:model]
      begin
        model_class = model_name(options[:path][:model]).classify.constantize
        if options[:path][:attribute] then
          title = model_class.human_attribute_name(options[:path][:attribute].to_s) rescue I18n.t(options[:calculated_path]+'.title')
        else
          title = model_class.human_name
        end
      rescue
        title = I18n.t(options[:calculated_path]+'.title')
      end
    else
      title = I18n.t(options[:calculated_path]+'.title') if Rails.env.development? and ContextHelp::Base.config[:show_missing]
    end
  end
  title
end

.is_visible(options) ⇒ Object



174
175
176
177
178
# File 'lib/context_help/base.rb', line 174

def self.is_visible(options)
  return false if options[:calculated_path].nil?
  return false if self.get_text(options,false).nil?
  true
end

.merge_options(base, added) ⇒ Object



243
244
245
246
247
248
249
250
251
252
# File 'lib/context_help/base.rb', line 243

def self.merge_options(base, added)
  if base.is_a?(Hash) and added.is_a?(Hash)
    added.each do |key,value|
      base[key.to_sym] = self.merge_options(base[key.to_sym], value)
    end
    return base
  else
    return added || base
  end
end

.model_name(model) ⇒ Object



235
236
237
238
239
240
241
242
# File 'lib/context_help/base.rb', line 235

def self.model_name(model)
  model = model.to_s.underscore
  if model =~ /^[a-z][a-z0-9_]*\[[a-z][a-z0-9_]*_attributes\]/
    model.match(/^[a-z][a-z0-9_]*\[([a-z][a-z0-9_]*)_attributes\]/)[1].singularize
  else
    model
  end
end