h2ocube_rails_helper 0.0.16 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -2
  3. data/Gemfile +0 -3
  4. data/README.md +0 -1
  5. data/Rakefile +7 -7
  6. data/h2ocube_rails_helper.gemspec +9 -1
  7. data/h2ocube_rails_helper.sublime-project +15 -0
  8. data/lib/h2ocube_rails_helper.rb +93 -89
  9. data/spec/fake_app/models.rb +14 -0
  10. data/spec/fake_app/rails_app.rb +33 -0
  11. data/spec/helpers/render_canonical_spec.rb +16 -0
  12. data/spec/helpers/render_description_spec.rb +37 -0
  13. data/spec/helpers/render_ga_spec.rb +38 -0
  14. data/{test/render_html_class_test.rb → spec/helpers/render_html_class_spec.rb} +44 -45
  15. data/spec/helpers/render_keywords_spec.rb +39 -0
  16. data/spec/helpers/render_title_spec.rb +56 -0
  17. data/spec/spec_helper.rb +24 -0
  18. metadata +120 -57
  19. data/test/dummy/Rakefile +0 -7
  20. data/test/dummy/app/controllers/application_controller.rb +0 -3
  21. data/test/dummy/app/helpers/application_helper.rb +0 -2
  22. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  23. data/test/dummy/config.ru +0 -4
  24. data/test/dummy/config/application.rb +0 -20
  25. data/test/dummy/config/boot.rb +0 -10
  26. data/test/dummy/config/database.yml +0 -22
  27. data/test/dummy/config/environment.rb +0 -5
  28. data/test/dummy/config/environments/development.rb +0 -23
  29. data/test/dummy/config/environments/production.rb +0 -49
  30. data/test/dummy/config/environments/test.rb +0 -24
  31. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  32. data/test/dummy/config/initializers/inflections.rb +0 -10
  33. data/test/dummy/config/initializers/mime_types.rb +0 -5
  34. data/test/dummy/config/initializers/secret_token.rb +0 -7
  35. data/test/dummy/config/initializers/session_store.rb +0 -8
  36. data/test/dummy/config/locales/en.yml +0 -5
  37. data/test/dummy/config/routes.rb +0 -58
  38. data/test/dummy/script/rails +0 -6
  39. data/test/render_canonical_test.rb +0 -16
  40. data/test/render_description_test.rb +0 -37
  41. data/test/render_ga_test.rb +0 -37
  42. data/test/render_keywords_test.rb +0 -41
  43. data/test/render_title_test.rb +0 -56
  44. data/test/test_helper.rb +0 -7
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d6346f74f2f95a7d17d06881352fe5eb1b187a8f
4
- data.tar.gz: ac1947c454d034146e3767acd706f4de4a8320dd
3
+ metadata.gz: 98d190df931ced4c044766e99eeebcf8b919fad0
4
+ data.tar.gz: 5256b8f7852b970ab90cf17c7925eb53978f77d6
5
5
  SHA512:
6
- metadata.gz: c79e6db11803c7e4e3c1783afeacd6df37730567a5f06d0f5d40b281caf947f0df021036cfb5d8a18842c4f76a97049988909d18830600565cc60675f1327c68
7
- data.tar.gz: f59b0f35fcdf60dac4c6146ed63b48ecea9f1cd431510153b0955fe045ea9fbd64ed7b0d1793ae745181ade9fa6977496ad8b6e4be4c896f1f426fece55a9f56
6
+ metadata.gz: a00ca62e798dcc89f301516cd545bb9395350dfac5505a6601fd00c0cf682d091474f5b3e2470d0090ce43064ef96115acaf01b909f4c041776a75752f9f004e
7
+ data.tar.gz: 0bf887356575bf47460e9456146fffc2ecc2cf2b54229764710fd05c54ff4a750f46b094614f23211d39ce8dba6f172b2d7fe26dd4868ba0af2b08e4e236127d
data/.travis.yml CHANGED
@@ -1,6 +1,5 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
3
  - 2.0.0
