nesta-plugin-related-by-category 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +47 -0
- data/Rakefile +1 -0
- data/lib/nesta-plugin-related-by-category.rb +3 -0
- data/lib/nesta-plugin-related-by-category/init.rb +57 -0
- data/lib/nesta-plugin-related-by-category/version.rb +7 -0
- data/nesta-plugin-related-by-category.gemspec +21 -0
- data/spec/fixtures/content/pages/bach.mdown +6 -0
- data/spec/fixtures/content/pages/blues.mdown +5 -0
- data/spec/fixtures/content/pages/classical.mdown +5 -0
- data/spec/fixtures/content/pages/cream.mdown +6 -0
- data/spec/fixtures/content/pages/folk.mdown +5 -0
- data/spec/fixtures/content/pages/funk.mdown +5 -0
- data/spec/fixtures/content/pages/heavy.mdown +5 -0
- data/spec/fixtures/content/pages/hendrix.mdown +6 -0
- data/spec/fixtures/content/pages/jazz.mdown +5 -0
- data/spec/fixtures/content/pages/ledzep.mdown +6 -0
- data/spec/fixtures/content/pages/pentangle.mdown +6 -0
- data/spec/fixtures/content/pages/rare-earth.mdown +6 -0
- data/spec/fixtures/content/pages/rock.mdown +5 -0
- data/spec/fixtures/content/pages/the-meters.mdown +6 -0
- data/spec/related_articles_spec.rb +63 -0
- data/spec/spec_helper.rb +35 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
NzJlZDBjZDY1Yzc4ZGI5MGEwMWFkOTRkZjc5ZDFhNjQ3ODkxOWM5MA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZjJjYmE0YTg1YmJjZmMzYTY1YWNlMDc5NDJjZmE4MTFlYmVmYzU1ZA==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZWQ5MWNmZTY4ZTM2MGU2ODBkNTdiMzE0ZWYyYTJmMDI2MmEzZTYxNjIyMmUy
|
10
|
+
NTAzNjQ4MjQ2M2U1NzE5MTBlNDVmZWVkNWI0YmRhNGJjODFjZWVhMmRiZmQx
|
11
|
+
Njc4ZWIyODY5NWFiNjhhNWE4MWUyNmY2NDAwZDU4MzI0NWEwMjE=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
M2ZjYWQ2NTYyNzI0NzAzYzIzNGUzODc5M2E1MGY2ZmZlYjk5ZmFjMmVlMjYw
|
14
|
+
NmUxZjMyMWEyYzc2NGNkMjk4NzQxOWUzZDk0YTEyZjg2N2M0YTY0NGZhZTFj
|
15
|
+
MDRjNTUyZjhmNWRjZDRkZDU3YjJjZjU3YThlYWQyZTk5MTFkMjQ=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 James Abbott
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# Nesta::Plugin::RelatedByCategory
|
2
|
+
|
3
|
+
A plugin for finding related articles to a Nesta article, based on a dead-simple idea:
|
4
|
+
|
5
|
+
**Given pages A and B, the more categories A and B have in common, the higher B's relevance to A.**
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'nesta-plugin-related-by-category'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install nesta-plugin-related-by-category
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
In your Nesta `/views` folder, declare a `related_articles.haml` with something like the following:
|
24
|
+
|
25
|
+
- if has_related_articles?(@page)
|
26
|
+
%section.related-articles
|
27
|
+
%h2 Related articles
|
28
|
+
%ul
|
29
|
+
- related_by_category(@page).each do |article|
|
30
|
+
%li
|
31
|
+
%a(href="#{article.abspath}")= article.heading
|
32
|
+
|
33
|
+
Then, at the bottom of your template file (`page.haml` is default), reference it:
|
34
|
+
|
35
|
+
= haml :related_articles, :layout => false
|
36
|
+
|
37
|
+
By default, the plugin will display 3 articles; this can be overridden by a number of your choice:
|
38
|
+
|
39
|
+
related_by_category(@page, 5) # 2nd argument is the number of articles to display.
|
40
|
+
|
41
|
+
## Contributing
|
42
|
+
|
43
|
+
1. Fork it
|
44
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
45
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
46
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
47
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module Nesta
|
2
|
+
module Plugin
|
3
|
+
module RelatedByCategory
|
4
|
+
|
5
|
+
module Helpers
|
6
|
+
def has_related_articles?(page)
|
7
|
+
!(Nesta::Plugin::RelatedByCategory.articles_through_categories(page).empty?)
|
8
|
+
end
|
9
|
+
|
10
|
+
def related_by_category(page, number=3)
|
11
|
+
Nesta::Plugin::RelatedByCategory.for_page(page, number)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.ranked_by_relevance(page)
|
16
|
+
results = {}
|
17
|
+
articles_through_categories(page).each do |article|
|
18
|
+
rank = relevance(page, article)
|
19
|
+
if results.has_key? rank
|
20
|
+
results[rank] << article
|
21
|
+
else
|
22
|
+
results.store(rank, [article])
|
23
|
+
end
|
24
|
+
end
|
25
|
+
results.sort.reverse.flatten.reject {|el| !el.class.eql? Page}
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.articles_through_categories(page)
|
29
|
+
related = page.categories.map {|c| c.articles}.flatten
|
30
|
+
set = Set.new(related).to_a
|
31
|
+
set.delete page
|
32
|
+
set
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.relevance(page, other)
|
36
|
+
(tags(page) & tags(other)).count
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.tags(page)
|
40
|
+
page.categories.map {|c| c.heading}
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.display(results, number)
|
44
|
+
results.take(number)
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.for_page(page, number)
|
48
|
+
display(ranked_by_relevance(page), number)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class App
|
54
|
+
helpers Nesta::Plugin::RelatedByCategory::Helpers
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'nesta-plugin-related-by-category/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "nesta-plugin-related-by-category"
|
8
|
+
gem.version = Nesta::Plugin::RelatedByCategory::VERSION
|
9
|
+
gem.authors = ["James Abbott"]
|
10
|
+
gem.email = ["abbottjam@gmail.com"]
|
11
|
+
gem.description = %q{A plugin that finds related articles to a Nesta article, based on the relationship between its categories and other articles.}
|
12
|
+
gem.summary = %q{A plugin that finds related articles to a Nesta article, based on the relationship between its categories and other articles.}
|
13
|
+
gem.homepage = "https://github.com/abbottjam/nesta-plugin-related-by-category"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.add_dependency("nesta", ">= 0.9.11")
|
20
|
+
gem.add_development_dependency("rake")
|
21
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'nesta/app'
|
2
|
+
|
3
|
+
require_relative '../lib/nesta-plugin-related-by-category/init'
|
4
|
+
require_relative 'spec_helper'
|
5
|
+
|
6
|
+
require 'minitest/spec'
|
7
|
+
require 'minitest/autorun'
|
8
|
+
require 'mocha/setup'
|
9
|
+
|
10
|
+
describe "tests for finding related articles through an article's categories" do
|
11
|
+
include Nesta::Plugin::RelatedByCategory::Helpers
|
12
|
+
include ConfigSpecHelper
|
13
|
+
|
14
|
+
before do
|
15
|
+
stub_configuration
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "setting up data from fixtures" do
|
19
|
+
it "fetches the fixure content path" do
|
20
|
+
Nesta::Config.content_path.must_equal 'fixtures/content'
|
21
|
+
end
|
22
|
+
|
23
|
+
it "instantiates articles correctly" do
|
24
|
+
ledzep = Nesta::Page.find_by_path('ledzep')
|
25
|
+
ledzep.heading.must_equal 'Led Zeppelin'
|
26
|
+
ledzep.categories.count.must_equal 5
|
27
|
+
end
|
28
|
+
|
29
|
+
it "instantiates categories correctly" do
|
30
|
+
rock = Nesta::Page.find_by_path('rock')
|
31
|
+
rock.heading.must_equal 'Rock'
|
32
|
+
rock.articles.count.must_equal 4
|
33
|
+
headings = rock.articles.map { |a| a.heading }
|
34
|
+
headings.must_equal ["Cream", "Jimi Hendrix", "Led Zeppelin", "Rare Earth"]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "finding related articles" do
|
39
|
+
before do
|
40
|
+
@ledzep = Nesta::Page.find_by_path('ledzep')
|
41
|
+
@bach = Nesta::Page.find_by_path('bach')
|
42
|
+
end
|
43
|
+
|
44
|
+
it "tests whether a page has related articles" do
|
45
|
+
has_related_articles?(@ledzep).must_equal true
|
46
|
+
has_related_articles?(@bach).must_equal false
|
47
|
+
end
|
48
|
+
|
49
|
+
it "finds related articles bound by a number (default parammeter)" do
|
50
|
+
related_to_ledzep = related_by_category(@ledzep)
|
51
|
+
related_to_ledzep.count.must_equal 3
|
52
|
+
headings = related_to_ledzep.map { |article| article.heading }
|
53
|
+
headings.must_equal ["Cream", "Jimi Hendrix", "Rare Earth"]
|
54
|
+
end
|
55
|
+
|
56
|
+
it "finds related articles bound by a number (specified parammeter)" do
|
57
|
+
related_to_ledzep = related_by_category(@ledzep, 10)
|
58
|
+
related_to_ledzep.count.must_equal 5
|
59
|
+
headings = related_to_ledzep.map { |article| article.heading }
|
60
|
+
headings.must_equal ["Cream", "Jimi Hendrix", "Rare Earth", "Pentangle", "The Meters"]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
#This is a slimmed-down version of Graham's spec_helper, used for Nesta's own tests:
|
2
|
+
#https://github.com/gma/nesta/blob/master/spec/spec_helper.rb
|
3
|
+
#Mocha and MiniTest have replaced RSpec.
|
4
|
+
|
5
|
+
module Nesta
|
6
|
+
class App < Sinatra::Base
|
7
|
+
set :environment, :test
|
8
|
+
set :reload_templates, true
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
module ConfigSpecHelper
|
13
|
+
def stub_yaml_config
|
14
|
+
@config = {}
|
15
|
+
Nesta::Config.stubs(:yaml_exists?).returns true
|
16
|
+
Nesta::Config.stubs(:yaml_conf).returns @config
|
17
|
+
end
|
18
|
+
|
19
|
+
def stub_config_key(key, value, options = {})
|
20
|
+
stub_yaml_config unless @config
|
21
|
+
if options[:rack_env]
|
22
|
+
@config['test'] ||= {}
|
23
|
+
@config['test'][key] = value
|
24
|
+
else
|
25
|
+
@config[key] = value
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def stub_configuration(options = {})
|
30
|
+
stub_config_key('title', 'My blog', options)
|
31
|
+
stub_config_key('subtitle', 'about stuff', options)
|
32
|
+
stub_config_key(
|
33
|
+
'content', 'fixtures/content', options.merge(:rack_env => true))
|
34
|
+
end
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nesta-plugin-related-by-category
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Abbott
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-03-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: nesta
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ! '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.9.11
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ! '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.9.11
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: A plugin that finds related articles to a Nesta article, based on the
|
42
|
+
relationship between its categories and other articles.
|
43
|
+
email:
|
44
|
+
- abbottjam@gmail.com
|
45
|
+
executables: []
|
46
|
+
extensions: []
|
47
|
+
extra_rdoc_files: []
|
48
|
+
files:
|
49
|
+
- .gitignore
|
50
|
+
- Gemfile
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- lib/nesta-plugin-related-by-category.rb
|
55
|
+
- lib/nesta-plugin-related-by-category/init.rb
|
56
|
+
- lib/nesta-plugin-related-by-category/version.rb
|
57
|
+
- nesta-plugin-related-by-category.gemspec
|
58
|
+
- spec/fixtures/content/pages/bach.mdown
|
59
|
+
- spec/fixtures/content/pages/blues.mdown
|
60
|
+
- spec/fixtures/content/pages/classical.mdown
|
61
|
+
- spec/fixtures/content/pages/cream.mdown
|
62
|
+
- spec/fixtures/content/pages/folk.mdown
|
63
|
+
- spec/fixtures/content/pages/funk.mdown
|
64
|
+
- spec/fixtures/content/pages/heavy.mdown
|
65
|
+
- spec/fixtures/content/pages/hendrix.mdown
|
66
|
+
- spec/fixtures/content/pages/jazz.mdown
|
67
|
+
- spec/fixtures/content/pages/ledzep.mdown
|
68
|
+
- spec/fixtures/content/pages/pentangle.mdown
|
69
|
+
- spec/fixtures/content/pages/rare-earth.mdown
|
70
|
+
- spec/fixtures/content/pages/rock.mdown
|
71
|
+
- spec/fixtures/content/pages/the-meters.mdown
|
72
|
+
- spec/related_articles_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
homepage: https://github.com/abbottjam/nesta-plugin-related-by-category
|
75
|
+
licenses: []
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ! '>='
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ! '>='
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.0.3
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: A plugin that finds related articles to a Nesta article, based on the relationship
|
97
|
+
between its categories and other articles.
|
98
|
+
test_files:
|
99
|
+
- spec/fixtures/content/pages/bach.mdown
|
100
|
+
- spec/fixtures/content/pages/blues.mdown
|
101
|
+
- spec/fixtures/content/pages/classical.mdown
|
102
|
+
- spec/fixtures/content/pages/cream.mdown
|
103
|
+
- spec/fixtures/content/pages/folk.mdown
|
104
|
+
- spec/fixtures/content/pages/funk.mdown
|
105
|
+
- spec/fixtures/content/pages/heavy.mdown
|
106
|
+
- spec/fixtures/content/pages/hendrix.mdown
|
107
|
+
- spec/fixtures/content/pages/jazz.mdown
|
108
|
+
- spec/fixtures/content/pages/ledzep.mdown
|
109
|
+
- spec/fixtures/content/pages/pentangle.mdown
|
110
|
+
- spec/fixtures/content/pages/rare-earth.mdown
|
111
|
+
- spec/fixtures/content/pages/rock.mdown
|
112
|
+
- spec/fixtures/content/pages/the-meters.mdown
|
113
|
+
- spec/related_articles_spec.rb
|
114
|
+
- spec/spec_helper.rb
|