lib/fmrest/spyke/model/associations.rb in fmrest-spyke-0.17.1 vs lib/fmrest/spyke/model/associations.rb in fmrest-spyke-0.18.0.rc1
- old
+ new
@@ -1,7 +1,8 @@
# frozen_string_literal: true
+require "fmrest/spyke/portal_builder"
require "fmrest/spyke/portal"
module FmRest
module Spyke
module Model
@@ -11,10 +12,12 @@
extend ::ActiveSupport::Concern
included do
# Keep track of portal options by their FM keys as we could need it
# to parse the portalData JSON in SpykeFormatter
+ #
+ # TODO: Replace this with options in PortalBuilder
class_attribute :portal_options, instance_accessor: false, instance_predicate: false
# class_attribute supports a :default option since ActiveSupport 5.2,
# but we want to support previous versions too so we set the default
# manually instead
@@ -38,14 +41,16 @@
# class Person < FmRest::Spyke::Base
# has_portal :jobs, portal_key: "JobsTable", attribute_prefix: "Job"
# end
#
def has_portal(name, options = {})
- create_association(name, Portal, options)
+ # This is analogous to Spyke's create_association method, but using
+ # our custom builder instead
+ self.associations = associations.merge(name => PortalBuilder.new(self, name, Portal, options))
# Store options for SpykeFormatter to use if needed
portal_key = options[:portal_key] || name
- self.portal_options = portal_options.merge(portal_key.to_s => options.dup.merge(name: name.to_s)).freeze
+ self.portal_options = portal_options.merge(portal_key.to_s => options.dup.merge(name: name.to_s).freeze).freeze
define_method "#{name.to_s.singularize}_ids" do
association(name).map(&:id)
end
end