Skip to content

Commit

Permalink
The stub flag shouldn't affect slugs.
Browse files Browse the repository at this point in the history
To implement this I've had to change identifiable to call a method which doesn't directly access the attribute. Policy then overrides this method.
  • Loading branch information
lazyatom committed Jan 24, 2012
1 parent c511c69 commit 157a03e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
4 changes: 4 additions & 0 deletions app/models/document.rb
Expand Up @@ -124,6 +124,10 @@ def title_with_state
"#{title} (#{state})"
end

def sluggable_title
title
end

class << self
def authored_by(user)
joins(:document_authors).where(document_authors: {user_id: user}).group(:document_id)
Expand Down
4 changes: 2 additions & 2 deletions app/models/document/identifiable.rb
Expand Up @@ -10,11 +10,11 @@ module Document::Identifiable
end

def set_document_identity
self.document_identity ||= DocumentIdentity.new(sluggable_string: self.title)
self.document_identity ||= DocumentIdentity.new(sluggable_string: self.sluggable_title)
end

def update_document_identity_slug
self.document_identity.update_slug_if_possible(self.title)
self.document_identity.update_slug_if_possible(self.sluggable_title)
end

def set_document_type_on_document_identity
Expand Down
12 changes: 10 additions & 2 deletions app/models/policy.rb
Expand Up @@ -20,8 +20,16 @@ def process_associations_after_save(document)

scope :stub, where(stub: true)

def title
original_title = super
define_attribute_methods

def title_with_stub
original_title = title_without_stub
stub? ? "[Sample] #{original_title}" : original_title
end
alias_method_chain :title, :stub

def sluggable_title
title_without_stub
end

end
6 changes: 5 additions & 1 deletion test/unit/policy_test.rb
Expand Up @@ -33,7 +33,11 @@ class PolicyTest < ActiveSupport::TestCase
assert_equal [policy_area_1, policy_area_2], policy.policy_areas.reload
end

test "should prepend stub signifier to stub policy titles" do
test "prepends stub signifier to stub policy titles" do
assert_equal "[Sample] stub title", build(:policy, title: "stub title", stub: true).title
end

test "stub status doesn't affect slug generation" do
assert_equal "stub-title", create(:policy, title: "stub title", stub: true).document_identity.slug
end
end

0 comments on commit 157a03e

Please sign in to comment.