require 'time' require 'active_record' require 'activerecord-tableless' class ServiceDigitalAsset < ActiveRecord::Base has_no_table column :title, :string column :changed_at, :time column :audiences, :array, [] column :sami_code, :string column :product_ids, :array, [] column :published_at, :time column :unpublished_at, :time column :expires_at, :time column :guid, :string column :business_owner, :string column :summary, :string column :content_organization_ids, :array, [] column :program_ids, :array, [] column :omniture_codes, :array, [] column :orderable, :boolean, false # refactor version: column :path, :string column :legacy_path, :string column :doc_changed_at, :time column :content_type, :string column :pages, :integer, 0 column :size, :string column :mime_type, :string column :subject, :string column :keywords, :array, [] column :author, :string column :finra_path, :string column :fund_codes, :array, [] column :display_on_website, :boolean column :digital_asset_id, :string validates_presence_of :digital_asset_id, :title, :changed_at, :published_at, :expires_at, :audiences, :path validate :validate_future_expiration def validate_future_expiration errors.add(:expires_at, 'Expiration date must be at least 1 minute from now') unless expires_at and expires_at > 1.minute.from_now end def effective? display_on_website && !expires_at.blank? end def expired? expires_at.nil? || expires_at < 1.minute.from_now end def finra? DigitalAsset::ContentType::FINRA == content_type end def manifest_file? path.match('\/manifest\/') end def file_name path.split('/').last end def delete? !(unpublished_at.nil?) || expired? end def mark_for_deletion # not sure why unpublished_at = value didn't work in spec write_attribute(:unpublished_at, DateTime.now) end def as_hash hash = attributes # remove the file attribute fields that have default values # as the service won't update them if they aren't in the data being sent over %w(pages size mime_type subject keywords author).each do |field_name| hash.delete(field_name) if hash[field_name] == ServiceDigitalAsset.default_value(field_name) end # convert the times to strings unless they are nil %w(changed_at published_at unpublished_at expires_at doc_changed_at).each do |field_name| hash[field_name] = hash[field_name].to_s unless hash[field_name].nil? end # service expects 'digital_asset' not 'service_digital_asset' {'digital_asset' => hash} end def default_blank_time(time_field, default_time = 10.years.ago) time = send(time_field.to_sym) time.blank? ? default_time : time end def changed_since(date) !changed_at.blank? && changed_at.to_i >= date.to_i end def self.boolean_field?(field_name) columns_hash[field_name].sql_type == 'boolean' end def self.time_field?(field_name) %w(time datetime).include?(columns_hash[field_name].sql_type) end def self.array_field?(field_name) columns_hash[field_name].sql_type == 'array' end def self.default_value(field_name) column_defaults[field_name] end end