Class: PinboardItem
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- PinboardItem
- Includes:
- Housekeeping
- Defined in:
- app/models/pinboard_item.rb
Overview
A PinboardItem is an object on a user’s pinboard.
Instance Attribute Summary collapse
-
#inserted_count ⇒ Integer
(not implemented) - the number of times this item has been inserted.
-
#is_cross_project ⇒ Boolean
Pinboard item will show up regardless of which project is selected.
-
#is_inserted ⇒ Boolean
When true this pinboard item is automatically inserted into accepting form fields.
-
#pinned_object_id ⇒ Integer
The id of the object being pinned.
-
#pinned_object_type ⇒ String
The type of the object being pinned.
-
#position ⇒ Integer
The relative position of this object, nests within object type.
-
#project_id ⇒ Integer
the project ID for which the object is pinned.
-
#user_id ⇒ Integer
This identifies the pinboard.
Class Method Summary collapse
Instance Method Summary collapse
- #is_cross_project? ⇒ Boolean
- #is_inserted? ⇒ Boolean
- #update_insertable ⇒ Object protected
- #validate_is_cross_project ⇒ Object protected
Methods included from Housekeeping
#has_polymorphic_relationship?
Methods inherited from ApplicationRecord
Instance Attribute Details
#inserted_count ⇒ Integer
Returns (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_project ⇒ Boolean
Returns 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_inserted ⇒ Boolean
Returns 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_id ⇒ Integer
Returns 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_type ⇒ String
Returns 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 |
#position ⇒ Integer
Returns 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_id ⇒ Integer
the project ID for which the object is 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 |
#user_id ⇒ Integer
Returns 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
66 67 68 |
# File 'app/models/pinboard_item.rb', line 66 def is_cross_project? is_cross_project.present? end |
#is_inserted? ⇒ Boolean
62 63 64 |
# File 'app/models/pinboard_item.rb', line 62 def is_inserted? is_inserted.present? end |
#update_insertable ⇒ Object (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_project ⇒ Object (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 |