diff --git a/app/controllers/my_profile/profile_editor_controller.rb b/app/controllers/my_profile/profile_editor_controller.rb
index c4ed49c..58dc79c 100644
--- a/app/controllers/my_profile/profile_editor_controller.rb
+++ b/app/controllers/my_profile/profile_editor_controller.rb
@@ -12,12 +12,7 @@ class ProfileEditorController < MyProfileController
def edit
@profile_data = profile
if request.post?
- profile.image || profile.build_image
if profile.update_attributes(params[:profile_data])
- if !params[:image].blank? && !params[:image][:uploaded_data].blank? && !profile.image.update_attributes(params[:image])
- flash[:notice] = _('Could not upload image')
- return
- end
redirect_to :action => 'index'
end
end
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 006cb36..f622cf1 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -567,4 +567,8 @@ module ApplicationHelper
# observe_field(state_field_name, :update => city_field_name, :url => { :controller => 'geography', :action => 'cities' }, :with => 'state_id')
# end
+ def file_field_or_thumbnail(label, image, i)
+ display_form_field( label, (render :partial => (image && image.valid? ? 'shared/show_thumbnail' : 'shared/change_image'), :locals => { :i => i, :image => image }) )
+ end
+
end
diff --git a/app/models/category.rb b/app/models/category.rb
index ff7f32c..9f1c7be 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -32,6 +32,8 @@ class Category < ActiveRecord::Base
has_many :products, :through => :enterprises
+ acts_as_having_image
+
def recent_articles(limit = 10)
self.articles.recent(limit)
end
diff --git a/app/models/product.rb b/app/models/product.rb
index 3fc13ff..60172f4 100644
--- a/app/models/product.rb
+++ b/app/models/product.rb
@@ -6,8 +6,6 @@ class Product < ActiveRecord::Base
validates_uniqueness_of :name, :scope => :enterprise_id
validates_numericality_of :price, :allow_nil => true
- has_one :image, :as => :owner
-
after_update :save_image
acts_as_searchable :fields => [ :name, :description, :category_full_name ]
@@ -18,13 +16,7 @@ class Product < ActiveRecord::Base
product_category.full_name(" ")
end
- def image_builder=(img)
- if image && image.id == img[:id]
- image.attributes = img
- else
- build_image(img)
- end
- end
+ acts_as_having_image
def save_image
image.save if image
diff --git a/app/models/profile.rb b/app/models/profile.rb
index c18db38..93b753f 100644
--- a/app/models/profile.rb
+++ b/app/models/profile.rb
@@ -70,7 +70,7 @@ class Profile < ActiveRecord::Base
has_many :articles, :dependent => :destroy
belongs_to :home_page, :class_name => Article.name, :foreign_key => 'home_page_id'
- has_one :image, :as => :owner
+ acts_as_having_image
has_many :consumptions
has_many :consumed_product_categories, :through => :consumptions, :source => :product_category
diff --git a/app/views/categories/_form.rhtml b/app/views/categories/_form.rhtml
index fd0e68e..5be6ceb 100644
--- a/app/views/categories/_form.rhtml
+++ b/app/views/categories/_form.rhtml
@@ -1,6 +1,6 @@
<%= error_messages_for 'category' %>
-<% labelled_form_for 'category' do |f| %>
+<% labelled_form_for 'category', @category, :html => { :multipart => true} do |f| %>
<% if @category.new_record? %>
<% if @category.parent %>
<%= hidden_field_tag('parent_id', @category.parent.id) %>
@@ -13,6 +13,11 @@
<%= select_color_for_category %>
<%= f.text_field 'name' %>
+
+ <% f.fields_for :image_builder, @category.image do |i| %>
+ <%= file_field_or_thumbnail(_('Image:'), @category.image, i) %>
+ <% end %>
+
<% button_bar do %>
<%= submit_button('save', _('Save'), :cancel => {:action => 'index'}) %>
<% end%>
diff --git a/app/views/manage_products/_change_image.rhtml b/app/views/manage_products/_change_image.rhtml
deleted file mode 100644
index 8bc88c6..0000000
--- a/app/views/manage_products/_change_image.rhtml
+++ /dev/null
@@ -1,8 +0,0 @@
- <%= file_field_tag("product[image_builder][uploaded_data]") %>
-
-
-
- <%= link_to_function(_('Cancel'), nil, :id => 'cancel-change-image-link', :style => 'display: none') do |page|
- page['change-image-link'].show
- page['change-image'].replace_html ''
- end %>
diff --git a/app/views/manage_products/_form.rhtml b/app/views/manage_products/_form.rhtml
index d0ad3eb..65a9903 100644
--- a/app/views/manage_products/_form.rhtml
+++ b/app/views/manage_products/_form.rhtml
@@ -4,7 +4,9 @@
<%= display_form_field( _('Name:'), f.text_field(:name) ) %>
<%= display_form_field( _('Price:'), f.text_field(:price) ) %>
<%= display_form_field( _('Description:'), f.text_area(:description, :rows => 10) ) %>
- <%= display_form_field( _('Image:'), (render :partial => @product.image && @product.image.valid? ? 'show_thumbnail' : 'change_image') ) %>
+ <% f.fields_for :image_builder, @product.image do |i| %>
+ <%= file_field_or_thumbnail(_('Image:'), @product.image, i) %>
+ <% end %>
<%= render :partial => 'subcategories' %>
diff --git a/app/views/manage_products/_show_thumbnail.rhtml b/app/views/manage_products/_show_thumbnail.rhtml
deleted file mode 100644
index da06348..0000000
--- a/app/views/manage_products/_show_thumbnail.rhtml
+++ /dev/null
@@ -1,11 +0,0 @@
- <%= image_tag(@product.image.public_filename(:thumb)) %>
-
-
-
- <%= link_to_function(_('Change image'), nil, :id => 'change-image-link') do |page|
- page['change-image'].replace_html :partial => 'change_image'
- page['change-image-link'].hide
- page['cancel-change-image-link'].show
- end %>
-
-
diff --git a/app/views/profile_editor/edit.rhtml b/app/views/profile_editor/edit.rhtml
new file mode 100644
index 0000000..df8d6fa
--- /dev/null
+++ b/app/views/profile_editor/edit.rhtml
@@ -0,0 +1,18 @@
+
<%= _('Edit profile') %>
+
+<%= error_messages_for :profile %>
+
+<% labelled_form_for :profile_data, @profile, :html => { :multipart => true } do |f| %>
+ <%= render :partial => partial_for_class(@profile.class), :locals => { :f => f } %>
+
<%= _('Change your picture') %>
+ <% f.fields_for :image_builder, @profile.image do |i| %>
+ <%= file_field_or_thumbnail(_('Image:'), @profile.image, i) %>
+ <% end %>
+ <%= select_categories(:profile_data, _('Select the categories of your interest'), 1) %>
+ <% button_bar do %>
+ <%= submit_button('save', _('Save'), :cancel => {:action => 'index'}) %>
+ <%= button(:back, _('Back to control panel'), :controller => 'profile_editor') %>
+ <% end %>
+<% end %>
+
+<%# = generate_form :info, @info, {...} %>
diff --git a/app/views/profile_editor/index.rhtml b/app/views/profile_editor/index.rhtml
index 9ad9d63..793a5c6 100644
--- a/app/views/profile_editor/index.rhtml
+++ b/app/views/profile_editor/index.rhtml
@@ -6,8 +6,6 @@
<% file_manager do %>
- <%= file_manager_button(_('Change your picture'), profile_icon(@profile, :portrait), :controller => 'profile_editor', :action => 'change_image') %>
-
<%= file_manager_button(_('Edit Profile'), 'icons-app/edit-profile.png', :controller => 'profile_editor', :action => 'edit') %>
<%= file_manager_button(_('Pending tasks'), 'icons-app/todo.png', :controller => 'tasks', :action => 'index') %>
diff --git a/app/views/shared/_change_image.rhtml b/app/views/shared/_change_image.rhtml
new file mode 100644
index 0000000..7387eb6
--- /dev/null
+++ b/app/views/shared/_change_image.rhtml
@@ -0,0 +1,8 @@
+ <%= i.file_field( :uploaded_data, { :onchange => 'updateImg(this.value)' } ) %>
+
+
+
+ <%= link_to_function(_('Cancel'), nil, :id => 'cancel-change-image-link', :style => 'display: none') do |page|
+ page['change-image-link'].show
+ page['change-image'].replace_html ''
+ end %>
diff --git a/app/views/shared/_show_thumbnail.rhtml b/app/views/shared/_show_thumbnail.rhtml
new file mode 100644
index 0000000..8b09730
--- /dev/null
+++ b/app/views/shared/_show_thumbnail.rhtml
@@ -0,0 +1,11 @@
+ <%= image_tag(image.public_filename(:thumb)) %>
+
+
+
+ <%= link_to_function(_('Change image'), nil, :id => 'change-image-link') do |page|
+ page['change-image'].replace_html :partial => 'shared/change_image', :locals => { :i => i, :image => image }
+ page['change-image-link'].hide
+ page['cancel-change-image-link'].show
+ end %>
+
+
diff --git a/config/environment.rb b/config/environment.rb
index a1ac61d..91aa09a 100644
--- a/config/environment.rb
+++ b/config/environment.rb
@@ -96,6 +96,7 @@ require 'acts_as_filesystem'
require 'acts_as_searchable'
require 'acts_as_having_boxes'
require 'acts_as_having_settings'
+require 'acts_as_having_image'
require 'hacked_after_create'
# load a local configuration if present, but not under test environment.
diff --git a/lib/acts_as_having_image.rb b/lib/acts_as_having_image.rb
new file mode 100644
index 0000000..735b142
--- /dev/null
+++ b/lib/acts_as_having_image.rb
@@ -0,0 +1,20 @@
+module ActsAsHavingImage
+
+ module ClassMethods
+ def acts_as_having_image
+ has_one :image, :as => 'owner'
+ self.send(:include, ActsAsHavingImage)
+ end
+ end
+
+ def image_builder=(img)
+ if image && image.id == img[:id]
+ image.attributes = img
+ else
+ build_image(img)
+ end
+ end
+
+end
+
+ActiveRecord::Base.extend(ActsAsHavingImage::ClassMethods)
diff --git a/test/functional/categories_controller_test.rb b/test/functional/categories_controller_test.rb
index 717c9be..54ae568 100644
--- a/test/functional/categories_controller_test.rb
+++ b/test/functional/categories_controller_test.rb
@@ -81,5 +81,11 @@ class CategoriesControllerTest < Test::Unit::TestCase
end
end
+ should 'be able to upload a file' do
+ assert_difference Category, :count do
+ post :new, :category => { :name => 'new category', :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } }
+ assert_equal assigns(:category).image.filename, 'rails.png'
+ end
+ end
end
diff --git a/test/functional/profile_editor_controller_test.rb b/test/functional/profile_editor_controller_test.rb
index 3e6963b..3544b2a 100644
--- a/test/functional/profile_editor_controller_test.rb
+++ b/test/functional/profile_editor_controller_test.rb
@@ -72,7 +72,7 @@ class ProfileEditorControllerTest < Test::Unit::TestCase
cat1 = Environment.default.categories.build(:name => 'top category'); cat1.save!
cat2 = Environment.default.categories.build(:name => 'sub category', :parent => cat1); cat2.save!
person = create_user('test_user').person
- get :edit_categories, :profile => 'test_user'
+ get :edit, :profile => 'test_user'
assert_response :success
assert_template 'edit_categories'
assert_tag :tag => 'input', :attributes => {:name => 'profile_object[category_ids][]'}
@@ -225,7 +225,7 @@ class ProfileEditorControllerTest < Test::Unit::TestCase
should 'show image field on edit profile' do
person = create_user('testuser').person
get :edit, :profile => 'testuser'
- assert_tag :tag => 'input', :attributes => { :name => 'image[uploaded_data]' }
+ assert_tag :tag => 'input', :attributes => { :name => 'profile_data[image_builder][uploaded_data]' }
end
should 'show categories field on edit profile' do
@@ -251,7 +251,7 @@ class ProfileEditorControllerTest < Test::Unit::TestCase
should 'be able to upload an image' do
person = create_user('test_profile').person
assert_nil person.image
- post :edit, :profile => 'test_profile', :image => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')}
+ post :edit, :profile => 'test_profile', :profile_data => { :image_builder => { :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png') } }
assert_not_nil assigns(:profile).image
end
end
diff --git a/test/unit/category_test.rb b/test/unit/category_test.rb
index 13dc800..8de49e3 100644
--- a/test/unit/category_test.rb
+++ b/test/unit/category_test.rb
@@ -376,4 +376,13 @@ class CategoryTest < Test::Unit::TestCase
# assert_includes c1.people, person
#end
+ should 'have image' do
+ assert_difference Category, :count do
+ c = Category.create!(:name => 'test category1', :environment => Environment.default, :image_builder => {
+ :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')
+ })
+ assert_equal c.image(true).filename, 'rails.png'
+ end
+ end
+
end
--
libgit2 0.21.2