Skip to content

Commit

Permalink
增加了电影集和唯一生成的url
Browse files Browse the repository at this point in the history
  • Loading branch information
chucai committed Jun 5, 2012
1 parent 67db9f3 commit a2493fa
Show file tree
Hide file tree
Showing 32 changed files with 426 additions and 47 deletions.
12 changes: 7 additions & 5 deletions app/assets/stylesheets/photos.css.scss
Expand Up @@ -25,19 +25,18 @@ body{
padding: 0;
li{
@include column(5);
margin-top: 30px;
margin-top: 20px;
padding: 5px;
font-size: 18px;
text-align: center;

a{
/*border: 1px solid $blueprint-border-color ;*/
display: block;
padding: 3px;
padding: 8px 3px;
width: span(4);
text-decoration: none;
color: #ffffff ;
@include no-link-icon;
}
}
}
Expand Down Expand Up @@ -103,11 +102,14 @@ body{

.block{
margin-top: 30px;
@include column(7);
@include append(1);
background-color: $blueprint-background-color;
padding: 3px;
height: 150px;
}

.block_left { @include column(7);}
.block_center { @include column(8); }
.block_right { @include column(7); @include last; }
}
}

Expand Down
88 changes: 88 additions & 0 deletions app/controllers/albums_controller.rb
@@ -0,0 +1,88 @@
# -*- encoding: utf-8 -*-
class AlbumsController < ApplicationController

before_filter :authenticate_user!

# GET /albums
# GET /albums.json
def index
@albums = Album.all

respond_to do |format|
format.html # index.html.erb
format.json { render json: @albums }
end
end

# GET /albums/1
# GET /albums/1.json
def show
@album = Album.find(params[:id])

respond_to do |format|
format.html # show.html.erb
format.json { render json: @album }
end
end

# GET /albums/new
# GET /albums/new.json
def new
@album = Album.new

respond_to do |format|
format.html # new.html.erb
format.json { render json: @album }
end
end

# GET /albums/1/edit
def edit
@album = Album.find(params[:id])
end

# POST /albums
# POST /albums.json
def create
@album = Album.new(params[:album])
@album.private = params[:private]

respond_to do |format|
if @album.save
format.html { redirect_to @album, notice: '电影集创建成功.' }
format.json { render json: @album, status: :created, location: @album }
else
format.html { render action: "new" }
format.json { render json: @album.errors, status: :unprocessable_entity }
end
end
end

# PUT /albums/1
# PUT /albums/1.json
def update
@album = Album.find(params[:id])

respond_to do |format|
if @album.update_attributes(params[:album])
format.html { redirect_to @album, notice: 'Album was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @album.errors, status: :unprocessable_entity }
end
end
end

# DELETE /albums/1
# DELETE /albums/1.json
def destroy
@album = Album.find(params[:id])
@album.destroy

respond_to do |format|
format.html { redirect_to albums_url }
format.json { head :no_content }
end
end
end
19 changes: 12 additions & 7 deletions app/controllers/images_controller.rb
Expand Up @@ -5,7 +5,7 @@ class ImagesController < ApplicationController
# GET /images
# GET /images.json
def index
@images = Image.all
@images = album.images.all

respond_to do |format|
format.html # index.html.erb
Expand All @@ -16,7 +16,7 @@ def index
# GET /images/1
# GET /images/1.json
def show
@image = Image.find(params[:id])
@image = album.images.find(params[:id])

respond_to do |format|
format.html # show.html.erb
Expand All @@ -27,7 +27,7 @@ def show
# GET /images/new
# GET /images/new.json
def new
@image = Image.new
@image = album.images.new

respond_to do |format|
format.html # new.html.erb
Expand All @@ -37,13 +37,13 @@ def new

# GET /images/1/edit
def edit
@image = Image.find(params[:id])
@image = album.images.find(params[:id])
end

# POST /images
# POST /images.json
def create
@image = current_user.images.new(params[:image])
@image = current_user.images.new(params[:image]) if params[:image][:album_id].nil? || params[:image][:album_id] == album.id.to_s

respond_to do |format|
if @image.save
Expand All @@ -59,7 +59,7 @@ def create
# PUT /images/1
# PUT /images/1.json
def update
@image = Image.find(params[:id])
@image = album.images.find(params[:id])

respond_to do |format|
if @image.update_attributes(params[:image])
Expand All @@ -75,12 +75,17 @@ def update
# DELETE /images/1
# DELETE /images/1.json
def destroy
@image = Image.find(params[:id])
@image = album.images.find(params[:id])
@image.destroy

respond_to do |format|
format.html { redirect_to images_url }
format.json { head :no_content }
end
end

protected
def album
@album ||= Album.find(params[:album_id])
end
end
5 changes: 4 additions & 1 deletion app/controllers/photos_controller.rb
@@ -1,7 +1,10 @@
class PhotosController < ApplicationController

def index
gon.images = Image.where("user_id = 1").all.map { |image| image.file.url('main') }
token = params[:token]
album = Album.find_by_token(token)
images = album.images
gon.images = images.map { |image| image.file.url('main') }
end

end
5 changes: 5 additions & 0 deletions app/controllers/welcome_controller.rb
@@ -0,0 +1,5 @@
class WelcomeController < ApplicationController
def index
gon.images = Album.first.images.map { |image| image.file.url('main') }
end
end
2 changes: 2 additions & 0 deletions app/helpers/albums_helper.rb
@@ -0,0 +1,2 @@
module AlbumsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/welcome_helper.rb
@@ -0,0 +1,2 @@
module WelcomeHelper
end
34 changes: 34 additions & 0 deletions app/models/album.rb
@@ -0,0 +1,34 @@
class Album < ActiveRecord::Base
attr_accessible :desc

after_create :generate_token

has_many :images

#唯一url
def url
"http://localhost:3000/#{self.token}"
end

#生成唯一的token
def generate_token
begin
self.token = Album.random
self.save!
rescue ActiveRecord::RecordNotUnique => e
retry
end
end

ID_LENGTH = 128
class << self
def random(bitlength = ID_LENGTH)
limit = 2 ** bitlength - 1
max_size = limit.to_s(36).size
string = rand(limit).to_s(36)
string = '0' + string while string.size < max_size
string
end
end

end
1 change: 1 addition & 0 deletions app/models/image.rb
Expand Up @@ -6,5 +6,6 @@ class Image < ActiveRecord::Base
validates :file, :presence => true

belongs_to :user
belongs_to :album

end
25 changes: 25 additions & 0 deletions app/views/albums/_form.html.erb
@@ -0,0 +1,25 @@
<%= form_for(@album) do |f| %>
<% if @album.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@album.errors.count, "error") %> 引起数据无法保存:</h2>
<ul>
<% @album.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :desc %><br />
<%= f.text_area :desc, :rows => 10, :cols => 40 %>
</div>
<div class="field">
<%= f.label :private %>
<%= check_box_tag :private %>
Tips: 如果设置隐私,那么别人将无法观看您的图片。
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
6 changes: 6 additions & 0 deletions app/views/albums/edit.html.erb
@@ -0,0 +1,6 @@
<h1>Editing album</h1>

