diff --git a/app/controllers/closet_hangers_controller.rb b/app/controllers/closet_hangers_controller.rb index 4bd30974..ef21d496 100644 --- a/app/controllers/closet_hangers_controller.rb +++ b/app/controllers/closet_hangers_controller.rb @@ -17,8 +17,16 @@ class ClosetHangersController < ApplicationController @public_perspective = params.has_key?(:public) || !user_is?(@user) @perspective_user = current_user unless @public_perspective - @closet_lists_by_owned = @user.closet_lists.alphabetical. - includes(:hangers => :item).group_by(&:hangers_owned) + @closet_lists_by_owned = @user.closet_lists. + alphabetical.includes(:hangers => :item) + unless @perspective_user == @user + # If we run this when the user matches, we'll end up with effectively: + # WHERE belongs_to_user AND (is_public OR belongs_to_user) + # and it's a bit silly to put the SQL server through a condition that's + # always true. + @closet_lists_by_owned = @closet_lists_by_owned.visible_to(@perspective_user) + end + @closet_lists_by_owned = @closet_lists_by_owned.group_by(&:hangers_owned) visible_groups = @user.closet_hangers_groups_visible_to(@perspective_user) unless visible_groups.empty? diff --git a/app/helpers/closet_hangers_helper.rb b/app/helpers/closet_hangers_helper.rb index f05c0f02..65026331 100644 --- a/app/helpers/closet_hangers_helper.rb +++ b/app/helpers/closet_hangers_helper.rb @@ -17,9 +17,9 @@ module ClosetHangersHelper owned ? :owned_closet_hangers_visibility : :wanted_closet_hangers_visibility end - def hangers_group_visibility_choices(owned) + def closet_visibility_choices(*args) ClosetVisibility.levels.map do |level| - [level.name.to_s.humanize, level.id] + [level.send(*args), level.id] end end diff --git a/app/models/closet_list.rb b/app/models/closet_list.rb index d74fd956..10358be3 100644 --- a/app/models/closet_list.rb +++ b/app/models/closet_list.rb @@ -3,13 +3,18 @@ class ClosetList < ActiveRecord::Base has_many :hangers, :class_name => 'ClosetHanger', :foreign_key => 'list_id', :dependent => :nullify - attr_accessible :description, :hangers_owned, :name + attr_accessible :description, :hangers_owned, :name, :visibility validates :name, :presence => true, :uniqueness => {:scope => :user_id} validates :user, :presence => true validates :hangers_owned, :inclusion => {:in => [true, false], :message => "can't be blank"} scope :alphabetical, order(:name) + scope :visible_to, lambda { |user| + condition = arel_table[:visibility].gteq(ClosetVisibility[:public].id) + condition = condition.or(arel_table[:user_id].eq(user.id)) if user + where(condition) + } after_save :sync_hangers_owned! diff --git a/app/models/closet_visibility.rb b/app/models/closet_visibility.rb index d32c579b..4cbc0e9a 100644 --- a/app/models/closet_visibility.rb +++ b/app/models/closet_visibility.rb @@ -16,6 +16,10 @@ module ClosetVisibility @description end end + + def human_name + name.to_s.humanize + end end LEVELS = [ diff --git a/app/stylesheets/closet_lists/_form.sass b/app/stylesheets/closet_lists/_form.sass index 21f916d5..2c451ef9 100644 --- a/app/stylesheets/closet_lists/_form.sass +++ b/app/stylesheets/closet_lists/_form.sass @@ -12,7 +12,7 @@ body.closet_lists-new, body.closet_lists-create, body.closet_lists-edit, body.cl li padding: 0.75em 0 - width: 25em + width: 35em input, textarea, select clear: both diff --git a/app/views/closet_hangers/index.html.haml b/app/views/closet_hangers/index.html.haml index c1a0cf3c..914b259e 100644 --- a/app/views/closet_hangers/index.html.haml +++ b/app/views/closet_hangers/index.html.haml @@ -72,7 +72,7 @@ - unless public_perspective? = form_for @user, :html => {:class => 'visibility-form'} do |f| = f.select hangers_group_visibility_field_name(owned), - hangers_group_visibility_choices(owned) + closet_visibility_choices(:human_name) = f.submit "Save" = closet_visibility_descriptions - if has_lists?(owned) diff --git a/app/views/closet_lists/_form.html.haml b/app/views/closet_lists/_form.html.haml index d4d43c03..fb84c0d2 100644 --- a/app/views/closet_lists/_form.html.haml +++ b/app/views/closet_lists/_form.html.haml @@ -10,6 +10,9 @@ %li = f.label :hangers_owned, 'This is a list for…'.html_safe = f.select :hangers_owned, hangers_owned_options + %li + = f.label :visibility, 'Who can see this list?' + = f.select :visibility, closet_visibility_choices(:description, 'Items in this list') %li = f.label :description %span.hint diff --git a/db/migrate/20110731021808_add_visibility_to_closet_lists.rb b/db/migrate/20110731021808_add_visibility_to_closet_lists.rb new file mode 100644 index 00000000..68d62874 --- /dev/null +++ b/db/migrate/20110731021808_add_visibility_to_closet_lists.rb @@ -0,0 +1,10 @@ +class AddVisibilityToClosetLists < ActiveRecord::Migration + def self.up + add_column :closet_lists, :visibility, :integer, :null => false, :default => 1 + end + + def self.down + remove_column :closet_lists, :visibility + end +end + diff --git a/db/schema.rb b/db/schema.rb index 59c49369..b966ec98 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110730174148) do +ActiveRecord::Schema.define(:version => 20110731021808) do create_table "auth_servers", :force => true do |t| t.string "short_name", :limit => 10, :null => false @@ -34,9 +34,10 @@ ActiveRecord::Schema.define(:version => 20110730174148) do t.string "name" t.text "description" t.integer "user_id" - t.boolean "hangers_owned", :null => false + t.boolean "hangers_owned", :null => false t.datetime "created_at" t.datetime "updated_at" + t.integer "visibility", :default => 1, :null => false end create_table "contributions", :force => true do |t| diff --git a/public/stylesheets/compiled/screen.css b/public/stylesheets/compiled/screen.css index d3aacccd..bdc51640 100644 --- a/public/stylesheets/compiled/screen.css +++ b/public/stylesheets/compiled/screen.css @@ -1180,7 +1180,7 @@ body.closet_lists-new form ul.fields label, body.closet_lists-create form ul.fie /* line 13, ../../../app/stylesheets/closet_lists/_form.sass */ body.closet_lists-new form ul.fields li, body.closet_lists-create form ul.fields li, body.closet_lists-edit form ul.fields li, body.closet_lists-update form ul.fields li { padding: 0.75em 0; - width: 25em; + width: 35em; } /* line 17, ../../../app/stylesheets/closet_lists/_form.sass */ body.closet_lists-new form ul.fields input, body.closet_lists-new form ul.fields textarea, body.closet_lists-new form ul.fields select, body.closet_lists-create form ul.fields input, body.closet_lists-create form ul.fields textarea, body.closet_lists-create form ul.fields select, body.closet_lists-edit form ul.fields input, body.closet_lists-edit form ul.fields textarea, body.closet_lists-edit form ul.fields select, body.closet_lists-update form ul.fields input, body.closet_lists-update form ul.fields textarea, body.closet_lists-update form ul.fields select {