Module: Sipity::Queries::SipQueries

Defined in:
app/repositories/sipity/queries/sip_queries.rb

Overview

Queries

Constant Summary collapse

BASE_HEADER_ATTRIBUTES =
[:title, :work_publication_strategy].freeze

Instance Method Summary collapse

Instance Method Details

#build_create_sip_form(attributes: {}) ⇒ Object


22
23
24
# File 'app/repositories/sipity/queries/sip_queries.rb', line 22

def build_create_sip_form(attributes: {})
  Forms::CreateSipForm.new(attributes)
end

#build_update_sip_form(sip:, attributes: {}) ⇒ Object


26
27
28
29
30
31
# File 'app/repositories/sipity/queries/sip_queries.rb', line 26

def build_update_sip_form(sip:, attributes: {})
  fail "Expected #{sip} to be persisted" unless sip.persisted?
  new_attributes = existing_sip_attributes_for(sip).merge(attributes)
  exposed_attribute_names = exposed_sip_attribute_names_for(sip: sip)
  Forms::UpdateSipForm.new(sip: sip, exposed_attribute_names: exposed_attribute_names, attributes: new_attributes)
end

#existing_sip_attributes_for(sip) ⇒ Object (private)


35
36
37
38
39
40
41
42
# File 'app/repositories/sipity/queries/sip_queries.rb', line 35

def existing_sip_attributes_for(sip)
  # TODO: How to account for additional fields and basic fields of sip
  existing_attributes = { title: sip.title, work_publication_strategy: sip.work_publication_strategy }
  Models::AdditionalAttribute.where(sip: sip).each_with_object(existing_attributes) do |attr, mem|
    # TODO: How to handle multi-value options
    mem[attr.key] = attr.value
  end
end

#exposed_sip_attribute_names_for(sip:, additional_attribute_names: BASE_HEADER_ATTRIBUTES) ⇒ Object (private)


44
45
46
47
48
49
50
# File 'app/repositories/sipity/queries/sip_queries.rb', line 44

def exposed_sip_attribute_names_for(sip:, additional_attribute_names: BASE_HEADER_ATTRIBUTES)
  (
    AdditionalAttributeQueries.sip_default_attribute_keys_for(sip: sip) +
    AdditionalAttributeQueries.sip_attribute_keys_for(sip: sip) +
    additional_attribute_names
  ).uniq
end

#find_sip(sip_id) ⇒ Object


6
7
8
# File 'app/repositories/sipity/queries/sip_queries.rb', line 6

def find_sip(sip_id)
  Models::Sip.find(sip_id)
end

#find_sips_for(user:) ⇒ Object


17
18
19
20
# File 'app/repositories/sipity/queries/sip_queries.rb', line 17

def find_sips_for(user:)
  # REVIEW: Is this bleeding into the authorization layer?
  Policies::SipPolicy::Scope.resolve(user: user, scope: Models::Sip)
end

#permanent_uri_for_sip_id(sip_id) ⇒ Object

TODO:

Is this the right place for this? Should there a permanency layer? That is to say something responsible for resolving records and providing redirection.


13
14
15
# File 'app/repositories/sipity/queries/sip_queries.rb', line 13

def permanent_uri_for_sip_id(sip_id)
  URI.parse("http://change.me/show/#{sip_id}")
end