Permalink
Browse files

Basic user/subdomain support

  • Loading branch information...
1 parent cad2ffa commit cda0f9b94ac8b02fa0fa35bfcde0fb59e421d4c3 @sachac committed Nov 7, 2011
Showing with 546 additions and 316 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 .rspec
  3. +6 −4 Gemfile
  4. +27 −5 Gemfile.lock
  5. +22 −2 app/controllers/application_controller.rb
  6. +20 −12 app/controllers/clothing_controller.rb
  7. +1 −1 app/controllers/clothing_logs_controller.rb
  8. +3 −3 app/controllers/csa_foods_controller.rb
  9. +5 −7 app/controllers/home_controller.rb
  10. +1 −1 app/controllers/time_controller.rb
  11. +11 −1 app/helpers/application_helper.rb
  12. +6 −24 app/models/ability.rb
  13. +0 −2 app/models/book.rb
  14. +1 −0 app/models/clothing.rb
  15. +1 −0 app/models/clothing_log.rb
  16. +1 −0 app/models/clothing_match.rb
  17. +3 −2 app/models/csa_food.rb
  18. +1 −0 app/models/day.rb
  19. +1 −0 app/models/decision.rb
  20. +1 −0 app/models/decision_log.rb
  21. +1 −0 app/models/food.rb
  22. +3 −2 app/models/library_item.rb
  23. +1 −0 app/models/location.rb
  24. +1 −0 app/models/location_history.rb
  25. +1 −0 app/models/measurement.rb
  26. +1 −0 app/models/measurement_log.rb
  27. +3 −0 app/models/setting.rb
  28. +0 −4 app/models/settings.rb
  29. +1 −0 app/models/stuff.rb
  30. +1 −0 app/models/time_record.rb
  31. +3 −2 app/models/toronto_library.rb
  32. +77 −1 app/models/user.rb
  33. +40 −40 app/views/clothing/index.html.haml
  34. +3 −3 app/views/devise/passwords/new.html.erb
  35. +3 −0 app/views/devise/registrations/edit.html.erb
  36. +3 −0 app/views/devise/registrations/new.html.erb
  37. +5 −5 app/views/devise/sessions/new.html.haml
  38. +25 −28 app/views/home/index.html.haml
  39. +2 −2 app/views/layouts/application.html.haml
  40. +22 −0 app/views/partials/_login.html.haml
  41. +3 −5 app/views/partials/_sidebar.html.haml
  42. +1 −1 config/initializers/devise.rb
  43. +6 −0 config/initializers/subdomain_fu.rb
  44. +4 −4 config/locales/en.yml
  45. BIN db/development.sqlite3
  46. +18 −0 db/migrate/20111107111930_add_user_ids_to_everything.rb
  47. +11 −0 db/migrate/20111107113903_add_role_to_users.rb
  48. +17 −0 db/migrate/20111107114455_create_settings.rb
  49. +13 −0 db/migrate/20111107120811_add_username_to_user.rb
  50. +61 −9 db/schema.rb
  51. +7 −0 spec/awesome.rb
  52. +27 −0 spec/factories.rb
  53. +15 −0 spec/models/user_spec.rb
  54. +7 −0 spec/requests/stuff_spec.rb
  55. +14 −0 spec/requests/users_spec.rb
  56. +33 −0 spec/spec_helper.rb
  57. +0 −15 test/fixtures/location_histories.yml
  58. +0 −9 test/fixtures/locations.yml
  59. +0 −49 test/functional/location_histories_controller_test.rb
  60. +0 −49 test/functional/locations_controller_test.rb
  61. +0 −4 test/unit/helpers/location_histories_helper_test.rb
  62. +0 −4 test/unit/helpers/locations_helper_test.rb
  63. +0 −8 test/unit/location_history_test.rb
  64. +0 −8 test/unit/location_test.rb
  65. BIN vendor/cache/acts-as-taggable-on-2.1.1.gem
  66. BIN vendor/cache/settingslogic-2.0.6.gem
