Class: PinboardItem

Inherits:
ApplicationRecord show all
Includes:
Housekeeping
Defined in:
app/models/pinboard_item.rb

Overview

A PinboardItem is an object on a user’s pinboard.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#inserted_countInteger

Returns (not implemented) - the number of times this item has been inserted.

Returns:

  • (Integer)

    (not implemented) - the number of times this item has been inserted



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
# File 'app/models/pinboard_item.rb', line 35

class PinboardItem < ApplicationRecord
  include Housekeeping

  acts_as_list scope: [:project_id, :pinned_object_type, :user_id]

  belongs_to :user
  belongs_to :pinned_object, polymorphic: true, validate: true, inverse_of: :pinboard_items

  before_validation  :validate_is_cross_project

  validates_presence_of :user_id, :pinned_object
  validates_uniqueness_of :user_id, scope: [ :pinned_object_id, :pinned_object_type, :project_id ]

  after_save :update_insertable

  scope :for_object, -> (object) { where(pinned_object_id: object.id, pinned_object_type: object.class.to_s) }

  def self.reorder_by(pinboard_item_ids)
    pinboard_item_ids.each_with_index do |id, i|
      PinboardItem.find(id).update_attribute(:position, i)
    end
  end

  def self.clear(klass: nil, user_id: nil, project_id: nil)
    PinboardItem.where(user_id:, project_id:, pinned_object_type: klass).delete_all
  end

  def is_inserted?
    is_inserted.present?
  end

  def is_cross_project?
    is_cross_project.present?
  end

  protected

  def update_insertable
    if is_inserted?
      r = PinboardItem.where(project_id:, pinned_object_type:, user_id:).where.not(id:)
      if pinned_object_type == 'ControlledVocabularyTerm'
        n = pinned_object.class.name
        r.each do |i|
          i.update_column(:is_inserted, false) if i.pinned_object.type == n
        end
      else
        r.update_all(is_inserted: false)
      end
    end
  end

  def validate_is_cross_project
    errors.add(:is_cross_project, 'model is not a cross project model') if is_cross_project? && !pinned_object.class.ancestors.map(&:name).include?('Shared::SharedAcrossProjects')
  end

end

#is_cross_projectBoolean

Returns pinboard item will show up regardless of which project is selected.

Returns:

  • (Boolean)

    pinboard item will show up regardless of which project is selected



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
# File 'app/models/pinboard_item.rb', line 35

class PinboardItem < ApplicationRecord
  include Housekeeping

  acts_as_list scope: [:project_id, :pinned_object_type, :user_id]

  belongs_to :user
  belongs_to :pinned_object, polymorphic: true, validate: true, inverse_of: :pinboard_items

  before_validation  :validate_is_cross_project

  validates_presence_of :user_id, :pinned_object
  validates_uniqueness_of :user_id, scope: [ :pinned_object_id, :pinned_object_type, :project_id ]

  after_save :update_insertable

  scope :for_object, -> (object) { where(pinned_object_id: object.id, pinned_object_type: object.class.to_s) }

  def self.reorder_by(pinboard_item_ids)
    pinboard_item_ids.each_with_index do |id, i|
      PinboardItem.find(id).update_attribute(:position, i)
    end
  end

  def self.clear(klass: nil, user_id: nil, project_id: nil)
    PinboardItem.where(user_id:, project_id:, pinned_object_type: klass).delete_all
  end

  def is_inserted?
    is_inserted.present?
  end

  def is_cross_project?
    is_cross_project.present?
  end

  protected

  def update_insertable
    if is_inserted?
      r = PinboardItem.where(project_id:, pinned_object_type:, user_id:).where.not(id:)
      if pinned_object_type == 'ControlledVocabularyTerm'
        n = pinned_object.class.name
        r.each do |i|
          i.update_column(:is_inserted, false) if i.pinned_object.type == n
        end
      else
        r.update_all(is_inserted: false)
      end
    end
  end

  def validate_is_cross_project
    errors.add(:is_cross_project, 'model is not a cross project model') if is_cross_project? && !pinned_object.class.ancestors.map(&:name).include?('Shared::SharedAcrossProjects')
  end

end

#is_insertedBoolean

Returns when true this pinboard item is automatically inserted into accepting form fields.

Returns:

  • (Boolean)

    when true this pinboard item is automatically inserted into accepting form fields



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
# File 'app/models/pinboard_item.rb', line 35