5
4
  - 2.1.0
6
- script: "bundle exec rake test"
5
+ script: "bundle exec rspec"
data/Gemfile CHANGED
@@ -3,6 +3,3 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in h2ocube_rails_helper.gemspec
4
4
  gemspec
5
5
  gem 'rake'
6
-
7
- gem 'rails', '>= 3.1.0'
8
- gem 'garelic'
data/README.md CHANGED
@@ -9,7 +9,6 @@ Just an helper collection.
9
9
 
10
10
  Add this line to your application's Gemfile:
11
11
 
12
- gem 'garelic' # if you use render_ga
13
12
  gem 'h2ocube_rails_helper'
14
13
 
15
14
  And then execute:
data/Rakefile CHANGED
@@ -1,11 +1,11 @@
1
- require 'bundler/gem_tasks'
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
2
3
 
3
- require 'rake/testtask'
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
4
6
 
5
- Rake::TestTask.new(:test) do |t|
6
- t.libs << 'test'
7
- t.test_files = FileList['test/*_test.rb']
8
- t.verbose = true
7
+ RSpec::Core::RakeTask.new(:spec) do |spec|
8
+ spec.pattern = FileList['spec/**/*_spec.rb']
9
9
  end
10
10
 
11
- task default: :test
11
+ task default: 'spec'
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |gem|
6
6
  gem.name = 'h2ocube_rails_helper'
7
- gem.version = '0.0.16'
7
+ gem.version = '0.1.0'
8
8
  gem.authors = ['Ben']
9
9
  gem.email = ['ben@h2ocube.com']
10
10
  gem.description = %q{Just an helper collection}
@@ -19,4 +19,12 @@ Gem::Specification.new do |gem|
19
19
 
20
20
  gem.add_dependency 'settingslogic'
21
21
  gem.add_dependency 'browser'
22
+
23
+ gem.add_development_dependency 'tzinfo'
24
+ gem.add_development_dependency 'rspec'
25
+ gem.add_development_dependency 'rspec-rails'
26
+ gem.add_development_dependency 'rr'
27
+ gem.add_development_dependency 'capybara'
28
+ gem.add_development_dependency 'rails', '< 4.0.0'
29
+ gem.add_development_dependency 'sqlite3'
22
30
  end
@@ -0,0 +1,15 @@
1
+ {
2
+ "folders":
3
+ [
4
+ {
5
+ "path": "./",
6
+ "folder_exclude_patterns": ["tmp", "log", "dump"],
7
+ "file_exclude_patterns": ["*.sassc", "*.log", "*.scssc"]
8
+ }
9
+ ],
10
+ "tab_size": 2,
11
+ "translate_tabs_to_spaces": true,
12
+ "ensure_newline_at_eof_on_save": true,
13
+ "trim_trailing_white_space_on_save": true,
14
+ "scroll_past_end": true
15
+ }
@@ -1,110 +1,114 @@
1
- # coding: utf-8
2
-
3
1
  require 'settingslogic'
4
2
  require 'browser'
5
3
 
6
4
  module H2ocubeRailsHelper
7
- module Rails
8
- class Rails::Engine < ::Rails::Engine
9
- end
10
- end
11
- end
5
+ module ActionViewExtension
6
+ def render_html_class
7
+ cls = []
8
+ if params[:controller].include?('/')
9
+ cls.push params[:controller].gsub('/', '_')
10
+ params[:controller].split('/').each { |c| cls.push c }
11
+ else
12
+ cls.push params[:controller]
13
+ end
14
+
15
+ cls.push params[:action]
16
+ cls.push cls[0] + '_' + params[:action]
17
+
18
+ if params.has_key?(:html_class)
19
+ if params[:html_class].class != Array
20
+ cls.push params[:html_class].to_s
21
+ else
22
+ params[:html_class].each { |c| cls.push c }
23
+ end
24
+ end
25
+
26
+ cls.push Browser.new(accept_language: request.headers['Accept-Language'], ua: request.headers['User-Agent']).to_s
27
+
28
+ cls.compact.uniq.join ' '
29
+ end
12
30
 
