camping 1.4.2 → 1.5

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -1,8 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  $:.unshift File.dirname(__FILE__) + "/../../lib"
4
- require 'rubygems'
5
- require_gem 'camping', '>=1.4'
4
+ require 'camping'
6
5
  require 'camping/session'
7
6
 
8
7
  Camping.goes :Blog
@@ -12,35 +11,37 @@ module Blog
12
11
  end
13
12
 
14
13
  module Blog::Models
15
- def self.schema(&block)
16
- @@schema = block if block_given?
17
- @@schema
18
- end
19
-
20
14
  class Post < Base; belongs_to :user; end
21
15
  class Comment < Base; belongs_to :user; end
22
16
  class User < Base; end
23
- end
24
17
 
25
- Blog::Models.schema do
26
- create_table :blog_posts, :force => true do |t|
27
- t.column :id, :integer, :null => false
28
- t.column :user_id, :integer, :null => false
29
- t.column :title, :string, :limit => 255
30
- t.column :body, :text
31
- end
32
- create_table :blog_users, :force => true do |t|
33
- t.column :id, :integer, :null => false
34
- t.column :username, :string
35
- t.column :password, :string
36
- end
37
- create_table :blog_comments, :force => true do |t|
38
- t.column :id, :integer, :null => false
39
- t.column :post_id, :integer, :null => false
40
- t.column :username, :string
41
- t.column :body, :text
18
+ class CreateTheBasics < V 1.0
19
+ def self.up
20
+ create_table :blog_posts, :force => true do |t|
21
+ t.column :id, :integer, :null => false
22
+ t.column :user_id, :integer, :null => false
23
+ t.column :title, :string, :limit => 255
24
+ t.column :body, :text
25
+ end
26
+ create_table :blog_users, :force => true do |t|
27
+ t.column :id, :integer, :null => false
28
+ t.column :username, :string
29
+ t.column :password, :string
30
+ end
31
+ create_table :blog_comments, :force => true do |t|
32
+ t.column :id, :integer, :null => false
33
+ t.column :post_id, :integer, :null => false
34
+ t.column :username, :string
35
+ t.column :body, :text
36
+ end
37
+ User.create :username => 'admin', :password => 'camping'
38
+ end
39
+ def self.down
40
+ drop_table :blog_posts
41
+ drop_table :blog_users
42
+ drop_table :blog_comments
43
+ end
42
44
  end
43
- execute "INSERT INTO blog_users (username, password) VALUES ('admin', 'camping')"
44
45
  end
45
46
 
46
47
  module Blog::Controllers
@@ -60,9 +61,11 @@ module Blog::Controllers
60
61
  render :add
61
62
  end
62
63
  def post
63
- post = Post.create :title => input.post_title, :body => input.post_body,
64
- :user_id => @state.user_id
65
- redirect View, post
64
+ unless @state.user_id.blank?
65
+ post = Post.create :title => input.post_title, :body => input.post_body,
66
+ :user_id => @state.user_id
67
+ redirect View, post
68
+ end
66
69
  end
67
70
  end
68
71
 
@@ -70,7 +73,7 @@ module Blog::Controllers
70
73
  def get(*args)
71
74
  div do
72
75
  code args.inspect; br; br
73
- code ENV.inspect; br
76
+ code @env.inspect; br
74
77
  code "Link: #{R(Info, 1, 2)}"
75
78
  end
76
79
  end
@@ -94,9 +97,11 @@ module Blog::Controllers
94
97
  end
95
98
 
96
99
  def post
97
- @post = Post.find input.post_id
98
- @post.update_attributes :title => input.post_title, :body => input.post_body
99
- redirect View, @post
100
+ unless @state.user_id.blank?
101
+ @post = Post.find input.post_id
102
+ @post.update_attributes :title => input.post_title, :body => input.post_body
103
+ redirect View, @post
104
+ end
100
105
  end
101
106
  end
102
107
 
@@ -189,7 +194,7 @@ module Blog::Views
189
194
 
190
195
  def add
191
196
  if @user
192
- _form(post, :action => R(Add))
197
+ _form(@post, :action => R(Add))
193
198
  else
194
199
  _login
195
200
  end
@@ -197,14 +202,14 @@ module Blog::Views
197
202
 
198
203
  def edit
199
204
  if @user
200
- _form(post, :action => R(Edit))
205
+ _form(@post, :action => R(Edit))
201
206
  else
202
207
  _login
203
208
  end
204
209
  end
205
210
 
206
211
  def view
