Skip to content

Commit

Permalink
Introduce channel ownership; delete button only displayed for destroy…
Browse files Browse the repository at this point in the history
…able channels (non-primary, owned, or all except primary channel if you're an admin).
  • Loading branch information
mjtko committed Mar 15, 2013
1 parent b6b983a commit 91d40be
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 14 deletions.
1 change: 0 additions & 1 deletion app/assets/javascripts/backbone/kandan.js.coffee
Expand Up @@ -97,7 +97,6 @@ window.Kandan =
<span class="tab_left"></span>
<span class="tab_content">
<cite>#{label}</cite>
<cite class="close_channel" title="close channel">x</cite>
</span>
</a>
</li>
Expand Down
9 changes: 8 additions & 1 deletion app/assets/javascripts/backbone/models/channel.js.coffee
Expand Up @@ -8,4 +8,11 @@ class Kandan.Models.Channel extends Backbone.Model
activities.add(response.activities)
@activities = activities
@moreActivities = response.more_activities
response
response

isDestroyable: ->
current_user = _(Kandan.Helpers.Users.all()).find (u) ->
u.id == Kandan.Helpers.Users.currentUser().id
@get('id') != 1 &&
current_user.is_admin ||
@get('user_id') == current_user.id
17 changes: 11 additions & 6 deletions app/assets/javascripts/backbone/views/channel_pane.js.coffee
Expand Up @@ -2,23 +2,28 @@ class Kandan.Views.ChannelPane extends Backbone.View
tagName: 'div'

render: (container)->
container = container || $(@el)
$(container).html @paginatedActivitiesView()
$(container).append @chatboxView()
@setIdAndData(container)
$container = $(container || @el)
$container.html @paginatedActivitiesView()
$container.append @chatboxView()
@setIdAndData($container)
$li = $("a[href=#channels-#{@options.channel.get('id')}]").parent()
if @options.channel.isDestroyable()
$li.addClass 'destroyable'
$li.find('cite').after '<cite class="close_channel" title="close channel">x</cite>'
else
$li.addClass 'protected'
Kandan.Helpers.Audio.createAudioChannel(@options.channel.get('id'))
@

setIdAndData: (container)->
$(container).attr "id", "channels-#{@options.channel.get("id")}"
$(container).attr "class", "channels-pane"
$(container).data "channel-id", @options.channel.get('id')

paginatedActivitiesView: ()->
view = new Kandan.Views.PaginatedActivities({channel: @options.channel})
view.render().el

chatboxView: ()->
view = new Kandan.Views.Chatbox({channel: @options.channel})
view.render().el

3 changes: 0 additions & 3 deletions app/assets/templates/channel_tabs.jst.eco
Expand Up @@ -5,9 +5,6 @@
<span class="tab_left"></span>
<span class="tab_content">
<cite><%= channel.get('name') %></cite>
<% if channel.get('destroyable'): %>
<cite class="close_channel" title="close channel">x</cite>
<% end %>
</span>
</a>
</li>
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/channels_controller.rb
Expand Up @@ -2,6 +2,7 @@ class ChannelsController < ApplicationController
before_filter :authenticate_user!
before_filter :find_channel_by_name, :only => :show
load_and_authorize_resource
before_filter :set_channel_owner, only: :create

def index
# NOTE Eager loading doesn't respect limit
Expand Down Expand Up @@ -62,4 +63,8 @@ def destroy
def find_channel_by_name
@channel = Channel.where("LOWER(name) = ?", params['id'].downcase).first
end

def set_channel_owner
@channel.user = current_user
end
end
4 changes: 3 additions & 1 deletion app/models/channel.rb
@@ -1,9 +1,11 @@
class Channel < ActiveRecord::Base
has_many :activities, :dependent => :destroy
has_many :attachments, :dependent => :destroy
belongs_to :user

validates :name, :presence => { :message => "Room name cannot be blank"}, :uniqueness => { :message => "Room name is already taken" }

validates :user, :presence => { :message => "Room must belong to a user"}

before_create :ensure_app_max_rooms

def ensure_app_max_rooms
Expand Down
6 changes: 6 additions & 0 deletions db/migrate/20130315214129_add_user_to_channels.rb
@@ -0,0 +1,6 @@
class AddUserToChannels < ActiveRecord::Migration
def change
add_column :channels, :user_id, :integer
add_index :channels, :user_id
end
end
7 changes: 5 additions & 2 deletions db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130301022941) do
ActiveRecord::Schema.define(:version => 20130315214129) do

create_table "activities", :force => true do |t|
t.text "content"
Expand All @@ -38,8 +38,11 @@
t.text "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "user_id"
end

add_index "channels", ["user_id"], :name => "index_channels_on_user_id"

create_table "sessions", :force => true do |t|
t.string "session_id", :null => false
t.text "data"
Expand Down Expand Up @@ -83,4 +86,4 @@
add_index "users", ["authentication_token"], :name => "index_users_on_authentication_token", :unique => true
add_index "users", ["email"], :name => "index_users_on_email", :unique => true

end
end

0 comments on commit 91d40be

Please sign in to comment.