Skip to content

Commit

Permalink
Create multi-user support behind the scenes, but hack it so that we
Browse files Browse the repository at this point in the history
really only have the single user atm
  • Loading branch information
anaisbetts committed Feb 11, 2010
1 parent 0c1b2c7 commit b2cbed0
Show file tree
Hide file tree
Showing 24 changed files with 185 additions and 29 deletions.
1 change: 1 addition & 0 deletions app/controllers/actions_controller.rb
@@ -1,4 +1,5 @@
class ActionsController < ApplicationController
before_filter :authenticate

def invoke
end
Expand Down
14 changes: 8 additions & 6 deletions app/controllers/application_controller.rb
@@ -1,5 +1,7 @@
class ApplicationController < ActionController::Base
before_filter :initial_photo_scan
include Clearance::Authentication

before_filter :hack_sign_in

helper :all

Expand All @@ -8,11 +10,11 @@ class ApplicationController < ActionController::Base
include HoptoadNotifier::Catcher

private
def initial_photo_scan
if Photo.count == 0
RAILS_DEFAULT_LOGGER.info "No known photos to display, scanning..."
PhotoCollectorWorker.async_collect
end
def hack_sign_in
# Sign us in to the first user, always
return true if signed_in?
return true unless User.count > 0
sign_in(User.find(:all)[0])

true
end
Expand Down
20 changes: 18 additions & 2 deletions app/controllers/photos_controller.rb
@@ -1,24 +1,40 @@
class PhotosController < ApplicationController
before_filter :authenticate
before_filter :initial_photo_scan

acts_as_iphone_controller :test_mode => true

def index
@items = Photo.find(:all).map do |p|
@items = Photo.find_all_by_user_id(current_user.id, :all).map do |p|
item = ListModel.new(nil,
render_to_string(:partial => "tile",
:locals => { :item => p}),
photo_url(p))
user_photo_url(current_user, p))
end
end

def show
@item = Photo.find_by_id(params[:id], :limit => 1)
raise "Denied!" unless @item.user_id == current_user.id
@actions = ActionsFramework.actions.select{|x| ActionsFramework[x].can_invoke? @item}.map{|x| ActionsFramework[x]}
end

def destroy
item = Photo.find_by_id(params[:id], :limit => 1)
raise "Denied!" unless @item.user_id == current_user.id
item.destroy

redirect_to :action => 'index'
end

private

def initial_photo_scan
if Photo.count == 0
logger.info "No known photos to display, scanning '#{current_user.import_path}'..."
PhotoCollectorWorker.async_collect(:root_folder => current_user.import_path, :user_id => current_user.id)
end

true
end
end
6 changes: 6 additions & 0 deletions app/controllers/sessions_controller.rb
@@ -0,0 +1,6 @@
class SessionsController < Clearance::SessionsController
private
def url_after_create
photos_url
end
end
2 changes: 2 additions & 0 deletions app/controllers/users_controller.rb
@@ -0,0 +1,2 @@
class UsersController < Clearance::UsersController
end
1 change: 1 addition & 0 deletions app/models/actionentry.rb
@@ -1,2 +1,3 @@
class Actionentry < ActiveRecord::Base
belongs_to :photo
end
16 changes: 9 additions & 7 deletions app/models/photo.rb
Expand Up @@ -4,6 +4,9 @@
}

class Photo < ActiveRecord::Base
belongs_to :user
has_many :actionentries

validates_uniqueness_of :relativepath
validates_presence_of :tinythumbnail
validates_presence_of :largethumbnail
Expand All @@ -28,11 +31,11 @@ def relativepath_doesnt_escape_photo_dir
def absolutepath
pn = nil
begin
pn = Pathname.new(File.join(Photo.photo_import_folder, relativepath)).realpath
raise "Fail" unless pn.exist? and pn.to_s.starts_with? Photo.photo_import_folder
pn = Pathname.new(File.join(photo_import_folder, relativepath)).realpath
raise "Fail" unless pn.exist? and pn.to_s.starts_with? photo_import_folder
rescue
logger.error "absolutepath '#{relativepath}' is outside import folder: #{$!.message}\n#{$!.backtrace}"
logger.debug "expected is '#{Photo.photo_import_folder}'"
logger.debug "expected is '#{photo_import_folder}'"
return nil
end

