app/models/spotlight/exhibit.rb in blacklight-spotlight-0.2.0 vs app/models/spotlight/exhibit.rb in blacklight-spotlight-0.3.0
- old
+ new
@@ -8,10 +8,11 @@
has_many :searches, dependent: :destroy, extend: FriendlyId::FinderMethods
has_many :pages, dependent: :destroy
has_many :about_pages, extend: FriendlyId::FinderMethods
has_many :feature_pages, extend: FriendlyId::FinderMethods
has_one :home_page
+ has_one :masthead
has_many :users, through: :roles, class_name: '::User'
has_many :custom_fields, dependent: :delete_all
has_many :contacts, dependent: :delete_all # These are the contacts who appear in the sidebar
has_many :contact_emails, dependent: :delete_all # These are the contacts who get "Contact us" emails
@@ -28,10 +29,11 @@
accepts_nested_attributes_for :blacklight_configuration, update_only: true
accepts_nested_attributes_for :searches
accepts_nested_attributes_for :about_pages
accepts_nested_attributes_for :feature_pages
accepts_nested_attributes_for :home_page, update_only: true
+ accepts_nested_attributes_for :masthead, update_only: true
accepts_nested_attributes_for :main_navigations
accepts_nested_attributes_for :contacts
accepts_nested_attributes_for :contact_emails, reject_if: proc {|attr| attr['email'].blank?}
accepts_nested_attributes_for :roles, allow_destroy: true, reject_if: proc {|attr| attr['user_key'].blank?}
accepts_nested_attributes_for :custom_fields
@@ -45,10 +47,12 @@
after_create :initialize_config
after_create :initialize_browse
after_create :initialize_main_navigation
before_save :sanitize_description
+ mount_uploader :featured_image, Spotlight::FeaturedImageUploader
+
after_destroy do
# Touch the default exhibit to ensure caching knows that
# the exhibits have changed.
Spotlight::Exhibit.default.touch
end
@@ -76,26 +80,41 @@
def to_s
title
end
def import hash
- # remove the default browse category -- it might be in the import
- # and we don't want to have a conflicting slug
- if persisted? and hash.fetch("searches_attributes", []).any? { |x| x["slug"] == "all-exhibit-items"}
- searches.where(slug: "all-exhibit-items").destroy_all
- reload
- end
+ Spotlight::ExhibitExportSerializer.prepare(self).from_hash(hash)
+ self
+ end
- if hash["owned_taggings_attributes"]
- hash["owned_taggings_attributes"].each do |tagging|
- tag = tagging.delete "tag_attributes"
- tagging["context"] = "tags"
- tagging["tag"] = ActsAsTaggableOn::Tag.find_or_create_by name: tag["name"]
- end
+ def solr_data
+ { :"#{Spotlight::Engine.config.solr_fields.prefix}spotlight_exhibit_slug_#{slug}#{Spotlight::Engine.config.solr_fields.boolean_suffix}" => true }
+ end
+
+ def appearance
+ Spotlight::Appearance.new(blacklight_configuration)
+ end
+
+ def analytics start_date = 1.month, path = nil
+ return OpenStruct.new unless analytics_provider and analytics_provider.enabled?
+ @analytics ||= {}
+ @analytics[start_date] ||= begin
+ analytics_provider.exhibit_data(path || self, start_date: start_date.ago)
end
+ end
+
+ def page_analytics start_date = 1.month, path = nil
+ return [] unless analytics_provider and analytics_provider.enabled?
+
+ @page_analytics ||= {}
+ @page_analytics[start_date] ||= begin
+ analytics_provider.page_data(path || self, start_date: start_date.ago)
+ end
+ end
- update hash
+ def reindex_later
+ Spotlight::ReindexJob.perform_later(self)
end
protected
def initialize_config
@@ -115,13 +134,17 @@
self.main_navigations.create nav_type: nav_type, weight: weight
end
end
def sanitize_description
- self.description = HTML::FullSanitizer.new.sanitize(description) if description_changed?
+ self.description = ::Rails::Html::FullSanitizer.new.sanitize(description) if description_changed?
end
def default_main_navigations
- [:curated_features, :browse, :about]
+ Spotlight::Engine.config.exhibit_main_navigation.dup
+ end
+
+ def analytics_provider
+ Spotlight::Engine.config.analytics_provider
end
end