Module: TbCore::ApplicationHelper

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/helpers/tb_core/application_helper.rb', line 91

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

rubocop:enable Rails/HelperInstanceVariable



87
88
89
# File 'app/helpers/tb_core/application_helper.rb', line 87

def current_site_name
  return TbCore.config.site_name
end

#tb_form_error_field(record, attribute) ⇒ Object



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

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



51
52
53
54
55
56
# File 'app/helpers/tb_core/application_helper.rb', line 51

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/helpers/tb_core/application_helper.rb', line 31

def tb_form_errors(record, *fields_to_display)
  if record.errors.any?
     :div, class: 'form-errors test' do
      concat(
        tb_form_error_header(record) +
        (:ul) do
          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
          messages.each do |msg|
            concat (:li, msg)
          end
        end
      )
    end
  end
end

#tb_form_field(attribute) ⇒ Object



65
66
67
68
69
70
71
72
# File 'app/helpers/tb_core/application_helper.rb', line 65

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_form_with(record, **options, &block) ⇒ Object



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

def tb_form_with(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

  options[:model] = record

  return form_with(options, &block)
end

#tb_page_titleObject

rubocop:disable Rails/HelperInstanceVariable



75
76
77
78
79
80
81
82
83
84
# File 'app/helpers/tb_core/application_helper.rb', line 75

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