Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
support for creating end destroting buckets (EU and US)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcinbunsch committed Sep 22, 2009
1 parent 77d1558 commit 5f49a70
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 9 deletions.
16 changes: 15 additions & 1 deletion lib/actions/s3.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
# S3

get '/:project/buckets' do
@buckets = @s3.buckets.map{|b| b.name}
@buckets = @s3.buckets.map{|b| { :name => b.name, :location => (b.location != '' ? b.location : 'US') } }
erb :buckets
end

post '/:project/buckets' do
if params[:bucket] and params[:bucket][:name]
headers = {}
headers[:location] = :eu if params[:bucket][:location] == 'eu'
@s3.interface.create_bucket(params[:bucket][:name], headers)
end
redirect "/#{@project}/buckets"
end

get '/:project/bucket/:bucket_name/delete' do
@s3.interface.delete_bucket(params[:bucket_name])
redirect "/#{@project}/buckets"
end

get '/:project/bucket/:bucket_name/keys' do
@bucket = @s3.bucket(params[:bucket_name])
@keys = @bucket.keys
Expand Down
23 changes: 23 additions & 0 deletions lib/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,28 @@ def time_ago_or_time_stamp(from_time, to_time = Time.now, include_seconds = true
return time
end

def table(collection, *columns)
raise 'Collection element is not a hash' if collection.first.class != Hash
if columns == []
# auto load a list of keys
first = collection.first
columns = first.keys
end
html = '<table class="list">'
html << "\n<tr><th>" + columns.join('</th><th>') + '</th>'
html << '<th>options</th>' if block_given?
html << '</tr>'
iterator = 0
collection.each do |item|
html << "\n<tr#{ ' class="alt"' if iterator%2==0}>"
item.each_pair { |key, value| html << "<td>#{value}</td>" if columns.include?(key.to_sym) }
if block_given?
html << "<td>#{yield(item)}</td>"
end
html << '<tr>'
iterator += 1
end
html << "\n</table>"
end

end
13 changes: 12 additions & 1 deletion public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,15 @@ body {
background-color: white;
z-index:1002;
overflow: auto;
}
}

a { color: #36B2E5; text-decoration: none;}
a:hover { text-decoration: underline;}

/* this table is inspired by http://www.duoh.com/csstutorials/csstables/ */
table.list { background-color: #C1DAD7; border-spacing:1px; border: 0px; width: 100%;}
table.list th { background-color: #D3EDEE; color: #4F6B83; font-size: 0.7em; padding: 5px; text-transform: uppercase; font-weight: bold;}
table.list th.short { width: 130px; }
table.list td { background-color: #fff; text-align: left; font-size:0.9em; padding: 5px; vertical-align: top;}
table.list td.options { width: 200px; }
table.list tr.alt td { background-color: #F5FAFA; }
4 changes: 0 additions & 4 deletions snail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@
set :config, config_file
set :projects, config_file.keys

configure do
set :sessions, true
end

before do
@projects = options.projects
first = request.path.split('/')[1]
Expand Down
33 changes: 33 additions & 0 deletions spec/helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require 'spec'
require File.dirname(__FILE__) + '/../lib/helpers'

describe Helpers do
include Helpers

describe '#table' do

before(:each) do
@collection = [
{:name => 'Foo', :email => '[email protected]', :id => 11},
{:name => 'Boo', :email => '[email protected]', :id => 34}
]
end

it 'should render table with no columns' do
result = table(@collection)
result.include?('<tr><th>name</th><th>email</th><th>id</th></tr>').should == true
result.include?('<tr><td>Foo</td><td>[email protected]</td><td>11</td><tr>').should == true
end

it 'should render table with a block' do
result = table(@collection) { |item| 'copy' }
result.include?('<tr><th>name</th><th>email</th><th>id</th><th>options</th></tr>').should == true
result.include?('<tr><td>Foo</td><td>[email protected]</td><td>11</td><td>copy</td><tr>').should == true
end

it 'should render table with columns and block' do
result = table(@collection, :name, :email) { |item| 'copy' }
end

end
end
15 changes: 12 additions & 3 deletions views/buckets.erb
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
<% @buckets.each do |bucket| %>
<%= bucket %> <%= link_to 'keys', "/bucket/#{bucket}/keys" %><br/>
<% end %>
<form action="/<%= @project %>/buckets" method="post">
<input type="text" name="bucket[name]" value="" />
<select name="bucket[location]">
<option value="eu">EU</option>
<option value="us">US</option>
</select>
<input type="submit">
</form>
<%= table(@buckets) do |bucket|
result = link_to('view', "/#{@project}/bucket/#{bucket[:name]}/keys")
result << ' ' + link_to('delete', "/#{@project}/bucket/#{bucket[:name]}/delete")
end %>

0 comments on commit 5f49a70

Please sign in to comment.