From d83508309ce724fbcc5458f4989cf4244c83554b Mon Sep 17 00:00:00 2001 From: Akash Manohar J Date: Fri, 16 Mar 2012 18:36:11 +0530 Subject: [PATCH] adds basic file upload widget, dependencies and resources for attachments Signed-off-by: Akash Manohar J --- Gemfile | 3 + Gemfile.lock | 20 ++++++ .../backbone/data/channels.js.coffee | 3 + .../backbone/helpers/channels.js.coffee | 1 - .../backbone/plugins/attachments.js.coffee | 39 +++++++++++ app/controllers/attachments_controller.rb | 67 +++++++++++++++++++ app/models/attachment.rb | 24 +++++++ config/routes.rb | 1 + config/s3.yml | 4 ++ 9 files changed, 161 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/backbone/data/channels.js.coffee create mode 100644 app/assets/javascripts/backbone/plugins/attachments.js.coffee create mode 100644 app/controllers/attachments_controller.rb create mode 100644 app/models/attachment.rb create mode 100644 config/s3.yml diff --git a/Gemfile b/Gemfile index 63e856c1..6f42729d 100644 --- a/Gemfile +++ b/Gemfile @@ -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. diff --git a/Gemfile.lock b/Gemfile.lock index 3f6b7267..70175cdd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) @@ -214,6 +231,7 @@ PLATFORMS ruby DEPENDENCIES + aws-sdk bushido capybara coffee-rails (~> 3.2.1) @@ -226,7 +244,9 @@ DEPENDENCIES jasmine! jquery-rails kaminari + paperclip rails (= 3.2.2) + remotipart rspec-rails sass-rails (~> 3.2.3) shoulda-matchers diff --git a/app/assets/javascripts/backbone/data/channels.js.coffee b/app/assets/javascripts/backbone/data/channels.js.coffee new file mode 100644 index 00000000..8e6d7dec --- /dev/null +++ b/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() \ No newline at end of file diff --git a/app/assets/javascripts/backbone/helpers/channels.js.coffee b/app/assets/javascripts/backbone/helpers/channels.js.coffee index 04d861c1..021d2d0a 100644 --- a/app/assets/javascripts/backbone/helpers/channels.js.coffee +++ b/app/assets/javascripts/backbone/helpers/channels.js.coffee @@ -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()) diff --git a/app/assets/javascripts/backbone/plugins/attachments.js.coffee b/app/assets/javascripts/backbone/plugins/attachments.js.coffee new file mode 100644 index 00000000..b41fc74f --- /dev/null +++ b/app/assets/javascripts/backbone/plugins/attachments.js.coffee @@ -0,0 +1,39 @@ +class Kandan.Plugins.Attachments + + @plugin_namespace: "Kandan.Plugins.Attachments" + + @template: _.template(''' +
+
+ + + +
+ ''') + + @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" \ No newline at end of file diff --git a/app/controllers/attachments_controller.rb b/app/controllers/attachments_controller.rb new file mode 100644 index 00000000..d367734b --- /dev/null +++ b/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 diff --git a/app/models/attachment.rb b/app/models/attachment.rb new file mode 100644 index 00000000..94cf0318 --- /dev/null +++ b/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 diff --git a/config/routes.rb b/config/routes.rb index 355da6a6..14ab06d5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,7 @@ resources :channels do resources :activities + resources :attachments end get "/active_users" => "apis#active_users" diff --git a/config/s3.yml b/config/s3.yml new file mode 100644 index 00000000..a5c37997 --- /dev/null +++ b/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'] %>