-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathajas.rb
More file actions
224 lines (193 loc) · 6.07 KB
/
ajas.rb
File metadata and controls
224 lines (193 loc) · 6.07 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
# encoding: utf-8
$KCODE = 'u'
require 'camping/ar'
require 'camping/session'
require 'htmlentities/string'
require 'lib/common'
Camping.goes :Ajas
module Ajas
PATH = File.expand_path(File.dirname(__FILE__)) + '/ajas'
include Camping::Session
end
module Ajas::Models
class Admin < Base
end
class CreateModels < V 0.5
def self.up
create_table :ajas_admins do |table|
table.string :name, :password
end
end
end
end
module Ajas::Controllers
class AdminLogin < R '/admin/login', '/admin/logout'
def post
admin_login input.username, input.password
admin_logged_in? ? redirect('/admin/blog') : get
end
def get
admin_logout
render :admin_login
end
end
AdminLogout = AdminLogin
class Index < R '/'
def get
@blog_posts = BlogPost.find(:all, :order => 'created_at DESC', :limit => 1)
render :index
end
end
include StaticAssetsClass
end
module Ajas::Helpers
# include ActionView::Helpers::
def add_success(msg)
@state[:flash] = { :success => [], :errors => [] } unless @state.key? :flash
@state[:flash][:success] << msg
end
def add_error(msg)
@state[:flash] = { :success => [], :errors => [] } unless @state.key? :flash
@state[:flash][:errors] << msg
end
# login system
def admin_current_user
@current_user ||= Ajas::Models::Admin.find(@state.admin_id) unless @state.admin_id.blank?
end
alias admin_logged_in? admin_current_user
def admin_login name, password
@current_user = Ajas::Models::Admin.find_by_name_and_password input.name, input.password
@state.admin_id = @current_user.id if @current_user
end
def admin_logout
@state.admin_id = nil
end
# shortcut for error-aware labels
def label_for name, record = nil, attr = name, options = {}
errors = record && !record.body.blank? && !record.valid? && record.errors.on(attr)
label name.to_s.humanize, { :for => name }, options.merge(errors ? { :class => :error } : {})
end
def nice_date_time(date)
return date.strftime("%d/%m/%Y %I:%M%p")
end
def nice_date(date)
return date.strftime("%d/%m/%Y")
end
def h(text)
CGI::escapeHTML(text)
end
end
module Ajas::Views
def layout
content = yield
@content_for ||= {}
if @render_layout.nil?
@render_layout = true
end
if @render_layout
xhtml_transitional do
head do
title { "AJAS: Adelaide Japanese Animation Society" }
link :href => '/style.css', :rel => 'stylesheet', :type => 'text/css'
self << @content_for[:head]
end
body do
div.wrapper! do
div.header! do
h1 { a(:href => '/') { img(:src => '/logo.png', :alt => "AJAS: Adelaide Japanese Animation Society") } }
end
div.main! do
div.header_image_wrap! do
pages = Ajas::Models::Page.find(:all, :conditions => 'id NOT IN (1, 2)', :order => 'title ASC')
p.nav! { ([a('Home', :href => '/'), a('Anime Titles', :href => '/anime/index'), a('Forum', :href => 'http://ajas.org.au/forum/')] + pages.map { |p| a(p.title, :href => "/page/#{p.id}") }).join(' | ') }
if defined?(@anime_title) and @anime_title.has_banner?
img(:src => @anime_title.banner_large_url)
else
t = Ajas::Models::AnimeTitle.random_with_image
a(:href => "/anime/#{t.id}", :id => 'header_image_link') { img(:src => t.banner_large_url) }
end
end
@state[:flash] = { :success => [], :errors => [] } unless @state.key? :flash
unless @state[:flash][:success].empty?
div.success! do
ul { @state[:flash][:success].each { |s| li { h s } } }
end
end
@state[:flash][:success] = []
unless @state[:flash][:errors].empty?
div.errors! do
p { "There were some errors:" }
ul { @state[:flash][:errors].each { |s| li { h s } } }
end
end
@state[:flash][:errors] = []
if admin_logged_in?
p { [a('Blog', :href => '/admin/blog'), a('Anime', :href => '/admin/anime'), a("Pages", :href => '/admin/pages'),
a('Logout', :href => '/admin/logout')].join(" | ") }
end
self << content
end
div.footer! do
p { "Website powered by Ruby. #{a('Admin login', :href => '/admin/login')}." }
end
end
end
end
else
self << content
end
end
def admin_login
_login_form
end
def _login_form
form :action => '/admin/login', :method => :post do
div do
label_for :name
input :name => :name, :type => :text
end
div do
label_for :password
input :name => :password, :type => :password
end
div do
input :type => :submit, :class => "submit", :name => :login, :value => 'Login'
end
end
end
def index
div.column1! do
div.inner do
h2 { "Blog" }
blog_list_posts(true)
p { a('More...', :href => '/blog/archive/1') } unless Ajas::Models::BlogPost.count(:all) <= 1
end
end
div.column2! do
div.inner do
h2 { "What's next" }
text Ajas::Models::Page.find(2).body
end
end
div.column3! do
div.inner do
h2 { "Welcome to AJAS" }
text Ajas::Models::Page.find(1).body
h2 { "Affiliates" }
p { a(:href => 'http://www.unisa.edu.au/unilife/') { img :src => '/unilife.png' } }
p { a(:href => 'http://www.unisaga.org/') { img :src => '/unisaga.png' } }
p { a(:href => 'http://www.facebook.com/group.php?gid=18203679976') { img :src => '/facebook.png' } }
end
end
div.clear { "" }
end
end
require 'ajas/blog'
require 'ajas/pages'
require 'ajas/anime'
require 'ajas/whats_next'
require 'ajas/home_page_content'
def Ajas.create
Ajas::Models.create_schema
ActiveRecord::Base.default_timezone = :utc
end