207
- _post(post)
212
+ _post(@post)
208
213
 
209
214
  p "Comment for this post:"
210
215
  for c in @comments
@@ -217,7 +222,7 @@ module Blog::Views
217
222
  input :name => 'post_username', :type => 'text'; br
218
223
  label 'Comment', :for => 'post_body'; br
219
224
  textarea :name => 'post_body' do; end; br
220
- input :type => 'hidden', :name => 'post_id', :value => post.id
225
+ input :type => 'hidden', :name => 'post_id', :value => @post.id
221
226
  input :type => 'submit'
222
227
  end
223
228
  end
@@ -239,16 +244,12 @@ module Blog::Views
239
244
  h1 post.title
240
245
  p post.body
241
246
  p do
242
- a "Edit", :href => R(Edit, post)
243
- a "View", :href => R(View, post)
247
+ [a("Edit", :href => R(Edit, post)), a("View", :href => R(View, post))].join " | "
244
248
  end
245
249
  end
246
250
 
247
251
  def _form(post, opts)
248
- p do
249
- text "You are logged in as #{@user.username} | "
250
- a 'Logout', :href => R(Logout)
251
- end
252
+ p { "You are logged in as #{@user.username} | #{a 'Logout', :href => R(Logout)}" }
252
253
  form({:method => 'post'}.merge(opts)) do
253
254
  label 'Title', :for => 'post_title'; br
254
255
  input :name => 'post_title', :type => 'text',
@@ -265,21 +266,6 @@ end
265
266
 
266
267
  def Blog.create
267
268
  Camping::Models::Session.create_schema
268
- unless Blog::Models::Post.table_exists?
269
- ActiveRecord::Schema.define(&Blog::Models.schema)
270
- end
269
+ Blog::Models.create_schema :assume => (Blog::Models::Post.table_exists? ? 1.0 : 0.0)
271
270
  end
272
271
 
273
- if __FILE__ == $0
274
- require 'mongrel/camping'
275
-
276
- Blog::Models::Base.establish_connection :adapter => 'sqlite3', :database => 'blog.db'
277
- Blog::Models::Base.logger = Logger.new('camping.log')
278
- Blog::Models::Base.threaded_connections=false
279
- Blog.create
280
-
281
- server = Mongrel::Camping::start("0.0.0.0",3002,"/blog",Blog)
282
- puts "** Blog example is running at http://localhost:3002/blog"
283
- puts "** Default username is `admin', password is `camping'"
284
- server.run.join
285
- end
@@ -6,7 +6,7 @@ $:.unshift File.dirname(__FILE__) + "/../../lib"
6
6
  Camping.goes :CampSh
7
7
 
8
8
  module CampSh
