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
# 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

  before_validation  :validate_is_cross_project
  validates_presence_of :user_id, :pinned_object_id, :pinned_object_type
  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 is_inserted?
    !is_inserted.blank?
  end

  def is_cross_project?
    !is_cross_project.blank?
  end

  protected

  def update_insertable
    if is_inserted?
      r = PinboardItem.where(project_id: project_id, pinned_object_type: pinned_object_type, user_id: user_id).where.not(id: id)
      if pinned_object_type == 'ControlledVocabularyTerm'
        n = pinned_object.class.name
        r.find_each do |i| 
          i.update(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
# 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

  before_validation  :validate_is_cross_project
  validates_presence_of :user_id, :pinned_object_id, :pinned_object_type
  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 is_inserted?
    !is_inserted.blank?
  end

  def is_cross_project?
    !is_cross_project.blank?
  end

  protected

  def update_insertable
    if is_inserted?
      r = PinboardItem.where(project_id: project_id, pinned_object_type: pinned_object_type, user_id: user_id).where.not(id: id)
      if pinned_object_type == 'ControlledVocabularyTerm'
        n = pinned_object.class.name
        r.find_each do |i| 
          i.update(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
# 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

  before_validation  :validate_is_cross_project
  validates_presence_of :user_id, :pinned_object_id, :pinned_object_type
  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 is_inserted?
    !is_inserted.blank?
  end

  def is_cross_project?
    !is_cross_project.blank?
  end

  protected

  def update_insertable
    if is_inserted?
      r = PinboardItem.where(project_id: project_id, pinned_object_type: pinned_object_type, user_id: user_id).where.not(id: id)
      if pinned_object_type == 'ControlledVocabularyTerm'
        n = pinned_object.class.name
        r.find_each do |i| 
          i.update(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
# 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

  before_validation  :validate_is_cross_project
  validates_presence_of :user_id, :pinned_object_id, :pinned_object_type
  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 is_inserted?
    !is_inserted.blank?
  end

  def is_cross_project?
    !is_cross_project.blank?
  end

  protected

  def update_insertable
    if is_inserted?
      r = PinboardItem.where(project_id: project_id, pinned_object_type: pinned_object_type, user_id: user_id).where.not(id: id)
      if pinned_object_type == 'ControlledVocabularyTerm'
        n = pinned_object.class.name
        r.find_each do |i| 
          i.update(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
# 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

  before_validation  :validate_is_cross_project
  validates_presence_of :user_id, :pinned_object_id, :pinned_object_type
  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 is_inserted?
    !is_inserted.blank?
  end

  def is_cross_project?
    !is_cross_project.blank?
  end

  protected

  def update_insertable
    if is_inserted?
      r = PinboardItem.where(project_id: project_id, pinned_object_type: pinned_object_type, user_id: user_id).where.not(id: id)
      if pinned_object_type == 'ControlledVocabularyTerm'
        n = pinned_object.class.name
        r.find_each do |i| 
          i.update(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
# 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

  before_validation  :validate_is_cross_project
  validates_presence_of :user_id, :pinned_object_id, :pinned_object_type
  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 is_inserted?
    !is_inserted.blank?
  end

  def is_cross_project?
    !is_cross_project.blank?
  end

  protected

  def update_insertable
    if is_inserted?
      r = PinboardItem.where(project_id: project_id, pinned_object_type: pinned_object_type, user_id: user_id).where.not(id: id)
      if pinned_object_type == 'ControlledVocabularyTerm'
        n = pinned_object.class.name
        r.find_each do |i| 
          i.update(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
# 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

  before_validation  :validate_is_cross_project
  validates_presence_of :user_id, :pinned_object_id, :pinned_object_type
  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 is_inserted?
    !is_inserted.blank?
  end

  def is_cross_project?
    !is_cross_project.blank?
  end

  protected

  def update_insertable
    if is_inserted?
      r = PinboardItem.where(project_id: project_id, pinned_object_type: pinned_object_type, user_id: user_id).where.not(id: id)
      if pinned_object_type == 'ControlledVocabularyTerm'
        n = pinned_object.class.name
        r.find_each do |i| 
          i.update(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
# 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

  before_validation  :validate_is_cross_project
  validates_presence_of :user_id, :pinned_object_id, :pinned_object_type
  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 is_inserted?
    !is_inserted.blank?
  end

  def is_cross_project?
    !is_cross_project.blank?
  end

  protected

  def update_insertable
    if is_inserted?
      r = PinboardItem.where(project_id: project_id, pinned_object_type: pinned_object_type, user_id: user_id).where.not(id: id)
      if pinned_object_type == 'ControlledVocabularyTerm'
        n = pinned_object.class.name
        r.find_each do |i| 
          i.update(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

.reorder_by(pinboard_item_ids) ⇒ Object


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

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)

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

def is_cross_project?
  !is_cross_project.blank?
end

#is_inserted?Boolean

Returns:

  • (Boolean)

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

def is_inserted?
  !is_inserted.blank?
end

#update_insertableObject (protected)


67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/pinboard_item.rb', line 67

def update_insertable
  if is_inserted?
    r = PinboardItem.where(project_id: project_id, pinned_object_type: pinned_object_type, user_id: user_id).where.not(id: id)
    if pinned_object_type == 'ControlledVocabularyTerm'
      n = pinned_object.class.name
      r.find_each do |i| 
        i.update(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)


81
82
83
# File 'app/models/pinboard_item.rb', line 81

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