Class: Attribution

Inherits:
ApplicationRecord show all
Includes:
Housekeeping, Shared::BatchByFilterScope, Shared::Confidences, Shared::IsData, Shared::Notes, Shared::PolymorphicAnnotator, Shared::Tags
Defined in:
app/models/attribution.rb

Overview

An attribution is an explicit assertion of who is responsible for different attributes of the content of tied data.

Constant Summary collapse

ATTRIBUTION_ROLES =

TODO: Consider DRYing with Source roles.

[
  :creator,
  :editor,
  :owner,
  :copyright_holder
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shared::PolymorphicAnnotator

#annotated_object_is_persisted?

Methods included from Shared::IsData

#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar

Methods included from Shared::Tags

#reject_tags, #tag_with, #tagged?, #tagged_with?

Methods included from Shared::Confidences

#reject_confidences

Methods included from Shared::Notes

#concatenated_notes_string, #reject_notes

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

Returns 4 digit year of copyright.

Returns:

  • (Integer)

    4 digit year of copyright



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/attribution.rb', line 11

class Attribution < ApplicationRecord
  include Housekeeping
  include Shared::BatchByFilterScope
  include Shared::Notes
  include Shared::Confidences
  include Shared::Tags
  include Shared::IsData
  include Shared::PolymorphicAnnotator
  polymorphic_annotates('attribution_object', inverse_of: :attribution)

  # TODO: Consider DRYing with Source roles.

  ATTRIBUTION_ROLES = [
    :creator,
    :editor,
    :owner,
    :copyright_holder
  ].freeze

  ATTRIBUTION_ROLES.each do |r|
    role_name = "#{r}_roles".to_sym
    role_person = "attribution_#{r.to_s.pluralize}".to_sym
    role_organization = "attribution_organization_#{r.to_s.pluralize}".to_sym

    has_many role_name, -> { order('roles.position ASC') }, class_name: "Attribution#{r.to_s.camelize}", as: :role_object, inverse_of: :role_object
    has_many role_person, -> { order('roles.position ASC') }, through: role_name, source: :person, validate: true
    has_many role_organization, -> { order('roles.position ASC') }, through: role_name, source: :organization, validate: true

    accepts_nested_attributes_for role_name, allow_destroy: true
    accepts_nested_attributes_for role_person
    accepts_nested_attributes_for role_organization
  end

  validates :attribution_object_id, uniqueness: { scope: [:attribution_object_type, :project_id] }

  validates :license, inclusion: {in: CREATIVE_COMMONS_LICENSES.keys}, allow_nil: true

  validates :copyright_year, date_year: {
    min_year: 1000, max_year: Time.zone.now.year + 5,
    message: 'must be an integer greater than 999 and no more than 5 years in the future'}

  validate :some_data_provided

  def self.process_batch_by_filter_scope(
    batch_response: nil, query: nil, hash_query: nil, mode: nil, params: nil,
    async: nil, project_id: nil, user_id: nil,
    called_from_async: false
  )
    # Don't call async from async (the point is we do the same processing in
    # async and not in async, and this function handles both that processing and
    # making the async call, so it's this much janky).
    async = false if called_from_async == true
    r = batch_response

    case mode.to_sym
    when :add
      if async && !called_from_async
        BatchByFilterScopeJob.perform_later(
          klass: self.name,
          hash_query:,
          mode:,
          params:,
          project_id:,
          user_id:
        )
      else
        attribution_object_type = query.klass.name
        attribution = params[:attribution]
        query.find_each do |o|
          o_params = attribution.merge({
            attribution_object_id: o.id,
            attribution_object_type:
          })
          o = Attribution.create(o_params)

          if o.valid?
            r.updated.push o.id
          else
            r.not_updated.push nil # no id to add
          end
        end
      end

    # when :remove
    #   # Just delete, async or not
    #   Attribution
    #     .where(
    #       attribution_object_id: q.pluck(:id),
    #       attribution_object_type: b.referenced_klass.name
    #     ).delete_all
    end

    r
  end

  protected

  def some_roles_present
    ATTRIBUTION_ROLES.each do |r|
      return true if send("#{r}_roles".to_sym).any?
    end

    if self.roles.any?
      self.roles.each do |r|
        return true if r.type.present? && (r.person_id.present? || r.organization_id.present?)
      end
    end

    false
  end

  def some_data_provided
    if license.blank? && copyright_year.blank? && !some_roles_present
      errors.add(:base, 'no attribution metadata')
    end
  end

end

#licenseString

Returns A creative-commons copyright.

Returns:

  • (String)

    A creative-commons copyright



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/attribution.rb', line 11

class Attribution < ApplicationRecord
  include Housekeeping
  include Shared::BatchByFilterScope
  include Shared::Notes
  include Shared::Confidences
  include Shared::Tags
  include Shared::IsData
  include Shared::PolymorphicAnnotator
  polymorphic_annotates('attribution_object', inverse_of: :attribution)

  # TODO: Consider DRYing with Source roles.

  ATTRIBUTION_ROLES = [
    :creator,
    :editor,
    :owner,
    :copyright_holder
  ].freeze

  ATTRIBUTION_ROLES.each do |r|
    role_name = "#{r}_roles".to_sym
    role_person = "attribution_#{r.to_s.pluralize}".to_sym
    role_organization = "attribution_organization_#{r.to_s.pluralize}".to_sym

    has_many role_name, -> { order('roles.position ASC') }, class_name: "Attribution#{r.to_s.camelize}", as: :role_object, inverse_of: :role_object
    has_many role_person, -> { order('roles.position ASC') }, through: role_name, source: :person, validate: true
    has_many role_organization, -> { order('roles.position ASC') }, through: role_name, source: :organization, validate: true

    accepts_nested_attributes_for role_name, allow_destroy: true
    accepts_nested_attributes_for role_person
    accepts_nested_attributes_for role_organization
  end

  validates :attribution_object_id, uniqueness: { scope: [:attribution_object_type, :project_id] }

  validates :license, inclusion: {in: CREATIVE_COMMONS_LICENSES.keys}, allow_nil: true

  validates :copyright_year, date_year: {
    min_year: 1000, max_year: Time.zone.now.year + 5,
    message: 'must be an integer greater than 999 and no more than 5 years in the future'}

  validate :some_data_provided

  def self.process_batch_by_filter_scope(
    batch_response: nil, query: nil, hash_query: nil, mode: nil, params: nil,
    async: nil, project_id: nil, user_id: nil,
    called_from_async: false
  )
    # Don't call async from async (the point is we do the same processing in
    # async and not in async, and this function handles both that processing and
    # making the async call, so it's this much janky).
    async = false if called_from_async == true
    r = batch_response

    case mode.to_sym
    when :add
      if async && !called_from_async
        BatchByFilterScopeJob.perform_later(
          klass: self.name,
          hash_query:,
          mode:,
          params:,
          project_id:,
          user_id:
        )
      else
        attribution_object_type = query.klass.name
        attribution = params[:attribution]
        query.find_each do |o|
          o_params = attribution.merge({
            attribution_object_id: o.id,
            attribution_object_type:
          })
          o = Attribution.create(o_params)

          if o.valid?
            r.updated.push o.id
          else
            r.not_updated.push nil # no id to add
          end
        end
      end

    # when :remove
    #   # Just delete, async or not
    #   Attribution
    #     .where(
    #       attribution_object_id: q.pluck(:id),
    #       attribution_object_type: b.referenced_klass.name
    #     ).delete_all
    end

    r
  end

  protected

  def some_roles_present
    ATTRIBUTION_ROLES.each do |r|
      return true if send("#{r}_roles".to_sym).any?
    end

    if self.roles.any?
      self.roles.each do |r|
        return true if r.type.present? && (r.person_id.present? || r.organization_id.present?)
      end
    end

    false
  end

  def some_data_provided
    if license.blank? && copyright_year.blank? && !some_roles_present
      errors.add(:base, 'no attribution metadata')
    end
  end

end

Class Method Details

.process_batch_by_filter_scope(batch_response: nil, query: nil, hash_query: nil, mode: nil, params: nil, async: nil, project_id: nil, user_id: nil, called_from_async: false) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/attribution.rb', line 54

def self.process_batch_by_filter_scope(
  batch_response: nil, query: nil, hash_query: nil, mode: nil, params: nil,
  async: nil, project_id: nil, user_id: nil,
  called_from_async: false
)
  # Don't call async from async (the point is we do the same processing in
  # async and not in async, and this function handles both that processing and
  # making the async call, so it's this much janky).
  async = false if called_from_async == true
  r = batch_response

  case mode.to_sym
  when :add
    if async && !called_from_async
      BatchByFilterScopeJob.perform_later(
        klass: self.name,
        hash_query:,
        mode:,
        params:,
        project_id:,
        user_id:
      )
    else
      attribution_object_type = query.klass.name
      attribution = params[:attribution]
      query.find_each do |o|
        o_params = attribution.merge({
          attribution_object_id: o.id,
          attribution_object_type:
        })
        o = Attribution.create(o_params)

        if o.valid?
          r.updated.push o.id
        else
          r.not_updated.push nil # no id to add
        end
      end
    end

  # when :remove
  #   # Just delete, async or not
  #   Attribution
  #     .where(
  #       attribution_object_id: q.pluck(:id),
  #       attribution_object_type: b.referenced_klass.name
  #     ).delete_all
  end

  r
end

Instance Method Details

#some_data_providedObject (protected)



122
123
124
125
126
# File 'app/models/attribution.rb', line 122

def some_data_provided
  if license.blank? && copyright_year.blank? && !some_roles_present
    errors.add(:base, 'no attribution metadata')
  end
end

#some_roles_presentObject (protected)



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/models/attribution.rb', line 108

def some_roles_present
  ATTRIBUTION_ROLES.each do |r|
    return true if send("#{r}_roles".to_sym).any?
  end

  if self.roles.any?
    self.roles.each do |r|
      return true if r.type.present? && (r.person_id.present? || r.organization_id.present?)
    end
  end

  false
end