Class: Image

Inherits:
ApplicationRecord show all
Defined in:
app/models/image.rb,
app/models/image.rb

Overview

Schema Information

Table name: images

id                 :integer          not null, primary key
path               :string
website_id         :integer          not null
context_id         :integer          not null
created_at         :datetime
updated_at         :datetime
canonical_id       :string
assignments_count  :integer          default(0), not null
descriptions_count :integer          default(0), not null
title              :text
priority           :boolean          default(FALSE), not null
status_code        :integer          default(0), not null
page_urls          :string           not null, is an Array

Indexes

index_images_on_context_id  (context_id)
index_images_on_website_id  (website_id)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.import(file) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/models/image.rb', line 154

def self.import(file)
  spreadsheet = open_spreadsheet(file)
  puts "YAML:"
  puts spreadsheet.to_yaml #DEBUG
  header = spreadsheet.row(1)
  (2..spreadsheet.last_row).each do |i|
    row = Hash[[header, spreadsheet.row(i)].transpose]
    image = find_by_id(row["id"]) || new
    #image.attributes = row.to_hash.slice(*accessible_attributes)
    image.attributes = row.to_hash
    image.save!
  end
end

.latest_timestampActiveSupport::TimeWithZone?

Returns:

  • (ActiveSupport::TimeWithZone)

    if one more images exist, this is the created_at time for the most recently-created image

  • (nil)

    if no images exist



62
63
64
# File 'app/models/image.rb', line 62

def self.latest_timestamp
  self.order(:created_at).last
end

.open_spreadsheet(file) ⇒ Object



168
169
170
171
172
173
174
175
# File 'app/models/image.rb', line 168

def self.open_spreadsheet(file)
  case File.extname(file.original_filename)
  when ".csv" then Roo::CSV.new(file.path)
  when ".xls" then Roo::Excel.new(file.path)
  when ".xlsx" then Roo::Excelx.new(file.pat)
  else raise "Unknown file type: #{file.original_filename}"
  end
end

.to_csv(options = {}) ⇒ Object



177
178
179
180
181
182
183
184
# File 'app/models/image.rb', line 177

def self.to_csv(options = {})
  CSV.generate(options) do |csv|
    csv << column_names
    all.each do |product|
      csv << product.attributes.values_at(*column_names)
    end
  end
end

Instance Method Details

#alt(status_ids = [2]) ⇒ Object

TODO are these both the most recent? c.f. apipie doc in images#index



63
64
65
66
67
68
69
70
# File 'app/models/image.rb', line 63

def alt(status_ids=[2])
  d = descriptions.where(metum_id: 1, status_id: status_ids, locale: "en").first
  if d
    d.text
  else
    ""
  end
end

#begun?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'app/models/image.rb', line 136

def begun?
  descriptions.begun.count > 0
end

#completed?Boolean

completed all meta in any combo of locales

Returns:

  • (Boolean)


145
146
147
148
149
150
151
152
# File 'app/models/image.rb', line 145

def completed?
  meta_ids = Metum.all.map{|m| m.id}
  if meta_ids.count == descriptions.approved.map{|d| d.metum_id unless d.nil?}.uniq.compact.count
    true
  else
    false
  end
end

#completed_by?(user) ⇒ Boolean

TODO consider locale has 1 of each metum by this user ? in any combo of locales and status'es

Returns:

  • (Boolean)


115
116
117
118
119
# File 'app/models/image.rb', line 115

def completed_by?(user)
  meta_ids = Metum.all.map{|m| m.id}
  described_meta_ids = descriptions.collect{ |d| d.metum_id if d.user_id == user.id}.compact 
  (meta_ids - described_meta_ids).empty?
end

#described_by?(user) ⇒ Boolean

has 1 of description by this user

Returns:

  • (Boolean)


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

def described_by?(user)
 descriptions.collect{ |d| d.user_id}.compact.include?(user.id)
end

#is_mca?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
87
# File 'app/models/image.rb', line 81

def is_mca?
  if website.url.include?("mcachicago")
    true
  else 
    false
  end
end

#long(status_ids = [2]) ⇒ Object



72
73
74
75
76
77
78
79
# File 'app/models/image.rb', line 72

def long(status_ids=[2])
  d = descriptions.where(metum_id: 3, status_id: status_ids, locale: "en").first
  if d
    d.text 
  else
    ""
  end
end

#ready_to_review?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'app/models/image.rb', line 140

def ready_to_review?
  descriptions.ready_to_review.count > 0
end

#statusObject



121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/image.rb', line 121

def status
  case status_code
  when 0
    "Not Described"
  when 1
    "Partially Completed"
  when 2
    "Ready to Review" 
  when 3
    "Completed" 
  else
    return "Not Described"
  end
end

#to_sObject



58
59
60
# File 'app/models/image.rb', line 58

def to_s
  path
end

#undescribed_by?(user) ⇒ Boolean

TODO per locale has no descriptions by this user

Returns:

  • (Boolean)


104
105
106
# File 'app/models/image.rb', line 104

def undescribed_by?(user)
  !descriptions.collect{ |d| d.user_id}.compact.include?(user.id)
end

#update_status_codeObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'app/models/image.rb', line 186

def update_status_code
  if begun? 
    if completed?
      self.status_code = 3
    elsif ready_to_review?
      self.status_code = 2
    else
      self.status_code = 1
    end
  else
    self.status_code = 0
  end
  return true
end

#url(protocol = "https:") ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/image.rb', line 90

def url(protocol="https:")
  if path.starts_with?("//")
    protocol + path
  elsif path.starts_with?("http")
    path
  elsif website
    website.url + path
  else 
    path
  end
end