Guest User

Untitled

a guest
Apr 16th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.30 KB | None | 0 0
  1. require 'rubygems'
  2. require 'camping'
  3. require 'camping/db'
  4. require 'camping/session'
  5. require 'redcloth'
  6. require 'mime/types'
  7. require 'lib/taggable'
  8.  
  9. Camping.goes :Blog
  10.  
  11. module Blog
  12. include Camping::Session
  13.  
  14. class AuthenticationException < StandardError; end
  15.  
  16. module Controllers
  17. class Index < R '/'
  18. def get
  19. @entries = Entry.find_latest_10
  20. render :index
  21. end
  22. end
  23.  
  24. class Archives
  25. def get
  26. @entries = Entry.find_all
  27. render :all
  28. end
  29. end
  30.  
  31. class Post < R '/p/([a-z0-9\-]+)/edit', '/post'
  32. def get(p=nil)
  33. render :error_404 unless logged_in?
  34.  
  35. unless p.blank?
  36. begin
  37. @entry = Entry.find_by_permalink(p)
  38. rescue ActiveRecord::RecordNotFound
  39. render :error_404
  40. else
  41. render :edit
  42. end
  43. else
  44. render :post
  45. end
  46. end
  47.  
  48. def post(p=nil)
  49. render :error_404 unless logged_in?
  50. end
  51. end
  52.  
  53. class View < R '/p/([a-z0-9\-]+)'
  54. def get(p)
  55. @entry = Entry.find_by_permalink(p)
  56. rescue ActiveRecord::RecordNotFound
  57. render :error_404
  58. else
  59. render :view
  60. end
  61. end
  62.  
  63. class Login < R '/sign-in'
  64. def get
  65. render :login
  66. end
  67.  
  68. def post
  69. begin
  70. @user = User.authenticate(input.email, input.password)
  71. @state.user = @user.id
  72. rescue AuthenticationException => e
  73. @error = e.message
  74. render :login
  75. else
  76. redirect Index
  77. end
  78. end
  79. end
  80.  
  81. class Static < R '/static/(.+)'
  82. PATH = File.dirname(__FILE__)
  83.  
  84. def get(f)
  85. @headers['Content-type'] = (MIME::Types.type_for(f)[0] || 'text/plain')
  86.  
  87. unless File.readable? File.join(PATH, 'static', f)
  88. render :error_404
  89. else
  90. File.read(File.join(PATH, 'static', f))
  91. end
  92. end
  93. end
  94.  
  95. class Logout < R '/signout'
  96. def get
  97. @state.user = nil
  98. redirect Index
  99. end
  100. end
  101.  
  102. protected
  103. def logged_in?
  104. !@state.user.blank? == true
  105. end
  106. end
  107.  
  108. module Views
  109. def index
  110.  
  111. end
  112.  
  113. def login
  114.  
  115. end
  116.  
  117. def error_404
  118. h1 'Oops'
  119. p 'Whatever you were looking for? It ain\'t here.'
  120. end
  121.  
  122. def layout
  123. html do
  124. head do
  125. title 'blog'
  126. link :href => R(Static, 'style.css'), :rel => "stylesheet", :type => "text/css"
  127. end
  128. body do
  129. h1 do
  130. a 'blog', :href => R(Index)
  131. end
  132.  
  133. div.menu! do
  134. ul do
  135. li { a 'home', :href => R(Index) }
  136. li { a 'archives', :href => R(Archives) }
  137. li { a 'about', :href => R(Static, 'about.html') }
  138. end
  139. end
  140.  
  141. hr
  142.  
  143. self << yield
  144.  
  145. hr
  146.  
  147. p 'Copyright (c) 2008 Cameron Cox'
  148. end
  149. end
  150. end
  151. end
  152.  
  153. module Models
  154. class Tag < Base
  155. validates_uniqueness_of :name
  156. validates_presence_of :name
  157. end
  158.  
  159. class Comment < Base
  160. belongs_to :entry
  161.  
  162. validates_presence_of :email, :name, :body
  163. validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
  164. # validates_format_of :website, :with => /\A\Z/i, :if => proc { |comment| !comment.website.blank? } # -- lazy ass :D
  165. end
  166.  
  167. class Entry < Base
  168. acts_as_taggable :tag_class_name => 'Blog::Models::Tag'
  169.  
  170. belongs_to :user
  171. has_many :comments
  172.  
  173. def self.find_latest_10
  174. find(:all, :limit => 10, :order => "created_at DESC")
  175. end
  176.  
  177. def find_all
  178. find(:all, :order => "created_at DESC")
  179. end
  180.  
  181. def self.find_by_permalink(permalink)
  182. entry = find(:first, :conditions => ["permalink = ?", permalink])
  183. raise ActiveRecord::RecordNotFound, "No Entry with permalink=#{permalink}." unless entry
  184.  
  185. entry
  186. end
  187. end
  188.  
  189. class User < Base
  190. validates_presence_of :username, :email
  191. validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
  192. validates_uniqueness_of :username, :email
  193. validates_presence_of :password, :password_confirmation, :if => :password_required?
  194. validates_confirmation_of :password, :if => :password_required?
  195. validates_length_of :password, :minimum => 6, :if => :password_required?
  196.  
  197. after_validation :hash_password_if_neccessary
  198. attr_accessor :password, :password_confirmation
  199.  
  200. def self.authenticate(email, password)
  201. user = find(:first, :conditions => ["email = ?", email])
  202.  
  203. raise AuthenticationException.new('User does not exist.') unless user
  204. raise AuthenticationException.new('Incorrect Password.') unless user.verify_password(password)
  205.  
  206. user
  207. end
  208.  
  209. def verify_password(password)
  210. self.hashed_password == hash_password(password)
  211. end
  212.  
  213. protected
  214. def self.crypt(data, salt)
  215. Digest::SHA1.hexdigest("--#{data}--#{salt}--")
  216. end
  217.  
  218. def hash_password(password)
  219. self.class.crypt(password, self.salt)
  220. end
  221.  
  222. def hash_password_if_neccessary
  223. self.salt ||= self.class.crypt(self.object_id, Time.now.to_s)
  224. self.hashed_password = hash_password(self.password)
  225. end
  226.  
  227. def password_required?
  228. hashed_password.blank? || !password.blank?
  229. end
  230. end
  231.  
  232. class CreateBlogTables < V 1.0
  233. def self.up
  234. create_table :blog_tags do |t|
  235. t.string :name
  236. t.timestamps
  237. end
  238.  
  239. create_table :blog_tags_entries, :id => false do |t|
  240. t.integer :tag_id, :entry_id
  241. end
  242.  
  243. create_table :blog_users do |t|
  244. t.string :email, :username, :hashed_password, :salt
  245. t.timestamps
  246. end
  247.  
  248. create_table :blog_entries do |t|
  249. t.string :permalink, :title
  250. t.text :body
  251. t.integer :user_id
  252. t.timestamps
  253. end
  254.  
  255. create_table :blog_comments do |t|
  256. t.string :email, :name, :website
  257. t.text :body
  258. t.integer :entry_id
  259. t.timestamps
  260. end
  261. end
  262.  
  263. def self.down
  264. drop_table :blog_tags
  265. drop_table :blog_tags_entries
  266. drop_table :blog_users
  267. drop_table :blog_entries
  268. drop_table :blog_comments
  269. end
  270. end
  271. end
  272.  
  273. def Blog.create
  274. Models.create_schema
  275. end
  276.  
  277. # protected
  278. # def render(m, layout = true)
  279. # content = Erubis::Eruby.new(IO.read(File.join(File.dirname(__FILE__), 'templates', "#{m}.erb"))).result(binding)
  280. # content = Erubis::Eruby.new(IO.read(File.join(File.dirname(__FILE__), 'templates', 'layout.erb'))).result(binding) if layout
  281. #
  282. # return content
  283. # end
  284. end
Add Comment
Please, Sign In to add comment