Skip to content

Instantly share code, notes, and snippets.

@janxious
Created October 13, 2011 20:02
Show Gist options
  • Save janxious/1285340 to your computer and use it in GitHub Desktop.
Save janxious/1285340 to your computer and use it in GitHub Desktop.
class Post < ActiveRecord::Base
include Summarizeable
belongs_to :author
alias :body :summarizeable
end
# spec/models/summarizeable_spec.rb
require 'spec/helper'
describe Summarizeable do
context "summary" do
it "should return the summary" do
thing = SummaryStub.new
thing.text_to_summarize = "This is what I expect.\nI don't expect this!"
assert_equal "This is what I expect.", thing.summary
end
end
end
module Summarizeable
def summary(delimiter="\n")
matcher = /#{Regexp.escape(delimiter)}/i
@summary = if summarizeable =~ matcher
summerizeable.split(matcher).first.strip
else
summerizeable
end
end
end
class SummaryStub
include Summarizeable
attr_accessible :text_to_summarize
alias :text_to_summarize :summarizeable
end
@agrimm
Copy link

agrimm commented Oct 13, 2011

Summarizeable#summary creates the instance variable @summary. Is that appropriate behavior for a module?

@janxious
Copy link
Author

Sometimes. Probably not in this instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment