Skip to content

Commit b02c381

Browse files
author
jineok12
committed
project images uploadify
haml used
1 parent 77895a9 commit b02c381

24 files changed

+331
-6
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Place all the behaviors and hooks related to the matching controller here.
2+
# All this logic will automatically be available in application.js.
3+
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Place all the styles related to the ProjectImages controller here.
2+
// They will automatically be included in application.css.
3+
// You can use Sass (SCSS) here: http://sass-lang.com/
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class ProjectImagesController < ApplicationController
2+
before_filter :find_project
3+
before_filter :find_or_build_photo, :except => [:index, :sort]
4+
def index
5+
end
6+
def new
7+
end
8+
9+
def edit
10+
end
11+
12+
def show
13+
end
14+
15+
def create
16+
if @photo.save
17+
respond_to do |format|
18+
format.js
19+
end
20+
else
21+
end
22+
end
23+
24+
def sort
25+
params[:image].each_with_index do |id, idx|
26+
@project.project_images.find(id).position = idx
27+
end
28+
@project.save
29+
render :nothing => true
30+
end
31+
32+
def destroy
33+
@photo.destroy
34+
redirect_to @project
35+
end
36+
37+
private
38+
39+
def find_project
40+
@project = Project.find(params[:project_id])
41+
@story = @project
42+
end
43+
44+
def find_or_build_photo
45+
@photo = params[:id] ? @project.project_images.find(params[:id]) : @project.project_images.build(params[:photo])
46+
end
47+
48+
49+
end
Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
class ProjectsController < ApplicationController
2+
before_filter :find_or_build_story, :except => :index
23
def index
34
@projects = Project.all
45
@sketches = Sketch.all
5-
66
end
77

88
def new
9-
@project = Project.new
10-
119
end
1210

1311
def edit
1412
end
1513

1614
def create
15+
if @project.save
16+
redirect_to edit_project_path(@project)
17+
else
18+
render :new
19+
end
1720
end
1821

1922
def update
@@ -22,4 +25,9 @@ def update
2225
def destroy
2326
end
2427

28+
private
29+
def find_or_build_story
30+
@project = params[:id] ? Project.find(params[:id]) : Project.new(params[:project])
31+
@story = @project
32+
end
2533
end

app/helpers/application_helper.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,44 @@
11
module ApplicationHelper
2+
3+
def photo_uploadify
4+
# Putting the uploadify trigger script in the helper gives us
5+
# full access to the view and native rails objects without having
6+
# to set javascript variables.
7+
#
8+
# ScriptData:
9+
# Sets the http headers to accept javascript plus adds
10+
# the session key and authenticity token for XSS protection.
11+
# The "FlashSessionCookieMiddleware" rack module deconstructs these
12+
# parameters into something Rails will actually use.
13+
14+
session_key_name = Rails.application.config.session_options[:key]
15+
%Q{
16+
17+
<script type='text/javascript'>
18+
$(document).ready(function() {
19+
$('#photo_upload').uploadify({
20+
script : '#{project_project_images_path(@story)}',
21+
fileDataName : 'photo[image]',
22+
uploader : '/uploadify/uploadify.swf',
23+
cancelImg : '/uploadify/cancel.png',
24+
fileDesc : 'Images',
25+
fileExt : '*.jpg;*.jpeg;*.png;*.gif',
26+
multi : true,
27+
auto : true,
28+
buttonText : 'upload Photos',
29+
scriptData : {
30+
'_http_accept': 'application/javascript',
31+
'#{session_key_name}' : encodeURIComponent('#{u(cookies[session_key_name])}'),
32+
'authenticity_token' : encodeURIComponent('#{u(form_authenticity_token)}')
33+
},
34+
onComplete : function(event, ID, fileObj, response, data){ eval(response) },
35+
onError : function(event, ID, fileObj, errorObj){
36+
alert(errorObj.type + ' Error: ' + errorObj.info);
37+
}
38+
});
39+
});
40+
</script>
41+
42+
}.gsub(/[\n ]+/, ' ').strip.html_safe
43+
end
244
end

app/helpers/project_images_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
module ProjectImagesHelper
2+
end

app/models/project.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
class Project
33
include Mongoid::Document
44
include Mongoid::Timestamps
5+
include Mongoid::Paperclip
56
field :title, :type => String
67
field :description, :type => String
78
field :mail, :type => String
9+
has_mongoid_attached_file :avatar
810

911
has_many :project_images
1012
has_and_belongs_to_many :designers, class_name: "Member", inverse_of: :project

app/models/project_image.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ class ProjectImage
66
include Mongoid::Paperclip
77
field :featured, :type => Boolean
88

9-
belongs_to :project
10-
119
def self.styles_dict
1210
{:large => "500x300", :small => "100x100"}
1311
end
1412

13+
belongs_to :project, :inverse_of => :project_images
14+
1515
include ::ItemImageTemplate
1616
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
= form_for [@story, @photo], :html => { :multipart => true } do |f|
2+
-if @photo.errors.any?
3+
#errorExplanation
4+
%h2= "#{pluralize(@photo.errors.count, "error")} prohibited this photo from being saved:"
5+
%ul
6+
- @photo.errors.full_messages.each do |msg|
7+
%li= msg
8+
9+
.field
10+
= f.label :image
11+
= f.file_field :image
12+
.actions
13+
= f.submit
14+
or
15+
= link_to 'Cancel', [@story, @photo]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.photo[project_image]
2+
= link_to(image_tag(project_image.image.url(:small)), [@story, project_image]) if project_image.image.url(:small)
3+
%br
4+
= project_image.image_file_name
5+
%br
6+
= link_to 'edit', edit_project_project_image_path(@story, project_image)
7+
|
8+
= link_to 'del', [@story, project_image], :confirm => 'Are you sure?', :method => :delete

0 commit comments

Comments
 (0)