Skip to content
Browse files

added sharing packages feature

  • Loading branch information...
1 parent a1f3263 commit 4e7ce5a64169df19e5719e8c0fbd8bdfc5b6c935 @pedromtavares pedromtavares committed Mar 23, 2012
View
1 Gemfile
@@ -10,6 +10,7 @@ group :development, :test do
gem 'thin'
end
+gem 'mysql'
gem 'heroku'
gem 'devise'
View
2 Gemfile.lock
@@ -93,6 +93,7 @@ GEM
treetop (~> 1.4.8)
mime-types (1.17.2)
multi_json (1.1.0)
+ mysql (2.8.1)
net-scp (1.0.4)
net-ssh (>= 1.99.1)
net-ssh (2.3.0)
@@ -175,6 +176,7 @@ DEPENDENCIES
fog
heroku
jquery-rails
+ mysql
rails (= 3.2.1)
sass-rails (~> 3.2.3)
simple_form
View
10 app/controllers/application_controller.rb
@@ -5,17 +5,11 @@ class ApplicationController < ActionController::Base
def authorize_admin!
authenticate_user!
- unless current_user.admin?
- flash[:alert] = "You must be an admin to do that."
- redirect_to root_path
- end
+ redirect_to root_path, alert: "You must be an admin to do that." unless current_user.admin?
end
def authorize_renter!
authenticate_user!
- unless current_user.renter?
- flash[:alert] = "You must be a renter to do that."
- redirect_to root_path
- end
+ redirect_to root_path, alert: "You must be a renter to do that." unless current_user.renter?
end
end
View
13 app/controllers/packages_controller.rb
@@ -5,6 +5,7 @@ class PackagesController < ApplicationController
# GET /packages.json
def index
@packages = current_user.packages
+ @shared_packages = current_user.shared_packages
respond_to do |format|
format.html # index.html.erb
@@ -83,4 +84,16 @@ def destroy
format.json { head :no_content }
end
end
+
+ def share
+ id = params[:id]
+ target = User.find_by_email(params[:email])
+ if target.present?
+ target.share_package(id)
+ flash[:notice] = "Package shared successfully"
+ else
+ flash[:error] = "User not found!"
+ end
+ redirect_to package_path(id)
+ end
end
View
7 app/controllers/shared_packages_controller.rb
@@ -0,0 +1,7 @@
+class SharedPackagesController < ApplicationController
+ before_filter :authorize_renter!
+
+ def show
+ @shared_package = SharedPackage.find(params[:id])
+ end
+end
View
10 app/mailers/notifications.rb
@@ -0,0 +1,10 @@
+class Notifications < ActionMailer::Base
+
+ default :from => 'renterswallet@venator.vc'
+
+ def shared_package(shared_package)
+ @shared_package = shared_package
+ mail(:to => "#{@shared_package.receiver} <#{@shared_package.receiver.email}>", :subject => "Shared Package in Renter's Wallet")
+ end
+
+end
View
11 app/models/package.rb
@@ -3,8 +3,9 @@ class Package < ActiveRecord::Base
belongs_to :user
has_many :document_packages, :dependent => :destroy
has_many :documents, :through => :document_packages
- before_create :generate_package_identifier
- before_update :generate_package_identifier
+ has_many :shared_packages
+
+ before_save :generate_package_identifier
def add_document(document)
@@ -18,8 +19,8 @@ def remove_document(document)
private
def generate_package_identifier
- begin
- self[:identifier] = SecureRandom.urlsafe_base64[0..5].upcase
- end while Package.exists?(:identifier => self[:identifier])
+ while Package.exists?(:identifier => self.identifier) do
+ self.identifier = SecureRandom.urlsafe_base64[0..5].upcase
+ end
end
end
View
4 app/models/shared_package.rb
@@ -0,0 +1,4 @@
+class SharedPackage < ActiveRecord::Base
+ belongs_to :receiver, polymorphic: true
+ belongs_to :package
+end
View
11 app/models/user.rb
@@ -8,4 +8,15 @@ class User < ActiveRecord::Base
attr_accessible :email, :first_name, :last_name, :renter, :broker, :password, :password_confirmation, :remember_me
has_many :documents, :dependent => :destroy
has_many :packages
+ has_many :shared_packages, :as => :receiver
+
+ def to_s
@jvenator
Owner
jvenator added a note Mar 24, 2012

What was the purpose/rationale behind adding this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ "#{self.first_name} #{self.last_name}"
+ end
+
+
+ def share_package(package_id)
+ shared_package = self.shared_packages.create(:package_id => package_id)
+ Notifications.shared_package(shared_package).deliver
+ end
end
View
3 app/views/notifications/shared_package.html.erb
@@ -0,0 +1,3 @@
+Hi,
+
+A package has been shared with you, you can view it by clicking here: <%= link_to shared_package_path(@shared_package)%>
View
17 app/views/packages/index.html.erb
@@ -22,3 +22,20 @@
</table>
<%= link_to 'New', new_package_path, :class => 'btn btn-primary' %>
+
+
+<h1>Shared Packages</h1>
+<table class="table table-striped">
+ <thead>
+ <tr>
+ <th>Name</th>
+ </tr>
+ </thead>
+ <tbody>
+ <% @shared_packages.each do |shared_package| %>
+ <tr>

