Module: TbCore::ApplicationHelper

Included in:
Spud::ApplicationController
Defined in:
app/helpers/tb_core/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#cache_key_for_spud_collection(collection, key: 'view', cache_params: [], for_user: false) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/helpers/tb_core/application_helper.rb', line 70

def cache_key_for_spud_collection(collection, key:'view', cache_params:[], for_user:false)
  cache_keys = [controller_name, action_name, key]
  cache_keys << collection.collect(&:updated_at).max().try(:utc).try(:to_i)
  if for_user
    cache_keys << current_user_id
  end
  if cache_params.any?
    cache_keys += cache_params.collect{ |cache_param| params[cache_param] || 'nil' }
  end
  cache_keys += collection.collect(&:id)
  cache_key = cache_keys.join('/')
  if cache_key.length > 250
    return Digest::SHA1.hexdigest(cache_key)
  else
    return cache_key
  end
end

#current_site_nameObject



66
67
68
# File 'app/helpers/tb_core/application_helper.rb', line 66

def current_site_name
  return Spud::Core.config.site_name
end

#tb_form_error_field(record, attribute) ⇒ Object



39
40
41
42
43
44
# File 'app/helpers/tb_core/application_helper.rb', line 39

def tb_form_error_field(record, attribute)
  message = record.errors[attribute].first
  if message
    return  :p, message, :class => 'help-block form-field-error'
  end
end

#tb_form_error_header(record) ⇒ Object



32
33
34
35
36
37
# File 'app/helpers/tb_core/application_helper.rb', line 32

def tb_form_error_header(record)
  if record.errors.any?
    message = "Please correct the following #{'error'.pluralize(record.errors.size)}:"
    return  :h4, message, :class => 'form-field-error'
  end
end

#tb_form_errors(record, *fields_to_display) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/tb_core/application_helper.rb', line 16

def tb_form_errors(record, *fields_to_display)
  if record.errors.any?
     :div, :class => 'form-errors' do
      concat(tb_form_error_header(record))
      concat(raw "<ul>")
      if fields_to_display.any?
        messages = fields_to_display.collect{ |field| record.errors.full_messages_for(field) }.flatten()
      else
        messages = record.errors.full_messages
      end
      concat(raw messages.collect{ |msg| "<li>#{msg}</li>" }.join())
      concat(raw "</ul>")
    end
  end
end

#tb_form_field(attribute) ⇒ Object



46
47
48
49
50
51
52
53
# File 'app/helpers/tb_core/application_helper.rb', line 46

def tb_form_field(attribute)
   :div, :class => 'form-group' do
    concat label_tag attribute, attribute, :class => 'col-sm-2 control-label'
    concat((:div, :class => 'col-sm-10') do
      text_field_tag attribute, :placeholder => attribute, :class => 'form-control'
    end)
  end
end

#tb_form_for(record, options = {}, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'app/helpers/tb_core/application_helper.rb', line 3

def tb_form_for(record, options = {}, &block)
  options[:builder] = TbCore::FormBuilder

  options[:html] ||= {}
  if options[:html][:class]
    options[:html][:class] += ' form-horizontal'
  else
    options[:html][:class] = 'form-horizontal'
  end

  return form_for(record, options, &block)
end

#tb_page_titleObject



55
56
57
58
59
60
61
62
63
64
# File 'app/helpers/tb_core/application_helper.rb', line 55

def tb_page_title
  if content_for?(:title)
    title = content_for(:title) + ' | ' + Spud::Core.site_name
  elsif @page_title
    title = @page_title + ' | ' + Spud::Core.site_name
  else
    title = Spud::Core.site_name
  end
  return  :title, title
end