From 1d0e8721e15d750e664859d51770b15bc12a0995 Mon Sep 17 00:00:00 2001 From: Will Bradley Date: Sun, 9 Feb 2014 00:54:03 -0700 Subject: [PATCH] Adding resources (imported from old toolshare) --- .gitignore | 1 + Gemfile | 3 +- Gemfile.lock | 19 ++++++++ app/controllers/resources_controller.rb | 4 ++ app/helpers/application_helper.rb | 6 +++ app/models/ability.rb | 1 + app/models/resource.rb | 12 +++++ app/models/resource_category.rb | 3 ++ app/models/toolshare_user.rb | 4 ++ app/views/resources/index.html.erb | 38 +++++++++++++++ config/routes.rb | 1 + config/s3.yml.example | 2 + db/migrate/20140209065240_create_resources.rb | 26 ++++++++++ .../20140209070141_create_categories.rb | 14 ++++++ .../20140209070300_create_toolshare_users.rb | 27 +++++++++++ ..._change_resource_type_to_super_category.rb | 5 ++ db/schema.rb | 48 ++++++++++++++++++- 17 files changed, 212 insertions(+), 2 deletions(-) create mode 100644 app/controllers/resources_controller.rb create mode 100644 app/models/resource.rb create mode 100644 app/models/resource_category.rb create mode 100644 app/models/toolshare_user.rb create mode 100644 app/views/resources/index.html.erb create mode 100644 config/s3.yml.example create mode 100644 db/migrate/20140209065240_create_resources.rb create mode 100644 db/migrate/20140209070141_create_categories.rb create mode 100644 db/migrate/20140209070300_create_toolshare_users.rb create mode 100644 db/migrate/20140209072532_change_resource_type_to_super_category.rb diff --git a/.gitignore b/.gitignore index 8890ac1..0c6251b 100644 --- a/.gitignore +++ b/.gitignore @@ -21,5 +21,6 @@ tmp/ # Ignore config and database files (passwords) /config/config.yml +/config/s3.yml /config/database.yml /config/initializers/secret_token.rb diff --git a/Gemfile b/Gemfile index ab26f86..2e9b84b 100644 --- a/Gemfile +++ b/Gemfile @@ -46,7 +46,8 @@ gem 'bcrypt-ruby', '~> 3.0.0' # To use debugger #gem 'debugger' -#gem "paperclip", "~> 3.0" +gem "paperclip", "~> 3.0" +gem "aws-sdk" gem 'gravtastic' gem 'passenger' diff --git a/Gemfile.lock b/Gemfile.lock index 33af3c3..68c7a92 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -29,9 +29,17 @@ GEM i18n (~> 0.6) multi_json (~> 1.0) arel (3.0.2) + aws-sdk (1.33.0) + json (~> 1.4) + nokogiri (>= 1.4.4) + uuidtools (~> 2.1) bcrypt-ruby (3.0.1) builder (3.0.4) cancan (1.6.10) + climate_control (0.0.3) + activesupport (>= 3.0) + cocaine (0.5.3) + climate_control (>= 0.0.3, < 1.0) coffee-rails (3.2.2) coffee-script (>= 2.2.0) railties (~> 3.2.0) @@ -61,8 +69,16 @@ GEM mime-types (~> 1.16) treetop (~> 1.4.8) mime-types (1.25) + mini_portile (0.5.2) multi_json (1.8.2) + nokogiri (1.6.1) + mini_portile (~> 0.5.0) orm_adapter (0.4.0) + paperclip (3.5.4) + activemodel (>= 3.0.0) + activesupport (>= 3.0.0) + cocaine (~> 0.5.3) + mime-types passenger (4.0.19) daemon_controller (>= 1.1.0) rack @@ -129,6 +145,7 @@ GEM uglifier (2.2.1) execjs (>= 0.3.0) multi_json (~> 1.0, >= 1.0.2) + uuidtools (2.1.4) warden (1.2.3) rack (>= 1.0) @@ -136,6 +153,7 @@ PLATFORMS ruby DEPENDENCIES + aws-sdk bcrypt-ruby (~> 3.0.0) cancan coffee-rails (~> 3.2.1) @@ -143,6 +161,7 @@ DEPENDENCIES gravtastic jquery-rails json + paperclip (~> 3.0) passenger pg rails (= 3.2.8) diff --git a/app/controllers/resources_controller.rb b/app/controllers/resources_controller.rb new file mode 100644 index 0000000..ad75e95 --- /dev/null +++ b/app/controllers/resources_controller.rb @@ -0,0 +1,4 @@ +class ResourcesController < ApplicationController + load_and_authorize_resource + before_filter :authenticate_user! +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 66caaa4..2fdeae7 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,4 +1,10 @@ module ApplicationHelper @payment_methods = [[nil],["PayPal"],["Dwolla"],["Bill Pay"],["Check"],["Cash"],["Other"]] @payment_instructions = {nil => nil, :paypal => "Set up a monthly recurring payment to hslfinances@gmail.com", :dwolla => "Set up a monthly recurring payment to hslfinances@gmail.com", :billpay => "Have your bank send a monthly check to HeatSync Labs Treasurer, 140 W Main St, Mesa AZ 85201", :check => "Mail to HeatSync Labs Treasurer, 140 W Main St, Mesa AZ 85201 OR put in the drop safe at the Lab with a deposit slip firmly attached each month.", :cash => "Put in the drop safe at the Lab with a deposit slip firmly attached each month.", :other => "Hmm... talk to a Treasurer!"} + + def sort_link(title, column, options = {}) + condition = options[:unless] if options.has_key?(:unless) + sort_dir = params[:dir] == 'up' ? 'down' : 'up' + link_to_unless condition, title, request.parameters.merge( {:sort => column, :dir => sort_dir} ) + end end diff --git a/app/models/ability.rb b/app/models/ability.rb index 1d1f9b6..8577e4a 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -14,6 +14,7 @@ class Ability can [:update], Mac, :user_id => nil can [:create,:update], Mac, :user_id => user.id can :read, Payment, :user_id => user.id + can :read, Resource can :read, UserCertification, :user_id => user.id can :read, User, :id => user.id #TODO: why can users update themselves? Maybe because Devise doesn't check users/edit? can :compose_email, User diff --git a/app/models/resource.rb b/app/models/resource.rb new file mode 100644 index 0000000..d1d1646 --- /dev/null +++ b/app/models/resource.rb @@ -0,0 +1,12 @@ +class Resource < ActiveRecord::Base + belongs_to :owner, :class_name => "ToolshareUser" + belongs_to :category, :class_name => "ResourceCategory" + has_attached_file :picture, + :styles => { :medium => "300x300>", + :thumb => "100x100>", + :tiny => "50x50>"}, + :storage => :s3, + :s3_credentials => Rails.root.join('config', 's3.yml'), + :path => ":attachment/:id/:style.:extension", + :bucket => 'Toolshare' +end diff --git a/app/models/resource_category.rb b/app/models/resource_category.rb new file mode 100644 index 0000000..afc9435 --- /dev/null +++ b/app/models/resource_category.rb @@ -0,0 +1,3 @@ +class ResourceCategory < ActiveRecord::Base + has_many :resources +end diff --git a/app/models/toolshare_user.rb b/app/models/toolshare_user.rb new file mode 100644 index 0000000..74236d5 --- /dev/null +++ b/app/models/toolshare_user.rb @@ -0,0 +1,4 @@ +class ToolshareUser < ActiveRecord::Base + has_many :resources, :foreign_key => "owner_id" + attr_accessible :name, :email +end diff --git a/app/views/resources/index.html.erb b/app/views/resources/index.html.erb new file mode 100644 index 0000000..00b8ab6 --- /dev/null +++ b/app/views/resources/index.html.erb @@ -0,0 +1,38 @@ +

