Class: Comic
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Comic
- 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
- .create_comic(params) ⇒ Object
- .current ⇒ Object
- .current_created ⇒ Object
- .feed ⇒ Object
- .from_number(num, only_posted = false) ⇒ Object
- .largest_number ⇒ Object
- .largest_posted_number ⇒ Object
- .next_number(comic) ⇒ Object
- .next_post_date(comic) ⇒ Object
- .numerical ⇒ Object
- .preview_current ⇒ Object
- .reverse_numerical ⇒ Object
- .search(query) ⇒ Object
- .sitemap ⇒ Object
- .update_comic(params) ⇒ Object
Instance Method Summary collapse
- #absolute_img_url ⇒ Object
- #edit_url ⇒ Object
- #formatted_description ⇒ Object
- #img_url ⇒ Object
- #maybe_newest? ⇒ Boolean
- #next_number ⇒ Object
- #oldest? ⇒ Boolean
- #preview_img_url ⇒ Object
- #preview_url ⇒ Object
- #previous_number ⇒ Object
- #real? ⇒ Boolean
- #url ⇒ Object
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 => next_post_date(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 |
.current ⇒ Object
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_created ⇒ Object
141 142 143 |
# File 'app/models/comic.rb', line 141 def current_created reverse_numerical.first end |
.feed ⇒ Object
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
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_number ⇒ Object
121 122 123 |
# File 'app/models/comic.rb', line 121 def largest_number preview_current.number end |
.largest_posted_number ⇒ Object
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 next_post_date(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 |
.numerical ⇒ Object
129 130 131 |
# File 'app/models/comic.rb', line 129 def numerical order "comics.number ASC" end |
.preview_current ⇒ Object
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_numerical ⇒ Object
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 |
.sitemap ⇒ Object
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_url ⇒ Object
63 64 65 |
# File 'app/models/comic.rb', line 63 def absolute_img_url "http://#{Setting[:domain]}#{img_url}" end |
#edit_url ⇒ Object
21 22 23 |
# File 'app/models/comic.rb', line 21 def edit_url "/admin/comic/#{number}/edit" end |
#formatted_description ⇒ Object
25 26 27 |
# File 'app/models/comic.rb', line 25 def formatted_description Markdown.render description end |
#img_url ⇒ Object
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_number ⇒ Object
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_url ⇒ Object
59 60 61 |
# File 'app/models/comic.rb', line 59 def preview_img_url "/admin/comic/#{number}/preview.png" end |
#preview_url ⇒ Object
13 14 15 |
# File 'app/models/comic.rb', line 13 def preview_url "/admin/comic/#{number}/preview" end |
#previous_number ⇒ Object
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 |
#url ⇒ Object
17 18 19 |
# File 'app/models/comic.rb', line 17 def url "/comic/#{number}" end |