meta_manager 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +93 -0
  3. data/Rakefile +34 -0
  4. data/app/models/meta_tag.rb +23 -0
  5. data/db/migrate/20111228124815_create_meta_tags.rb +21 -0
  6. data/lib/meta_manager.rb +7 -0
  7. data/lib/meta_manager/engine.rb +12 -0
  8. data/lib/meta_manager/helper.rb +62 -0
  9. data/lib/meta_manager/taggable.rb +52 -0
  10. data/lib/meta_manager/version.rb +3 -0
  11. data/lib/tasks/meta_manager_tasks.rake +4 -0
  12. data/test/controllers/posts_controller_test.rb +72 -0
  13. data/test/dummy/Rakefile +7 -0
  14. data/test/dummy/app/assets/javascripts/application.js +9 -0
  15. data/test/dummy/app/assets/stylesheets/application.css +7 -0
  16. data/test/dummy/app/controllers/application_controller.rb +5 -0
  17. data/test/dummy/app/controllers/posts_controller.rb +25 -0
  18. data/test/dummy/app/helpers/application_helper.rb +2 -0
  19. data/test/dummy/app/models/category.rb +7 -0
  20. data/test/dummy/app/models/post.rb +5 -0
  21. data/test/dummy/app/views/layouts/application.html.erb +15 -0
  22. data/test/dummy/app/views/posts/index.html.erb +0 -0
  23. data/test/dummy/app/views/posts/show.html.erb +0 -0
  24. data/test/dummy/config.ru +4 -0
  25. data/test/dummy/config/application.rb +45 -0
  26. data/test/dummy/config/boot.rb +10 -0
  27. data/test/dummy/config/database.yml +11 -0
  28. data/test/dummy/config/environment.rb +5 -0
  29. data/test/dummy/config/environments/development.rb +30 -0
  30. data/test/dummy/config/environments/production.rb +60 -0
  31. data/test/dummy/config/environments/test.rb +39 -0
  32. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  33. data/test/dummy/config/initializers/inflections.rb +10 -0
  34. data/test/dummy/config/initializers/mime_types.rb +5 -0
  35. data/test/dummy/config/initializers/secret_token.rb +7 -0
  36. data/test/dummy/config/initializers/session_store.rb +8 -0
  37. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  38. data/test/dummy/config/locales/en.yml +5 -0
  39. data/test/dummy/config/routes.rb +60 -0
  40. data/test/dummy/db/migrate/20111226131910_create_categories.rb +9 -0
  41. data/test/dummy/db/migrate/20111226131911_create_posts.rb +11 -0
  42. data/test/dummy/db/test.sqlite.db +0 -0
  43. data/test/dummy/log/development.log +1 -0
  44. data/test/dummy/log/test.log +8598 -0
  45. data/test/dummy/public/404.html +26 -0
  46. data/test/dummy/public/422.html +26 -0
  47. data/test/dummy/public/500.html +26 -0
  48. data/test/dummy/public/favicon.ico +0 -0
  49. data/test/dummy/script/rails +6 -0
  50. data/test/meta_manager_test.rb +11 -0
  51. data/test/models/category_test.rb +41 -0
  52. data/test/models/meta_tag_test.rb +67 -0
  53. data/test/test_helper.rb +14 -0
  54. metadata +162 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,93 @@
