Module: Voluntary::ApplicationHelper

Includes:
AutoHtml
Defined in:
app/helpers/voluntary/application_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.root_model_class_name_helper(resource) ⇒ Object



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

def self.root_model_class_name_helper(resource)
  if resource.class.superclass.name == 'ActiveRecord::Base'
    resource.class.name
  elsif resource.class.superclass.name == 'Object'
    # classes like mongo db models without a specific superclass
    resource.class.name
  else
    resource.class.superclass.name
  end
end

Instance Method Details

#attribute_translation(attribute, current_resource = nil) ⇒ Object



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

def attribute_translation(attribute, current_resource = nil)
  current_resource = current_resource || resource
  t("activerecord.attributes.#{root_model_class_name(current_resource).underscore}.#{attribute}",
    default: t("attributes.#{attribute}")
  )
end

#directory_for_resource(resource) ⇒ Object



42
43
44
# File 'app/helpers/voluntary/application_helper.rb', line 42

def directory_for_resource(resource)
  resource.class.name.split('::').last.underscore
end


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/helpers/voluntary/application_helper.rb', line 83

def event_links_for_resource(current_resource, type)
  return [] unless current_resource.respond_to? :state_events
  
  links = []
  
  current_resource.state_events.select{|event| can? event, current_resource }.each do |event|
    path = "#{event}_#{type.singularize}_path"
    
    next unless respond_to? path
    
    path = eval("#{path}(current_resource)")
    links << link_to(t("#{type}.show.events.#{event}"), path, method: :put)
  end
  
  links
end

#general_attribute?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


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

def general_attribute?(attribute)
  begin
    I18n.t("attributes.#{attribute}", raise: true)
    true
  rescue
    false
  end
end


46
47
48
# File 'app/helpers/voluntary/application_helper.rb', line 46

def link_list(collection)
  raw collection.map{|a| link_to a.name, a}.join(', ')
end

#markdown(text) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/helpers/voluntary/application_helper.rb', line 16

def markdown(text)
  text = Redcarpet::Markdown.new(Redcarpet::Render::XHTML.new(filter_html: true)).render(text)
  
  auto_html(text) do 
    youtube(width: 515, height: 300)
    dailymotion(width: 515, height: 300)
    vimeo(width: 515, height: 300)
    google_video(width: 515, height: 300)
    image
  
    redcarpet(
      renderer: Redcarpet::Render::XHTML.new(
        no_images: true, no_styles: true, hard_wrap: true, with_toc_data: true
      ),
      markdown_options: { no_intra_emphasis: true, autolink: true, fenced_code_blocks: true }
    )
    link :target => "_blank", :rel => "nofollow"
  end.gsub(/(>https?:\/\/[^\<\\]+)/) do |match| 
    truncate(match)
  end.html_safe
end

#name_with_apostrophe(value) ⇒ Object



79
80
81
# File 'app/helpers/voluntary/application_helper.rb', line 79

def name_with_apostrophe(value)
  value =~ /s$/i ? "#{value}'" : "#{value}'s"
end

#polymorphic_or_resource_path(record) ⇒ Object



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

def polymorphic_or_resource_path(record)
  begin 
    polymorphic_path(record) 
  rescue NoMethodError
    resource_path(record)
  end
end

#resource_path(record) ⇒ Object



74
75
76
77
# File 'app/helpers/voluntary/application_helper.rb', line 74

def resource_path(record)
  klass = ::Voluntary::ApplicationHelper.root_model_class_name_helper(record)
  eval("#{klass.tableize.singularize}_path(record)")
end

#root_model_class_name(resource) ⇒ Object



38
39
40
# File 'app/helpers/voluntary/application_helper.rb', line 38

def root_model_class_name(resource)
  ::Voluntary::ApplicationHelper.root_model_class_name_helper(resource)
end