Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect entity representation on attribute values #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand All @@ -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) }) || \
Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down