1
+ = MetaManager
2
+
3
+ Enable meta tags in your model
4
+
5
+ == Install
6
+
7
+ gem "meta_manager"
8
+
9
+ rake meta_manager_engine:install:migrations
10
+
11
+ == Usage
12
+
13
+ class Category < ActiveRecord::Base
14
+ include MetaManager::Taggable
15
+ end
16
+
17
+ @category = Category.new
18
+ @category.tag_title = 'category test title'
19
+ @category.tag_keywords = "Some keywords"
20
+ @category.tag_description = "Some description"
21
+
22
+ @category.meta_tags.build(:name => "og:title", :content => 'category og:title')
23
+
24
+ # create dynamic meta tags, who will be overwrited the same category tag names
25
+ # only with @meta_dynamic=true in controller
26
+ @category.meta_tags.build(:name => "og:title", :content => 'dynamic og:title - %{post.title}', :is_dynamic => true)
27
+ @category.meta_tags.build(:name => "title", :content => '%{post.title} - %{post.notes}', :is_dynamic => true)
28
+
29
+ @category.save
30
+
31
+ # create post for dynamic example
32
+ @post = Post.create(:title => 'post test title', :notes => 'post test notes')
33
+
34
+ == Rendering example
35
+
36
+ At layouts/application.html.erb
37
+
38
+ <head>
39
+ <%= raw(render_meta_tags(@category)) %>
40
+ <title><%= render_page_title(@category) %></title>
41
+
42
+ At controllers/posts_controller.rb
43
+
44
+ before_filter :find_category
45
+ before_filter :prepare_dynamic_page, :only => [:show]
46
+
47
+ def index
48
+ @posts = Post.order('id')
49
+ respond_with(@posts)
50
+ end
51
+
52
+ def show
53
+ @post = Post.find(params[:id])
54
+ respond_with(@post)
55
+ end
56
+
57
+ protected
58
+
59
+ def find_category
60
+ @category = Category.first
61
+ end
62
+
63
+ # set @meta_dynamic true to turn on dymanic meta tags.
64
+ def prepare_dynamic_page
65
+ @meta_dynamic = true
66
+ end
67
+
68
+ It will be generate meta tags and title for @category.
69
+ In action show we wont to generate dynamic meta tags from instance @post.
70
+ It means that meta tag 'og:title' and tag 'title' will be overwrited with attributes from instance @post.
71
+
72
+ === Results
73
+
74
+ Action index:
75
+
76
+ <meta content='Some keywords' name='keywords' />
77
+ <meta content='Some description' name='description' />
78
+ <meta content='category og:title' property='og:title' />
79
+ <title>category test title</title>
80
+
81
+ Action show:
82
+
83
+ <meta content='Some keywords' name='keywords' />
84
+ <meta content='Some description' name='description' />
85
+ <meta content='dynamic og:title - post test title' property='og:title' />
86
+ <title>post test title - post test notes</title>
87
+
88
+
89
+ == Test
90
+
91
+ rake test
92
+
93
+ Copyright (c) 2011 Aimbulance, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'MetaManager'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ Bundler::GemHelper.install_tasks
24
+
25
+ require 'rake/testtask'
26
+
27
+ Rake::TestTask.new(:test) do |t|
28
+ t.libs << 'lib'
29
+ t.libs << 'test'
30
+ t.pattern = 'test/**/*_test.rb'
31
+ t.verbose = false
32
+ end
33
+
34
+ task :default => :test
@@ -0,0 +1,23 @@
1
+ class MetaTag < ::ActiveRecord::Base
2
+ belongs_to :taggable, :polymorphic => true
3
+
4
+ validates_presence_of :name, :taggable_type
5
+ validates_uniqueness_of :name, :scope => [:taggable_type, :taggable_id, :is_dynamic]
6
+
7
+ attr_accessible :name, :content, :is_dynamic
8
+
9
+ def get_content(controller=nil)
10
+ self.is_dynamic ? dynamic_content(controller) : self.content
11
+ end
12
+
13
+ def dynamic_content(controller)
14
+ self.content.gsub /%{([\w\.]+)}/ do
15
+ items = $1.split('.')
16
+ instance_name = items.shift
17
+ method_name = items.join('.')
18
+ record = controller.instance_variable_get("@#{instance_name}")
19
+
20
+ record.try(:"#{method_name}")
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ class CreateMetaTags < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :meta_tags do |t|
4
+ t.string :name, :limit => 50, :null => false
5
+ t.text :content
6
+ t.boolean :is_dynamic, :default => false
7
+
8
+ t.integer :taggable_id, :null => false
9
+ t.string :taggable_type, :limit => 50, :null => false
10
+
11
+ t.timestamps
12
+ end
13
+
14
+ add_index :meta_tags, :name
15
+ add_index :meta_tags, [:taggable_type, :taggable_id]
16
+ end
17
+
18
+ def self.down
19
+ drop_table :meta_tags
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ require 'meta_manager/taggable'
2
+ require 'meta_manager/helper'
3
+
4
+ module MetaManager
5
+ end
6
+
7
+ require 'meta_manager/engine'
@@ -0,0 +1,12 @@
1
+ require 'rails'
2
+ require 'meta_manager'
3
+
4
+ module MetaManager #:nodoc:
5
+ class Engine < ::Rails::Engine #:nodoc:
6
+ initializer "meta_manager.setup" do
7
+ ActiveSupport.on_load :action_controller do
8
+ ActionController::Base.send :include, MetaManager::Helper
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,62 @@
1
+ module MetaManager
2
+ module Helper
3
+ #extend ::ActionView::Helpers::TagHelper
4
+
5
+ def self.included(base)
6
+ base.send :helper_method, :render_meta_tags, :render_page_title
7
+ end
8
+
9
+ def render_meta_tags(record)
10
+ return if record.nil?
11
+
12
+ dynamic = self.instance_variable_get("@meta_dynamic")
13
+ tags = []
14
+
15
+ get_actual_meta_tags(record, dynamic).each do |meta_tag|
16
+ unless meta_tag.name == 'title'
17
+ type = meta_tag.name =~ /og:/ ? 'property' : 'name'
18
+ tags << "<meta content='#{meta_tag.get_content(self)}' #{type}='#{meta_tag.name}' />"
19
+ end
20
+ end
21
+
22
+ tags.join("\n\s\s")
23
+ end
24
+
25
+ def render_page_title(record=nil, options = {})
26
+ dynamic = self.instance_variable_get("@meta_dynamic")
27
+
28
+ meta_tags = get_actual_meta_tags(record, dynamic)
29
+ meta_tags.detect{|t| t.name == 'title'}.try(:get_content, self) || get_page_title(record, options)
30
+ end
31
+
32
+ private
33
+
34
+ def get_page_title(record, options)
35
+ options = { :spliter => ' - ', :append_title => true }.merge(options)
36
+
37
+ view_title = if record.respond_to?(:title)
38
+ record.title
39
+ elsif record.respond_to(:name)
40
+ record.name
41
+ end
42
+
43
+ page_title = []
44
+ page_title << options[:title] if options.key?(:title)
45
+ page_title << view_title
46
+ page_title << I18n.t("page.title") if options[:append_title]
47
+
48
+ page_title.flatten.compact.uniq.join(options[:spliter])
49
+ end
50
+
51
+ def get_actual_meta_tags(record, dynamic)
52
+ meta_tags = []
53
+ _meta_tags = record.respond_to?(:meta_tags) ? record.meta_tags : []
54
+
55
+ _meta_tags.group_by(&:name).each do |name, items|
56
+ meta_tags << (items.detect{|r| r.is_dynamic && dynamic} || items.detect{|r| !r.is_dynamic})
57
+ end
58
+
59
+ meta_tags.compact
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,52 @@
1
+ module MetaManager
2
+ module Taggable
3
+ extend ::ActiveSupport::Concern
4
+
5
+ included do
6
+ has_many :meta_tags, :as => :taggable, :dependent => :destroy, :autosave => true
7
+
8
+ accepts_nested_attributes_for :meta_tags, :reject_if => :all_blank, :allow_destroy => true
9
+ end
10
+
11
+ module InstanceMethods
12
+
13
+ # Find meta tag by name or build new record
14
+ def find_or_build_meta_tag(attr_name)
15
+ key = normalize_meta_tag_name(attr_name)
16
+ self.meta_tags.where(:name => key).first || self.meta_tags.build(:name => key)
17
+ end
18
+
19
+ # Save meta tags records into one hash
20
+ def meta_tag(attr_name)
21
+ key = normalize_meta_tag_name(attr_name)
22
+ @meta_tag ||= {}
23
+ @meta_tag[key] ||= find_or_build_meta_tag(key)
24
+ end
25
+
26
+ protected
27
+
28
+ def normalize_meta_tag_name(value)
29
+ key = value.to_s.downcase.strip
30
+ key
31
+ end
32
+
33
+ def method_missing(method, *args, &block)
34
+ key = method.to_s
35
+
36
+ if key =~ /^tag_/
37
+ attr_name = key.gsub(/^tag_/, '')
38
+
39
+ if key =~ /=$/
40
+ record = meta_tag(attr_name.chop)
41
+ record.content = args.first
42
+ else
43
+ meta_tag(attr_name).try(:content)
44
+ end
45
+ else
46
+ super
47
+ end
48
+ end
49
+
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ module MetaManager
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :meta_manager do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,72 @@
1
+ require 'test_helper'
2
+
3
+ class PostsControllerTest < ActionController::TestCase
4
+
5
+ setup do
6
+ @category = Category.new :title => 'Test category'
7
+
8
+ @category.tag_title = 'test title'
9
+ @category.tag_keywords = 'test, keywords'
10
+ @category.tag_description = 'test description'
11
+
12
+ @category.meta_tags.build(:name => "og:title", :content => 'category og:title')
13
+ @category.meta_tags.build(:name => "title", :content => 'title %{post.title}, content %{post.content}, notes %{post.notes}', :is_dynamic => true)
14
+ @category.meta_tags.build(:name => "og:title", :content => 'dynamic og:title - %{post.title}', :is_dynamic => true)
15
+
16
+ @category.save
17
+
18
+ @post = Post.create(:title => 'test_title', :content => 'test_content', :notes => 'test_notes')
19
+ end
20
+
21
+ test "should get index" do
22
+ get :index
23
+ assert_response :success
24
+ assert_not_nil assigns(:posts)
25
+ end
26
+
27
+ test "should get show" do
28
+ get :show, :id => 1
29
+ assert_response :success
30
+ assert_not_nil assigns(:post)
31
+ end
32
+
33
+
34
+
35
+ test "should have meta tags at index action" do
36
+ get :index
37
+
38
+ assert_select 'title', {:count => 1, :text => "test title"}
39
+
40
+ assert_select "meta[name=keywords]", {:count => 1} do
41
+ assert_select "[content=?]", 'test, keywords'
42
+ end
43
+
44
+ assert_select "meta[name=description]", {:count => 1} do
45
+ assert_select "[content=?]", 'test description'
46
+ end
47
+
48
+ assert_select "meta[property=og:title]", {:count => 1} do
49
+ assert_select "[content=?]", 'category og:title'
50
+ end
51
+ end
52
+
53
+
54
+
55
+ test "should have dynamic meta tags at show action" do
56
+ get :show, :id => 1
57
+
58
+ assert_select 'title', {:count => 1, :text => "title test_title, content test_content, notes test_notes"}
59
+
60
+ assert_select "meta[name=keywords]", {:count => 1} do
61
+ assert_select "[content=?]", 'test, keywords'
62
+ end
63
+
64
+ assert_select "meta[name=description]", {:count => 1} do
65
+ assert_select "[content=?]", 'test description'
66
+ end
67
+
68
+ assert_select "meta[property=og:title]", {:count => 1} do
69
+ assert_select "[content=?]", 'dynamic og:title - test_title'
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require File.expand_path('../config/application', __FILE__)
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,9 @@
1
+ // This is a manifest file that'll be compiled into including all the files listed below.
2
+ // Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
3
+ // be included in the compiled file accessible from http://example.com/assets/application.js
4
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
5
+ // the compiled file.
6
+ //
7
+ //= require jquery
8
+ //= require jquery_ujs
9
+ //= require_tree .
@@ -0,0 +1,7 @@
1
+ /*
2
+ * This is a manifest file that'll automatically include all the stylesheets available in this directory
3
+ * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
4
+ * the top of the compiled file, but it's generally better to create a new file per style scope.
5
+ *= require_self
6
+ *= require_tree .
7
+ */