From a1bf1e4e87684d30288a244e261219ba7ba025a0 Mon Sep 17 00:00:00 2001 From: Bryan Hockey Date: Mon, 14 Nov 2016 14:23:16 -0600 Subject: [PATCH] map_belongs_to now maps the association to (and from) the model object, not its id --- lib/rails_view_adapters/definition_proxy.rb | 7 +++---- lib/rails_view_adapters/version.rb | 2 +- spec/integration/an_adapter_spec.rb | 2 +- spec/lib/definition_proxy_spec.rb | 6 +++--- 4 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/rails_view_adapters/definition_proxy.rb b/lib/rails_view_adapters/definition_proxy.rb index e9c145e..6f17831 100644 --- a/lib/rails_view_adapters/definition_proxy.rb +++ b/lib/rails_view_adapters/definition_proxy.rb @@ -94,18 +94,17 @@ def map_bool(model_field, public_field) def map_belongs_to(model_field, public_field, options = {}) model_class = options[:model_class] || model_field.to_s.classify.constantize sub_method = options[:sub_method] || :id - model_field_id = :"#{model_field.to_s.sub(/(_id|)\Z/, "_id")}" unless options[:only] == :to map_from_public public_field do |value| record = model_class.send(:"find_by_#{sub_method}", value) - { model_field_id => record ? record.id : nil } + { model_field => record ? record : model_class.new(sub_method => value) } end end unless options[:only] == :from - map_to_public model_field_id do |id| - { public_field => model_class.find_by(id: id).send(sub_method) } + map_to_public model_field do |record| + { public_field => record.send(sub_method) } end end end diff --git a/lib/rails_view_adapters/version.rb b/lib/rails_view_adapters/version.rb index d3d401e..6c047f3 100644 --- a/lib/rails_view_adapters/version.rb +++ b/lib/rails_view_adapters/version.rb @@ -1,4 +1,4 @@ # frozen_string_literal: true module RailsViewAdapters - VERSION = "0.2.4" + VERSION = "0.3.0" end diff --git a/spec/integration/an_adapter_spec.rb b/spec/integration/an_adapter_spec.rb index 726feba..c6612d8 100644 --- a/spec/integration/an_adapter_spec.rb +++ b/spec/integration/an_adapter_spec.rb @@ -40,7 +40,7 @@ module RailsViewAdapters join_date: @model.join_date, admin: @model.admin, secret: @model.secret, - team_id: @model.team_id, + team: @model.team, posts: @model.posts, created_at: @model.created_at, updated_at: @model.updated_at diff --git a/spec/lib/definition_proxy_spec.rb b/spec/lib/definition_proxy_spec.rb index 9c83e80..dd4da95 100644 --- a/spec/lib/definition_proxy_spec.rb +++ b/spec/lib/definition_proxy_spec.rb @@ -190,13 +190,13 @@ def to_hash end it "defines a to_map that converts the model's parent to the public's field:value string" do proxy.map_belongs_to(model_field, public_field, options) - expect(proxy.map.to_maps[0][1].call(post.user.id)) + expect(proxy.map.to_maps[0][1].call(post.user)) .to eql(public_field => post.user.name) end - it "defines a from_map that converts the public's string to the model's owning model id" do + it "defines a from_map that converts the public's string to the model's owning model object" do proxy.map_belongs_to(model_field, public_field, options) expect(proxy.map.from_maps[0][1].call(post.user.name)) - .to eql(model_field => post.user.id) + .to eql(model_field => post.user) end end