Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Container
- include Mongoid::Document
- include Mongoid::Timestamps
- # include Mongoid::Versioning
- include Mongoid::Userstamp
- # include VersionExtras
- # max_versions 5
- ACCEPTED_MODELS = %w[
- Article Page Film LinkItem CollectionEntry
- ].map(&:freeze).freeze
- field :name, type: String
- field :template_tag, type: String
- field :description, type: String
- field :url, type: String
- field :allowed_models, type: Array, default: ACCEPTED_MODELS
- field :hash_string, type: String, default: "{}"
- index({ name: 1 })
- index({ template_tag: 1 })
- belongs_to :category
- has_and_belongs_to_many :articles
- has_and_belongs_to_many :films
- has_and_belongs_to_many :pages
- has_and_belongs_to_many :link_items
- has_and_belongs_to_many :collection_entries
- validate :validate_accepted_models
- validates :name, presence: true
- before_save :build_associations
- before_validation -> { allowed_models.reject!(&:blank?) }
- after_save :clear_cache
- # Build the associations from the hash_string
- def build_associations
- parsed_hash_string.flat_map {|i| i[:klass] }.uniq.each do |klass|
- ids_array = parsed_hash_string.find_all { |i| i[:klass] == klass}
- # Pull docs into array
- documents = klass.constantize.where(id: ids_array)
- # Clear then add the record's associations
- self.send(klass.tableize).substitute(documents)
- end
- end
- # Used as callback from foreign association. Cleans up hashes.
- def self.remove_associations_from_hash(item)
- item.containers.each do |container|
- new_hash = container.parsed_hash_string
- new_hash.delete_if{|o| o[:id] == item.id.to_s}
- container.hash_string = new_hash
- container.save!
- end
- end
- # Array of containing objects
- def contents
- parsed_hash_string.map { |i| i[:klass].constantize.find(i[:id]) }
- end
- def get_items
- contents.reject do |item|
- (item.class == Article && item.is_scheduled? && !@user_is_admin)
- end
- end
- # CACHES
- # Clear attached caches
- def clear_cache
- puts "CLEARING #{template_tag}"
- Rails.cache.delete("containers/#{template_tag}")
- case template_tag
- when "homeCarouselHighlights", "moduleCallSheet"
- Rails.cache.delete("views/action/home")
- Rails.cache.delete("views/action/home/admin")
- when "catpopular"
- Rails.cache.delete("views/super_nav")
- when "festCarouselHighlights"
- Rails.cache.delete("views/page/fest_home")
- Rails.cache.delete("views/page/fest_home_admin")
- when "todayAtTribeca"
- Rails.cache.delete("views/module_last_call")
- when "audienceAwardNarrative", 'audienceAwardDocs'
- @page = Page.find_by(title: 'Audience Award');
- Rails.cache.delete("pages/#{@page.id}/#{@page.updated_at.to_i}") if @page
- end
- end
- # clear any caches associated w/ this content
- def self.clear_content_caches(item)
- item.containers.each(&:clear_cache)
- end
- # clear all caches
- def self.flush_all_caches
- Container.all.each(&:clear_cache)
- end
- private
- def validate_accepted_models
- return if allowed_models.all? { |m| ACCEPTED_MODELS.include?(m) }
- errors.add(:allowed_models, "contains invalid models")
- end
- def parsed_hash_string
- JSON.parse(hash_string, symbolize_names: true)
- end
- end
Add Comment
Please, Sign In to add comment