Skip to content

Commit

Permalink
adds basic file upload widget, dependencies and resources for attachm…
Browse files Browse the repository at this point in the history
…ents

Signed-off-by: Akash Manohar J <akash@akash.im>
  • Loading branch information
HashNuke committed Mar 16, 2012
1 parent 6f7ad84 commit d835083
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Gemfile
Expand Up @@ -12,6 +12,9 @@ gem 'bushido'
gem 'thin'
gem 'faye'
gem 'kaminari'
gem 'aws-sdk'
gem 'paperclip'
gem 'remotipart'

# Gems used only for assets and not required
# in production environments by default.
Expand Down
20 changes: 20 additions & 0 deletions Gemfile.lock
Expand Up @@ -41,6 +41,11 @@ GEM
multi_json (~> 1.0)
addressable (2.2.7)
arel (3.0.2)
aws-sdk (1.3.8)
httparty (~> 0.7)
json (~> 1.4)
nokogiri (<= 1.5.0)
uuidtools (~> 2.1)
bcrypt-ruby (3.0.1)
builder (3.0.0)
bushido (0.0.36)
Expand All @@ -57,6 +62,7 @@ GEM
xpath (~> 0.1.4)
childprocess (0.3.1)
ffi (~> 1.0.6)
cocaine (0.2.1)
coffee-rails (3.2.2)
coffee-script (>= 2.2.0)
railties (~> 3.2.0)
Expand Down Expand Up @@ -109,6 +115,9 @@ GEM
highline (1.6.11)
hike (1.2.1)
http_parser.rb (0.5.3)
httparty (0.8.1)
multi_json
multi_xml
i18n (0.6.0)
jasmine-core (1.2.0.rc1)
journey (1.0.3)
Expand All @@ -127,8 +136,14 @@ GEM
treetop (~> 1.4.8)
mime-types (1.17.2)
multi_json (1.1.0)
multi_xml (0.4.2)
nokogiri (1.5.0)
orm_adapter (0.0.6)
paperclip (2.7.0)
activerecord (>= 2.3.0)
activesupport (>= 2.3.2)
cocaine (>= 0.0.2)
mime-types
polyglot (0.3.3)
rack (1.4.1)
rack-cache (1.2)
Expand All @@ -155,6 +170,7 @@ GEM
rake (0.9.2.2)
rdoc (3.12)
json (~> 1.4)
remotipart (1.0.2)
rest-client (1.6.7)
mime-types (>= 1.16)
rspec (2.8.0)
Expand Down Expand Up @@ -204,6 +220,7 @@ GEM
uglifier (1.2.3)
execjs (>= 0.3.0)
multi_json (>= 1.0.2)
uuidtools (2.1.2)
warden (1.1.1)
rack (>= 1.0)
xpath (0.1.4)
Expand All @@ -214,6 +231,7 @@ PLATFORMS
ruby

DEPENDENCIES
aws-sdk
bushido
capybara
coffee-rails (~> 3.2.1)
Expand All @@ -226,7 +244,9 @@ DEPENDENCIES
jasmine!
jquery-rails
kaminari
paperclip
rails (= 3.2.2)
remotipart
rspec-rails
sass-rails (~> 3.2.3)
shoulda-matchers
Expand Down
3 changes: 3 additions & 0 deletions app/assets/javascripts/backbone/data/channels.js.coffee
@@ -0,0 +1,3 @@
class Kandan.Data.Channels
@active_channel_id: ()->
Kandan.Helpers.Channels.get_active_channel_id()
1 change: 0 additions & 1 deletion app/assets/javascripts/backbone/helpers/channels.js.coffee
Expand Up @@ -9,7 +9,6 @@ class Kandan.Helpers.Channels
@selected_tab: ()->
$('#channels').tabs('option', 'selected')


@get_active_channel_id: ()->
$("#channels .ui-tabs-panel")
.eq(@selected_tab())
Expand Down
39 changes: 39 additions & 0 deletions app/assets/javascripts/backbone/plugins/attachments.js.coffee
@@ -0,0 +1,39 @@
class Kandan.Plugins.Attachments

@plugin_namespace: "Kandan.Plugins.Attachments"

@template: _.template('''
<form accept-charset="UTF-8" action="/channels/<%= channel_id %>/attachments.js" data-remote="true" html="{:multipart=&gt;true}" id="file_upload" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓">
<input name="<%=csrf_param %>" type="hidden" value="<%= csrf_token %>"></div>
<input id="channel_id_<%= channel_id %>" name="channel_id[<%= channel_id %>]" type="hidden">
<input id="file" name="file" type="file">
<input name="commit" type="submit" value="Upload">
</form>
''')

@channel_id: ()->
Kandan.Data.Channels.active_channel_id()

@csrf_param: ->
$('meta[name=csrf-param]').attr('content')


@csrf_token: ->
$('meta[name=csrf-token]').attr('content')


@render: ($widget_el)->
$upload_form = @template({
channel_id: @channel_id(),
csrf_param: @csrf_param(),
csrf_token: @csrf_token()
})
$widget_el.append($upload_form)
# $widget_el.append($file_list)


@init: ()->
Kandan.Widgets.register "attachments", @plugin_namespace


Kandan.Plugins.register "Kandan.Plugins.Attachments"
67 changes: 67 additions & 0 deletions app/controllers/attachments_controller.rb
@@ -0,0 +1,67 @@
# -*- coding: utf-8 -*-
class AttachmentsController < ApplicationController

before_filter :authenticate_user!

# GET /attachments
# GET /attachments.json
def index
@channel = Channel.find_by_name(params[:channel_id])
@attachments = @channel.attachments.order("created_at DESC")

respond_to do |format|
format.json { render :json => @attachments.to_json(:methods => :url) }
end
end

# POST /attachments
# POST /attachments.json
def create
@channel = Channel.find_by_name(params[:channel_id])
@attachment = Attachment.new(params[:attachment])

@attachment.user = current_user
@attachment.channel = Channel.find_by_name(params[:channel_id])
@attachment.file = params[:file]

respond_to do |format|
if @attachment.save
format.html { }
format.js
format.json { render json: @attachment, status: :created }
else
format.html { render action: "new" }
format.js
format.json { render json: @attachment.errors, status: :unprocessable_entity }
end
end
end

# PUT /attachments/1
# PUT /attachments/1.json
def update
@attachment = Attachment.find_by_name(params[:id])

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

# DELETE /attachments/1
# DELETE /attachments/1.json
def destroy
@attachment = Attachment.find_by_name(params[:id])
@attachment.destroy

respond_to do |format|
format.html { redirect_to attachments_url }
format.json { head :ok }
end
end
end
24 changes: 24 additions & 0 deletions app/models/attachment.rb
@@ -0,0 +1,24 @@
require 'paperclip'

class Attachment < ActiveRecord::Base
belongs_to :channel
belongs_to :user
belongs_to :message

has_attached_file :file,
:storage => :s3,
:s3_credentials => {
:access_key_id => ENV['S3_ACCESS_KEY_ID'],
:secret_access_key => ENV['S3_SECRET_ACCESS_KEY'],
:session_token => ENV['STS_SESSION_TOKEN']
},
:bucket => ENV['S3_BUCKET'],
:url => "/:attachment/:id/:style/:basename.:extension",
:path => "#{ENV['S3_PREFIX']}/:attachment/:id/:style/:basename.:extension"

attr_accessible :file

def url
file.to_s
end
end
1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -5,6 +5,7 @@

resources :channels do
resources :activities
resources :attachments
end

get "/active_users" => "apis#active_users"
Expand Down
4 changes: 4 additions & 0 deletions config/s3.yml
@@ -0,0 +1,4 @@
access_key_id: <%= ENV['S3_ACCESS_KEY_ID'] %>
secret_access_key: <%= ENV['S3_SECRET_ACCESS_KEY'] %>
bucket: <%= ENV['S3_BUCKET'] %>
session_token: <%= ENV['STS_SESSION_TOKEN'] %>

0 comments on commit d835083

Please sign in to comment.