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?

Instance Attribute Details

- (Integer) inserted_count

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

class PinboardItem < ApplicationRecord
  include Housekeeping

  acts_as_list scope: [:project_id, :pinned_object_type]

  belongs_to :user
  belongs_to :pinned_object, polymorphic: true

  before_validation :validate_is_inserted, :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 ]

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

  def self.reorder(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

  def set_as_insertable
    PinboardItem.where(project_id: project_id, pinned_object_type: pinned_object_type).find_each do |p|
      p.update(is_inserted: false)
    end
    update(is_inserted: true)
  end

  protected

  def validate_is_inserted
    errors.add(:is_inserted, 'only one item per type can be inserted automatically') if is_inserted? && PinboardItem.where(is_inserted: true, pinned_object_type: pinned_object.class.to_s, project_id: $project_id).reload.count > 0
  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

- (Boolean) is_cross_project

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

class PinboardItem < ApplicationRecord
  include Housekeeping

  acts_as_list scope: [:project_id, :pinned_object_type]

  belongs_to :user
  belongs_to :pinned_object, polymorphic: true

  before_validation :validate_is_inserted, :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 ]

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

  def self.reorder(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

  def set_as_insertable
    PinboardItem.where(project_id: project_id, pinned_object_type: pinned_object_type).find_each do |p|
      p.update(is_inserted: false)
    end
    update(is_inserted: true)
  end

  protected

  def validate_is_inserted
    errors.add(:is_inserted, 'only one item per type can be inserted automatically') if is_inserted? && PinboardItem.where(is_inserted: true, pinned_object_type: pinned_object.class.to_s, project_id: $project_id).reload.count > 0
  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

- (Boolean) is_inserted

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

class PinboardItem < ApplicationRecord
  include Housekeeping

  acts_as_list scope: [:project_id, :pinned_object_type]

  belongs_to :user
  belongs_to :pinned_object, polymorphic: true

  before_validation :validate_is_inserted, :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 ]

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

  def self.reorder(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

  def set_as_insertable
    PinboardItem.where(project_id: project_id, pinned_object_type: pinned_object_type).find_each do |p|
      p.update(is_inserted: false)
    end
    update(is_inserted: true)
  end

  protected

  def validate_is_inserted
    errors.add(:is_inserted, 'only one item per type can be inserted automatically') if is_inserted? && PinboardItem.where(is_inserted: true, pinned_object_type: pinned_object.class.to_s, project_id: $project_id).reload.count > 0
  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

- (Integer) pinned_object_id

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

class PinboardItem < ApplicationRecord
  include Housekeeping

  acts_as_list scope: [:project_id, :pinned_object_type]

  belongs_to :user
  belongs_to :pinned_object, polymorphic: true

  before_validation :validate_is_inserted, :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 ]

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

  def self.reorder(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

  def set_as_insertable
    PinboardItem.where(project_id: project_id, pinned_object_type: pinned_object_type).find_each do |p|
      p.update(is_inserted: false)
    end
    update(is_inserted: true)
  end

  protected

  def validate_is_inserted
    errors.add(:is_inserted, 'only one item per type can be inserted automatically') if is_inserted? && PinboardItem.where(is_inserted: true, pinned_object_type: pinned_object.class.to_s, project_id: $project_id).reload.count > 0
  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

- (String) pinned_object_type

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

class PinboardItem < ApplicationRecord
  include Housekeeping

  acts_as_list scope: [:project_id, :pinned_object_type]

  belongs_to :user
  belongs_to :pinned_object, polymorphic: true

  before_validation :validate_is_inserted, :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 ]

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

  def self.reorder(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

  def set_as_insertable
    PinboardItem.where(project_id: project_id, pinned_object_type: pinned_object_type).find_each do |p|
      p.update(is_inserted: false)
    end
    update(is_inserted: true)
  end

  protected

  def validate_is_inserted
    errors.add(:is_inserted, 'only one item per type can be inserted automatically') if is_inserted? && PinboardItem.where(is_inserted: true, pinned_object_type: pinned_object.class.to_s, project_id: $project_id).reload.count > 0
  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

- (Integer) position

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

class PinboardItem < ApplicationRecord
  include Housekeeping

  acts_as_list scope: [:project_id, :pinned_object_type]

  belongs_to :user
  belongs_to :pinned_object, polymorphic: true

  before_validation :validate_is_inserted, :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 ]

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

  def self.reorder(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

  def set_as_insertable
    PinboardItem.where(project_id: project_id, pinned_object_type: pinned_object_type).find_each do |p|
      p.update(is_inserted: false)
    end
    update(is_inserted: true)
  end

  protected

  def validate_is_inserted
    errors.add(:is_inserted, 'only one item per type can be inserted automatically') if is_inserted? && PinboardItem.where(is_inserted: true, pinned_object_type: pinned_object.class.to_s, project_id: $project_id).reload.count > 0
  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

- (Integer) project_id

the project ID

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

class PinboardItem < ApplicationRecord
  include Housekeeping

  acts_as_list scope: [:project_id, :pinned_object_type]

  belongs_to :user
  belongs_to :pinned_object, polymorphic: true

  before_validation :validate_is_inserted, :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 ]

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

  def self.reorder(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

  def set_as_insertable
    PinboardItem.where(project_id: project_id, pinned_object_type: pinned_object_type).find_each do |p|
      p.update(is_inserted: false)
    end
    update(is_inserted: true)
  end

  protected

  def validate_is_inserted
    errors.add(:is_inserted, 'only one item per type can be inserted automatically') if is_inserted? && PinboardItem.where(is_inserted: true, pinned_object_type: pinned_object.class.to_s, project_id: $project_id).reload.count > 0
  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

- (Integer) user_id

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

class PinboardItem < ApplicationRecord
  include Housekeeping

  acts_as_list scope: [:project_id, :pinned_object_type]

  belongs_to :user
  belongs_to :pinned_object, polymorphic: true

  before_validation :validate_is_inserted, :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 ]

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

  def self.reorder(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

  def set_as_insertable
    PinboardItem.where(project_id: project_id, pinned_object_type: pinned_object_type).find_each do |p|
      p.update(is_inserted: false)
    end
    update(is_inserted: true)
  end

  protected

  def validate_is_inserted
    errors.add(:is_inserted, 'only one item per type can be inserted automatically') if is_inserted? && PinboardItem.where(is_inserted: true, pinned_object_type: pinned_object.class.to_s, project_id: $project_id).reload.count > 0
  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

+ (Object) reorder(pinboard_item_ids)



49
50
51
52
53
# File 'app/models/pinboard_item.rb', line 49

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

Instance Method Details

- (Boolean) is_cross_project?

Returns:

  • (Boolean)


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

def is_cross_project?
  !is_cross_project.blank?
end

- (Boolean) is_inserted?

Returns:

  • (Boolean)


55
56
57
# File 'app/models/pinboard_item.rb', line 55

def is_inserted?
  !is_inserted.blank?
end

- (Object) set_as_insertable



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

def set_as_insertable
  PinboardItem.where(project_id: project_id, pinned_object_type: pinned_object_type).find_each do |p|
    p.update(is_inserted: false)
  end
  update(is_inserted: true)
end

- (Object) validate_is_cross_project (protected)



76
77
78
# File 'app/models/pinboard_item.rb', line 76

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

- (Object) validate_is_inserted (protected)



72
73
74
# File 'app/models/pinboard_item.rb', line 72

def validate_is_inserted
  errors.add(:is_inserted, 'only one item per type can be inserted automatically') if is_inserted? && PinboardItem.where(is_inserted: true, pinned_object_type: pinned_object.class.to_s, project_id: $project_id).reload.count > 0
end