<%= render 'form' %>
<%= link_to 'Show', @album %> |
<%= link_to 'Back', albums_path %>
29 changes: 29 additions & 0 deletions app/views/albums/index.html.erb
@@ -0,0 +1,29 @@
<h2>电影集列表</h2>
<%= link_to '创建新电影集', new_album_path %>
<table>
<tr>
<th>描述</th>
<th>是否隐私</th>
<th>唯一URL</th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>

<% @albums.each do |album| %>
<tr>
<td><%= album.desc %></td>
<td><%= album.private %></td>
<td><%= album.url %></td>
<td><%= link_to '显示', album_images_path(album) %></td>
<td><%= link_to '修改', edit_album_path(album) %></td>
<td><%= link_to '删除', album, confirm: '肯定删除?', method: :delete %></td>
<td><%= link_to "上传图片", %></td>
</tr>
<% end %>
</table>

<br />


5 changes: 5 additions & 0 deletions app/views/albums/new.html.erb
@@ -0,0 +1,5 @@
<h1>New album</h1>

<%= render 'form' %>
<%= link_to 'Back', albums_path %>
19 changes: 19 additions & 0 deletions app/views/albums/show.html.erb
@@ -0,0 +1,19 @@

<p>
<b>Desc:</b>
<%= @album.desc %>
</p>

<p>
<b>Private:</b>
<%= @album.private %>
</p>

<p>
<b>Token:</b>
<%= @album.token %>
</p>


<%= link_to 'Edit', edit_album_path(@album) %> |
<%= link_to 'Back', albums_path %>
2 changes: 1 addition & 1 deletion app/views/images/_form.html.erb
@@ -1,4 +1,4 @@
<%= form_for(@image, :html => {:multipart => true}) do |f| %>
<%= form_for(@image,:url => album_image_path(@image.album ,@image), :html => {:multipart => true}) do |f| %>
<% if @image.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@image.errors.count, "error") %> 不能保存数据:</h2>
Expand Down
2 changes: 0 additions & 2 deletions app/views/images/edit.html.erb
Expand Up @@ -2,5 +2,3 @@

<%= render 'form' %>

<%= link_to 'Show', @image %> |
<%= link_to 'Back', images_path %>
21 changes: 19 additions & 2 deletions app/views/images/index.html.erb
@@ -1,12 +1,29 @@
<p>
<h1>图片列表</h1> <%= link_to '上传图片', new_image_path %>
<b>电影描述:</b>
<%= @album.desc %>
</p>

<p>
<b>隐私状态:</b>
<%= @album.private %>
</p>

<p>
<b>唯一地址:</b>
<%= @album.url %>
</p>

<%= link_to 'Edit', edit_album_path(@album) %> |
<%= link_to 'Back', albums_path %>
<p>
<h1>图片列表</h1> <%= link_to '上传图片', new_album_image_path(@album) %>
</p>
<div id="images-pane">
<% @images.each do |image| %>
<div class="pane">
<%=image_tag image.file.url("thumb"), :width => 150 %>
<p>
<%= link_to '修改', edit_image_path(image) %>
<%= link_to '修改', edit_album_image_path(@album, image) %>
<%= link_to '删除', image, confirm: '确定删除?', method: :delete %>
</p>
</div>
Expand Down

0 comments on commit a2493fa

Please sign in to comment.