class PinboardItem < ApplicationRecord
  include Housekeeping

  acts_as_list scope: [:project_id, :pinned_object_type, :user_id]

  belongs_to :user
  belongs_to :pinned_object, polymorphic: true, validate: true, inverse_of: :pinboard_items

  before_validation  :validate_is_cross_project

  validates_presence_of :user_id, :pinned_object
  validates_uniqueness_of :user_id, scope: [ :pinned_object_id, :pinned_object_type, :project_id ]

  after_save :update_insertable

  scope :for_object, -> (object) { where(pinned_object_id: object.id, pinned_object_type: object.class.to_s) }

  def self.reorder_by(pinboard_item_ids)
    pinboard_item_ids.each_with_index do |id, i|
      PinboardItem.find(id).update_attribute(:position, i)
    end
  end

  def self.clear(klass: nil, user_id: nil, project_id: nil)
    PinboardItem.where(user_id:, project_id:, pinned_object_type: klass).delete_all
  end

  def is_inserted?
    is_inserted.present?
  end

  def is_cross_project?
    is_cross_project.present?
  end

  protected

  def update_insertable
    if is_inserted?
      r = PinboardItem.where(project_id:, pinned_object_type:, user_id:).where.not(id:)
      if pinned_object_type == 'ControlledVocabularyTerm'
        n = pinned_object.class.name
        r.each do |i|
          i.update_column(:is_inserted, false) if i.pinned_object.type == n
        end
      else
        r.update_all(is_inserted: false)
      end
    end
  end

  def validate_is_cross_project
    errors.add(:is_cross_project, 'model is not a cross project model') if is_cross_project? && !pinned_object.class.ancestors.map(&:name).include?('Shared::SharedAcrossProjects')
  end

end

#pinned_object_idInteger

Returns the id of the object being pinned.

Returns:

  • (Integer)

    the id of the object being pinned



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
# File 'app/models/pinboard_item.rb', line 35

class PinboardItem < ApplicationRecord
  include Housekeeping

  acts_as_list scope: [:project_id, :pinned_object_type, :user_id]

  belongs_to :user
  belongs_to :pinned_object, polymorphic: true, validate: true, inverse_of: :pinboard_items

  before_validation  :validate_is_cross_project

  validates_presence_of :user_id, :pinned_object
  validates_uniqueness_of :user_id, scope: [ :pinned_object_id, :pinned_object_type, :project_id ]

  after_save :update_insertable

  scope :for_object, -> (object) { where(pinned_object_id: object.id, pinned_object_type: object.class.to_s) }

  def self.reorder_by(pinboard_item_ids)
    pinboard_item_ids.each_with_index do |id, i|
      PinboardItem.find(id).update_attribute(:position, i)
    end
  end

  def self.clear(klass: nil, user_id: nil, project_id: nil)
    PinboardItem.where(user_id:, project_id:, pinned_object_type: klass).delete_all
  end

  def is_inserted?
    is_inserted.present?
  end

  def is_cross_project?
    is_cross_project.present?
  end

  protected

  def update_insertable
    if is_inserted?
      r = PinboardItem.where(project_id:, pinned_object_type:, user_id:).where.not(id:)
      if pinned_object_type == 'ControlledVocabularyTerm'
        n = pinned_object.class.name
        r.each do |i|
          i.update_column(:is_inserted, false) if i.pinned_object.type == n
        end
      else
        r.update_all(is_inserted: false)
      end
    end
  end

  def validate_is_cross_project
    errors.add(:is_cross_project, 'model is not a cross project model') if is_cross_project? && !pinned_object.class.ancestors.map(&:name).include?('Shared::SharedAcrossProjects')
  end

end

#pinned_object_typeString

Returns the type of the object being pinned.

Returns:

  • (String)

    the type of the object being pinned



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
# File 'app/models/pinboard_item.rb', line 35

