-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathportfolio.rb
More file actions
428 lines (374 loc) · 12.5 KB
/
portfolio.rb
File metadata and controls
428 lines (374 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# encoding: utf-8
require 'camping/session'
require 'htmlentities/string'
require 'lib/acts-as-taggable-on/models'
require 'lib/acts-as-taggable-on/acts_as_taggable_on'
require 'lib/acts-as-taggable-on/acts_as_tagger'
require 'lib/common'
require 'lib/has_image'
Camping.goes :Portfolio
module Portfolio
include Camping::Session
PATH = File.expand_path(File.dirname(__FILE__)) + '/portfolio'
end
module Portfolio::Models
include ActsAsTaggableModels
Base.send :include, ActiveRecord::Acts::TaggableOn
Base.send :include, ActiveRecord::Acts::Tagger
class Work < Base
acts_as_taggable_on :tags
has_image(true, 'image', [120, 120], [120, 120])
end
class CreatePortfolio < V 0.1
def self.up
create_table :portfolio_tags do |t|
t.column :name, :string
end
create_table :portfolio_taggings do |t|
t.column :tag_id, :integer
t.column :taggable_id, :integer
t.column :tagger_id, :integer
t.column :tagger_type, :string
# You should make sure that the column created is
# long enough to store the required class names.
t.column :taggable_type, :string
t.column :context, :string
t.column :created_at, :datetime
end
add_index :portfolio_taggings, :tag_id, :name => "index_ptflo_taggings_on_tag_id"
add_index :portfolio_taggings, [:taggable_id, :taggable_type, :context], :name => "index_ptflo_taggings_on_tid_ttype_ctext"
create_table :portfolio_works do |t|
t.column :title, :string
t.column :url, :string
t.column :description, :text
t.column :has_image, :boolean
t.column :created, :datetime
t.column :active, :boolean, :default => true
t.column :flash_movie, :string
end
end
end
end
module Portfolio::Controllers
class AdminLogin < R '/admin/login'
def get
render :admin_login
end
def post
if input.user == "bloopletech" and input.pass == PORTFOLIO_PASSWORD
@state.admin = true
redirect '/admin/'
else
redirect '/admin/login'
end
end
end
class AdminList < R '/admin/'
def get
return unless admin?
if input.q
@heading = "Works with tag '#{input.q}'"
@works = Work.find_tagged_with(input.q, :order => 'LOWER(title)')
else
@heading = 'All Works'
@works = Work.find(:all, :order => 'LOWER(title)')
end
@tags = Work.tag_counts
render :admin_list
end
end
class AdminCreate < R '/admin/create'
def get
return unless admin?
@work = Work.new(:url => "http://", :active => true)
render :admin_create
end
def post
return unless admin?
@work = Work.new(:title => input.title, :url => input.url, :description => input.description, :has_image => input.has_image,
:image => (!input.image.nil? ? input.image[:tempfile] : nil), :flash_movie => input.flash_movie, :tag_list => input.tag_list, :active => input.active)
@work.created = DateTime.now
if @work.save
redirect '/admin/'
else
render :admin_create
end
end
end
class AdminEdit < R '/admin/(\d+)/edit'
def get(id)
return unless admin?
@work = Work.find(id.to_i)
render :admin_edit
end
def post(id)
return unless admin?
@work = Work.find(id.to_i)
puts "image: #{input.image.inspect}"
@work.update_attributes(:title => input.title, :url => input.url, :description => input.description, :has_image => input.has_image,
:image => (!input.image.nil? ? input.image[:tempfile] : nil), :flash_movie => input.flash_movie, :tag_list => input.tag_list, :active => input.active)
if @work.save
redirect '/admin/'
else
render :admin_create
end
end
end
class Index < R '/'
def get
@latest_work = Work.find(:all, :conditions => "active=TRUE", :order => 'created DESC', :limit => 1)
fw = Work.find_tagged_with('home_page', :conditions => "active=TRUE")
@featured_works_1 = fw[0..(fw.size/2 - 1)]
@featured_works_2 = fw[(fw.size/2)..fw.size]
@tags = Work.tag_counts
render :index
end
end
class Tagged < R '/tagged/(.+)'
def get(id)
@search_terms = id
@works = Work.find_tagged_with(@search_terms, :conditions => "active=TRUE").sort { rand <=> 0.5 }
@tags = Work.tag_counts
render :tagged
end
end
class Show < R '/(\d+)'
def get(id)
@search_terms = "id #{id.to_i}"
@works = [Work.find(id.to_i, :conditions => "active=TRUE")]
@tags = Work.tag_counts
render :tagged
end
end
include StaticAssetsClass
end
module Portfolio::Helpers
def label_for name, record = nil, attr = name, options = {}
errors = record && !record.valid? && record.errors.on(attr)
label name.to_s.humanize, { :for => name }, options.merge(errors ? { :class => :error } : {})
end
def admin?
@state.admin
end
def nice_date(date)
return date.strftime("%d/%m/%Y")
end
def nice_date_time(date)
return date.strftime("%d/%m/%Y %I:%M%p")
end
def h(text)
CGI::escapeHTML(text)
end
def tag_cloud(tags, classes)
return if tags.empty?
max_count = tags.sort_by(&:count).last.count.to_f
tags.each do |tag|
index = ((tag.count / max_count) * (classes.size - 1)).round
yield tag, classes[index]
end
end
begin
require_library_or_gem "superredcloth" unless Object.const_defined?(:SuperRedCloth)
def textilize(text)
if text.blank?
""
else
textilized = SuperRedCloth.new(text)
textilized.to_html
end
end # def textilize
rescue LoadError
end
def image_column(record, name)
has_file = record.send("has_#{name}?")
out = "<div class='upload'>#{has_file ? img(:src => record.send("#{name}_url")) : ""}<div>"
out << "#{input :type => :radio, :name => "has_#{name}", :value => '0', :onclick => "$('#{name}').disabled = true;", :class => 'radio'} No file<br />"
out << "#{input :type => :radio, :name => "has_#{name}", :value => '1', :onclick => "$('#{name}').disabled = true;", :class => 'radio', :checked => 'checked'} Keep same file<br />" if has_file
attrs = { :type => :radio, :name => "has_#{name}", :value => '1', :onclick => "$('#{name}').disabled = false;", :class => 'radio' }
attrs[:checked] = "checked" if !has_file
out << "#{input attrs} Upload new file"
attrs = { :type => :file, :name => name, :id => name, :class => 'file' }
attrs[:disabled] = "disabled" if has_file
attrs[:checked] = "checked" if !has_file
out << "#{input attrs}</div><div class='clear'></div></div>"
out
end
end
module Portfolio::Views
def layout
xhtml_transitional do
head do
title "Portfolio"
link :rel => 'stylesheet', :type => 'text/css', :href => '/style.css', :media => 'screen'
script :type => 'text/javascript', :src => '/js.js'
link :rel => 'shortcut icon', :href => '/favicon.ico'
end
body do
div.wrap! do
div.header! { a('', :href => '/') }
self << yield
div.copyright! { "© 2008-#{Date.today.year} Brenton Fletcher. Comments? <a href='mailto:i@bloople.net'>i@bloople.net</a>. Made on a Mac with Ruby on Rails." }
end
end
end
end
def index
h2 { "About Me" }
p { "Welcome to the portfolio of Brenton Fletcher. Here you will find a selection of the works I have created as a developer over a wide range of languages and technologies- just click on the tag cloud below. I currently hold the position of Ruby on Rails developer with the award-winning <a href='http://www.katalyst.com.au'>Katalyst Web Design</a> in Adelaide, SA, Australia. My work has been mentioned in the media several times, <a href='/tagged/media'>click here</a> to see them all." }
h2 { "Featured Works" }
div.works_wrapper do
@featured_works_1.each do |fw|
_featured_work(fw, true, @featured_works_1.first == fw)
end
end
div(:class => 'works_wrapper works_wrapper_last') do
@featured_works_2.each do |fw|
_featured_work(fw, true, @featured_works_2.first == fw)
end
end
div.clear { "" }
h2 { "Works by category" }
p do
tag_cloud @tags.sort { |a, b| a.name.downcase <=> b.name.downcase }, %w(css1 css2 css3 css4) do |tag, css_class|
a(tag.name, :href => "/tagged/#{tag.name}", :class => css_class)
text ' '
end
end
p.works_count { "There are #{Portfolio::Models::Work.count} items in my portfolio." }
end
def tagged
h2 { "Works by category" }
p do
tag_cloud @tags.sort { |a, b| a.name.downcase <=> b.name.downcase }, %w(css1 css2 css3 css4) do |tag, css_class|
a(tag.name, :href => "/tagged/#{tag.name}", :class => css_class)
text ' '
end
end
h2 { "Works tagged with #{@search_terms}" }
@works.each do |work|
_work(work, work == @works.first)
end
end
def _featured_work(work, featured, first)
classes = "featured_work"
classes << " featured_work_first" if first
div(:class => classes) do
h3 { a(h(work.title), :href => work.url) } unless work.url.nil? or work.url.blank?
object(:type => 'application/x-shockwave-flash', :data => "/flash/#{work.flash_movie}", :width => 480, :height => 400) do
param :name => 'movie', :data => "/flash/#{work.flash_movie}"
end
div.desc do
textilize work.description
end
div.clear { "" }
end
end
def _work(work, first)
classes = "work"
classes << " work_first" if first
div(:class => classes) do
div.image do
img :src => work.image_url if work.has_image?
end
div.desc do
if work.url.nil? or work.url.blank?
h3 { h(work.title) }
else
h3 { a(work.title, :href => work.url) }
end
text textilize work.description
end
div.clear { "" }
end
end
def admin_login
form :action => '/admin/login', :method => :post do
div do
label_for :user
input :name => :user, :type => :text
end
div do
label_for :pass
input :name => :pass, :type => :password
end
div do
input.submit :type => :submit, :name => :login, :value => 'Login'
end
end
end
def admin_list
h2 { @heading }
p { a('Add work', :href => "/admin/create") }
table do
tr do
th { "Name" }
th { "Created" }
th { }
end
@works.each do |work|
tr do
td { a(work.title, :href => "/admin/#{work.id}/edit") }
td { nice_date work.created }
td { a("Delete", :href => "/admin/#{work.id}/destroy", :onclick => "return confirm('Are you surety sure?');") }
end
end
end
h2 { "Works by category" }
p do
tag_cloud @tags.sort { |a, b| a.name.downcase <=> b.name.downcase }, %w(css1 css2 css3 css4) do |tag, css_class|
a(tag.name, :href => "/admin/#{h tag.name}", :class => css_class)
end
end
p { "I've created a total of #{Portfolio::Models::Work.count} works." }
p { a('Show all works', :href => '/admin/') }
end
def _admin_form
div do
label_for :title
input :name => :title, :type => :text, :value => @work.title
end
div do
label_for :url
input :name => :url, :type => :text, :value => @work.url
end
div do
label_for :description
textarea :name => :description do
@work.description
end
end
div do
label_for :image
text image_column(@work, 'image')
end
div do
label_for :flash_movie
input :name => :flash_movie, :type => :text, :value => @work.flash_movie
end
div do
label_for :tags
input :name => 'tag_list', :value => @work.tag_list
end
div do
label_for :active
input :type => :checkbox, :name => :active, :value => '1', :checked => @work.active?
end
div do
input.submit :type => :submit, :name => :login, :value => 'Save'
end
end
def admin_create
form :action => '/admin/create', :method => :post, :enctype => 'multipart/form-data' do
_admin_form
end
end
def admin_edit
form :action => "/admin/#{@work.id}/edit", :method => :post, :enctype => 'multipart/form-data' do
_admin_form
end
end
end
def Portfolio.create
Portfolio::Models.create_schema
ActiveRecord::Base.default_timezone = :utc
end