View
@@ -7,3 +7,4 @@ tmp/
.#*
*~
vendor/cache
+webrat.log
View
1 .rspec
@@ -0,0 +1 @@
+--colour
View
10 Gemfile
@@ -40,7 +40,7 @@ gem 'cancan'
# gem 'webrat'
# end
gem 'rails3-jquery-autocomplete'
-gem 'acts-as-taggable-on', '~>2.1.0'
+gem 'acts-as-taggable-on', :git => 'git://github.com/mbleigh/acts-as-taggable-on.git'
gem 'chronic'
#gem 'sparklines'
#gem 'sparklines_generator'
@@ -51,9 +51,9 @@ gem 'ruby_parser'
gem 'sass'
gem 'barometer'
gem 'mechanize'
-gem 'settingslogic'
+gem 'rails-settings-cached', :require => 'rails-settings'
gem 'color'
-
+gem 'subdomain-fu', :git => "git://github.com/nhowell/subdomain-fu.git"
group :development, :test do
gem 'cucumber'
gem 'hpricot'
@@ -62,6 +62,8 @@ group :development, :test do
gem 'rspec'
gem 'rspec-rails'
gem 'factory_girl'
+ gem 'factory_girl_rails'
+ gem 'forgery'
end
gem 'yaml_db'
-
+gem 'email_validator'
View
@@ -1,3 +1,16 @@
+GIT
+ remote: git://github.com/mbleigh/acts-as-taggable-on.git
+ revision: 0662e665866d4a668158d1ceff91fc243b3f63f6
+ specs:
+ acts-as-taggable-on (2.1.1)
+ rails
+
+GIT
+ remote: git://github.com/nhowell/subdomain-fu.git
+ revision: c6fbf945cf3f4ab344f9c8e0604dc5dcca9ee6cf
+ specs:
+ subdomain-fu (1.0.0.beta2)
+
GEM
remote: http://rubygems.org/
specs:
@@ -28,8 +41,6 @@ GEM
activemodel (= 3.0.10)
activesupport (= 3.0.10)
activesupport (3.0.10)
- acts-as-taggable-on (2.1.1)
- rails
arel (2.0.10)
barometer (0.7.3)
httparty (>= 0.4.5)
@@ -64,11 +75,17 @@ GEM
orm_adapter (~> 0.0.3)
warden (~> 1.0.3)
diff-lcs (1.1.3)
+ email_validator (1.2.4)
+ activemodel
erubis (2.6.6)
abstract (>= 1.0.0)
factory_girl (2.2.0)
activesupport
+ factory_girl_rails (1.3.0)
+ factory_girl (~> 2.2.0)
+ railties (>= 3.0.0)
ffi (1.0.9)
+ forgery (0.5.0)
formtastic (2.0.0)
rails (~> 3.0)
gdata (1.1.2)
@@ -130,6 +147,8 @@ GEM
activesupport (= 3.0.10)
bundler (~> 1.0)
railties (= 3.0.10)
+ rails-settings-cached (0.1.2)
+ rails (>= 3.0.0)
rails3-jquery-autocomplete (1.0.4)
rails (~> 3.0)
railties (3.0.10)
@@ -162,7 +181,6 @@ GEM
ffi (= 1.0.9)
json_pure
rubyzip
- settingslogic (2.0.6)
sexp_processor (3.0.7)
sqlite3 (1.3.4)
term-ansicolor (1.0.7)
@@ -189,15 +207,18 @@ PLATFORMS
ruby
DEPENDENCIES
- acts-as-taggable-on (~> 2.1.0)
+ acts-as-taggable-on!
barometer
cancan
chronic
color
cucumber
cucumber-rails
devise
+ email_validator
factory_girl
+ factory_girl_rails
+ forgery
formtastic
gdata
haml
@@ -209,13 +230,14 @@ DEPENDENCIES
mechanize
mysql
rails (= 3.0.10)
+ rails-settings-cached
rails3-jquery-autocomplete
rspec
rspec-rails
ruby_parser
sass
- settingslogic
sqlite3
+ subdomain-fu!
web-app-theme
webrat
workflow
@@ -10,20 +10,40 @@ def notice_layout!
protect_from_forgery
MOBILE_BROWSERS = ["android", "ipod", "opera mini", "blackberry", "palm","hiptop","avantgo","plucker", "xiino","blazer","elaine", "windows ce; ppc;", "windows ce; smartphone;","windows ce; iemobile", "up.browser","up.link","mmp","symbian","smartphone", "midp","wap","vodafone","o2","pocket","kindle", "mobile","pda","psp","treo"]
- layout :select_layout
- # http://stackoverflow.com/questions/1284169/mobile-version-of-views-for-ruby-on-rails
+# layout :select_layout
def select_layout
session.inspect # force session load
if session.has_key? "layout"
return (session["layout"] == "mobile") ? "mobile_application" : "application"
end
return mobile ? "mobile_application" : "application"
end
+
+ protected
+ # http://stackoverflow.com/questions/1284169/mobile-version-of-views-for-ruby-on-rails
def mobile
agent = request.headers["HTTP_USER_AGENT"].downcase
MOBILE_BROWSERS.each do |m|
return m if agent.match(m)
end
false
end
+
+ def current_account
+ if (current_subdomain.nil?)
+ @account = User.find_by_email('sacha@sachachua.com')
+ else
+ @account ||= User.find_by_username(current_subdomain)
+ end
+ end
+ # Make this method visible to views as well
+ helper_method :current_account
+
+ # This is a before_filter we'll use in other controllers
+ def account_required
+ unless current_account
+ flash[:error] = "Could not find the account '#{current_subdomain}'"
+ redirect_to :controller => "home", :action => "index", :subdomain => false
+ end
+ end
end
@@ -6,13 +6,21 @@ class ClothingController < ApplicationController
# GET /clothing
# GET /clothing.xml
def index
- @tags = Clothing.tag_counts_on(:tags).sort_by(&:name)
- order = sortable_column_order
- order ||= "clothing_type asc, hue asc"
- @clothing = Clothing.find(:all,
- :conditions => ["status IS 'active' OR status IS NULL OR status=''"],
- :order => order)
-
+ @tags = current_account.clothing.tag_counts_on(:tags).sort_by(&:name)
+ params[:sort] ||= 'clothing_type'
+ order = nil
+ sortable_column_order do |column, direction|
+ case column
+ when 'clothing_type', 'id', 'name', 'status', 'clothing_logs_count', 'last_worn'
+ order = "#{column} #{direction}"
+ else
+ order = 'clothing_type ASC, hue ASC'
+ end
+ end
+ @clothing = current_account.clothing
+ @clothing = @clothing.find(:all,
+ :conditions => ["status IS 'active' OR status IS NULL OR status=''"],
+ :order => order)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @clothing }
@@ -22,8 +30,8 @@ def index
# GET /clothing/1
# GET /clothing/1.xml
def show
- @clothing = Clothing.find(params[:id])
- @logs = ClothingLog.find(:all, :conditions => ["clothing_id=?", @clothing.id], :order => 'date DESC')
+ @clothing = current_account.clothing.find(params[:id])
+ @logs = current_account.clothing_logs.find(:all, :conditions => ["clothing_id=?", @clothing.id], :order => 'date DESC')
@previous = @clothing.previous_by_id
@next = @clothing.next_by_id
@@ -78,6 +86,7 @@ def show
def new
@clothing = Clothing.new
@clothing.status = 'active'
+ @clothing.user_id = current_account.id
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @clothing }
@@ -86,14 +95,14 @@ def new
# GET /clothing/1/edit
def edit
- @clothing = Clothing.find(params[:id])
+ @clothing = current_account.clothing.find(params[:id])
end
# POST /clothing
# POST /clothing.xml
def create
@clothing = Clothing.new(params[:clothing])
-
+ @clothing.user_id = current_account.id
respond_to do |format|
if @clothing.save
format.html { redirect_to(new_clothing_path, :notice => 'Clothing was successfully created.') }
@@ -109,7 +118,6 @@ def create
# PUT /clothing/1.xml
def update
@clothing = Clothing.find(params[:id])
-
respond_to do |format|
if @clothing.update_attributes(params[:clothing])
format.html { redirect_to(clothing_path(@clothing), :notice => 'Clothing was successfully updated.') and return }
@@ -3,7 +3,7 @@ class ClothingLogsController < ApplicationController
# GET /clothing_logs.xml
before_filter :authenticate_user!, :except => [:index, :show, :by_date]
def index
- @clothing_logs = ClothingLog.find(:all, :order => "date DESC, outfit_id DESC, clothing.clothing_type", :include => [:clothing])
+ @clothing_logs = current_account.clothing_logs.find(:all, :order => "date DESC, outfit_id DESC, clothing.clothing_type", :include => [:clothing])
@by_date = Hash.new
@clothing_logs.each do |l|
@by_date[l.date] ||= Array.new
@@ -4,8 +4,8 @@ class CsaFoodsController < ApplicationController
# GET /csa_foods
# GET /csa_foods.xml
def index
- @csa_foods = CsaFood.includes(:food).order('date_received DESC, disposition ASC')
- @remaining = CsaFood.remaining
+ @csa_foods = current_account.csa_foods.includes(:food).order('date_received DESC, disposition ASC')
+ @remaining = CsaFood.remaining(current_user)
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @csa_foods }
@@ -15,7 +15,7 @@ def index
def bulk_update
if params[:bulk]
params[:bulk].each do |key, val|
- CsaFood.find(key).update_attributes(:disposition => val)
+ current_account.csa_foods.find(key).update_attributes(:disposition => val)
end
end
redirect_to csa_foods_path
@@ -2,16 +2,14 @@ class HomeController < ApplicationController
before_filter :authenticate_user!, :except => [:index]
def index
@clothing_today = ClothingLog.where('date = ?', Date.today)
- if current_user then
- @memento_mori = current_user.memento_mori
+ if current_account then
+ @memento_mori = current_account.memento_mori
@yesterday = Day.yesterday
@today = Day.today
- else
- @memento_mori = User.find(1).memento_mori
+ @clothing_logs = current_account.clothing_logs.includes(:clothing).where('date >= ? and date <= ?', Date.today - 1.week, Date.today).order('date, outfit_id DESC, clothing.clothing_type')
+ @by_date = current_account.clothing_logs.by_date(@clothing_logs)
+ @dates = 7.downto(0).collect { |i| Date.today - i.days }
end
- @clothing_logs = ClothingLog.includes(:clothing).where('date >= ? and date <= ?', Date.today - 1.week, Date.today).order('date, outfit_id DESC, clothing.clothing_type')
- @by_date = ClothingLog.by_date(@clothing_logs)
- @dates = 7.downto(0).collect { |i| Date.today - i.days }
end
def summary
@start = (!params[:start].blank? ? Time.parse(params[:start]) : Date.new(Date.today.year, Date.today.month, 1)).midnight
@@ -45,7 +45,7 @@ def graph
@start = (!params[:start].blank? ? Time.parse(params[:start]) : Date.new(Date.today.year, Date.today.month, 1)).midnight
@end = (!params[:end].blank? ? Time.parse(params[:end]) : Date.tomorrow).midnight
@height = (@day_height * (@end - @start) / 86400.0).to_i
- entries = TimeRecord.find(:all, :conditions => ["start_time >= ? AND start_time < ?", @start, @end], :order => "start_time")
+ entries = current_account.time_records.find(:all, :conditions => ["start_time >= ? AND start_time < ?", @start, @end], :order => "start_time")
total_time = (@end - @start).to_f
@time_records = Array.new
@day_height = (@height * 86400.0) / (@end.midnight - @start.midnight)
@@ -73,5 +73,15 @@ def recent_locations(stuff)
}.join(' ').html_safe
end
-
+ def resource_name
+ :user
+ end
+
+ def resource
+ @resource ||= User.new
+ end
+
+ def devise_mapping
+ @devise_mapping ||= Devise.mappings[:user]
+ end
end
View
@@ -3,32 +3,14 @@ class Ability
def initialize(user)
user ||= User.new
- if !user.email.blank?
+ if user.role == 'admin'
can :manage, :all
- else
+ elsif !user.id.blank?
+ [Clothing, ClothingLog, ClothingMatch, CsaFood, Day, DecisionLog, Decision, Food, LibraryItem, LocationHistory, Location, MeasurementLog, Measurement, Stuff, TimeRecord, TorontoLibrary].each do |item|
+ can :manage, item, :user_id => user.id
+ end
+ else # Not logged in
can :read, LibraryItem, :public => true
end
- # Define abilities for the passed in user here. For example:
- #
- # user ||= User.new # guest user (not logged in)
- # if user.admin?
- # can :manage, :all
- # else
- # can :read, :all
- # end
- #
- # The first argument to `can` is the action you are giving the user permission to do.
- # If you pass :manage it will apply to every action. Other common actions here are
- # :read, :create, :update and :destroy.
- #
- # The second argument is the resource the user can perform the action on. If you pass
- # :all it will apply to every resource. Otherwise pass a Ruby class of the resource.
- #
- # The third argument is an optional hash of conditions to further filter the objects.
- # For example, here the user can only update published articles.
- #
- # can :update, Article, :published => true
- #
- # See the wiki for details: https://github.com/ryanb/cancan/wiki/Defining-Abilities
end
end
View
@@ -1,2 +0,0 @@
-class Book < ActiveRecord::Base
-end
View
@@ -1,4 +1,5 @@
class Clothing < ActiveRecord::Base
+ belongs_to :user
acts_as_taggable_on :tags
has_many :clothing_logs
has_and_belongs_to_many :clothing_matches, \
@@ -1,4 +1,5 @@
class ClothingLog < ActiveRecord::Base
+ belongs_to :user
belongs_to :clothing, :counter_cache => true
has_and_belongs_to_many :clothing_matches, \
:class_name => "ClothingLog", \
@@ -1,4 +1,5 @@
class ClothingMatch < ActiveRecord::Base
+ belongs_to :user
belongs_to :clothing_a, :class_name => "Clothing"
belongs_to :clothing_b, :class_name => "Clothing"
belongs_to :clothing_log_a, :class_name => "ClothingLog"
View
@@ -1,7 +1,8 @@
class CsaFood < ActiveRecord::Base
+ belongs_to :user
belongs_to :food
- def self.remaining
- CsaFood.where('disposition = ""').sum(:quantity)
+ def self.remaining(account)
+ account.csa_foods.where('disposition = ""').sum(:quantity)
end
def self.next_delivery(date = nil)
date ||= Date.today
Oops, something went wrong.

0 comments on commit cda0f9b

Please sign in to comment.