lib/fmrest/spyke/relation.rb in fmrest-0.4.1 vs lib/fmrest/spyke/relation.rb in fmrest-0.5.0
- old
+ new
@@ -10,11 +10,11 @@
# "_limit" (or "_offset", etc.) as param keys depending on the type of
# request, so we can't set the params until the last moment
attr_accessor :limit_value, :offset_value, :sort_params, :query_params,
- :included_portals, :portal_params
+ :included_portals, :portal_params, :script_params
def initialize(*_args)
super
@limit_value = klass.default_limit
@@ -25,12 +25,48 @@
@query_params = []
@included_portals = nil
@portal_params = {}
+ @script_params = {}
end
+ # @param options [String, Array, Hash, nil, false] sets script params to
+ # execute in the next get or find request
+ #
+ # @example
+ # # Find records and run the script named "My script"
+ # Person.script("My script").find_some
+ #
+ # # Find records and run the script named "My script" with param "the param"
+ # Person.script(["My script", "the param"]).find_some
+ #
+ # # Find records and run a prerequest, presort and after (normal) script
+ # Person.script(after: "Script", prerequest: "Prereq script", presort: "Presort script").find_some
+ #
+ # # Same as above, but passing parameters too
+ # Person.script(
+ # after: ["After script", "the param"],
+ # prerequest: ["Prereq script", "the param"],
+ # presort: o ["Presort script", "the param"]
+ # ).find_some
+ #
+ # Person.script(nil).find_some # Disable script execution
+ # Person.script(false).find_some # Disable script execution
+ #
+ # @return [FmRest::Spyke::Relation] a new relation with the script
+ # options applied
+ def script(options)
+ with_clone do |r|
+ if options.eql?(false) || options.eql?(nil)
+ r.script_params = {}
+ else
+ r.script_params = script_params.merge(FmRest::V1.convert_script_params(options))
+ end
+ end
+ end
+
# @param value_or_hash [Integer, Hash] the limit value for this layout,
# or a hash with limits for the layout's portals
# @example
# Person.limit(10) # Set layout limit
# Person.limit(children: 10) # Set portal limit
@@ -138,16 +174,21 @@
# @return [Boolean] whether a query was set on this relation
def has_query?
query_params.present?
end
- # Finds a single instance of the model by forcing limit = 1
+ # Finds a single instance of the model by forcing limit = 1, or simply
+ # fetching the record by id if the primary key was set
#
# @return [FmRest::Spyke::Base]
def find_one
- return super if params[klass.primary_key].present?
- @find_one ||= klass.new_collection_from_result(limit(1).fetch).first
+ @find_one ||=
+ if primary_key_set?
+ without_collection_params { super }
+ else
+ klass.new_collection_from_result(limit(1).fetch).first
+ end
rescue ::Spyke::ConnectionError => error
fallback_or_reraise(error, default: nil)
end
protected
@@ -219,9 +260,21 @@
normalized[klass.mapped_attributes[k].to_s] = v
else
normalized[k.to_s] = v
end
end
+ end
+
+ def primary_key_set?
+ params[klass.primary_key].present?
+ end
+
+ def without_collection_params
+ orig_values = limit_value, offset_value, sort_params, query_params
+ self.limit_value = self.offset_value = self.sort_params = self.query_params = nil
+ yield
+ ensure
+ self.limit_value, self.offset_value, self.sort_params, self.query_params = orig_values
end
def with_clone
clone.tap do |relation|
yield relation