lib/fulfil_api/resource/relation/loadable.rb in fulfil_api-0.0.3 vs lib/fulfil_api/resource/relation/loadable.rb in fulfil_api-0.1.0

- old
+ new

@@ -12,25 +12,28 @@ # we only request data when we need to. module Loadable # Loads resources from Fulfil's API based on the current filters, fields, and limits # if they haven't been loaded yet. # - # Requires that {#name} is set; raises an exception if it's not. + # Requires that {#model_name} is set; raises an exception if it's not. # # @return [true, false] True if the resources were loaded successfully. - def load + def load # rubocop:disable Metrics/MethodLength return true if loaded? - if name.nil? + if model_name.nil? raise FulfilApi::Resource::Relation::ModelNameMissing, "The model name is missing. Use #set to define it." end response = FulfilApi.client.put( - "/model/#{name}/search_read", + "/model/#{model_name}/search_read", body: { filters: conditions, fields: fields, limit: request_limit }.compact_blank ) - @resources = response.map { |resource| @resource_klass.new(resource) } + @resources = response.map do |attributes| + @resource_klass.new(attributes.merge(model_name: model_name)) + end + @loaded = true end # Checks whether the resources have been loaded to avoid repeated API calls when # using enumerable methods.