class PinboardItem < ApplicationRecord
  include Housekeeping

  acts_as_list scope: [:project_id, :pinned_object_type, :user_id]

  belongs_to :user
  belongs_to :pinned_object, polymorphic: true, validate: true, inverse_of: :pinboard_items

  before_validation  :validate_is_cross_project

  validates_presence_of :user_id, :pinned_object
  validates_uniqueness_of :user_id, scope: [ :pinned_object_id, :pinned_object_type, :project_id ]

  after_save :update_insertable

  scope :for_object, -> (object) { where(pinned_object_id: object.id, pinned_object_type: object.class.to_s) }

  def self.reorder_by(pinboard_item_ids)
    pinboard_item_ids.each_with_index do |id, i|
      PinboardItem.find(id).update_attribute(:position, i)
    end
  end

  def self.clear(klass: nil, user_id: nil, project_id: nil)
    PinboardItem.where(user_id:, project_id:, pinned_object_type: klass).delete_all
  end

  def is_inserted?
    is_inserted.present?
  end

  def is_cross_project?
    is_cross_project.present?
  end

  protected

  def update_insertable
    if is_inserted?
      r = PinboardItem.where(project_id:, pinned_object_type:, user_id:).where.not(id:)
      if pinned_object_type == 'ControlledVocabularyTerm'
        n = pinned_object.class.name
        r.each do |i|
          i.update_column(:is_inserted, false) if i.pinned_object.type == n
        end
      else
        r.update_all(is_inserted: false)
      end
    end
  end

  def validate_is_cross_project
    errors.add(:is_cross_project, 'model is not a cross project model') if is_cross_project? && !pinned_object.class.ancestors.map(&:name).include?('Shared::SharedAcrossProjects')
  end

end

#positionInteger

Returns the relative position of this object, nests within object type.

Returns:

  • (Integer)

    the relative position of this object, nests within object type



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
# File 'app/models/pinboard_item.rb', line 35

class PinboardItem < ApplicationRecord
  include Housekeeping

  acts_as_list scope: [:project_id, :pinned_object_type, :user_id]

  belongs_to :user
  belongs_to :pinned_object, polymorphic: true, validate: true, inverse_of: :pinboard_items

  before_validation  :validate_is_cross_project

  validates_presence_of :user_id, :pinned_object
  validates_uniqueness_of :user_id, scope: [ :pinned_object_id, :pinned_object_type, :project_id ]

  after_save :update_insertable

  scope :for_object, -> (object) { where(pinned_object_id: object.id, pinned_object_type: object.class.to_s) }

  def self.reorder_by(pinboard_item_ids)
    pinboard_item_ids.each_with_index do |id, i|
      PinboardItem.find(id).update_attribute(:position, i)
    end
  end

  def self.clear(klass: nil, user_id: nil, project_id: nil)
    PinboardItem.where(user_id:, project_id:, pinned_object_type: klass).delete_all
  end

  def is_inserted?
    is_inserted.present?
  end

  def is_cross_project?
    is_cross_project.present?
  end

  protected

  def update_insertable
    if is_inserted?
      r = PinboardItem.where(project_id:, pinned_object_type:, user_id:).where.not(id:)
      if pinned_object_type == 'ControlledVocabularyTerm'
        n = pinned_object.class.name
        r.each do |i|
          i.update_column(:is_inserted, false) if i.pinned_object.type == n
        end
      else
        r.update_all(is_inserted: false)
      end
    end
  end

  def validate_is_cross_project
    errors.add(:is_cross_project, 'model is not a cross project model') if is_cross_project? && !pinned_object.class.ancestors.map(&:name).include?('Shared::SharedAcrossProjects')
  end

end

#project_idInteger

the project ID for which the object is pinned

Returns:

  • (Integer)


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
# File 'app/models/pinboard_item.rb', line 35

class PinboardItem < ApplicationRecord
  include Housekeeping

  acts_as_list scope: [:project_id, :pinned_object_type, :user_id]

  belongs_to :user
  belongs_to :pinned_object, polymorphic: true, validate: true, inverse_of: :pinboard_items

  before_validation  :validate_is_cross_project

  validates_presence_of :user_id, :pinned_object
  validates_uniqueness_of :user_id, scope: [ :pinned_object_id, :pinned_object_type, :project_id ]

  after_save :update_insertable

  scope :for_object, -> (object) { where(pinned_object_id: object.id, pinned_object_type: object.class.to_s) }

  def self.reorder_by(pinboard_item_ids)
    pinboard_item_ids.each_with_index do |id, i|
      PinboardItem.find(id).update_attribute(:position, i)
    end
  end

  def self.clear(klass: nil, user_id: nil, project_id: nil)
    PinboardItem.where(user_id:, project_id:, pinned_object_type: klass).delete_all
  end

  def is_inserted?
    is_inserted.present?
  end

  def is_cross_project?
    is_cross_project.present?
  end

  protected

  def update_insertable
    if is_inserted?
      r = PinboardItem.where(project_id:, pinned_object_type:, user_id:).where.not(id:)
      if pinned_object_type == 'ControlledVocabularyTerm'
        n = pinned_object.class.name
        r.each do |i|
          i.update_column(:is_inserted, false) if i.pinned_object.type == n
        end
      else
        r.update_all(is_inserted: false)
      end
    end
  end

  def validate_is_cross_project
    errors.add(:is_cross_project, 'model is not a cross project model') if is_cross_project? && !pinned_object.class.ancestors.map(&:name).include?('Shared::SharedAcrossProjects')
  end

