metaa 0.0.3 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,26 +1,54 @@
1
1
  [![Build Status](https://travis-ci.org/anhkind/metaa.png)](https://travis-ci.org/anhkind/metaa)
2
2
 
3
- # Metaa
3
+ # Metaa: meta tags with ease
4
4
 
5
5
  Metaa adds meta tags to your Rails application with ease.
6
6
 
7
- ## Installation
7
+ With Metaa, you can easily add meta tags to your current model with simple DSL, then render these tags on your views with a single method.
8
8
 
9
- Add this line to your application's Gemfile:
9
+ ## Generator
10
10
 
11
- gem 'metaa'
11
+ The command is simple, just add the model you want to create the meta for:
12
+
13
+ ```
14
+ rails generate meta Product
15
+ ```
16
+
17
+ This will generate `ProductMeta` meta class in your app/meta folder.
18
+
19
+ ## Meta definition
20
+
21
+ After generated, the meta class will look like this:
12
22
 
13
- And then execute:
23
+ ```
24
+ # app/meta/product_meta.rb
25
+ class ProductMeta < Metaa::Meta
26
+ def define_meta
27
+ # Define meta tags of your record here. Each definition will require a hash for the meta attributes,
28
+ # and each attribute can be a string, symbol or lambda/proc, for example:
29
+ # meta name: "title",
30
+ # content: object.title # you can access the record through 'object' variable
14
31
 
15
- $ bundle
32
+ end
33
+ end
34
+ ```
16
35
 
17
- Or install it yourself as:
36
+ You can define meta tags multiple times and these tags will be displayed in the order you define in meta class.
18
37
 
19
- $ gem install metaa
38
+ To access to the final rendered html, just use method `meta_tags` on your instance
39
+
40
+ ```
41
+ product.meta_tags
42
+ ```
43
+
44
+ ## Installation
45
+
46
+ Add Metaa to your Gemfile:
47
+
48
+ gem 'metaa'
20
49
 
21
- ## Usage
50
+ And run bundle install within your app's directory.
22
51
 
23
- TODO: Write usage instructions here
24
52
 
25
53
  ## Contributing
26
54
 
@@ -0,0 +1,37 @@
1
+ require "rails/generators"
2
+
3
+ module Rails
4
+ module Generators
5
+ class MetaGenerator < NamedBase
6
+ source_root File.expand_path("../templates", __FILE__)
7
+ check_class_collision suffix: "Meta"
8
+
9
+ class_option :parent, type: :string, desc: "The parent class for the generated meta"
10
+
11
+ def create_meta_file
12
+ template 'meta.rb', File.join('app/meta', class_path, "#{file_name}_meta.rb")
13
+ end
14
+
15
+ hook_for :test_framework
16
+
17
+ private
18
+ def parent_class_name
19
+ options.fetch("parent") do
20
+ begin
21
+ require 'application_meta'
22
+ ApplicationMeta
23
+ rescue LoadError
24
+ "Metaa::Meta"
25
+ end
26
+ end
27
+ end
28
+
29
+ # Rails 3.0.X compatibility, stolen from https://github.com/jnunemaker/mongomapper/pull/385/files#L1R32
30
+ unless methods.include?(:module_namespacing)
31
+ def module_namespacing
32
+ yield if block_given?
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,15 @@
1
+ <%- module_namespacing do -%>
2
+ <%- if parent_class_name.present? -%>
3
+ class <%= class_name %>Meta < <%= parent_class_name %>
4
+ <%- else -%>
5
+ class <%= class_name %>
6
+ <%- end -%>
7
+ def define_meta
8
+ # Define meta tags of your record here. Each definition will require a hash for the meta attributes,
9
+ # and each attribute can be a string, symbol or lambda/proc, for example:
10
+ # meta name: "title",
11
+ # content: object.title # you can access to the model through 'object' method
12
+
13
+ end
14
+ end
15
+ <% end -%>
@@ -0,0 +1,9 @@
1
+ module Rspec
2
+ class MetaGenerator < ::Rails::Generators::NamedBase
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ def create_spec_file
6
+ template 'meta_spec.rb', File.join('spec/meta', class_path, "#{singular_name}_meta_spec.rb")
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe <%= class_name %>Meta do
4
+ end
@@ -3,11 +3,21 @@ require 'action_view'
3
3
  require "metaa/version"
4
4
  require 'metaa/tag'
5
5
  require 'metaa/tag_collection'
6
- require 'metaa/view_helper'
7
6
  require 'metaa/definition'
8
7
  require 'metaa/meta'
8
+ require 'metaa/concern'
9
+ require 'metaa/railtie' if defined?(Rails)
9
10
 
10
11
  module Metaa
11
- end
12
+ def self.setup_orm(base)
13
+ base.class_eval do
14
+ include Metaa::Concern
15
+ end
16
+ end
12
17
 
13
- ActionView::Base.send :include, Metaa::ViewHelpers
18
+ class UninferrableMetaError < NameError
19
+ def initialize(klass)
20
+ super("Could not infer a meta for #{klass}.")
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,33 @@
1
+ module Metaa
2
+ module Concern
3
+ extend ActiveSupport::Concern
4
+
5
+ def meta
6
+ @meta ||= meta_class.new(self)
7
+ end
8
+
9
+ def meta_tags
10
+ meta.html
11
+ end
12
+
13
+ # (see ClassMethods#meta_class)
14
+ def meta_class
15
+ self.class.meta_class
16
+ end
17
+
18
+ module ClassMethods
19
+ # Infers the meta class to be used by {Concern#meta} (e.g.
20
+ # `Product` maps to `ProductMeta`).
21
+ #
22
+ # @return [Class] the inferred meta class.
23
+ def meta_class
24
+ prefix = respond_to?(:model_name) ? model_name : name
25
+ meta_name = "#{prefix}Meta"
26
+ meta_name.constantize
27
+ rescue NameError => error
28
+ raise unless error.missing_name?(meta_name)
29
+ raise Metaa::UninferrableMetaError.new(self)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,29 +1,38 @@
1
1
  module Metaa
2
2
  class Meta
3
- class << self
4
- def meta(attributes = {})
5
- definitions << Definition.new(attributes)
6
- end
3
+ attr_reader :object
7
4
 
8
- def definitions
9
- @definitions ||= []
10
- end
5
+ def initialize(object = nil)
6
+ @object = object
11
7
  end
12
8
 
13
- attr_reader :object
9
+ def meta(attributes = {})
10
+ definitions << Definition.new(attributes)
11
+ end
14
12
 
15
- def initialize(object)
16
- @object = object
13
+ def definitions
14
+ @definitions ||= []
17
15
  end
18
16
 
17
+ # lazy define meta tags then collect
19
18
  def tags
20
- @tag_collection ||= TagCollection.new(
21
- self.class.definitions.map do |definition|
19
+ @tags ||= begin
20
+ define_meta
21
+ collect_tags
22
+ end
23
+ end
24
+
25
+ def html
26
+ tags.map(&:html).join.html_safe
27
+ end
28
+
29
+ private
30
+ def collect_tags
31
+ TagCollection.new(
32
+ definitions.map do |definition|
22
33
  definition.attributes_for(object)
23
34
  end
24
- )
25
-
26
- @tag_collection.html
35
+ ).tags
27
36
  end
28
37
  end
29
38
  end
@@ -0,0 +1,28 @@
1
+ require 'rails/railtie'
2
+
3
+ module ActiveModel
4
+ class Railtie < Rails::Railtie
5
+ generators do |app|
6
+ app ||= Rails.application # Rails 3.0.x does not yield `app`
7
+
8
+ Rails::Generators.configure! app.config.generators
9
+ end
10
+ end
11
+ end
12
+
13
+ module Metaa
14
+ class Railtie < Rails::Railtie
15
+
16
+ config.after_initialize do |app|
17
+ app.config.paths.add 'app/meta', eager_load: true
18
+ end
19
+
20
+ initializer "Metaa.setup_orm" do |app|
21
+ [:active_record].each do |orm|
22
+ ActiveSupport.on_load orm do
23
+ Metaa.setup_orm self
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -1,3 +1,3 @@
1
1
  module Metaa
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.11"
3
3
  end
@@ -8,14 +8,14 @@ Gem::Specification.new do |gem|
8
8
  gem.version = Metaa::VERSION
9
9
  gem.authors = ["anhkind"]
10
10
  gem.email = ["anhkind@gmail.com"]
11
- gem.description = "Metaa adds meta tags to your Rails application with ease"
12
- gem.summary = "Meta tags with ease"
11
+ gem.description = "Metaa adds meta tags to your Rails application with ease."
12
+ gem.summary = "Meta tags with ease."
13
13
  gem.homepage = "https://github.com/anhkind/metaa"
14
14
 
15
15
  gem.add_dependency 'activesupport', '>= 3.0'
16
16
  gem.add_dependency 'actionpack', '>= 3.0'
17
- gem.add_dependency 'activemodel', '>= 3.0'
18
17
 
18
+ gem.add_development_dependency 'ammeter'
19
19
  gem.add_development_dependency 'rake', '>= 0.9.2'
20
20
  gem.add_development_dependency 'rspec', '~> 2.12'
21
21
  gem.add_development_dependency 'rspec-mocks', '>= 2.12.1'
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+ require 'rails'
3
+ require 'ammeter/init'
4
+ require 'generators/rails/meta_generator'
5
+
6
+ describe Rails::Generators::MetaGenerator do
7
+ destination File.expand_path("../tmp", __FILE__)
8
+
9
+ before { prepare_destination }
10
+ after(:all) { FileUtils.rm_rf destination_root }
11
+
12
+ describe "the generated meta" do
13
+ subject { file("app/meta/your_model_meta.rb") }
14
+
15
+ describe "naming" do
16
+ before { run_generator %w(YourModel) }
17
+
18
+ it { should contain "class YourModelMeta" }
19
+ end
20
+
21
+ describe "define_meta method" do
22
+ before { run_generator %w(YourModel) }
23
+
24
+ it { should contain "def define_meta" }
25
+ end
26
+
27
+ describe "namespacing" do
28
+ subject { file("app/meta/namespace/your_model_meta.rb") }
29
+ before { run_generator %w(Namespace::YourModel) }
30
+
31
+ it { should contain "class Namespace::YourModelMeta" }
32
+ end
33
+
34
+ describe "inheritance" do
35
+ context "by default" do
36
+ before { run_generator %w(YourModel) }
37
+
38
+ it { should contain "class YourModelMeta < Metaa::Meta" }
39
+ end
40
+
41
+ context "with the --parent option" do
42
+ before { run_generator %w(YourModel --parent=FooMeta) }
43
+
44
+ it { should contain "class YourModelMeta < FooMeta" }
45
+ end
46
+
47
+ context "with an ApplicationMeta" do
48
+ before do
49
+ Object.any_instance.stub(:require).with("application_meta").and_return do
50
+ stub_const "ApplicationMeta", Class.new
51
+ end
52
+ end
53
+
54
+ before { run_generator %w(YourModel) }
55
+
56
+ it { should contain "class YourModelMeta < ApplicationMeta" }
57
+ end
58
+ end
59
+ end
60
+
61
+ context "with -t=rspec" do
62
+ describe "the generated spec" do
63
+ subject { file("spec/meta/your_model_meta_spec.rb") }
64
+
65
+ describe "naming" do
66
+ before { run_generator %w(YourModel -t=rspec) }
67
+
68
+ it { should contain "describe YourModelMeta" }
69
+ end
70
+
71
+ describe "namespacing" do
72
+ subject { file("spec/meta/namespace/your_model_meta_spec.rb") }
73
+ before { run_generator %w(Namespace::YourModel -t=rspec) }
74
+
75
+ it { should contain "describe Namespace::YourModelMeta" }
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ module Metaa
4
+ describe Concern do
5
+ describe "meta" do
6
+ it "returns meta object for self" do
7
+ model = Model.new
8
+ meta = model.meta
9
+
10
+ expect(meta).to be_a ModelMeta
11
+ expect(meta.object).to be model
12
+ end
13
+
14
+ it "uses the #meta_class" do
15
+ model = Model.new
16
+ model.stub meta_class: OtherMeta
17
+
18
+ expect(model.meta).to be_an_instance_of OtherMeta
19
+ end
20
+ end
21
+
22
+ describe "#meta_tags" do
23
+ it "returns html meta tags for self" do
24
+ meta_mock = double :meta, html: "<meta content=\"a title\" name=\"title\" property=\"text\" />"
25
+ model = Model.new
26
+ model.stub meta: meta_mock
27
+
28
+ html = model.meta_tags
29
+
30
+ expect(html).to eq "<meta content=\"a title\" name=\"title\" property=\"text\" />"
31
+ end
32
+ end
33
+
34
+ describe "#meta_class" do
35
+ it "delegates to .meta_class" do
36
+ model = Model.new
37
+
38
+ Model.should_receive(:meta_class).and_return(:some_meta)
39
+ expect(model.meta_class).to be :some_meta
40
+ end
41
+ end
42
+ end
43
+ end
@@ -2,44 +2,60 @@ require 'spec_helper'
2
2
 
3
3
  module Metaa
4
4
  describe Meta do
5
- describe 'class methods' do
6
- describe '#meta' do
7
- it 'defines a meta tag' do
8
- klass = Class.new(Meta)
9
- block = ->(object){ object.title }
10
-
11
- klass.class_eval do
12
- meta name: 'title',
13
- property: :text,
14
- content: block
5
+ describe '#meta' do
6
+ it 'defines a meta tag' do
7
+ klass = Class.new(Meta) do
8
+ def define_meta
9
+ meta name: 'title'
15
10
  end
11
+ end
16
12
 
17
- expect(klass.definitions.length).to eq 1
13
+ meta_obj = klass.new
14
+ meta_obj.define_meta # trigger due to lazy load
18
15
 
19
- definition = klass.definitions.first
20
- expect(definition.attributes).to eq({
21
- name: 'title',
22
- property: :text,
23
- content: block
24
- })
25
- end
16
+ expect(meta_obj.definitions.length).to eq 1
17
+
18
+ definition = meta_obj.definitions.first
19
+ expect(definition.attributes).to eq({
20
+ name: 'title'
21
+ })
26
22
  end
27
23
  end
28
24
 
29
25
  describe '#tags' do
30
26
  it 'returns meta tags for an object' do
31
- klass = Class.new(Meta)
32
-
33
- klass.class_eval do
34
- meta name: 'title',
35
- property: :text,
36
- content: ->(object){ object.title }
27
+ klass = Class.new(Meta) do
28
+ def define_meta
29
+ meta name: 'title',
30
+ property: :text,
31
+ content: object.title
32
+ end
37
33
  end
38
34
 
39
35
  object_mock = double title: 'a title'
40
36
  meta = klass.new(object_mock)
41
37
 
42
- expect(meta.tags).to eq "<meta content=\"a title\" name=\"title\" property=\"text\" />"
38
+ expect(meta.tags.length).to eq 1
39
+
40
+ tag = meta.tags.first
41
+ expect(tag.html).to eq "<meta content=\"a title\" name=\"title\" property=\"text\" />"
42
+ end
43
+ end
44
+
45
+ describe '#html' do
46
+ it 'returns the meta html tags for an object' do
47
+ object_mock = double title: 'a title'
48
+ klass = Class.new(Meta) do
49
+ def define_meta
50
+ meta name: 'title',
51
+ property: :text,
52
+ content: object.title
53
+ end
54
+ end
55
+
56
+ meta = klass.new(object_mock)
57
+
58
+ expect(meta.html).to eq "<meta content=\"a title\" name=\"title\" property=\"text\" />"
43
59
  end
44
60
  end
45
61
  end
@@ -1,2 +1,13 @@
1
1
  require 'bundler/setup'
2
- require 'metaa'
2
+ require 'metaa'
3
+
4
+ RSpec.configure do |config|
5
+ config.treat_symbols_as_metadata_keys_with_true_values = true
6
+ config.expect_with(:rspec) {|c| c.syntax = :expect}
7
+ config.order = :random
8
+ end
9
+
10
+ class Model; include Metaa::Concern; end
11
+ class ModelMeta < Metaa::Meta; end
12
+
13
+ class OtherMeta < Metaa::Meta; end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metaa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.11
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -44,21 +44,21 @@ dependencies:
44
44
  - !ruby/object:Gem::Version
45
45
  version: '3.0'
46
46
  - !ruby/object:Gem::Dependency
47
- name: activemodel
47
+ name: ammeter
48
48
  requirement: !ruby/object:Gem::Requirement
49
49
  none: false
50
50
  requirements:
51
51
  - - ! '>='
52
52
  - !ruby/object:Gem::Version
53
- version: '3.0'
54
- type: :runtime
53
+ version: '0'
54
+ type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  none: false
58
58
  requirements:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
- version: '3.0'
61
+ version: '0'
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: rake
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -123,7 +123,7 @@ dependencies:
123
123
  - - ~>
124
124
  - !ruby/object:Gem::Version
125
125
  version: '2.12'
126
- description: Metaa adds meta tags to your Rails application with ease
126
+ description: Metaa adds meta tags to your Rails application with ease.
127
127
  email:
128
128
  - anhkind@gmail.com
129
129
  executables: []
@@ -136,19 +136,25 @@ files:
136
136
  - LICENSE.txt
137
137
  - README.md
138
138
  - Rakefile
139
+ - lib/generators/rails/meta_generator.rb
140
+ - lib/generators/rails/templates/meta.rb
141
+ - lib/generators/rspec/meta_generator.rb
142
+ - lib/generators/rspec/templates/meta_spec.rb
139
143
  - lib/metaa.rb
144
+ - lib/metaa/concern.rb
140
145
  - lib/metaa/definition.rb
141
146
  - lib/metaa/meta.rb
147
+ - lib/metaa/railtie.rb
142
148
  - lib/metaa/tag.rb
143
149
  - lib/metaa/tag_collection.rb
144
150
  - lib/metaa/version.rb
145
- - lib/metaa/view_helper.rb
146
151
  - metaa.gemspec
152
+ - spec/generators/meta/meta_generator_spec.rb
153
+ - spec/metaa/concern_spec.rb
147
154
  - spec/metaa/definition_spec.rb
148
155
  - spec/metaa/meta_spec.rb
149
156
  - spec/metaa/tag_collection_spec.rb
150
157
  - spec/metaa/tag_spec.rb
151
- - spec/metaa/view_helper_spec.rb
152
158
  - spec/spec_helper.rb
153
159
  homepage: https://github.com/anhkind/metaa
154
160
  licenses: []
@@ -173,11 +179,12 @@ rubyforge_project:
173
179
  rubygems_version: 1.8.24
174
180
  signing_key:
175
181
  specification_version: 3
176
- summary: Meta tags with ease
182
+ summary: Meta tags with ease.
177
183
  test_files:
184
+ - spec/generators/meta/meta_generator_spec.rb
185
+ - spec/metaa/concern_spec.rb
178
186
  - spec/metaa/definition_spec.rb
179
187
  - spec/metaa/meta_spec.rb
180
188
  - spec/metaa/tag_collection_spec.rb
181
189
  - spec/metaa/tag_spec.rb
182
- - spec/metaa/view_helper_spec.rb
183
190
  - spec/spec_helper.rb
@@ -1,16 +0,0 @@
1
- module Metaa
2
- module ViewHelpers
3
- def add_meta_tags(meta = [])
4
- meta = [meta] if !meta.is_a? Array
5
- self.meta_tags.concat meta
6
- end
7
-
8
- def display_meta_tags
9
- TagCollection.new(meta_tags).html
10
- end
11
-
12
- def meta_tags
13
- @meta_tags ||= []
14
- end
15
- end
16
- end
@@ -1,40 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module Metaa
4
- describe ViewHelpers do
5
- let(:subject) { Class.new{include ViewHelpers}.new }
6
-
7
- describe "#add_meta_tags" do
8
- it "adds the meta tags from an input array" do
9
- meta = [
10
- { name: 'keywords', content: 'abc'}
11
- ]
12
-
13
- subject.add_meta_tags meta
14
- expect(subject.send :meta_tags).to eq([
15
- { name: 'keywords', content: 'abc'}
16
- ])
17
- end
18
-
19
- it "adds the meta tags from an input hash" do
20
- meta = { name: 'keywords', content: 'abc'}
21
-
22
- subject.add_meta_tags meta
23
- expect(subject.send :meta_tags).to eq([
24
- { name: 'keywords', content: 'abc'}
25
- ])
26
- end
27
- end
28
-
29
- describe "#display_meta_tags" do
30
- it "displays the added meta tags" do
31
- subject.stub meta_tags: [
32
- { name: 'keywords', content: 'abc'}
33
- ]
34
-
35
- expect(subject.display_meta_tags).to eq "<meta content=\"abc\" name=\"keywords\" />"
36
- end
37
- end
38
- end
39
- end
40
-