Expand Down Expand Up @@ -68,6 +71,9 @@ def delete_associated_files
FileUtils.rm absolutepath
end

def photo_import_folder
return user.import_path
end

##
## Class Methods
Expand All @@ -83,10 +89,6 @@ def thumbnail_path(type, enclosing_dir, full_image_path, image_cache_dir = nil)
File.join(cachedir, [path_hash, contents_hash, type].join('_')) + ".jpg"
end

def photo_import_folder
return PHOTO_IMPORT_FOLDER
end

def photo_thumbnail_folder
return PHOTO_THUMBNAIL_FOLDER
end
Expand Down
21 changes: 21 additions & 0 deletions app/models/user.rb
@@ -0,0 +1,21 @@
class User < ActiveRecord::Base
include Clearance::User
has_many :photos

before_save :initialize_import_path

private

def initialize_import_path
# HACK: This is a constant for now
logger.fatal "WHAT THE FUCK"
self.import_path = PHOTO_IMPORT_FOLDER
end

class << self
def photo_import_folder
return PHOTO_IMPORT_FOLDER
end
end

end
16 changes: 16 additions & 0 deletions app/views/passwords/edit.html.haml
@@ -0,0 +1,16 @@
%h2
Change your password

%p
Your password has been reset. Choose a new password below.

- semantic_form_for(:user,
- :url => user_password_path(@user, :token => @user.confirmation_token),
- :html => { :method => :put }) do |form|
= form.error_messages
- form.inputs do
= form.input :password, :as => :password, :label => "Choose password"
= form.input :password_confirmation, :as => :password, :label => "Confirm password"

- form.buttons do
= form.commit_button "Save this password"
10 changes: 10 additions & 0 deletions app/views/passwords/new.html.haml
@@ -0,0 +1,10 @@
%h2
Reset your password

%p
We will email you a link to reset your password.
- semantic_form_for :password, :url => passwords_path do |form|
- form.inputs do
= form.input :email, :label => "Email address"
- form.buttons do
= form.commit_button "Reset password"
2 changes: 1 addition & 1 deletion app/views/photos/show.iphone.haml
@@ -1,4 +1,4 @@
= te_navigation_bar({:url => photos_url, :caption => "Back", :html_options => {}}, @item.filename, {})
= te_navigation_bar({:url => user_photos_url, :caption => "Back", :html_options => {}}, @item.filename, {})
.image
%a{:href => fullimage_url(@item)}
%img{:src => largethumbnail_url(@item)}
Expand Down
13 changes: 13 additions & 0 deletions app/views/sessions/new.html.haml
@@ -0,0 +1,13 @@
%h2 Sign in
- semantic_form_for :session, :url => session_path do |form|
- form.inputs do
= form.input :email
= form.input :password, :as => :password
- form.buttons do
= form.commit_button "Sign in"

%ul
%li
= link_to "Sign up", new_user_path
%li
= link_to "Forgot password?", new_password_path
5 changes: 2 additions & 3 deletions app/views/shared/_flashes.html.haml
@@ -1,5 +1,4 @@
#flash
- flash.each do |key, value|
%div{ :id => "flash_#{key}" }
= h value
- end
%div{ :id => "flash_#{key}" }
= h value
4 changes: 4 additions & 0 deletions app/views/users/_inputs.html.haml
@@ -0,0 +1,4 @@
- form.inputs do
= form.input :email
= form.input :password
= form.input :password_confirmation, :label => "Confirm password"
7 changes: 7 additions & 0 deletions app/views/users/new.html.haml
@@ -0,0 +1,7 @@
%h2
Sign up
- semantic_form_for @user do |form|
= form.error_messages
= render :partial => "/users/inputs", :locals => { :form => form }
- form.buttons do
= form.commit_button "Sign up"
5 changes: 3 additions & 2 deletions app/workers/photo_collector_worker.rb
Expand Up @@ -2,11 +2,11 @@

class PhotoCollectorWorker < Workling::Base
def collect(options = {})
rf_path = options[:root_folder] || PHOTO_IMPORT_FOLDER
rf_path = options[:root_folder]
root_folder = nil