13
- def render_html_class
14
- cls = []
15
- if params[:controller].include?('/')
16
- cls.push params[:controller].gsub('/', '_')
17
- params[:controller].split('/').each { |c| cls.push c }
18
- else
19
- cls.push params[:controller]
20
- end
31
+ def _title opts = {}
32
+ return [@_title] if defined?(@_title)
33
+ if defined?(@title) && !@title.blank?
34
+ title = @title.is_a?(Array) ? @title : [ @title.to_s ]
35
+ else
36
+ begin
37
+ title = [resource.title, resource.class.model_name.human] if defined?(resource) && resource.respond_to?(:title) && resource.class.respond_to?(:model_name)
38
+ rescue
39
+ end
40
+
41
+ title ||= []
42
+ end
43
+
44
+ if opts.has_key? :title
45
+ title.push opts[:title]
46
+ else
47
+ title.push HelperSettings.title
48
+ end
49
+ title.compact.map{ |t| t = t.strip; t == '' ? nil : t }.compact
50
+ end
21
51
 
22
- cls.push params[:action]
23
- cls.push cls[0] + '_' + params[:action]
52
+ def render_title opts = {}
53
+ "<title>#{_title(opts).join(' - ')}</title>".html_safe
54
+ end
24
55
 
25
- if params.has_key?(:html_class)
26
- if params[:html_class].class != Array
27
- cls.push params[:html_class].to_s
28
- else
29
- params[:html_class].each { |c| cls.push c }
56
+ def _keywords opts = {}
57
+ if defined? @keywords
58
+ keywords = @keywords
59
+ elsif defined?(@item) && @item.respond_to?(:keywords) && !@item.keywords.blank?
60
+ keywords = @item.keywords.strip.split(/(,|,)/)
61
+ else
62
+ keywords = opts.has_key?(:keywords) ? opts[:keywords] : HelperSettings.keywords
63
+ end
64
+ [keywords].flatten.compact.map{ |k| k.to_s.strip.split(/(,|,)/) }.flatten.map{ |k| k.gsub(/(,|,)/, '').blank? ? nil : k }.compact.uniq
30
65
  end
31
- end
32
66
 
33
- cls.push Browser.new(accept_language: request.headers['Accept-Language'], ua: request.headers['User-Agent']).to_s
67
+ def render_keywords opts = {}
68
+ return '' if _keywords.length == 0
69
+ "<meta name=\"keywords\" content=\"#{_keywords(opts).join(',')}\" />".html_safe
70
+ end
34
71
 
35
- cls.compact.uniq.join ' '
36
- end
72
+ def _description opts = {}
73
+ if defined? @description
74
+ description = @description
75
+ elsif defined?(@item) && @item.respond_to?(:description) && !@item.description.blank?
76
+ description = @item.description
77
+ else
78
+ description = opts.has_key?(:description) ? opts[:description] : HelperSettings.description
79
+ end
80
+ description.to_s.strip
81
+ end
37
82
 
38
- def _title opts = {}
39
- return [@_title] if defined?(@_title)
40
- if defined?(@title)
41
- title = @title.is_a?(Array) ? @title : [ @title.to_s ]
42
- else
43
- if defined?(resource)
44
- title = [resource.title, resource.class.model_name.human] if resource.respond_to?(:title) && resource.class.respond_to?(:model_name)
83
+ def render_description opts = {}
84
+ return '' if _description == ''
85
+ "<meta name=\"description\" content=\"#{_description(opts)}\" />".html_safe
45
86
  end
46
- title ||= []
47
- end
48
87
 
