diff options
-rw-r--r-- | app/models/profile_photo.rb | 4 | ||||
-rw-r--r-- | app/models/user.rb | 11 | ||||
-rw-r--r-- | app/views/user/show.rhtml | 10 | ||||
-rw-r--r-- | db/migrate/084_alter_profile_photo.rb | 4 | ||||
-rw-r--r-- | db/schema.rb | 4 | ||||
-rw-r--r-- | spec/models/profile_photo_spec.rb | 10 |
6 files changed, 26 insertions, 17 deletions
diff --git a/app/models/profile_photo.rb b/app/models/profile_photo.rb index 0811978fe..4ac684f5e 100644 --- a/app/models/profile_photo.rb +++ b/app/models/profile_photo.rb @@ -23,7 +23,9 @@ class ProfilePhoto < ActiveRecord::Base WIDTH = 96 HEIGHT = 96 - # has_one :user + belongs_to :user + + attr_accessor :draft # deliberately don't strip_attributes, so keeps raw photo properly diff --git a/app/models/user.rb b/app/models/user.rb index 00fd332ad..7af3ad2d3 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -277,19 +277,12 @@ class User < ActiveRecord::Base # A photograph of the user (to make it all more human) def set_profile_photo(new_profile_photo) - old_profile_photo = nil ActiveRecord::Base.transaction do if !self.profile_photo.nil? - old_profile_photo = self.profile_photo - self.profile_photo = nil + self.profile_photo.destroy end self.profile_photo = new_profile_photo - end - if !old_profile_photo.nil? - # This doesn't work in the transaction, as destroy starts - # a new transaction immediately (the database foreign key - # constraint detects it). Yuck. - old_profile_photo.destroy + self.save end end diff --git a/app/views/user/show.rhtml b/app/views/user/show.rhtml index a7e19e138..1aeb58391 100644 --- a/app/views/user/show.rhtml +++ b/app/views/user/show.rhtml @@ -39,7 +39,7 @@ <div class="single_user"> <p id="user_photo_on_profile"> - <% if @display_user.profile_photo_id %> + <% if @display_user.profile_photo %> <img src="<%= get_profile_photo_url(:url_name => @display_user.url_name) %>"> <% else %> <% if @is_you %> @@ -63,8 +63,12 @@ <%= link_to "Send message to " + h(@display_user.name), contact_user_url(:id => @display_user.id) %> <% if @is_you %> (just to see how it works) - <p id="user_change_password_email"><%= link_to "Change your password", signchangepassword_url() %> | - <%= link_to "Change your email", signchangeemail_url() %> + <p id="user_change_password_email"> + <% if @display_user.profile_photo %> + <%= link_to "Change profile photo", set_profile_photo_url() %> | + <% end %> + <%= link_to "Change your password", signchangepassword_url() %> | + <%= link_to "Change your email", signchangeemail_url() %> </p> <% end %> </p> diff --git a/db/migrate/084_alter_profile_photo.rb b/db/migrate/084_alter_profile_photo.rb index 52e798b23..d2717b047 100644 --- a/db/migrate/084_alter_profile_photo.rb +++ b/db/migrate/084_alter_profile_photo.rb @@ -1,9 +1,9 @@ class AlterProfilePhoto < ActiveRecord::Migration def self.up - remove_column :profile_photos, :user_id + remove_column :users, :profile_photo_id end def self.down - raise "Reverse migrations not supported" + raise "No reverse migration" end end diff --git a/db/schema.rb b/db/schema.rb index d1b682d48..f2a8812b0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -144,7 +144,8 @@ ActiveRecord::Schema.define(:version => 84) do add_index "post_redirects", ["updated_at"], :name => "index_post_redirects_on_updated_at" create_table "profile_photos", :force => true do |t| - t.binary "data", :null => false + t.binary "data", :null => false + t.integer "user_id" end create_table "public_bodies", :force => true do |t| @@ -243,7 +244,6 @@ ActiveRecord::Schema.define(:version => 84) do t.datetime "last_daily_track_email", :default => '2000-01-01 00:00:00' t.string "admin_level", :default => "none", :null => false t.text "ban_text", :default => "", :null => false - t.integer "profile_photo_id" end add_index "users", ["url_name"], :name => "index_users_on_url_name", :unique => true diff --git a/spec/models/profile_photo_spec.rb b/spec/models/profile_photo_spec.rb index b7d45b8ae..1e33c5b61 100644 --- a/spec/models/profile_photo_spec.rb +++ b/spec/models/profile_photo_spec.rb @@ -32,6 +32,16 @@ describe ProfilePhoto, "when constructing a new photo" do profile_photo.image.columns.should == 96 profile_photo.image.rows.should == 96 end + + it 'should accept a draft PNG and not resize it' do + data = load_file_fixture("parrot.png") + profile_photo = ProfilePhoto.new(:draft => true, :data => data) + profile_photo.valid?.should == true + profile_photo.image.format.should == 'PNG' + profile_photo.image.columns.should == 198 + profile_photo.image.rows.should == 289 + end + end |