Class: Lentil::Image

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

Overview

Schema Information

Table name: lentil_images

id                             :integer          not null, primary key
description                    :text
created_at                     :datetime         not null
updated_at                     :datetime         not null
like_votes_count               :integer          default(0)
url                            :string(255)
user_id                        :integer
state                          :integer          default(0)
external_identifier            :string(255)
long_url                       :string(255)
original_metadata              :text
original_datetime              :datetime
staff_like                     :boolean          default(FALSE)
moderator_id                   :integer
moderated_at                   :datetime
second_moderation              :boolean          default(FALSE)
wins_count                     :integer          default(0)
losses_count                   :integer          default(0)
win_pct                        :float
popular_score                  :integer          default(0)
file_harvested_date            :datetime
file_harvest_failed            :integer          default(0)
donor_agreement_submitted_date :datetime
donor_agreement_failed         :integer          default(0)
failed_file_checks             :integer          default(0)
file_last_checked              :datetime
donor_agreement_rejected       :datetime
do_not_request_donation        :boolean

Constant Summary collapse

States =
{
 :pending => 0,
 :approved => 1,
 :rejected => 2,
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.approvedObject



82
83
84
# File 'app/models/lentil/image.rb', line 82

def self.approved
  where(state: self::States[:approved]).where(:suppressed => false)
end

.approved_allObject



86
87
88
# File 'app/models/lentil/image.rb', line 86

def self.approved_all
  where(state: self::States[:approved])
end

.blendObject



90
91
92
# File 'app/models/lentil/image.rb', line 90

def self.blend
  (popular.limit(50) + recent.limit(100) + staff_picks.limit(150)).uniq.shuffle
end


78
79
80
# File 'app/models/lentil/image.rb', line 78

def self.popular
  order("popular_score DESC").order("like_votes_count DESC")
end

.recentObject



70
71
72
# File 'app/models/lentil/image.rb', line 70

def self.recent
  order("original_datetime DESC")
end

.search(page, number_to_show = nil) ⇒ Object



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

def self.search(page, number_to_show = nil)
  unless number_to_show.nil?
    paginate :per_page => 20, :page => page, :total_entries => number_to_show
  else
    paginate :per_page => 20, :page => page
  end
end

.staff_picksObject



74
75
76
# File 'app/models/lentil/image.rb', line 74

def self.staff_picks
  where(:staff_like => true).order("original_datetime DESC")
end

Instance Method Details

#available_staff_tags(all_tags) ⇒ Object



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

def available_staff_tags(all_tags)
  tags = all_tags - (self.tags - self.staff_tags)

  if tags.length > 0
    tags = tags.sort_by(&:name)
  end
end

#battlesObject



122
123
124
# File 'app/models/lentil/image.rb', line 122

def battles
  self.won_battles + self.lost_battles
end

#battles_countObject



126
127
128
# File 'app/models/lentil/image.rb', line 126

def battles_count
  self.wins_count + self.losses_count
end

#image_urlObject

legacy



131
132
133
# File 'app/models/lentil/image.rb', line 131

def image_url
  large_url
end

#jpegObject

legacy



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

def jpeg
  large_url
end

#large_url(protocol_relative = true) ⇒ Object



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

def large_url(protocol_relative = true)
  if protocol_relative
    protocol_relative_url + 'media/?size=l'
  else
    url + 'media/?size=l'
  end
end

#medium_url(protocol_relative = true) ⇒ Object



153
154
155
156
157
158
159
# File 'app/models/lentil/image.rb', line 153

def medium_url(protocol_relative = true)
  if protocol_relative
    protocol_relative_url + 'media/?size=m'
  else
    url + 'media/?size=m'
  end
end

#original_metadataObject



193
194
195
# File 'app/models/lentil/image.rb', line 193

def 
  Oj.load(Emojimmy.token_to_emoji(super.to_json))
end

#original_metadata=(meta) ⇒ Object



189
190
191
# File 'app/models/lentil/image.rb', line 189

def original_metadata=(meta)
  write_attribute(:original_metadata, Oj.load(Emojimmy.emoji_to_token(meta.to_hash.to_json)))
end

#protocol_relative_urlObject



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

def protocol_relative_url
  # instagr.am returns 301 to instagram.com and invalid SSL certificate
  url.sub(/^http:/, '').sub(/\/\/instagr\.am/, '//instagram.com')
end

#service_tagsObject



94
95
96
97
98
99
100
101
102
# File 'app/models/lentil/image.rb', line 94

def service_tags
  begin
    tag_ids = self.taggings.select { |tagging| tagging.staff_tag == false }.map(&:tag_id)
    tags = self.tags.select { |tag| tag_ids.include? tag.id}.sort_by(&:name)
  rescue
    Rails.logger.error "Error retrieving service_tags"
    tags = []
  end
end

#staff_tagsObject



104
105
106
107
108
109
110
111
112
# File 'app/models/lentil/image.rb', line 104

def staff_tags
  begin
    tag_ids = self.taggings.select { |tagging| tagging.staff_tag == true }.map(&:tag_id)
    tags = self.tags.select { |tag| tag_ids.include? tag.id}.sort_by(&:name)
  rescue
    Rails.logger.error "Error retrieving staff_tags"
    tags = []
  end
end

#thumbnail_url(protocol_relative = true) ⇒ Object



161
162
163
164
165
166
167
# File 'app/models/lentil/image.rb', line 161

def thumbnail_url(protocol_relative = true)
  if protocol_relative
    protocol_relative_url + 'media/?size=t'
  else
    url + 'media/?size=t'
  end
end