Class: Comic

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Entity, Lockable, Postable
Defined in:
app/models/comic.rb

Constant Summary collapse

MONDAY =
1
WEDNESDAY =
3
FRIDAY =
5
VALID_DAYS =
[MONDAY, WEDNESDAY, FRIDAY]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_comic(params) ⇒ Object



78
79
80
81
# File 'app/models/comic.rb', line 78

def create_comic(params)
  last = current_created
  create :number => next_number(last), :title => params[:title], :posted_at => (last), :description => params[:description], :scene_description => params[:scene_description], :dialogue => params[:dialogue], :title_text => params[:title_text], :database_file => DatabaseFile.create_from_param(params[:image], :allowed_extensions => ["png"]), :locked => true
end

.currentObject



145
146
147
148
149
# File 'app/models/comic.rb', line 145

def current
  comic = posted.reverse_numerical.first
  comic = new :title => "No Comics Yet", :description => "Check back later for the first comic!" unless comic
  comic
end

.current_createdObject



141
142
143
# File 'app/models/comic.rb', line 141

def current_created
  reverse_numerical.first
end

.feedObject



133
134
135
# File 'app/models/comic.rb', line 133

def feed
  posted.reverse_numerical.take 10
end

.from_number(num, only_posted = false) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'app/models/comic.rb', line 157

def from_number(num, only_posted = false)
  comic = where :number => num.to_i

  if only_posted
    max_number = Comic.posted.select("MAX(comics.number)").to_sql
    comic = comic.posted.select("comics.*, (#{max_number}) AS max_number")
  else
    max_number = Comic.select("MAX(comics.number)").to_sql
    comic = comic.select("comics.*, (#{max_number}) AS max_number")
  end

  comic = comic.first
  raise ActiveRecord::RecordNotFound.new("No records found!") unless comic
  comic
end

.largest_numberObject



121
122
123
# File 'app/models/comic.rb', line 121

def largest_number
  preview_current.number
end

.largest_posted_numberObject



117
118
119
# File 'app/models/comic.rb', line 117

def largest_posted_number
  current.number
end

.next_number(comic) ⇒ Object



97
98
99
100
101
102
103
# File 'app/models/comic.rb', line 97

def next_number(comic)
  if comic
    comic.number + 1
  else
    1
  end
end

.next_post_date(comic) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/comic.rb', line 105

def (comic)
  if comic
    from = comic.posted_at
  else
    from = Date.today
  end

  result = from
  result += 1.day until result > from && VALID_DAYS.include?(result.wday)
  result
end

.numericalObject



129
130
131
# File 'app/models/comic.rb', line 129

def numerical
  order "comics.number ASC"
end

.preview_currentObject



151
152
153
154
155
# File 'app/models/comic.rb', line 151

def preview_current
  comic = reverse_numerical.first
  comic = new :title => "No Comics Yet", :description => "Check back later for the first comic!" unless comic
  comic
end

.reverse_numericalObject



125
126
127
# File 'app/models/comic.rb', line 125

def reverse_numerical
  order "comics.number DESC"
end

.search(query) ⇒ Object



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

def search(query)
  reverse_numerical.where "LOWER(title) LIKE :query OR LOWER(description) LIKE :query OR LOWER(scene_description) LIKE :query OR LOWER(dialogue) LIKE :query OR LOWER(title_text) LIKE :query", :query => "%#{query.downcase}%"
end

.sitemapObject



137
138
139
# File 'app/models/comic.rb', line 137

def sitemap
  posted.reverse_numerical.to_a
end

.update_comic(params) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/comic.rb', line 83

def update_comic(params)
  comic = from_number params[:id].to_i
  comic.ensure_unlocked!
  comic.title = params[:title]
  comic.description = params[:description]
  comic.scene_description = params[:scene_description]
  comic.dialogue = params[:dialogue]
  comic.title_text = params[:title_text]
  comic.locked = true
  comic.database_file = DatabaseFile.create_from_param params[:image], :allowed_extensions => ["png"] if params[:image]
  comic.save!
  comic
end

Instance Method Details

#absolute_img_urlObject



63
64
65
# File 'app/models/comic.rb', line 63

def absolute_img_url
  "http://#{Setting[:domain]}#{img_url}"
end

#edit_urlObject



21
22
23
# File 'app/models/comic.rb', line 21

def edit_url
  "/admin/comic/#{number}/edit"
end

#formatted_descriptionObject



25
26
27
# File 'app/models/comic.rb', line 25

def formatted_description
  Markdown.render description
end

#img_urlObject



55
56
57
# File 'app/models/comic.rb', line 55

def img_url
  "/comic/#{number}.png"
end

#maybe_newest?Boolean



49
50
51
52
53
# File 'app/models/comic.rb', line 49

def maybe_newest?
  if respond_to? :max_number
    max_number.to_i == number
  end
end

#next_numberObject



39
40
41
42
43
# File 'app/models/comic.rb', line 39

def next_number
  if number
    number + 1
  end
end

#oldest?Boolean



45
46
47
# File 'app/models/comic.rb', line 45

def oldest?
  number == 1 || !number
end

#preview_img_urlObject



59
60
61
# File 'app/models/comic.rb', line 59

def preview_img_url
  "/admin/comic/#{number}/preview.png"
end

#preview_urlObject



13
14
15
# File 'app/models/comic.rb', line 13

def preview_url
  "/admin/comic/#{number}/preview"
end

#previous_numberObject



33
34
35
36
37
# File 'app/models/comic.rb', line 33

def previous_number
  if number
    number - 1
  end
end

#real?Boolean



29
30
31
# File 'app/models/comic.rb', line 29

def real?
  number
end

#urlObject



17
18
19
# File 'app/models/comic.rb', line 17

def url
  "/comic/#{number}"
end