diff --git a/lib/grape_entity/entity.rb b/lib/grape_entity/entity.rb index 1bf1cc8b..bbf7b1da 100644 --- a/lib/grape_entity/entity.rb +++ b/lib/grape_entity/entity.rb @@ -423,9 +423,7 @@ def value_for(attribute, options = {}) if exposure_options[:using] exposure_options[:using] = exposure_options[:using].constantize if exposure_options[:using].respond_to? :constantize - using_options = options.dup - using_options.delete(:collection) - using_options[:root] = nil + using_options = attribute_entity_options(options) if exposure_options[:proc] exposure_options[:using].represent(instance_exec(object, options, &exposure_options[:proc]), using_options) @@ -451,9 +449,8 @@ def value_for(attribute, options = {}) Hash[nested_exposures.map do |nested_attribute, _| [self.class.key_for(nested_attribute), value_for(nested_attribute, options)] end] - else - delegate_attribute(attribute) + delegate_representable_attribute(attribute, options) end end @@ -466,6 +463,14 @@ def delegate_attribute(attribute) end end + def delegate_representable_attribute(attribute, options = {}) + val = delegate_attribute(attribute) + val = val.entity if val.respond_to?(:entity) + val = val.class.entity_class.represent(val, attribute_entity_options(options)) if val.class.respond_to?(:entity_class) + + val + end + def valid_exposure?(attribute, exposure_options) nested_exposures = self.class.nested_exposures_for(attribute) (nested_exposures.any? && nested_exposures.all? { |a, o| valid_exposure?(a, o) }) || \ @@ -502,6 +507,13 @@ def conditions_met?(exposure_options, options) private + def attribute_entity_options(options) + opts = options.dup + opts.delete(:collection) + opts[:root] = nil + opts + end + # All supported options. OPTIONS = [ :as, :if, :unless, :using, :with, :proc, :documentation, :format_with, :safe, :if_extras, :unless_extras diff --git a/spec/grape_entity/entity_spec.rb b/spec/grape_entity/entity_spec.rb index cb4fb042..4056a699 100644 --- a/spec/grape_entity/entity_spec.rb +++ b/spec/grape_entity/entity_spec.rb @@ -924,6 +924,28 @@ class UserEntity < Grape::Entity rep.all? { |r| r.is_a?(EntitySpec::UserEntity) }.should be_true end end + + context "object from DSL class" do + it "should use the DSL class's entity" do + module EntitySpec + class ObjectWithDSL + include Grape::Entity::DSL + entity do + expose(:foo) { "FOO" } + end + + def as_json + { bar: "BAR" } + end + alias_method :serializable_hash, :as_json + end + end + + fresh_class.expose(:object_with_dsl) + model.should_receive(:object_with_dsl).and_return(EntitySpec::ObjectWithDSL.new) + subject.send(:value_for, :object_with_dsl).should be_kind_of(EntitySpec::ObjectWithDSL::Entity) + end + end end describe '#documentation' do