Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow storing document (views) in attributes #694

Merged
merged 1 commit into from Sep 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/nanoc/base/views/mixins/mutable_document_view_mixin.rb
@@ -1,11 +1,34 @@
module Nanoc
module MutableDocumentViewMixin
# @api private
class DisallowedAttributeValueError < Nanoc::Error
attr_reader :value

def initialize(value)
@value = value
end

def message
"The #{value.class} cannot be stored inside an attribute. Store its identifier instead."
end
end

# Sets the value for the given attribute.
#
# @param [Symbol] key
#
# @see Hash#[]=
def []=(key, value)
disallowed_value_classes = Set.new([
Nanoc::Int::Item,
Nanoc::Int::Layout,
Nanoc::ItemView,
Nanoc::LayoutView,
])
if disallowed_value_classes.include?(value.class)
raise DisallowedAttributeValueError.new(value)
end

unwrap.attributes[key] = value
end

Expand Down
20 changes: 20 additions & 0 deletions spec/nanoc/base/views/mutable_document_view_spec.rb
Expand Up @@ -7,6 +7,26 @@
view[:title] = 'Donkey'
expect(view[:title]).to eq('Donkey')
end

it 'disallows items' do
item = Nanoc::Int::Item.new('content', {}, '/foo.md')
expect { view[:item] = item }.to raise_error(Nanoc::MutableDocumentViewMixin::DisallowedAttributeValueError)
end

it 'disallows layouts' do
layout = Nanoc::Int::Layout.new('content', {}, '/foo.md')
expect { view[:layout] = layout }.to raise_error(Nanoc::MutableDocumentViewMixin::DisallowedAttributeValueError)
end

it 'disallows item views' do
item = Nanoc::ItemView.new(Nanoc::Int::Item.new('content', {}, '/foo.md'))
expect { view[:item] = item }.to raise_error(Nanoc::MutableDocumentViewMixin::DisallowedAttributeValueError)
end

it 'disallows layout views' do
layout = Nanoc::LayoutView.new(Nanoc::Int::Layout.new('content', {}, '/foo.md'))
expect { view[:layout] = layout }.to raise_error(Nanoc::MutableDocumentViewMixin::DisallowedAttributeValueError)
end
end

describe '#update_attributes' do
Expand Down