Resources

+ +<%= link_to 'New Resource', new_resource_path, :class => "btn" if can? :create, Resource %> + + + + + + + + + + + +<% unless @resources.blank? %> + <% @resources.group_by(&:supercategory).each do |supercategory,resources| %> + + <% resources.each do |resource| %> + + + + + + + + + + <% end %> + <% end %> +<% end %> +
<%= sort_link 'Owner', :owner_id %> <%= sort_link 'Category', :category_id %> <%= sort_link 'Name', :name %> <%= sort_link 'Serial', :serial %> <%= sort_link 'Status/Location', :status %>
<%= supercategory %>
+ <%= link_to image_tag(resource.picture.url(:tiny)), resource.picture.url, :popup => ['index_image_preview', 'height=300,width=400'] if resource.picture? %> + + <%=h resource.owner.name if resource.owner %> + <%=h resource.category.name unless resource.category.nil? %><%=h resource.name %><%=h resource.serial %><%=h resource.status %><%= link_to 'Edit', edit_resource_path(resource) if can? :update, resource %><%= link_to 'Destroy', resource, :confirm => 'Are you sure you want to delete this?', :method => :delete if can? :destroy, resource %>
+ +
+ diff --git a/config/routes.rb b/config/routes.rb index 7d1ca5b..dac4b94 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -8,6 +8,7 @@ Dooraccess::Application.routes.draw do match 'paypal_csvs/:id/link' => 'paypal_csvs#link', :as => :link_paypal_csv resources :payments + resources :resources match 'statistics' => 'statistics#index', :as => :statistics match 'statistics/mac_log' => 'statistics#mac_log', :as => :mac_statistics diff --git a/config/s3.yml.example b/config/s3.yml.example new file mode 100644 index 0000000..59cc44e --- /dev/null +++ b/config/s3.yml.example @@ -0,0 +1,2 @@ +access_key_id: YOUR_PUBLIC_ACCESS_KEY +secret_access_key: YOUR_SECRET_ACCESS_KEY diff --git a/db/migrate/20140209065240_create_resources.rb b/db/migrate/20140209065240_create_resources.rb new file mode 100644 index 0000000..7a5d01f --- /dev/null +++ b/db/migrate/20140209065240_create_resources.rb @@ -0,0 +1,26 @@ +class CreateResources < ActiveRecord::Migration + def up + create_table "resources", :force => true do |t| + t.string "type" + t.integer "owner_id" + t.integer "category_id" + t.string "name" + t.string "serial" + t.string "specs" + t.string "status" + t.boolean "donatable" + t.string "picture_file_name" + t.string "picture_content_type" + t.integer "picture_file_size" + t.datetime "picture_updated_at" + t.text "notes" + t.datetime "created_at" + t.datetime "updated_at" + t.string "estimated_value" + end + end + + def down + drop_table :resources + end +end diff --git a/db/migrate/20140209070141_create_categories.rb b/db/migrate/20140209070141_create_categories.rb new file mode 100644 index 0000000..8dacf5c --- /dev/null +++ b/db/migrate/20140209070141_create_categories.rb @@ -0,0 +1,14 @@ +class CreateCategories < ActiveRecord::Migration + def up + create_table "resource_categories", :force => true do |t| + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + t.string "parent" + end + end + + def down + drop_table :resource_categories + end +end diff --git a/db/migrate/20140209070300_create_toolshare_users.rb b/db/migrate/20140209070300_create_toolshare_users.rb new file mode 100644 index 0000000..2c46611 --- /dev/null +++ b/db/migrate/20140209070300_create_toolshare_users.rb @@ -0,0 +1,27 @@ +class CreateToolshareUsers < ActiveRecord::Migration + def up + create_table "toolshare_users", :force => true do |t| + t.string "email", :default => "", :null => false + t.string "encrypted_password", :limit => 128, :default => "", :null => false + t.string "password_salt", :default => "", :null => false + t.string "confirmation_token" + t.datetime "confirmed_at" + t.datetime "confirmation_sent_at" + t.string "reset_password_token" + t.string "remember_token" + t.datetime "remember_created_at" + t.integer "sign_in_count", :default => 0 + t.datetime "current_sign_in_at" + t.datetime "last_sign_in_at" + t.string "current_sign_in_ip" + t.string "last_sign_in_ip" + t.datetime "created_at" + t.datetime "updated_at" + t.string "name" + end + end + + def down + drop_table :toolshare_users + end +end diff --git a/db/migrate/20140209072532_change_resource_type_to_super_category.rb b/db/migrate/20140209072532_change_resource_type_to_super_category.rb new file mode 100644 index 0000000..171b7f8 --- /dev/null +++ b/db/migrate/20140209072532_change_resource_type_to_super_category.rb @@ -0,0 +1,5 @@ +class ChangeResourceTypeToSuperCategory < ActiveRecord::Migration + def change + rename_column :resources, :type, :supercategory + end +end diff --git a/db/schema.rb b/db/schema.rb index d5522db..67edaf6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20140209025344) do +ActiveRecord::Schema.define(:version => 20140209072532) do create_table "cards", :force => true do |t| t.string "card_number" @@ -116,6 +116,32 @@ ActiveRecord::Schema.define(:version => 20140209025344) do t.datetime "updated_at", :null => false end + create_table "resource_categories", :force => true do |t| + t.string "name" + t.datetime "created_at" + t.datetime "updated_at" + t.string "parent" + end + + create_table "resources", :force => true do |t| + t.string "supercategory" + t.integer "owner_id" + t.integer "category_id" + t.string "name" + t.string "serial" + t.string "specs" + t.string "status" + t.boolean "donatable" + t.string "picture_file_name" + t.string "picture_content_type" + t.integer "picture_file_size" + t.datetime "picture_updated_at" + t.text "notes" + t.datetime "created_at" + t.datetime "updated_at" + t.string "estimated_value" + end + create_table "settings", :force => true do |t| t.string "var", :null => false t.text "value" @@ -127,6 +153,26 @@ ActiveRecord::Schema.define(:version => 20140209025344) do add_index "settings", ["thing_type", "thing_id", "var"], :name => "index_settings_on_thing_type_and_thing_id_and_var", :unique => true + create_table "toolshare_users", :force => true do |t| + t.string "email", :default => "", :null => false + t.string "encrypted_password", :limit => 128, :default => "", :null => false + t.string "password_salt", :default => "", :null => false + t.string "confirmation_token" + t.datetime "confirmed_at" + t.datetime "confirmation_sent_at" + t.string "reset_password_token" + t.string "remember_token" + t.datetime "remember_created_at" + t.integer "sign_in_count", :default => 0 + t.datetime "current_sign_in_at" + t.datetime "last_sign_in_at" + t.string "current_sign_in_ip" + t.string "last_sign_in_ip" + t.datetime "created_at" + t.datetime "updated_at" + t.string "name" + end + create_table "user_certifications", :force => true do |t| t.integer "user_id" t.integer "certification_id"