Module: H2ocubeRailsHelper::ActionView::Helpers
- Defined in:
- lib/h2ocube_rails_helper.rb
Instance Method Summary collapse
- #_description(opts = {}) ⇒ Object
- #_keywords(opts = {}) ⇒ Object
- #_render_keywords(opts = {}) ⇒ Object
- #_render_title(opts = {}) ⇒ Object
- #_title(opts = {}) ⇒ Object
- #browser ⇒ Object
- #escape(text) ⇒ Object
- #render_canonical(opts = {}) ⇒ Object
- #render_description(opts = {}) ⇒ Object
- #render_ga(opts = {}) ⇒ Object
- #render_html_class ⇒ Object
- #render_keywords(opts = {}) ⇒ Object
- #render_seo(opts = {}) ⇒ Object
- #render_title(opts = {}) ⇒ Object
- #unescape(text) ⇒ Object
Instance Method Details
#_description(opts = {}) ⇒ Object
149 150 151 152 153 154 155 156 157 158 |
# File 'lib/h2ocube_rails_helper.rb', line 149 def _description(opts = {}) if defined? @description description = @description elsif defined?(@item) && @item.respond_to?(:description) && !@item.description.blank? description = @item.description else description = opts.key?(:description) ? opts[:description] : rails_secrets[:description] end description.to_s.strip end |
#_keywords(opts = {}) ⇒ Object
129 130 131 132 133 134 135 136 137 138 |
# File 'lib/h2ocube_rails_helper.rb', line 129 def _keywords(opts = {}) if defined? @keywords keywords = @keywords elsif defined?(@item) && @item.respond_to?(:keywords) && !@item.keywords.blank? keywords = @item.keywords.strip.split(/(,|,)/) else keywords = opts.key?(:keywords) ? opts[:keywords] : rails_secrets[:keywords] end [keywords].flatten.compact.map{ |k| k.to_s.strip.split(/(,|,)/) }.flatten.map { |k| k.gsub(/(,|,)/, '').blank? ? nil : k }.compact.uniq end |
#_render_keywords(opts = {}) ⇒ Object
140 141 142 |
# File 'lib/h2ocube_rails_helper.rb', line 140 def _render_keywords(opts = {}) _keywords(opts).join(',') end |
#_render_title(opts = {}) ⇒ Object
121 122 123 |
# File 'lib/h2ocube_rails_helper.rb', line 121 def _render_title(opts = {}) _title(opts).join(' - ') end |
#_title(opts = {}) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/h2ocube_rails_helper.rb', line 100 def _title(opts = {}) return [@_title] if defined?(@_title) if defined?(@title) && !@title.blank? title = @title.is_a?(Array) ? @title : [ @title.to_s ] else begin title = [resource.title, resource.class.model_name.human] if defined?(resource) && resource.respond_to?(:title) && resource.class.respond_to?(:model_name) rescue end title ||= [] end if opts.key? :title title.push opts[:title] else title.push rails_secrets[:title] end title.compact.map { |t| t = t.strip; t == '' ? nil : t }.compact end |
#browser ⇒ Object
71 72 73 |
# File 'lib/h2ocube_rails_helper.rb', line 71 def browser @_browser ||= Browser.new request.env['HTTP_USER_AGENT'], accept_language: request.env['HTTP_ACCEPT_LANGUAGE'] end |
#escape(text) ⇒ Object
63 64 65 |
# File 'lib/h2ocube_rails_helper.rb', line 63 def escape(text) CGI.escapeHTML text end |
#render_canonical(opts = {}) ⇒ Object
165 166 167 |
# File 'lib/h2ocube_rails_helper.rb', line 165 def render_canonical(opts = {}) !@canonical.blank? ? "<link rel=\"canonical\" href=\"#{@canonical}\" />".html_safe : '' end |
#render_description(opts = {}) ⇒ Object
160 161 162 163 |
# File 'lib/h2ocube_rails_helper.rb', line 160 def render_description(opts = {}) return '' if _description == '' "<meta name=\"description\" content=\"#{_description(opts)}\" />".html_safe end |
#render_ga(opts = {}) ⇒ Object
173 174 175 176 177 178 179 |
# File 'lib/h2ocube_rails_helper.rb', line 173 def render_ga(opts = {}) return '' if Rails.env.development? ga = opts.key?(:ga) ? opts[:ga] : rails_secrets[:ga] domain = opts.key?(:domain) ? opts[:domain] : rails_secrets[:domain] return '' if ga.nil? "<script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','//www.google-analytics.com/analytics.js','ga');ga('create', '#{ga}', '#{domain}');ga('send', 'pageview');</script>".html_safe end |
#render_html_class ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/h2ocube_rails_helper.rb', line 75 def render_html_class cls = [] if params[:controller].include?('/') cls.push params[:controller].gsub('/', '_') params[:controller].split('/').each { |c| cls.push c } else cls.push params[:controller] end cls.push params[:action] cls.push cls[0] + '_' + params[:action] if params.key?(:html_class) if params[:html_class].class != Array cls.push params[:html_class].to_s else params[:html_class].each { |c| cls.push c } end end cls.push browser.to_s cls.compact.uniq.join ' ' end |
#render_keywords(opts = {}) ⇒ Object
144 145 146 147 |
# File 'lib/h2ocube_rails_helper.rb', line 144 def render_keywords(opts = {}) return '' if _keywords.blank? "<meta name=\"keywords\" content=\"#{_render_keywords opts}\" />".html_safe end |
#render_seo(opts = {}) ⇒ Object
169 170 171 |
# File 'lib/h2ocube_rails_helper.rb', line 169 def render_seo(opts = {}) render_title(opts) << render_canonical(opts) << render_keywords(opts) << render_description(opts) << render_ga(opts) << end |
#render_title(opts = {}) ⇒ Object
125 126 127 |
# File 'lib/h2ocube_rails_helper.rb', line 125 def render_title(opts = {}) "<title>#{_render_title opts}</title>".html_safe end |
#unescape(text) ⇒ Object
67 68 69 |
# File 'lib/h2ocube_rails_helper.rb', line 67 def unescape(text) CGI.unescapeHTML text end |