Skip to content

Commit

Permalink
when a representer module is extended we no longer set the @represent…
Browse files Browse the repository at this point in the history
…able_attrs ivar directly but use a setter. this makes it work with mongoid and fixes trailblazer/roar#10.
  • Loading branch information
apotonick committed Feb 6, 2012
1 parent f6f2fd1 commit b7eceac
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/representable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#
# hero.extend(HeroRepresenter).to_json
module Representable
attr_writer :representable_attrs

def self.included(base)
base.class_eval do
extend ClassMethods
Expand All @@ -37,10 +39,7 @@ def self.included(base)

# Copies the representable_attrs to the extended object.
def self.extended(object)
attrs = representable_attrs
object.instance_eval do
@representable_attrs = attrs
end
object.representable_attrs=(representable_attrs)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions representable.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ Gem::Specification.new do |s|
s.add_development_dependency "test_xml"
s.add_development_dependency "minitest", ">= 2.8.1"
s.add_development_dependency "mocha"
s.add_development_dependency "mongoid"
end
31 changes: 31 additions & 0 deletions test/mongoid_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'test_helper'
require 'mongoid'
require 'mongoid/document'

class MongoidTest < MiniTest::Spec
describe "Mongoid compatibility" do
it "allows #to_json" do
class Profile
include Mongoid::Document
field :name
end

class Dude
include Mongoid::Document
embeds_one :profile, :class_name => "MongoidTest::Profile"
end

module ProfileRepresenter
include Representable::JSON

property :name
end

dude = Dude.new
dude.profile = Profile.new
dude.profile.name = "Kofi"

assert_equal "{\"name\":\"Kofi\"}", dude.profile.extend(ProfileRepresenter).to_json
end
end
end

0 comments on commit b7eceac

Please sign in to comment.