Class: ServiceDigitalAsset

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/service_digital_asset.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.array_field?(field_name) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'app/models/service_digital_asset.rb', line 105

def self.array_field?(field_name)
  columns_hash[field_name].sql_type == 'array'
end

.boolean_field?(field_name) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'app/models/service_digital_asset.rb', line 97

def self.boolean_field?(field_name)
  columns_hash[field_name].sql_type == 'boolean'
end

.default_value(field_name) ⇒ Object



109
110
111
# File 'app/models/service_digital_asset.rb', line 109

def self.default_value(field_name)
  column_defaults[field_name]
end

.time_field?(field_name) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'app/models/service_digital_asset.rb', line 101

def self.time_field?(field_name)
  %w(time datetime).include?(columns_hash[field_name].sql_type)
end

Instance Method Details

#as_hashObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/service_digital_asset.rb', line 77

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

#default_blank_time(time_field) ⇒ Object



92
93
94
95
# File 'app/models/service_digital_asset.rb', line 92

def default_blank_time(time_field)
  time = send(time_field.to_sym)
  time.blank? ? 10.years.ago : time
end

#delete?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'app/models/service_digital_asset.rb', line 68

def delete?
  !(unpublished_at.nil?) || expired?
end

#effective?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/models/service_digital_asset.rb', line 48

def effective?
  display_on_website  && !expires_at.blank?
end

#expired?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'app/models/service_digital_asset.rb', line 52

def expired?
  expires_at.nil? ||  expires_at < 1.minute.from_now
end

#file_nameObject



64
65
66
# File 'app/models/service_digital_asset.rb', line 64

def file_name
  path.split('/').last
end

#finra?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'app/models/service_digital_asset.rb', line 56

def finra?
  DigitalAsset::ContentType::FINRA == content_type
end

#manifest_file?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'app/models/service_digital_asset.rb', line 60

def manifest_file?
  path.match('\/manifest\/')
end

#mark_for_deletionObject



72
73
74
75
# File 'app/models/service_digital_asset.rb', line 72

def mark_for_deletion
  # not sure why unpublished_at = value didn't work in spec
  write_attribute(:unpublished_at, DateTime.now)
end

#validate_future_expirationObject



44
45
46
# File 'app/models/service_digital_asset.rb', line 44

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