unless (rf_path and (root_folder = Pathname.new(rf_path).realpath).exist?)
logger.fatal "Import folder '#{root_folder ? root_folder.to_s : rf_path}' doesn't exist, set it in config/environments/*"
logger.fatal "Import folder '#{root_folder ? root_folder.to_s : rf_path}' doesn't exist for user"
return false
end

Expand Down Expand Up @@ -51,6 +51,7 @@ def collect(options = {})
p = Photo.create(ar_opts) do |f|
f.relativepath = relative_path
f.exif_data_yaml = exifdata.to_yaml
f.user_id = options[:user_id] || 0
end
p.save!
end
Expand Down
1 change: 1 addition & 0 deletions config/environments/development.rb
Expand Up @@ -17,6 +17,7 @@
config.action_mailer.raise_delivery_errors = false

HOST = 'localhost'
DO_NOT_REPLY = 'donotreply@example.com'

PHOTO_IMPORT_FOLDER = realpath(File.join(File.dirname(__FILE__), '..', '..', 'test', 'fixtures', 'PhotoImportTest'))
PHOTO_THUMBNAIL_FOLDER = realpath(File.join(File.dirname(__FILE__), '..', '..', 'tmp', 'photo_cache'))
16 changes: 9 additions & 7 deletions config/routes.rb
Expand Up @@ -21,18 +21,20 @@
# See how all your routes lay out with "rake routes"

# map.home '', :controller => 'home', :action => 'dashboard'
# map.with_options :controller => 'sessions' do |m|
# m.login '/login', :action => 'new'
# m.logout '/logout', :action => 'destroy'
# end
map.with_options :controller => 'sessions' do |m|
m.login '/login', :action => 'new'
m.logout '/logout', :action => 'destroy'
end

map.root :controller => 'photos', :action => 'index'

map.resources :photos

map.resources :users, :as => 'u', :shallow => false do |u|
u.resources :photos
u.connect "action/invoke/:name/:id", :controller => 'action', :action => 'invoke'
end

{ 'i' => 'index', 'lt' => 'largethumbnail', 'tt' => 'tinythumbnail' }.each do |k, action|
map.connect "#{k}/:id", :controller => 'image', :action => action
end

map.connect "action/invoke/:name/:id", :controller => 'action', :action => 'invoke'
end
2 changes: 1 addition & 1 deletion db/migrate/20100211045649_create_actionentries.rb
Expand Up @@ -3,7 +3,7 @@ def self.up
create_table :actionentries do |t|
t.string :action
t.string :error
t.integer :user_id
t.integer :photo_id

t.timestamps
end
Expand Down
21 changes: 21 additions & 0 deletions db/migrate/20100211050538_clearance_create_users.rb
@@ -0,0 +1,21 @@
class ClearanceCreateUsers < ActiveRecord::Migration
def self.up
create_table(:users) do |t|
t.string :email
t.string :encrypted_password, :limit => 128
t.string :salt, :limit => 128
t.string :confirmation_token, :limit => 128
t.string :remember_token, :limit => 128
t.boolean :email_confirmed, :default => false, :null => false
t.timestamps
end

add_index :users, [:id, :confirmation_token]
add_index :users, :email
add_index :users, :remember_token
end

def self.down
drop_table :users
end
end
9 changes: 9 additions & 0 deletions db/migrate/20100211051246_add_user_id_to_photo.rb
@@ -0,0 +1,9 @@
class AddUserIdToPhoto < ActiveRecord::Migration
def self.up
add_column :photos, :user_id, :integer
end

def self.down
remove_column :photos, :user_id
end
end
9 changes: 9 additions & 0 deletions db/migrate/20100211052831_add_import_path_to_user.rb
@@ -0,0 +1,9 @@
class AddImportPathToUser < ActiveRecord::Migration
def self.up
add_column :users, :import_path, :string
end

def self.down
remove_column :users, :import_path
end
end
Empty file added db/seeds.rb
Empty file.
13 changes: 13 additions & 0 deletions test/factories/clearance.rb
@@ -0,0 +1,13 @@
Factory.sequence :email do |n|
"user#{n}@example.com"
end

Factory.define :user do |user|
user.email { Factory.next :email }
user.password { "password" }
user.password_confirmation { "password" }
end

Factory.define :email_confirmed_user, :parent => :user do |user|
user.email_confirmed { true }
end

0 comments on commit b2cbed0

Please sign in to comment.