module Graphiti
robots.thoughtbot.com/mygem-configure-block
This could definitely use some refactoring love, but I have no time ATM The code is pretty self-contained; we’re just listening to notifications and taking action.
For “Rails STI” behavior CreditCard.all # => [<Visa>, <Mastercard>, etc]
If you’re using Rails
+ responders gem to get respond_with
Private, tested in resource specs
Todo: class purpose
A minimal implementation of an errors object similar to ‘ActiveModel::Errors`. Designed to support internal Graphiti
classes like the `RequestValidator` so that there does not need to be a dependency on activemodel.
Constants
- DEPRECATOR
- VERSION
Public Class Methods
Source
# File lib/graphiti.rb, line 65 def self.broadcast(name, payload) # AS::N prefers domain naming format with more specific towards end name = "#{name}.graphiti" ActiveSupport::Notifications.instrument(name, payload) do yield payload if block_given? end end
Source
# File lib/graphiti.rb, line 57 def self.configure yield config end
@example
Graphiti.configure do |c| c.raise_on_missing_sideload = false end
@see Configuration
Source
# File lib/graphiti.rb, line 25 def self.context Thread.current[:context] ||= {} end
@api private
Source
# File lib/graphiti.rb, line 30 def self.context=(val) Thread.current[:context] = val end
@api private
Source
# File lib/graphiti.rb, line 90 def self.log(msg, color = :white, bold = false) colored = if ::ActiveSupport.version >= Gem::Version.new("7.1") ActiveSupport::LogSubscriber.new.send(:color, msg, color, bold: bold) else ActiveSupport::LogSubscriber.new.send(:color, msg, color, bold) end logger.debug(colored) end
Source
# File lib/graphiti.rb, line 108 def self.setup! resources.each do |r| r.apply_sideloads_to_serializer end end
When we add a sideload, we need to do configuration, such as adding the relationship to the Resource’s serializer. However, the sideload’s Resource
class may not be loaded yet.
This is not a problem when Rails
autoloading, but is a problem when eager loading, or not using Rails
.
So, load every Resource
class then call Graphiti.setup!
Source
# File lib/graphiti.rb, line 78 def self.stdout_logger logger = Logger.new($stdout) logger.formatter = proc do |severity, datetime, progname, msg| "#{msg}\n" end logger end
Source
# File lib/graphiti.rb, line 35 def self.with_context(obj, namespace = nil) prior = context self.context = {object: obj, namespace: namespace} yield ensure self.context = prior resources.each do |resource_class| resource_class.sideloads.values.each(&:clear_resources) end end
@api private