Module: JSONAPI::ResourceActions::Sideposting

Extended by:
ActiveSupport::Concern
Includes:
SidepostingPrimaryFirst
Defined in:
lib/json_api/controllers/concerns/resource_actions/sideposting.rb

Instance Method Summary collapse

Methods included from SidepostingPrimaryFirst

#apply_lid_relationships_if_any, #build_lid_resolver_with_primary, #create_primary_and_lid_resolver, #create_with_sidepost_primary_first, #render_created_with_sidepost, #run_sidepost_processor

Instance Method Details

#apply_resolved_relationships!(resource, resolved_relationships) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/json_api/controllers/concerns/resource_actions/sideposting.rb', line 80

def apply_resolved_relationships!(resource, resolved_relationships)
  payload = { relationships: resolved_relationships }
  params_hash = JSONAPI::Deserializer.new(
    payload,
    model_class: resource.class,
    action: :update,
  ).to_model_attributes
  resource.update!(params_hash)
end

#build_resource_from_resolved_data(resolved_data) ⇒ Object



90
91
92
93
94
# File 'lib/json_api/controllers/concerns/resource_actions/sideposting.rb', line 90

def build_resource_from_resolved_data(resolved_data)
  sti_class = determine_sti_class
  params_hash, @create_attachments = prepare_create_params_from_data(sti_class, resolved_data)
  sti_class.new(params_hash)
end

#contains_lid?(rel_data) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
75
76
77
78
# File 'lib/json_api/controllers/concerns/resource_actions/sideposting.rb', line 70

def contains_lid?(rel_data)
  return false if rel_data.blank?

  if rel_data.is_a?(Array)
    rel_data.any? { |item| contains_lid?(item) }
  else
    (rel_data[:lid] || rel_data["lid"]).to_s.present?
  end
end

#create_with_sidepostObject



35
36
37
38
39
40
41
# File 'lib/json_api/controllers/concerns/resource_actions/sideposting.rb', line 35

def create_with_sidepost
  if self.class.included_first?
    create_with_sidepost_included_first
  else
    create_with_sidepost_primary_first
  end
end

#create_with_sidepost_included_firstObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/json_api/controllers/concerns/resource_actions/sideposting.rb', line 43

def create_with_sidepost_included_first
  ActiveRecord::Base.transaction do
    processor = JSONAPI::Sideposting::Processor.new(included: params[:included], controller: self)
    processor.create_all!
    resolved_data = processor.lid_resolver.resolve(raw_jsonapi_data)
    resource = build_resource_from_resolved_data(resolved_data)
    authorize_resource_action!(resource, action: :create)
    attach_active_storage_files(resource, @create_attachments, resource_class: determine_sti_resource_class)
    save_created_with_sidepost(resource, processor.created_records)
  end
end

#extract_lid_relationships!(data) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/json_api/controllers/concerns/resource_actions/sideposting.rb', line 55

def extract_lid_relationships!(data)
  extracted = {}
  rels = data[:relationships] || {}
  rels.each do |key, val|
    next unless val.is_a?(Hash)

    d = val[:data] || val["data"]
    next unless contains_lid?(d)

    extracted[key] = val
  end
  extracted.each_key { |k| rels.delete(k) }
  extracted
end

#sidepost_request?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/json_api/controllers/concerns/resource_actions/sideposting.rb', line 31

def sidepost_request?
  params[:included].present?
end