· 8 years ago · Feb 19, 2018, 06:48 PM
1!!schema
2
3CREATE TABLE `posts` (
4 `id` int(11) NOT NULL auto_increment,
5 `title` varchar(150) NOT NULL default '',
6 `entry` text NOT NULL,
7 `author_id` int(11) NOT NULL default '0',
8 `created_on` datetime NOT NULL default '0000-00-00 00:00:00',
9 `updated_on` datetime NOT NULL default '0000-00-00 00:00:00',
10 PRIMARY KEY (`id`)
11) ENGINE=MyISAM DEFAULT CHARSET=latin1;
12
13DROP TABLE IF EXISTS `accounts`;
14CREATE TABLE `accounts` (
15 `id` int(11) NOT NULL auto_increment,
16 `login` varchar(20) NOT NULL default '',
17 `password` varchar(40) NOT NULL default '',
18 `email` varchar(100) NOT NULL default '',
19 `salt` varchar(10) NOT NULL default '',
20 `pen_name` varchar(30) NOT NULL default '',
21 PRIMARY KEY (`id`)
22) ENGINE=MyISAM DEFAULT CHARSET=latin1;
23
24
25!!post.rb
26
27class Post < ActiveRecord::Base
28 has_many :comments
29 belongs_to :account, :class_name => "Account", :foreign_key => "account_id"
30 validates_presence_of :title, :entry, :author_id
31
32 validates_uniqueness_of :title
33 validates_length_of :title, :within => 3..100
34 attr_protected :id
35
36end
37
38!!account.rb
39
40require 'digest/sha1'
41class Account < ActiveRecord::Base
42 validates_presence_of :login, :password, :name, :pen_name
43 validates_length_of :login, :within => 3..20
44 validates_length_of :pen_name, :within => 3..30
45 validates_length_of :password, :within => 3..20
46 validates_confirmation_of :password, :confirm_password
47 attr_protected :id, :salt
48 ## Auth related methods
49end
50
51!!index.rhtml
52
53<% for post in @posts %>
54 <div class="entry">
55 <h2><%= post.title %></h2>
56 <%= hanging_indent textilize(post.entry), 80, 4 %>
57 <div class="links">Written by <%= post.account.pen_name %> at <%= post.created_on %> (<%= time_ago_in_words post.created_on %> ago)</div>
58 <%= link_to image_tag("icons/page_white.png", :border => 0), permalink_url(:id => post) %> <%= link_to 'Permalink', permalink_url(:id => post) %> - <%= link_to image_tag("icons/comments.png", :border => 0), permalink_url(:id => post) %> <%= link_to 'Comments (' + post.comments.size.to_s + ')', permalink_url(:id => post, :anchor => "comments") %>
59<% if !session[:user].nil? -%>
60 - <%= link_to image_tag("icons/page_edit.png", :border => 0), :controller => 'admin', :action => 'edit_post', :id => post.id %> <%= link_to 'Edit', :controller => 'admin', :action => 'edit_post', :id => post.id %> - <%= image_tag "icons/page_white_delete.png" %> <%= link_to 'Destroy', { :controller => 'admin', :action => 'delete_post', :id => post.id }, :confirm => 'Are you sure?', :post => true %>
61<% end -%>
62 </div>
63<% end -%>
64<%= render_partial "partial/paginator", @post_pages %>