app/models/spotlight/exhibit.rb in blacklight-spotlight-0.0.3 vs app/models/spotlight/exhibit.rb in blacklight-spotlight-0.1.0
- old
+ new
@@ -1,82 +1,111 @@
require 'mail'
class Spotlight::Exhibit < ActiveRecord::Base
- DEFAULT = 'default'.freeze
- has_many :roles
- has_many :searches
- has_many :about_pages
- has_many :feature_pages
+
+ extend FriendlyId
+ friendly_id :title, use: [:slugged,:finders]
+
+ # friendly id associations need to be 'destroy'ed to reap the slug history
+ 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_many :home_pages
+
has_many :users, through: :roles, class_name: '::User'
- has_many :custom_fields
- has_many :contacts
- has_one :blacklight_configuration, class_name: Spotlight::BlacklightConfiguration
+ 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
+ has_many :main_navigations, dependent: :delete_all
+ has_many :solr_document_sidecars, dependent: :delete_all
+ has_many :roles, dependent: :delete_all
+ has_many :attachments, dependent: :destroy
- accepts_nested_attributes_for :blacklight_configuration
+ has_one :blacklight_configuration, class_name: Spotlight::BlacklightConfiguration, dependent: :delete
+ has_many :resources
+
+ accepts_nested_attributes_for :solr_document_sidecars
+ accepts_nested_attributes_for :attachments
+ 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 :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
+ accepts_nested_attributes_for :attachments
+
delegate :blacklight_config, to: :blacklight_configuration
serialize :facets, Array
- serialize :contact_emails, Array
- after_create :add_default_home_page
+ before_create :build_home_page
+ after_create :initialize_config
+ after_create :initialize_browse
+ after_create :initialize_main_navigation
before_save :sanitize_description
- validate :name, :title, presence: true
- validate :valid_emails
+
+ validate :title, presence: true
acts_as_tagger
- # This is necessary so the form will draw as if we have nested attributes (fields_for).
- def contact_emails
- super.each do |e|
- def e.persisted?
- false
- end
- end
- end
-
- # The attributes setter is required so the form will draw as if we have nested attributes (fields_for)
- def contact_emails_attributes=(emails)
- attributes_collection = emails.is_a?(Hash) ? emails.values : emails
- self.contact_emails = attributes_collection.map {|e| e['email']}.reject(&:blank?)
- end
-
def main_about_page
@main_about_page ||= about_pages.published.first
end
# Find or create the default exhibit
def self.default
- self.find_or_create_by!(name: DEFAULT) do |e|
+ self.find_or_create_by!(default: true) do |e|
e.title = 'Default exhibit'.freeze
- e.blacklight_configuration = Spotlight::BlacklightConfiguration.create!
end
end
def has_browse_categories?
searches.published.any?
end
- protected
+ def to_s
+ title
+ end
- def valid_emails
- contact_emails.each do |email|
- begin
- parsed = Mail::Address.new(email)
- rescue Mail::Field::ParseError => e
- end
- errors.add :contact_emails, "#{email} is not valid" unless !parsed.nil? && parsed.address == email && parsed.local != email #cannot be a local address
+ 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?
+ searches.where(title: "All Exhibit Items").destroy_all
+ reload
end
+ update hash
end
- def add_default_home_page
- Spotlight::HomePage.create(exhibit: self).save
+ protected
+
+ def initialize_config
+ self.blacklight_configuration ||= Spotlight::BlacklightConfiguration.create!
end
+ def initialize_browse
+ return unless self.searches.blank?
+
+ self.searches.create title: "All Exhibit Items",
+ short_description: "Search results for all items in this exhibit",
+ long_description: "All items in this exhibit."
+ end
+
+ def initialize_main_navigation
+ self.default_main_navigations.each_with_index do |nav_type, weight|
+ 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?
end
+
+ def default_main_navigations
+ [:curated_features, :browse, :about]
+ end
+
end