Skip to content

Instantly share code, notes, and snippets.

@siassaj
Created March 11, 2016 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siassaj/b4f0164b68f190e738a5 to your computer and use it in GitHub Desktop.
Save siassaj/b4f0164b68f190e738a5 to your computer and use it in GitHub Desktop.
RSpec.describe ApplicationController do
it_behaves_like "JsonRepresenterConcern"
end
module JsonRepresenterConcern
extend ActiveSupport::Concern
included do
private
def represent(record)
return nil if record.blank?
klass_name = record.class.name
"#{klass_name}Presenter".constantize.new(record)
end
def represent_collection(records)
return [] if records.empty?
klass_name = record.class.name
records.map do |record|
"#{klass_name}Presenter".constantize.new(record)
end
end
end
end
RSpec.shared_examples "JsonRepresenterConcern" do
# let(:dummy) { Class.new } ## dummy.new.class.name is nil, I need some sort of class name
# Pollutes namespace & opens Dummy that may be defined in other specs
class Dummy
include JsonRepresenterConcern
end
# let(:subject) { double } ## subject.class.name returns RSpec::Mocks::Double which is .. well it feels wrong to use that
let(:subject) { Dummy.new }
describe "#represent_collection" do
it "wraps a record in a presenter" do
representation = subject.send :represent_collection, []
# Expect some stuff
end
end
describe "#represent_collection" do
it "maps a collection of records to records wrapped in a presenter" do
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment