Skip to content

Commit

Permalink
Serializing the inherited lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
priyankaiyer committed Oct 27, 2014
1 parent 6a50d86 commit 48446e8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
9 changes: 7 additions & 2 deletions lib/looksist/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ def find_alias(as_map, what)


def as_json(opts)
Looksist.driver.json_opts(self, self.class.lookup_attributes, opts)
parent_lookups = {}
class_lookups = self.class.lookup_attributes || {}
if self.class.superclass.respond_to?(:lookup_attributes)
parent_lookups = self.class.superclass.lookup_attributes || {}
end
Looksist.driver.json_opts(self, class_lookups.merge(parent_lookups), opts)
end

end
Expand All @@ -51,7 +56,7 @@ module Serializers
class Her
class << self
def json_opts(obj, lookup_attributes, _)
lookup_attributes ||= {}
lookup_attributes ||= {}
other_attributes = lookup_attributes.keys.each_with_object({}) do |a, acc|
using = lookup_attributes[a]
acc[a] = obj.send(a) if obj.respond_to?(using)
Expand Down
2 changes: 1 addition & 1 deletion lib/looksist/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Lookist
VERSION = '0.2.5'
VERSION = '0.2.6'
end
48 changes: 47 additions & 1 deletion spec/looksist/looksist_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,51 @@ def as_json(opts)
expect(e.to_json).to eq("{\"nome\":\"Rajini\",\"age\":16,\"id\":1}")
end
end

context 'Look up support in case of Inheritance' do
it 'should also get parent class lookups during to json conversion' do

module InheritedLookUp
class Employee
include Her::Model
use_api TEST_API
include Looksist
attr_accessor :id
attr_accessor :department_id
lookup :name, using: :id, bucket_name: 'employees'

def initialize(id, department_id)
@id = id
@department_id = department_id
end

def as_json(opts)
super(opts).merge(id: @id)
end
end

class Manager < Employee
lookup :name, using: :department_id, as: {name: 'department_name'}

def is_manager?
true
end

def as_json(opts)
super(opts).merge(is_manager: is_manager?)
end
end
end

expect(@mock).to receive(:get).once.with('employees/1').and_return('SuperStar')
expect(@mock).to receive(:get).once.with('departments/2').and_return('Kollywood')

e = InheritedLookUp::Manager.new(1, 2)

expect(e.to_json).to eq("{\"department_name\":\"Kollywood\",\"name\":\"SuperStar\",\"id\":1,\"is_manager\":true}")
end
end

end
context 'with l2 cache' do
before(:each) do
Expand Down Expand Up @@ -127,7 +172,7 @@ def as_json(opts)
end
end
end
expect(@mock).to receive(:get).never.with('employees_/1')
expect(@mock).to receive(:get).never.with('employees/1')
e = TolerantLookUp::Employee.new
expect(e.to_json).to eq('{}')
end
Expand Down Expand Up @@ -291,5 +336,6 @@ def initialize(id)
employee_second_instance.name
end
end

end
end

0 comments on commit 48446e8

Please sign in to comment.