the answer lies below

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ <td><%= link_to "#{shared_package.package.user} - #{shared_package.package.identifier}", shared_package_path(shared_package) %></td>
+ </tr>
+ <% end %>
+ </tbody>
+</table>
View
8 app/views/packages/show.html.erb
@@ -1,7 +1,7 @@
<p>
<h3>
<%= @package.name %>
- <% if @package.identifier? %>
+ <% if @package.identifier.present? %>
- (ID <%= @package.identifier %>)
<% end%>
</h3>
@@ -10,6 +10,12 @@
<%= link_to 'Back', packages_path, :class => 'btn' %>
<%= link_to 'Edit', edit_package_path(@package), :class => 'btn' %>
<%= link_to 'Delete', package_path(@package), :method => 'delete', :confirm => 'Are you sure?', :class => 'btn btn-danger' %>
+ <div style="float:right;text-align:left">
+ <%= form_tag share_package_path(@package) do %>
+ <%= text_field_tag :email, ''%>
+ <%= submit_tag "Share"%>
+ <% end%>
+ </div>
</div>
</p>
<div id="document_selector">
View
0 app/views/shared_packages/show.html.erb
No changes.
View
13 config/application.rb
@@ -49,6 +49,19 @@ class Application < Rails::Application
# in your app. As such, your models will need to explicitly whitelist or blacklist accessible
# parameters by using an attr_accessible or attr_protected declaration.
# config.active_record.whitelist_attributes = true
+
+ config.action_mailer.delivery_method = :smtp
+ config.action_mailer.raise_delivery_errors = true
+ config.action_mailer.smtp_settings = {
+ :address => "smtp.gmail.com",
+ :port => 587,
+ :domain => 'venator.vc',
+ :user_name => 'renterswallet@venator.vc',
+ :password => 'venator123',
+ :authentication => 'plain',
+ :enable_starttls_auto => true,
+ :tls => true
+ }
# Enable the asset pipeline
config.assets.enabled = true
View
4 config/database.yml
@@ -4,8 +4,10 @@
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
+ # adapter: mysql
+ # database: wallet_development
adapter: sqlite3
- database: db/development.sqlite3
+ database: db/test.sqlite3
pool: 5
timeout: 5000
View
2 config/environments/development.rb
@@ -15,6 +15,8 @@
# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false
+
+ config.action_mailer.default_url_options = { :host => 'localhost:3000' }
# Print deprecation notices to the Rails logger
config.active_support.deprecation = :log
View
5 config/routes.rb
@@ -4,10 +4,15 @@
resources :packages do
resource :documents, :controller => 'packages/documents'
+ member do
+ post :share
+ end
end
resources :documents
+ resources :shared_packages
+
namespace :admin do
root :to => "base#index"
resources :users
View
3 db/migrate/20120315013625_add_user_id_to_documents.rb
@@ -1,6 +1,5 @@
class AddUserIdToDocuments < ActiveRecord::Migration
def change
- add_column :documents, :user_id, :reference
-
+ add_column :documents, :user_id, :integer
end
end
View
3 db/migrate/20120316224312_add_user_id_to_packages.rb
@@ -1,6 +1,5 @@
class AddUserIdToPackages < ActiveRecord::Migration
def change
- add_column :packages, :user_id, :reference
-
+ add_column :packages, :user_id, :integer
end
end
View
10 db/migrate/20120323043357_create_shared_packages.rb
@@ -0,0 +1,10 @@
+class CreateSharedPackages < ActiveRecord::Migration
+ def change
+ create_table :shared_packages do |t|
+ t.string :receiver_type
+ t.integer :receiver_id
+ t.integer :package_id
+ t.timestamps
+ end
+ end
+end
View
18 db/schema.rb
@@ -20,11 +20,21 @@
t.datetime "updated_at", :null => false
end
-# Could not dump table "documents" because of following StandardError
-# Unknown type 'reference' for column 'user_id'
+ create_table "documents", :force => true do |t|
+ t.string "name"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.string "file"
+ t.integer "user_id"
+ end
-# Could not dump table "packages" because of following StandardError
-# Unknown type 'reference' for column 'user_id'
+ create_table "packages", :force => true do |t|
+ t.string "name"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
+ t.integer "user_id"
+ t.string "identifier"
+ end
create_table "users", :force => true do |t|
t.string "first_name"

0 comments on commit 4e7ce5a

Please sign in to comment.
Something went wrong with that request. Please try again.