end

#user_idInteger

Returns this identifies the pinboard.

Returns:

  • (Integer)

    this identifies the pinboard



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
# File 'app/models/pinboard_item.rb', line 35

class PinboardItem < ApplicationRecord
  include Housekeeping

  acts_as_list scope: [:project_id, :pinned_object_type, :user_id]

  belongs_to :user
  belongs_to :pinned_object, polymorphic: true, validate: true, inverse_of: :pinboard_items

  before_validation  :validate_is_cross_project

  validates_presence_of :user_id, :pinned_object
  validates_uniqueness_of :user_id, scope: [ :pinned_object_id, :pinned_object_type, :project_id ]

  after_save :update_insertable

  scope :for_object, -> (object) { where(pinned_object_id: object.id, pinned_object_type: object.class.to_s) }

  def self.reorder_by(pinboard_item_ids)
    pinboard_item_ids.each_with_index do |id, i|
      PinboardItem.find(id).update_attribute(:position, i)
    end
  end

  def self.clear(klass: nil, user_id: nil, project_id: nil)
    PinboardItem.where(user_id:, project_id:, pinned_object_type: klass).delete_all
  end

  def is_inserted?
    is_inserted.present?
  end

  def is_cross_project?
    is_cross_project.present?
  end

  protected

  def update_insertable
    if is_inserted?
      r = PinboardItem.where(project_id:, pinned_object_type:, user_id:).where.not(id:)
      if pinned_object_type == 'ControlledVocabularyTerm'
        n = pinned_object.class.name
        r.each do |i|
          i.update_column(:is_inserted, false) if i.pinned_object.type == n
        end
      else
        r.update_all(is_inserted: false)
      end
    end
  end

  def validate_is_cross_project
    errors.add(:is_cross_project, 'model is not a cross project model') if is_cross_project? && !pinned_object.class.ancestors.map(&:name).include?('Shared::SharedAcrossProjects')
  end

end

Class Method Details

.clear(klass: nil, user_id: nil, project_id: nil) ⇒ Object



58
59
60
# File 'app/models/pinboard_item.rb', line 58

def self.clear(klass: nil, user_id: nil, project_id: nil)
  PinboardItem.where(user_id:, project_id:, pinned_object_type: klass).delete_all
end

.reorder_by(pinboard_item_ids) ⇒ Object



52
53
54
55
56
# File 'app/models/pinboard_item.rb', line 52

def self.reorder_by(pinboard_item_ids)
  pinboard_item_ids.each_with_index do |id, i|
    PinboardItem.find(id).update_attribute(:position, i)
  end
end

Instance Method Details

#is_cross_project?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'app/models/pinboard_item.rb', line 66

def is_cross_project?
  is_cross_project.present?
end

#is_inserted?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'app/models/pinboard_item.rb', line 62

def is_inserted?
  is_inserted.present?
end

#update_insertableObject (protected)



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/pinboard_item.rb', line 72

def update_insertable
  if is_inserted?
    r = PinboardItem.where(project_id:, pinned_object_type:, user_id:).where.not(id:)
    if pinned_object_type == 'ControlledVocabularyTerm'
      n = pinned_object.class.name
      r.each do |i|
        i.update_column(:is_inserted, false) if i.pinned_object.type == n
      end
    else
      r.update_all(is_inserted: false)
    end
  end
end

#validate_is_cross_projectObject (protected)



86
87
88
# File 'app/models/pinboard_item.rb', line 86

def validate_is_cross_project
  errors.add(:is_cross_project, 'model is not a cross project model') if is_cross_project? && !pinned_object.class.ancestors.map(&:name).include?('Shared::SharedAcrossProjects')
end