49
- if opts.has_key? :title
50
- title.push opts[:title]
51
- else
52
- title.push HelperSettings.title
53
- end
54
- title.compact.map{ |t| t = t.strip; t == '' ? nil : t }.compact
55
- end
88
+ def render_canonical opts = {}
89
+ defined?(@canonical) && !@canonical.blank? ? "<link rel=\"canonical\" href=\"#{@canonical}\" />".html_safe : ''
90
+ end
56
91
 
57
- def render_title opts = {}
58
- "<title>#{_title(opts).join(' - ')}</title>".html_safe
59
- end
92
+ def render_seo opts = {}
93
+ render_title(opts) << render_canonical(opts) << render_keywords(opts) << render_description(opts) << render_ga(opts) << csrf_meta_tags
94
+ end
60
95
 
61
- def _keywords opts = {}
62
- if defined? @keywords
63
- keywords = @keywords
64
- elsif defined?(@item) && @item.respond_to?(:keywords) && !@item.keywords.blank?
65
- keywords = @item.keywords.strip.split(/(,|,)/)
66
- else
67
- keywords = opts.has_key?(:keywords) ? opts[:keywords] : HelperSettings.keywords
96
+ def render_ga opts = {}
97
+ return '' if Rails.env.development?
98
+ ga = opts.has_key?(:ga) ? opts[:ga] : HelperSettings.ga
99
+ domain = opts.has_key?(:domain) ? opts[:domain] : HelperSettings.domain
100
+ return '' if ga.nil?
101
+ return "<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
102
+ end
68
103
  end
69
- [keywords].flatten.compact.map{ |k| k.to_s.strip.split(/(,|,)/) }.flatten.map{ |k| k.gsub(/(,|,)/, '').blank? ? nil : k }.compact.uniq
70
- end
71
104
 
72
- def render_keywords opts = {}
73
- return '' if _keywords.length == 0
74
- "<meta name=\"keywords\" content=\"#{_keywords(opts).join(',')}\" />".html_safe
75
- end
76
-
77
- def _description opts = {}
78
- if defined? @description
79
- description = @description
80
- elsif defined?(@item) && @item.respond_to?(:description) && !@item.description.blank?
81
- description = @item.description
82
- else
83
- description = opts.has_key?(:description) ? opts[:description] : HelperSettings.description
105
+ class Railtie < ::Rails::Railtie
106
+ initializer 'h2ocube_rails_helper' do
107
+ ActiveSupport.on_load(:action_view) do
108
+ ::ActionView::Base.send :include, H2ocubeRailsHelper::ActionViewExtension
109
+ end
110
+ end
84
111
  end
85
- description.to_s.strip
86
112
  end
87
113
 
88
- def render_description opts = {}
89
- return '' if _description == ''
90
- "<meta name=\"description\" content=\"#{_description(opts)}\" />".html_safe
91
- end
92
-
93
- def render_canonical opts = {}
94
- defined?(@canonical) && !@canonical.blank? ? "<link rel=\"canonical\" href=\"#{@canonical}\" />".html_safe : ''
95
- end
96
114
 
