diff --git a/app/controllers/my_profile/profile_editor_controller.rb b/app/controllers/my_profile/profile_editor_controller.rb index 3240283..d937e6a 100644 --- a/app/controllers/my_profile/profile_editor_controller.rb +++ b/app/controllers/my_profile/profile_editor_controller.rb @@ -31,5 +31,15 @@ class ProfileEditorController < MyProfileController end end end + + def edit_categories + @profile_object = profile + if request.post? + if profile.update_attributes(params[:profile_object]) + redirect_to :action => 'index' + end + end + end + end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index efb8eff..bf9257e 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -442,4 +442,23 @@ module ApplicationHelper url end + attr_reader :environment + def select_categories(object_name) + object = instance_variable_get("@#{object_name}") + + result = content_tag('h4', _('Categories')) + environment.top_level_categories.each do |toplevel| + toplevel.map_traversal do |cat| + if cat.top_level? + result << content_tag('h5', toplevel.name) + else + checkbox_id = "#{object_name}_#{cat.full_name.downcase.gsub(/\s+|\//, '_')}" + result << content_tag('label', check_box_tag("#{object_name}[category_ids][]", cat.id, object.category_ids.include?(cat.id), :id => checkbox_id) + cat.full_name_without_leading(1), :for => checkbox_id) + end + end + end + + content_tag('div', result) + end + end diff --git a/app/helpers/cms_helper.rb b/app/helpers/cms_helper.rb index 5cf42cf..d053fdd 100644 --- a/app/helpers/cms_helper.rb +++ b/app/helpers/cms_helper.rb @@ -24,22 +24,4 @@ module CmsHelper attr_reader :environment - def select_categories(object_name) - object = instance_variable_get("@#{object_name}") - - result = content_tag('h4', _('Categories')) - environment.top_level_categories.each do |toplevel| - toplevel.map_traversal do |cat| - if cat.top_level? - result << content_tag('h5', toplevel.name) - else - checkbox_id = "#{object_name}_#{cat.full_name.downcase.gsub(/\s+|\//, '_')}" - result << content_tag('label', check_box_tag("#{object_name}[category_ids][]", cat.id, object.category_ids.include?(cat.id), :id => checkbox_id) + cat.full_name_without_leading(1), :for => checkbox_id) - end - end - end - - content_tag('div', result) - end - end diff --git a/app/models/category.rb b/app/models/category.rb index 6bb3836..eb1cae0 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -25,6 +25,9 @@ class Category < ActiveRecord::Base has_many :articles, :through => :article_categorizations has_many :comments, :through => :articles + has_many :profile_categorizations + has_many :profiles, :through => :profile_categorizations + def recent_articles(limit = 10) self.articles.recent(limit) end diff --git a/app/models/profile.rb b/app/models/profile.rb index 01a93a3..a75114e 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -73,6 +73,8 @@ class Profile < ActiveRecord::Base has_many :tasks, :foreign_key => :target_id + has_and_belongs_to_many :categories + def top_level_articles(reload = false) if reload @top_level_articles = nil diff --git a/app/models/profile_categorization.rb b/app/models/profile_categorization.rb new file mode 100644 index 0000000..5447e60 --- /dev/null +++ b/app/models/profile_categorization.rb @@ -0,0 +1,5 @@ +class ProfileCategorization < ActiveRecord::Base + set_table_name :categories_profiles + belongs_to :profile + belongs_to :category +end diff --git a/app/views/profile_editor/edit_categories.rhtml b/app/views/profile_editor/edit_categories.rhtml new file mode 100644 index 0000000..773023c --- /dev/null +++ b/app/views/profile_editor/edit_categories.rhtml @@ -0,0 +1,7 @@ +<% form_for :profile_object, @profile_object, :url => { :action => 'edit_categories'} do |f| %> + <%= select_categories('profile_object') %> + <% button_bar do %> + <%= submit_button('save', _('Save'), :cancel => {:action => 'edit_categories'}) %> + <%= button(:back, _('Back to control panel'), :controller => 'profile_editor') %> + <% end %> +<% end %> diff --git a/app/views/profile_editor/index.rhtml b/app/views/profile_editor/index.rhtml index e74e535..69b13a7 100644 --- a/app/views/profile_editor/index.rhtml +++ b/app/views/profile_editor/index.rhtml @@ -25,6 +25,8 @@ <%= file_manager_button(_('Manage Content'), 'icons-app/cms.png', :controller => 'cms') %> + <%= file_manager_button(_('Edit Categories'), 'icons-app/categories.png', :controller => 'profile_editor', :action => 'edit_categories') %> + <%= file_manager_button(_('Change Password'), 'icons-app/change-password.png', :controller => 'account', :action => 'change_password') if profile.person? %> <%= file_manager_button(_('Manage friends'), 'icons-app/friends.png', :controller => 'friends', :action => 'index') if profile.person? %> diff --git a/db/migrate/027_create_categories_profiles.rb b/db/migrate/027_create_categories_profiles.rb new file mode 100644 index 0000000..34a6b61 --- /dev/null +++ b/db/migrate/027_create_categories_profiles.rb @@ -0,0 +1,12 @@ +class CreateCategoriesProfiles < ActiveRecord::Migration + def self.up + create_table :categories_profiles, :id => false do |t| + t.integer :profile_id + t.integer :category_id + end + end + + def self.down + drop_table :categories_profiles + end +end diff --git a/public/images/icons-app/README b/public/images/icons-app/README index 2ac4f21..e82cbff 100644 --- a/public/images/icons-app/README +++ b/public/images/icons-app/README @@ -28,6 +28,7 @@ gtk-yes.png dlg-neu users.png (locally modified) dlg-neu stock_todo.png Nuovo friends.png (modified version of users.png) Nuovo +gtk-folder.png Nuovo Icons rasterization =================== diff --git a/public/images/icons-app/categories.png b/public/images/icons-app/categories.png new file mode 120000 index 0000000..534f32a --- /dev/null +++ b/public/images/icons-app/categories.png @@ -0,0 +1 @@ +gtk-folder.png \ No newline at end of file diff --git a/public/images/icons-app/gtk-folder.png b/public/images/icons-app/gtk-folder.png new file mode 100644 index 0000000..51fb7da Binary files /dev/null and b/public/images/icons-app/gtk-folder.png differ diff --git a/test/fixtures/categories_profiles.yml b/test/fixtures/categories_profiles.yml new file mode 100644 index 0000000..5bf0293 --- /dev/null +++ b/test/fixtures/categories_profiles.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + +# one: +# column: value +# +# two: +# column: value diff --git a/test/functional/profile_editor_controller_test.rb b/test/functional/profile_editor_controller_test.rb index 629b3c8..234ec5d 100644 --- a/test/functional/profile_editor_controller_test.rb +++ b/test/functional/profile_editor_controller_test.rb @@ -74,12 +74,23 @@ class ProfileEditorControllerTest < Test::Unit::TestCase end should 'display categories to choose to associate profile' do - cat = Environment.default.categories.build(:name => 'a category'); cat.save! + 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, :profile => 'test_user' - assert_tag :tag => 'input', :attributes => {:name => 'profile[category_ids][]'} + get :edit_categories, :profile => 'test_user' + assert_response :success + assert_template 'edit_categories' + assert_tag :tag => 'input', :attributes => {:name => 'profile_object[category_ids][]'} end - should 'save categorization of profile' + should 'save categorization of profile' do + 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 + post :edit_categories, :profile => 'test_user', :profile_object => {:category_ids => [cat2.id]} + assert_response :redirect + assert_redirected_to :action => 'index' + assert_includes person.categories, cat2 + end end diff --git a/test/unit/category_test.rb b/test/unit/category_test.rb index 3954b2e..967b372 100644 --- a/test/unit/category_test.rb +++ b/test/unit/category_test.rb @@ -276,4 +276,14 @@ class CategoryTest < Test::Unit::TestCase assert_equivalent [c1, c2, c3], c.comments end + should 'have profiles' do + c = @env.categories.build(:name => 'my category'); c.save! + person1 = create_user('testuser_one').person + person1.categories << c + person2 = create_user('testuser_two').person + person2.categories << c + assert_includes c.profiles, person1 + assert_includes c.profiles, person2 + end + end diff --git a/test/unit/profile_categorization_test.rb b/test/unit/profile_categorization_test.rb new file mode 100644 index 0000000..653aadd --- /dev/null +++ b/test/unit/profile_categorization_test.rb @@ -0,0 +1,13 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class ProfileCategorizationTest < ActiveSupport::TestCase + should 'have profile and category' do + person = create_user('test_user').person + cat = Environment.default.categories.build(:name => 'a category'); cat.save! + person.categories << cat + person.save! + assert_includes person.categories, cat + assert_includes cat.profiles, person + assert_equal [cat.id], person.category_ids + end +end diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 5dfb8da..f3ea5d8 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -377,6 +377,19 @@ class ProfileTest < Test::Unit::TestCase assert_equal [t2, t3], c.tasks.finished end + should 'responds to categories' do + c = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') + assert_respond_to c, :categories + end + + should 'have categories' do + c = Profile.create!(:name => 'my test profile', :identifier => 'mytestprofile') + cat = Environment.default.categories.build(:name => 'a category'); cat.save! + c.categories << cat + c.save! + assert_includes c.categories, cat + end + private def assert_invalid_identifier(id) -- libgit2 0.21.2