9
- NAME = 'CampCampSh'
9
+ NAME = 'CampSh'
10
10
  DESCRIPTION = %{
11
11
  Script your own URL commands, then run these commands through
12
12
  the proxy with "cmd/CommandName". All scripts are versioned
@@ -22,19 +22,7 @@ module CampSh
22
22
  end
23
23
 
24
24
  def self.create
25
- unless CampSh::Models::Command.table_exists?
26
- ActiveRecord::Schema.define do
27
- create_table :campsh_commands, :force => true do |t|
28
- t.column :id, :integer, :null => false
29
- t.column :author, :string, :limit => 40
30
- t.column :name, :string, :limit => 255
31
- t.column :created_at, :datetime
32
- t.column :doc, :text
33
- t.column :code, :text
34
- end
35
- CampSh::Models::Command.create_versioned_table
36
- end
37
- end
25
+ Models.create_schema :assume => (Models::Command.table_exists? ? 1.0 : 0.0)
38
26
  end
39
27
  end
40
28
 
@@ -44,6 +32,24 @@ module CampSh::Models
44
32
  validates_presence_of :author
45
33
  acts_as_versioned
46
34
  end
35
+ class CreateBasics < V 1.0
36
+ def self.up
37
+ create_table :campsh_commands, :force => true do |t|
38
+ t.column :id, :integer, :null => false
39
+ t.column :author, :string, :limit => 40
40
+ t.column :name, :string, :limit => 255
41
+ t.column :created_at, :datetime
42
+ t.column :doc, :text
43
+ t.column :code, :text
44
+ end
45
+ Command.create_versioned_table
46
+ Command.reset_column_information
47
+ end
48
+ def self.down
49
+ drop_table :campsh_commands
50
+ Command.drop_versioned_table
51
+ end
52
+ end
47
53
  end
48
54
 
49
55
  module CampSh::Controllers
@@ -101,7 +107,7 @@ module CampSh::Controllers
101
107
  class Show < R '/show/(\w+)', '/show/(\w+)/(\d+)', '/cancel_edit/(\w+)'
102
108
  def get(name, version = nil)
103
109
  unless @cmd = Command.find_by_name(name)
104
- redirect(Edit, page_name)
110
+ redirect(Edit, name)
105
111
  return
106
112
  end
107
113
  @version = (version.nil? or version == @cmd.version.to_s) ? @cmd : @cmd.versions.find_by_version(version)
@@ -127,13 +133,11 @@ module CampSh::Controllers
127
133
  @cmd = Command.find_by_name(name)
128
134
  @cmd = @cmd.versions.find_by_version(version) unless version.nil? or version == @cmd.version.to_s
129
135
  @title = "Editing #{name}"
130
- @author = cookies.cmd_author or CampSh::ANON
136
+ @author = @cookies.cmd_author || CampSh::ANON
131
137
  render :edit
132
138
  end
133
139
  def post(name)
134
- if input.author != CampSh::ANON
135
- cookies.cmd_author = input.author
136
- end
140
+ @cookies.cmd_author = input.command.author
137
141
  Command.find_or_create_by_name(name).update_attributes(input.command)
138
142
  redirect Show, name
139
143
  end
@@ -484,14 +488,13 @@ module CampSh::Views
484
488
 
485
489
  def _navigation
486
490
  form :id => "navigationForm", :class => "navigation", :style => "font-size: 10px" do
487
- a "Command List", :href => R(List), :title => "Alphabetical list of commands", :accesskey => "A"
488
- text " | "
489
- a "Recently Revised", :href => R(Recent), :title => "Pages sorted by when they were last changed",
490
- :accesskey => "U"
491
- text " | "
492
- a "Authors", :href => R(Authors), :title => "Who wrote what"
493
- text " | "
494
- a "How To", :href => R(HowTo), :title => "How to use CampShell", :accesskey => "H"
491
+ [["Command List", R(List), "Alphabetical list of commands", "A"],
492
+ ["Recently Revised", R(Recent), "Pages sorted by when they were last changed", "U"],
493
+ ["Authors", R(Authors), "Who wrote what", "W"],
494
+ ["How To", R(HowTo), "How to use CampShell", "H"]
495
+ ].map do |txt, link, title, key|
496
+ a txt, :href => link, :title => title, :accesskey => key
497
+ end.join(" | ")
495
498
  end
496
499
  end
497
500
 
@@ -522,11 +525,8 @@ module CampSh::Views
522
525
  ul.authorList! do
523
526
  @authors.each do |author, cmds|
524
527
  li do
525
- strong author
526
- text " worked on: " +
527
- cmds.map { |cmd|
528
- capture { a cmd.name, :href => R(Show, cmd.name) }
529
- }.join(", ")
528
+ strong(author) + " worked on: " +
529
+ cmds.map { |cmd| a cmd.name, :href => R(Show, cmd.name) }.join(", ")
530
530
  end
531
531
  end
532
532
  end
@@ -563,13 +563,18 @@ module CampSh::Views
563
563
  a "Edit Page", :href => R(Edit, @version.name, @version.version),
564
564
  :class => "navlink", :accesskey => "E"
565
565
  unless @version.version == 1
566
+ text " | "
566
567
  a 'Back in time', :href => R(Show, @version.name, @version.version-1)
567
568
  end
568
569
  unless @version.version == @cmd.version
570
+ text " | "
569
571
  a 'Next', :href => R(Show, @version.name, @version.version+1)
572
+ text " | "
570
573
  a 'Current', :href => R(Show, @version.name)
571
574
  end
572
- small "(#{ @cmd.version.size } revisions)"
575
+ if @cmd.versions.size > 1
576
+ small "(#{ @cmd.versions.size } revisions)"
577
+ end
573
578
  end
574
579
  end
575
580
 
@@ -623,9 +628,3 @@ module CampSh::Views
623
628
  end
624
629
  end
625
630
 
626
- if __FILE__ == $0
627
- CampSh::Models::Base.establish_connection :adapter => 'sqlite3', :database => 'campsh.db'
628
- CampSh::Models::Base.logger = Logger.new('camping.log')
629
- CampSh.create
630
- puts CampSh.run
631
- end
@@ -0,0 +1,242 @@
1
+ #!/usr/bin/ruby
2
+ $:.unshift File.dirname(__FILE__) + "/../../lib"
3
+ %w(rubygems redcloth camping acts_as_versioned).each { |lib| require lib }
4
+
5
+ Camping.goes :Tepee
6
+
7
+ module Tepee::Models
8
+
9
+ class Page < Base
10
+ PAGE_LINK = /\[\[([^\]|]*)[|]?([^\]]*)\]\]/
11
+ validates_uniqueness_of :title
12
+ before_save { |r| r.title = r.title.underscore }
13
+ acts_as_versioned
14
+ end
15
+
16
+ class CreateTepee < V 1.0
17
+ def self.up
18
+ create_table :tepee_pages, :force => true do |t|
19
+ t.column :title, :string, :limit => 255
20
+ t.column :body, :text
21
+ end
22
+ Page.create_versioned_table
23
+ Page.reset_column_information
24
+ end
25
+ def self.down
26
+ drop_table :tepee_pages
27
+ Page.drop_versioned_table
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ module Tepee::Controllers
34
+ class Index < R '/'
35
+ def get
36
+ redirect Show, 'home'
37
+ end
38
+ end
39
+
40
+ class Show < R '/(\w+)', '/(\w+)/(\d+)'
41
+ def get page_name, version = nil
42
+ redirect(Edit, page_name, 1) and return unless @page = Page.find_by_title(page_name)
43
+ @version = (version.nil? or version == @page.version.to_s) ? @page : @page.versions.find_by_version(version)
44
+ render :show
45
+ end
46
+ end
47
+
48
+ class Edit < R '/(\w+)/edit', '/(\w+)/(\d+)/edit'
49
+ def get page_name, version = nil
50
+ @page = Page.find_or_create_by_title(page_name)
51
+ @page = @page.versions.find_by_version(version) unless version.nil? or version == @page.version.to_s
52
+ render :edit
53
+ end
54
+
55
+ def post page_name
56
+ Page.find_or_create_by_title(page_name).update_attributes :body => input.post_body and redirect Show, page_name
57
+ end
58
+ end
59
+
60
+ class Versions < R '/(\w+)/versions'
61
+ def get page_name
62
+ @page = Page.find_or_create_by_title(page_name)
63
+ @versions = @page.versions
64
+ render :versions
65
+ end
66
+ end
67
+
68
+ class List < R '/all/list'
69
+ def get
70
+ @pages = Page.find :all, :order => 'title'
71
+ render :list
72
+ end
73
+ end
74
+
75
+ class Stylesheet < R '/css/tepee.css'
76
+ def get
77
+ @headers['Content-Type'] = 'text/css'
78
+ File.read(__FILE__).gsub(/.*__END__/m, '')
79
+ end
80
+ end
81
+ end
82
+
83
+ module Tepee::Views
84
+ def layout
85
+ html do
86
+ head do
87
+ title 'test'
88
+ link :href=>R(Stylesheet), :rel=>'stylesheet', :type=>'text/css'
89
+ end
90
+ style <<-END, :type => 'text/css'
91
+ body {
92
+ font-family: verdana, arial, sans-serif;
93
+ }
94
+ h1, h2, h3, h4, h5 {
95
+ font-weight: normal;
96
+ }
97
+ p.actions a {
98
+ margin-right: 6px;
99
+ }
100
+ END
101
+ body do
102
+ p do
103
+ small do
104
+ span "welcome to " ; a 'tepee', :href => "http://code.whytheluckystiff.net/svn/camping/trunk/examples/tepee.rb"
105
+ span '. go ' ; a 'home', :href => R(Show, 'home')
106
+ span '. list all ' ; a 'pages', :href => R(List)
107
+ end
108
+ end
109
+ div.content do
110
+ self << yield
111
+ end
112
+ end
113
+ end
114
+ end
115
+
116
+ def show
117
+ h1 @page.title
118
+ div { _markup @version.body }
119
+ p.actions do
120
+ _button 'edit', :href => R(Edit, @version.title, @version.version)
121
+ _button 'back', :href => R(Show, @version.title, @version.version-1) unless @version.version == 1
122
+ _button 'next', :href => R(Show, @version.title, @version.version+1) unless @version.version == @page.version
123
+ _button 'current', :href => R(Show, @version.title) unless @version.version == @page.version
124
+ _button 'versions', :href => R(Versions, @page.title)
125
+ end
126
+ end
127
+
128
+ def edit
129
+ h1 @page.title
130
+ form :method => 'post', :action => R(Edit, @page.title) do
131
+ p do
132
+ textarea @page.body, :name => 'post_body', :rows => 50, :cols => 100
133
+ end
134
+ input :type => 'submit', :value=>'change'
135
+ end
136
+ _button 'cancel', :href => R(Show, @page.title, @page.version)
137
+ a 'syntax', :href => 'http://hobix.com/textile/', :target=>'_blank'
138
+ end
139
+
140
+ def list
141
+ h1 'all pages'
142
+ ul { @pages.each { |p| li { a p.title, :href => R(Show, p.title) } } }
143
+ end
144
+
145
+ def versions
146
+ h1 @page.title
147
+ ul do
148
+ @versions.each do |page|
149
+ li do
150
+ span page.version
151
+ _button 'show', :href => R(Show, page.title, page.version)
152
+ _button 'edit', :href => R(Edit, page.title, page.version)
153
+ end
154
+ end
155
+ end
156
+ end
157
+
158
+ def _button(text, options={})
159
+ form :method=>:get, :action=>options[:href] do
160
+ input :type=>'submit', :name=>'submit', :value=>text
161
+ end
162
+ end
163
+
164
+ def _markup body
165
+ return '' if body.blank?
166
+ body.gsub!(Tepee::Models::Page::PAGE_LINK) do
167
+ page = title = $1
168
+ title = $2 unless $2.empty?
169
+ page = page.gsub /\W/, '_'
170
+ if Tepee::Models::Page.find(:all, :select => 'title').collect { |p| p.title }.include?(page)
171
+ %Q{<a href="#{self/R(Show, page)}">#{title}</a>}
172
+ else
173
+ %Q{<span>#{title}<a href="#{self/R(Edit, page, 1)}">?</a></span>}
174
+ end
175
+ end
176
+ RedCloth.new(body, [ :hard_breaks ]).to_html
177
+ end
178
+ end
179
+
180
+ def Tepee.create
181
+ Tepee::Models.create_schema :assume => (Tepee::Models::Page.table_exists? ? 1.0 : 0.0)
182
+ end
183
+ __END__
184
+ /** focus **/
185
+ /*
186
+ a:hover:active {
187
+ color: #10bae0;
188
+ }
189
+
190
+ a:not(:hover):active {
191
+ color: #0000ff;
192
+ }
193
+
194
+ *:focus {
195
+ -moz-outline: 2px solid #10bae0 !important;
196
+ -moz-outline-offset: 1px !important;
197
+ -moz-outline-radius: 3px !important;
198
+ }
199
+
200
+ button:focus,
201
+ input[type="reset"]:focus,
202
+ input[type="button"]:focus,
203
+ input[type="submit"]:focus,
204
+ input[type="file"] > input[type="button"]:focus {
205
+ -moz-outline-radius: 5px !important;
206
+ }
207
+
208
+ button:focus::-moz-focus-inner {
209
+ border-color: transparent !important;
210
+ }
211
+
212
+ button::-moz-focus-inner,
213
+ input[type="reset"]::-moz-focus-inner,
214
+ input[type="button"]::-moz-focus-inner,
215
+ input[type="submit"]::-moz-focus-inner,
216
+ input[type="file"] > input[type="button"]::-moz-focus-inner {
217
+ border: 1px dotted transparent !important;
218
+ }
219
+ textarea:focus, button:focus, select:focus, input:focus {
220
+ -moz-outline-offset: -1px !important;
221
+ }
222
+ input[type="radio"]:focus {
223
+ -moz-outline-radius: 12px;
224
+ -moz-outline-offset: 0px !important;
225
+ }
226
+ a:focus {
227
+ -moz-outline-offset: 0px !important;
228
+ }
229
+ */
230
+ form { display: inline; }
231
+
232
+ /** Gradient **/
233
+ small, pre, textarea, textfield, button, input, select {
234
+ color: #4B4B4C !important;
235
+ background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAAeCAMAAAAxfD/2AAAABGdBTUEAAK/INwWK6QAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAAtUExURfT09PLy8vHx8fv7+/j4+PX19fn5+fr6+vf39/z8/Pb29vPz8/39/f7+/v///0c8Y4oAAAA5SURBVHjaXMZJDgAgCMDAuouA/3+uHPRiMmlKzmhCFRorLOakVnpnDEpBBDHM8ODs/bz372+PAAMAXIQCfD6uIDsAAAAASUVORK5CYII=) !important;
236
+ background-color: #FFF !important;
237
+ background-repeat: repeat-x !important;
238
+ border: 1px solid #CCC !important;
239
+ }
240
+
241
+ button, input { margin: 3px; }
242
+