set list visibility in closet_lists#edit

This commit is contained in:
Emi Matchu 2011-07-30 22:34:27 -04:00
parent ad45d755da
commit 0c92bf5987
10 changed files with 41 additions and 10 deletions

View file

@ -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?

View file

@ -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

View file

@ -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!

View file

@ -16,6 +16,10 @@ module ClosetVisibility
@description
end
end
def human_name
name.to_s.humanize
end
end
LEVELS = [

View file

@ -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

View file

@ -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)

View file

@ -10,6 +10,9 @@
%li
= f.label :hangers_owned, 'This is a list for&hellip;'.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

View file

@ -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

View file

@ -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|

View file

@ -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 {