97
- def render_seo opts = {}
98
- render_title(opts) << render_canonical(opts) << render_keywords(opts) << render_description(opts) << render_ga(opts) << csrf_meta_tags
99
- end
100
-
101
- def render_ga opts = {}
102
- return '' if Rails.env.development?
103
- ga = opts.has_key?(:ga) ? opts[:ga] : HelperSettings.ga
104
- return '' if ga.nil?
105
- if defined?(Garelic)
106
- return ("<script>_gaq=[['_trackPageview'],['_trackPageLoadTime']];</script>" << Garelic.monitoring(ga)).html_safe
107
- else
108
- return "<script>var _gaq=[['_setAccount','#{ga}'],['_trackPageview'],['_trackPageLoadTime']];(function(){var ga=document.createElement('script');ga.type='text/javascript';ga.async=true;ga.src=('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';var s=document.getElementsByTagName('script')[0];s.parentNode.insertBefore(ga,s);})();</script>".html_safe
109
- end
110
- end
@@ -0,0 +1,14 @@
1
+ # models
2
+ class User < ActiveRecord::Base
3
+
4
+ end
5
+
6
+
7
+ #migrations
8
+ class CreateAllTables < ActiveRecord::Migration
9
+ def self.up
10
+ create_table(:users) {|t| t.string :name }
11
+ end
12
+ end
13
+ ActiveRecord::Migration.verbose = false
14
+ CreateAllTables.up
@@ -0,0 +1,33 @@
1
+ # require 'rails/all'
2
+ require 'action_controller/railtie'
3
+ require 'action_view/railtie'
4
+ require 'active_record'
5
+
6
+ ActiveRecord::Base.configurations = {'test' => {:adapter => 'sqlite3', :database => ':memory:'}}
7
+ ActiveRecord::Base.establish_connection('test')
8
+
9
+ # config
10
+ app = Class.new(Rails::Application)
11
+ app.config.secret_token = '3b7cd727ee24e8444053437c36cc66c4'
12
+ app.config.session_store :cookie_store, :key => '_myapp_session'
13
+ app.config.active_support.deprecation = :log
14
+ app.config.eager_load = false
15
+ # Rais.root
16
+ app.config.root = File.dirname(__FILE__)
17
+ Rails.backtrace_cleaner.remove_silencers!
18
+ app.initialize!
19
+
20
+ # routes
21
+ app.routes.draw do
22
+ resources :users
23
+ end
24
+
25
+ #models
26
+ require 'fake_app/models'
27
+
28
+ # controllers
29
+ class ApplicationController < ActionController::Base
30
+ end
31
+
32
+ # helpers
33
+ Object.const_set(:ApplicationHelper, Module.new)
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'render_canonical' do
4
+ it 'simple' do
5
+ render_canonical.should == ''
6
+ end
7
+
8
+ it '@canonical' do
9
+ @canonical = '@canonical'
10
+ render_canonical.should == '<link rel="canonical" href="@canonical" />'
11
+ [' ', '', nil].each do |desc|
12
+ @canonical = desc
13
+ render_canonical.should == ''
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ class HelperSettings
4
+ def self.description
5
+ 'description'
6
+ end
7
+ end
8
+
9
+ class Item
10
+ def description
11
+ 'item_description'
12
+ end
13
+ end
14
+
15
+ describe 'render_description' do
16
+ it 'simple' do
17
+ render_description.should == '<meta name="description" content="description" />'
18
+ end
19
+
20
+ it '@description' do
21
+ @description = '@description'
22
+ render_description.should == '<meta name="description" content="@description" />'
23
+ [' ', '', nil].each do |desc|
24
+ @description = desc
25
+ render_description.should == ''
26
+ end
27
+ end
28
+
29
+ it '@item' do
30
+ @item = Item.new
31
+ render_description.should == '<meta name="description" content="item_description" />'
32
+ end
33
+
34
+ it 'opts' do
35
+ render_description(description: 'opts').should == '<meta name="description" content="opts" />'
36
+ end
37
+ end
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ class HelperSettings
4
+ def self.title
5
+ 'title'
6
+ end
7
+
8
+ def self.keywords
9
+ 'keywords'
10
+ end
11
+
12
+ def self.description
13
+ 'description'
14
+ end
15
+
16
+ def self.ga
17
+ 'ga_code'
18
+ end
19
+
20
+ def self.domain
21
+ 'domain'
22
+ end
23
+ end
24
+
25
+ describe 'render_ga' do
26
+ it 'default' do
27
+ render_ga.include?('ga_code').should be_true
28
+ end
29
+
30
+ it 'with option' do
31
+ render_ga(ga: '123').include?('123').should be_true
32
+ end
33
+
34
+ it 'with nil' do
35
+ render_ga(ga: nil).should == ''
36
+ end
37
+ end
38
+